5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\Role;
7 use BookStack\Auth\User;
10 class RolesApiTest extends TestCase
14 protected string $baseEndpoint = '/api/roles';
16 protected array $endpointMap = [
17 ['get', '/api/roles'],
18 ['post', '/api/roles'],
19 ['get', '/api/roles/1'],
20 ['put', '/api/roles/1'],
21 ['delete', '/api/roles/1'],
24 public function test_user_roles_manage_permission_needed_for_all_endpoints()
26 $this->actingAsApiEditor();
27 foreach ($this->endpointMap as [$method, $uri]) {
28 $resp = $this->json($method, $uri);
29 $resp->assertStatus(403);
30 $resp->assertJson($this->permissionErrorResponse());
34 public function test_index_endpoint_returns_expected_role_and_count()
36 $this->actingAsApiAdmin();
37 /** @var Role $firstRole */
38 $firstRole = Role::query()->orderBy('id', 'asc')->first();
40 $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
41 $resp->assertJson(['data' => [
43 'id' => $firstRole->id,
44 'display_name' => $firstRole->display_name,
45 'description' => $firstRole->description,
46 'mfa_enforced' => $firstRole->mfa_enforced,
47 'permissions_count' => $firstRole->permissions()->count(),
48 'users_count' => $firstRole->users()->count(),
49 'created_at' => $firstRole->created_at->toJSON(),
50 'updated_at' => $firstRole->updated_at->toJSON(),
54 $resp->assertJson(['total' => Role::query()->count()]);
57 public function test_create_endpoint()
59 $this->actingAsApiAdmin();
60 /** @var Role $role */
61 $role = Role::query()->first();
63 $resp = $this->postJson($this->baseEndpoint, [
64 'display_name' => 'My awesome role',
65 'description' => 'My great role description',
66 'mfa_enforced' => true,
75 $resp->assertStatus(200);
77 'display_name' => 'My awesome role',
78 'description' => 'My great role description',
79 'mfa_enforced' => true,
88 $this->assertDatabaseHas('roles', [
89 'display_name' => 'My awesome role',
90 'description' => 'My great role description',
91 'mfa_enforced' => true,
94 /** @var Role $role */
95 $role = Role::query()->where('display_name', '=', 'My awesome role')->first();
96 $this->assertActivityExists(ActivityType::ROLE_CREATE, null, $role->logDescriptor());
97 $this->assertEquals(4, $role->permissions()->count());
100 public function test_create_name_and_description_validation()
102 $this->actingAsApiAdmin();
103 /** @var User $existingUser */
104 $existingUser = User::query()->first();
106 $resp = $this->postJson($this->baseEndpoint, [
107 'description' => 'My new role',
109 $resp->assertStatus(422);
110 $resp->assertJson($this->validationResponse(['display_name' => ['The display_name field is required.']]));
112 $resp = $this->postJson($this->baseEndpoint, [
113 'name' => 'My great role with a too long desc',
114 'description' => str_repeat('My great desc', 20),
116 $resp->assertStatus(422);
117 $resp->assertJson($this->validationResponse(['description' => ['The description may not be greater than 180 characters.']]));
120 public function test_read_endpoint()
122 $this->actingAsApiAdmin();
123 $role = $this->users->editor()->roles()->first();
124 $resp = $this->getJson($this->baseEndpoint . "/{$role->id}");
126 $resp->assertStatus(200);
128 'display_name' => $role->display_name,
129 'description' => $role->description,
130 'mfa_enforced' => $role->mfa_enforced,
131 'permissions' => $role->permissions()->pluck('name')->toArray(),
132 'users' => $role->users()->get()->map(function (User $user) {
135 'name' => $user->name,
136 'slug' => $user->slug,
142 public function test_update_endpoint()
144 $this->actingAsApiAdmin();
145 $role = $this->users->editor()->roles()->first();
146 $resp = $this->putJson($this->baseEndpoint . "/{$role->id}", [
147 'display_name' => 'My updated role',
148 'description' => 'My great role description',
149 'mfa_enforced' => true,
158 $resp->assertStatus(200);
161 'display_name' => 'My updated role',
162 'description' => 'My great role description',
163 'mfa_enforced' => true,
173 $this->assertEquals(4, $role->permissions()->count());
176 public function test_update_endpoint_does_not_remove_info_if_not_provided()
178 $this->actingAsApiAdmin();
179 $role = $this->users->editor()->roles()->first();
180 $resp = $this->putJson($this->baseEndpoint . "/{$role->id}", []);
181 $permissionCount = $role->permissions()->count();
183 $resp->assertStatus(200);
184 $this->assertDatabaseHas('users', [
186 'display_name' => $role->display_name,
187 'description' => $role->description,
191 $this->assertEquals($permissionCount, $role->permissions()->count());
194 public function test_delete_endpoint()
196 $this->actingAsApiAdmin();
197 $role = $this->users->editor()->roles()->first();
199 $resp = $this->deleteJson($this->baseEndpoint . "/{$role->id}");
201 $resp->assertStatus(204);
202 $this->assertActivityExists(ActivityType::ROLE_DELETE, null, $role->logDescriptor());
205 public function test_delete_endpoint_fails_deleting_system_role()
207 $this->actingAsApiAdmin();
208 $adminRole = Role::getSystemRole('admin');
210 $resp = $this->deleteJson($this->baseEndpoint . "/{$adminRole->id}");
212 $resp->assertStatus(500);
213 $resp->assertJson($this->errorResponse('This role is a system role and cannot be deleted', 500));
216 public function test_delete_endpoint_fails_deleting_default_registration_role()
218 $this->actingAsApiAdmin();
219 $role = $this->users->attachNewRole($this->users->editor());
220 $this->setSettings(['registration-role' => $role->id]);
222 $resp = $this->deleteJson($this->baseEndpoint . "/{$role->id}");
224 $resp->assertStatus(500);
225 $resp->assertJson($this->errorResponse('This role cannot be deleted while set as the default registration role', 500));