forked from BookStackApp/BookStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRolesApiTest.php
More file actions
236 lines (202 loc) · 8.15 KB
/
RolesApiTest.php
File metadata and controls
236 lines (202 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
namespace Tests\Api;
use BookStack\Activity\ActivityType;
use BookStack\Users\Models\Role;
use BookStack\Users\Models\User;
use Tests\TestCase;
class RolesApiTest extends TestCase
{
use TestsApi;
protected string $baseEndpoint = '/api/roles';
protected array $endpointMap = [
['get', '/api/roles'],
['post', '/api/roles'],
['get', '/api/roles/1'],
['put', '/api/roles/1'],
['delete', '/api/roles/1'],
];
public function test_user_roles_manage_permission_needed_for_all_endpoints()
{
$this->actingAsApiEditor();
foreach ($this->endpointMap as [$method, $uri]) {
$resp = $this->json($method, $uri);
$resp->assertStatus(403);
$resp->assertJson($this->permissionErrorResponse());
}
}
public function test_index_endpoint_returns_expected_role_and_count()
{
$this->actingAsApiAdmin();
/** @var Role $firstRole */
$firstRole = Role::query()->orderBy('id', 'asc')->first();
$resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
$resp->assertJson(['data' => [
[
'id' => $firstRole->id,
'display_name' => $firstRole->display_name,
'description' => $firstRole->description,
'mfa_enforced' => $firstRole->mfa_enforced,
'external_auth_id' => $firstRole->external_auth_id,
'permissions_count' => $firstRole->permissions()->count(),
'users_count' => $firstRole->users()->count(),
'created_at' => $firstRole->created_at->toJSON(),
'updated_at' => $firstRole->updated_at->toJSON(),
],
]]);
$resp->assertJson(['total' => Role::query()->count()]);
}
public function test_create_endpoint()
{
$this->actingAsApiAdmin();
/** @var Role $role */
$role = Role::query()->first();
$resp = $this->postJson($this->baseEndpoint, [
'display_name' => 'My awesome role',
'description' => 'My great role description',
'mfa_enforced' => true,
'external_auth_id' => 'auth_id',
'permissions' => [
'content-export',
'page-view-all',
'page-view-own',
'users-manage',
]
]);
$resp->assertStatus(200);
$resp->assertJson([
'display_name' => 'My awesome role',
'description' => 'My great role description',
'mfa_enforced' => true,
'external_auth_id' => 'auth_id',
'permissions' => [
'content-export',
'page-view-all',
'page-view-own',
'users-manage',
]
]);
$this->assertDatabaseHas('roles', [
'display_name' => 'My awesome role',
'description' => 'My great role description',
'mfa_enforced' => true,
'external_auth_id' => 'auth_id',
]);
/** @var Role $role */
$role = Role::query()->where('display_name', '=', 'My awesome role')->first();
$this->assertActivityExists(ActivityType::ROLE_CREATE, null, $role->logDescriptor());
$this->assertEquals(4, $role->permissions()->count());
}
public function test_create_name_and_description_validation()
{
$this->actingAsApiAdmin();
/** @var User $existingUser */
$existingUser = User::query()->first();
$resp = $this->postJson($this->baseEndpoint, [
'description' => 'My new role',
]);
$resp->assertStatus(422);
$resp->assertJson($this->validationResponse(['display_name' => ['The display name field is required.']]));
$resp = $this->postJson($this->baseEndpoint, [
'name' => 'My great role with a too long desc',
'description' => str_repeat('My great desc', 20),
]);
$resp->assertStatus(422);
$resp->assertJson($this->validationResponse(['description' => ['The description may not be greater than 180 characters.']]));
}
public function test_read_endpoint()
{
$this->actingAsApiAdmin();
$role = $this->users->editor()->roles()->first();
$resp = $this->getJson($this->baseEndpoint . "/{$role->id}");
$resp->assertStatus(200);
$resp->assertJson([
'display_name' => $role->display_name,
'description' => $role->description,
'mfa_enforced' => $role->mfa_enforced,
'external_auth_id' => $role->external_auth_id,
'permissions' => $role->permissions()->orderBy('name', 'asc')->pluck('name')->toArray(),
'users' => $role->users()->get()->map(function (User $user) {
return [
'id' => $user->id,
'name' => $user->name,
'slug' => $user->slug,
];
})->toArray(),
]);
}
public function test_update_endpoint()
{
$this->actingAsApiAdmin();
$role = $this->users->editor()->roles()->first();
$resp = $this->putJson($this->baseEndpoint . "/{$role->id}", [
'display_name' => 'My updated role',
'description' => 'My great role description',
'mfa_enforced' => true,
'external_auth_id' => 'updated_auth_id',
'permissions' => [
'content-export',
'page-view-all',
'page-view-own',
'users-manage',
]
]);
$resp->assertStatus(200);
$resp->assertJson([
'id' => $role->id,
'display_name' => 'My updated role',
'description' => 'My great role description',
'mfa_enforced' => true,
'external_auth_id' => 'updated_auth_id',
'permissions' => [
'content-export',
'page-view-all',
'page-view-own',
'users-manage',
]
]);
$role->refresh();
$this->assertEquals(4, $role->permissions()->count());
$this->assertActivityExists(ActivityType::ROLE_UPDATE);
}
public function test_update_endpoint_does_not_remove_info_if_not_provided()
{
$this->actingAsApiAdmin();
$role = $this->users->editor()->roles()->first();
$resp = $this->putJson($this->baseEndpoint . "/{$role->id}", []);
$permissionCount = $role->permissions()->count();
$resp->assertStatus(200);
$this->assertDatabaseHas('roles', [
'id' => $role->id,
'display_name' => $role->display_name,
'description' => $role->description,
'external_auth_id' => $role->external_auth_id,
]);
$role->refresh();
$this->assertEquals($permissionCount, $role->permissions()->count());
}
public function test_delete_endpoint()
{
$this->actingAsApiAdmin();
$role = $this->users->editor()->roles()->first();
$resp = $this->deleteJson($this->baseEndpoint . "/{$role->id}");
$resp->assertStatus(204);
$this->assertActivityExists(ActivityType::ROLE_DELETE, null, $role->logDescriptor());
}
public function test_delete_endpoint_fails_deleting_system_role()
{
$this->actingAsApiAdmin();
$adminRole = Role::getSystemRole('admin');
$resp = $this->deleteJson($this->baseEndpoint . "/{$adminRole->id}");
$resp->assertStatus(500);
$resp->assertJson($this->errorResponse('This role is a system role and cannot be deleted', 500));
}
public function test_delete_endpoint_fails_deleting_default_registration_role()
{
$this->actingAsApiAdmin();
$role = $this->users->attachNewRole($this->users->editor());
$this->setSettings(['registration-role' => $role->id]);
$resp = $this->deleteJson($this->baseEndpoint . "/{$role->id}");
$resp->assertStatus(500);
$resp->assertJson($this->errorResponse('This role cannot be deleted while set as the default registration role', 500));
}
}