-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathRespectAdminSettingsTest.php
More file actions
348 lines (322 loc) Β· 8.56 KB
/
RespectAdminSettingsTest.php
File metadata and controls
348 lines (322 loc) Β· 8.56 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Forms\Tests\Integration\Api;
use GuzzleHttp\Client;
use OCA\Forms\AppInfo\Application;
use OCA\Forms\Constants;
use OCA\Forms\Tests\Integration\IntegrationBase;
use OCP\IAppConfig;
/**
* This tests that the API respects all admin settings
* @group DB
*/
class RespectAdminSettingsTest extends IntegrationBase {
private Client $http;
protected array $users = [
'test' => 'Test user',
];
/**
* Store Test Forms Array.
* Necessary as function due to object type-casting.
*/
private function setTestForms() {
$this->testForms = [
[
'hash' => 'abcdefghij123456',
'title' => 'Title of owned Form',
'description' => '',
'owner_id' => 'test',
'access_enum' => 0,
'created' => 12345,
'expires' => 0,
'state' => 0,
'lockedBy' => null,
'lockedUntil' => null,
'is_anonymous' => false,
'submit_multiple' => false,
'allowEditSubmissions' => false,
'show_expiration' => false,
'last_updated' => 123456789,
'submission_message' => '',
'file_id' => null,
'file_format' => null,
'questions' => [],
'shares' => [],
'submissions' => [],
],
[
'hash' => '1234567890abcdef',
'title' => 'Title of a globally shared Form',
'description' => '',
'owner_id' => 'test1',
'access_enum' => 2,
'created' => 12345,
'expires' => 0,
'state' => 0,
'lockedBy' => null,
'lockedUntil' => null,
'is_anonymous' => false,
'submit_multiple' => false,
'allowEditSubmissions' => false,
'show_expiration' => false,
'last_updated' => 123456789,
'submission_message' => '',
'file_id' => null,
'file_format' => null,
'questions' => [],
'shares' => [],
'submissions' => [],
],
[
'hash' => 'bcdf011899881',
'title' => 'Title of a directly shared Form',
'description' => '',
'owner_id' => 'test1',
'access_enum' => 0,
'created' => 12345,
'expires' => 0,
'state' => 0,
'lockedBy' => null,
'lockedUntil' => null,
'is_anonymous' => false,
'submit_multiple' => false,
'allowEditSubmissions' => false,
'show_expiration' => false,
'last_updated' => 123456789,
'submission_message' => '',
'file_id' => null,
'file_format' => null,
'questions' => [],
'shares' => [
[
'shareType' => 0,
'shareWith' => 'test',
'permissions' => ['submit'],
],
],
'submissions' => [],
],
];
}
private static function sharedTestForms(): array {
return [
[
'hash' => 'abcdefghij123456',
'title' => 'Title of owned Form',
'description' => '',
'created' => 12345,
'expires' => 0,
'state' => 0,
'lockedBy' => null,
'lockedUntil' => null,
'questions' => [],
'shares' => [],
'ownerId' => 'test',
'fileId' => null,
'fileFormat' => null,
'access' => [
'permitAllUsers' => false,
'showToAllUsers' => false,
],
'isAnonymous' => false,
'submitMultiple' => false,
'allowEditSubmissions' => false,
'showExpiration' => false,
'submissionMessage' => '',
'permissions' => [
'edit',
'embed',
'results',
'results_delete',
'submit',
],
'canSubmit' => true,
'submissionCount' => 0,
'maxSubmissions' => null,
'isMaxSubmissionsReached' => false,
'confirmationEmailEnabled' => false,
'confirmationEmailSubject' => null,
'confirmationEmailBody' => null,
'confirmationEmailQuestionId' => null,
],
];
}
/**
* Set up test environment.
* Writing testforms into db, preparing http request
*/
public function setUp(): void {
$this->setTestForms();
$this->users = [
'test' => 'Test Displayname',
'user1' => 'User No. 1',
];
parent::setUp();
// Set up http Client
$this->http = new Client([
'base_uri' => 'http://localhost:8080/ocs/v2.php/apps/forms/',
'auth' => ['test', 'test'],
'headers' => [
'OCS-ApiRequest' => 'true',
'Accept' => 'application/json'
],
]);
}
public function tearDown(): void {
parent::tearDown();
}
// Small Wrapper for OCS-Response
private function OcsResponse2Data($resp) {
$arr = json_decode($resp->getBody()->getContents(), true);
return $arr['ocs']['data'];
}
/**
* Allow to update form if there are no admin settings
*/
public function testAllowUpdate(): void {
$resp = $this->http->request(
'PATCH',
"api/v3/forms/{$this->testForms[0]['id']}",
[
'json' => [
'keyValuePairs' => ['access' => ['permitAllUsers' => true, 'showToAllUsers' => true]],
],
],
);
$this->assertEquals(200, $resp->getStatusCode());
$resp = $this->http->request(
'GET',
"api/v3/forms/{$this->testForms[0]['id']}",
);
$data = $this->OcsResponse2Data($resp);
// we do not know the ID and the update time is flaky
unset($data['id']);
unset($data['lastUpdated']);
$data['lockedUntil'] = null;
$expected = self::sharedTestForms()[0];
$expected['access'] = ['permitAllUsers' => true, 'showToAllUsers' => true];
$expected['lockedBy'] = 'test';
$this->assertEquals(200, $resp->getStatusCode());
$this->assertEquals($expected, $data);
}
/**
* Forbid to update form if there are admin settings
* @dataProvider forbidUpdateAdminSettingsData
*/
public function testForbidUpdate(array $accessValue, array $adminConfigKeys): void {
$config = \OCP\Server::get(IAppConfig::class);
foreach ($adminConfigKeys as $key => $value) {
if (is_array($value)) {
$config->setValueArray(Application::APP_ID, $key, $value);
} else {
$bool = is_bool($value) ? $value : filter_var($value, FILTER_VALIDATE_BOOLEAN);
$config->setValueBool(Application::APP_ID, $key, $bool);
}
}
$resp = $this->http->request(
'PATCH',
"api/v3/forms/{$this->testForms[0]['id']}",
[
'json' => [
'keyValuePairs' => ['access' => $accessValue],
],
// do not throw on 403
'http_errors' => false,
],
);
$this->assertEquals(403, $resp->getStatusCode());
$resp = $this->http->request(
'GET',
"api/v3/forms/{$this->testForms[0]['id']}",
);
$data = $this->OcsResponse2Data($resp);
// we do not know the ID or the update
unset($data['id']);
unset($data['lastUpdated']);
$data['lockedUntil'] = null;
$this->assertEquals(200, $resp->getStatusCode());
$this->assertEquals(self::sharedTestForms()[0], $data);
}
public static function forbidUpdateAdminSettingsData(): array {
return [
'set both without show-to-all permission' => [
[
'permitAllUsers' => true,
'showToAllUsers' => true,
],
[
Constants::CONFIG_KEY_ALLOWSHOWTOALL => 'false',
Constants::CONFIG_KEY_ALLOWPERMITALL => 'true',
],
],
'set both without permit-all permission' => [
[
'permitAllUsers' => true,
'showToAllUsers' => true,
],
[
Constants::CONFIG_KEY_ALLOWSHOWTOALL => 'true',
Constants::CONFIG_KEY_ALLOWPERMITALL => 'false',
],
],
'set show-to-all without permission' => [
[
'showToAllUsers' => true,
],
[
Constants::CONFIG_KEY_ALLOWSHOWTOALL => 'false',
Constants::CONFIG_KEY_ALLOWPERMITALL => 'true',
],
],
'set permit-all without permission' => [
[
'permitAllUsers' => true,
],
[
Constants::CONFIG_KEY_ALLOWSHOWTOALL => 'true',
Constants::CONFIG_KEY_ALLOWPERMITALL => 'false',
],
],
];
}
/**
* Test that forms with public access are listed
*/
public function testListFormsAllowed(): void {
$resp = $this->http->request(
'GET',
'api/v3/forms?type=shared',
);
$this->assertEquals(200, $resp->getStatusCode());
$data = $this->OcsResponse2Data($resp);
$this->assertEqualsCanonicalizing(
[
'Title of a globally shared Form',
'Title of a directly shared Form',
],
array_map(fn ($form) => $form['title'], $data),
);
}
/**
* Test that only forms directly shared are listed if the admin setting forbid access to the form.
* Equivalent to creating form with "show to all" permission, but then the admin deactivates the "show all" globally.
*/
public function testListFormsNoAdminPermission(): void {
// Disable global access
\OCP\Server::get(IAppConfig::class)->setValueBool(Application::APP_ID, Constants::CONFIG_KEY_ALLOWPERMITALL, false);
$resp = $this->http->request(
'GET',
'api/v3/forms?type=shared',
);
$this->assertEquals(200, $resp->getStatusCode());
$data = $this->OcsResponse2Data($resp);
$this->assertEqualsCanonicalizing(
['Title of a directly shared Form'],
array_map(fn ($form) => $form['title'], $data),
);
}
};