5 use BookStack\Activity\ActivityType;
6 use BookStack\Users\Models\Role;
7 use BookStack\Users\Models\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 'external_auth_id' => $firstRole->external_auth_id,
48 'permissions_count' => $firstRole->permissions()->count(),
49 'users_count' => $firstRole->users()->count(),
50 'created_at' => $firstRole->created_at->toJSON(),
51 'updated_at' => $firstRole->updated_at->toJSON(),
55 $resp->assertJson(['total' => Role::query()->count()]);
58 public function test_create_endpoint()
60 $this->actingAsApiAdmin();
61 /** @var Role $role */
62 $role = Role::query()->first();
64 $resp = $this->postJson($this->baseEndpoint, [
65 'display_name' => 'My awesome role',
66 'description' => 'My great role description',
67 'mfa_enforced' => true,
68 'external_auth_id' => 'auth_id',
77 $resp->assertStatus(200);
79 'display_name' => 'My awesome role',
80 'description' => 'My great role description',
81 'mfa_enforced' => true,
82 'external_auth_id' => 'auth_id',
91 $this->assertDatabaseHas('roles', [
92 'display_name' => 'My awesome role',
93 'description' => 'My great role description',
94 'mfa_enforced' => true,
95 'external_auth_id' => 'auth_id',
98 /** @var Role $role */
99 $role = Role::query()->where('display_name', '=', 'My awesome role')->first();
100 $this->assertActivityExists(ActivityType::ROLE_CREATE, null, $role->logDescriptor());
101 $this->assertEquals(4, $role->permissions()->count());
104 public function test_create_name_and_description_validation()
106 $this->actingAsApiAdmin();
107 /** @var User $existingUser */
108 $existingUser = User::query()->first();
110 $resp = $this->postJson($this->baseEndpoint, [
111 'description' => 'My new role',
113 $resp->assertStatus(422);
114 $resp->assertJson($this->validationResponse(['display_name' => ['The display name field is required.']]));
116 $resp = $this->postJson($this->baseEndpoint, [
117 'name' => 'My great role with a too long desc',
118 'description' => str_repeat('My great desc', 20),
120 $resp->assertStatus(422);
121 $resp->assertJson($this->validationResponse(['description' => ['The description may not be greater than 180 characters.']]));
124 public function test_read_endpoint()
126 $this->actingAsApiAdmin();
127 $role = $this->users->editor()->roles()->first();
128 $resp = $this->getJson($this->baseEndpoint . "/{$role->id}");
130 $resp->assertStatus(200);
132 'display_name' => $role->display_name,
133 'description' => $role->description,
134 'mfa_enforced' => $role->mfa_enforced,
135 'external_auth_id' => $role->external_auth_id,
136 'permissions' => $role->permissions()->orderBy('name', 'asc')->pluck('name')->toArray(),
137 'users' => $role->users()->get()->map(function (User $user) {
140 'name' => $user->name,
141 'slug' => $user->slug,
147 public function test_update_endpoint()
149 $this->actingAsApiAdmin();
150 $role = $this->users->editor()->roles()->first();
151 $resp = $this->putJson($this->baseEndpoint . "/{$role->id}", [
152 'display_name' => 'My updated role',
153 'description' => 'My great role description',
154 'mfa_enforced' => true,
155 'external_auth_id' => 'updated_auth_id',
164 $resp->assertStatus(200);
167 'display_name' => 'My updated role',
168 'description' => 'My great role description',
169 'mfa_enforced' => true,
170 'external_auth_id' => 'updated_auth_id',
180 $this->assertEquals(4, $role->permissions()->count());
181 $this->assertActivityExists(ActivityType::ROLE_UPDATE);
184 public function test_update_endpoint_does_not_remove_info_if_not_provided()
186 $this->actingAsApiAdmin();
187 $role = $this->users->editor()->roles()->first();
188 $resp = $this->putJson($this->baseEndpoint . "/{$role->id}", []);
189 $permissionCount = $role->permissions()->count();
191 $resp->assertStatus(200);
192 $this->assertDatabaseHas('roles', [
194 'display_name' => $role->display_name,
195 'description' => $role->description,
196 'external_auth_id' => $role->external_auth_id,
200 $this->assertEquals($permissionCount, $role->permissions()->count());
203 public function test_delete_endpoint()
205 $this->actingAsApiAdmin();
206 $role = $this->users->editor()->roles()->first();
208 $resp = $this->deleteJson($this->baseEndpoint . "/{$role->id}");
210 $resp->assertStatus(204);
211 $this->assertActivityExists(ActivityType::ROLE_DELETE, null, $role->logDescriptor());
214 public function test_delete_endpoint_fails_deleting_system_role()
216 $this->actingAsApiAdmin();
217 $adminRole = Role::getSystemRole('admin');
219 $resp = $this->deleteJson($this->baseEndpoint . "/{$adminRole->id}");
221 $resp->assertStatus(500);
222 $resp->assertJson($this->errorResponse('This role is a system role and cannot be deleted', 500));
225 public function test_delete_endpoint_fails_deleting_default_registration_role()
227 $this->actingAsApiAdmin();
228 $role = $this->users->attachNewRole($this->users->editor());
229 $this->setSettings(['registration-role' => $role->id]);
231 $resp = $this->deleteJson($this->baseEndpoint . "/{$role->id}");
233 $resp->assertStatus(500);
234 $resp->assertJson($this->errorResponse('This role cannot be deleted while set as the default registration role', 500));