Skip to content

Commit a2fb34a

Browse files
committed
fixup
Signed-off-by: Ferdinand Thiessen <[email protected]>
1 parent 00e9e70 commit a2fb34a

File tree

2 files changed

+317
-3
lines changed

2 files changed

+317
-3
lines changed
Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
namespace OCA\Forms\Tests\Integration\Api;
9+
10+
use GuzzleHttp\Client;
11+
use OCA\Forms\AppInfo\Application;
12+
use OCA\Forms\Constants;
13+
use OCA\Forms\Tests\Integration\IntegrationBase;
14+
use OCP\IConfig;
15+
16+
/**
17+
* This tests that the API respects all admin settings
18+
* @group DB
19+
*/
20+
class RespectAdminSettingsTest extends IntegrationBase {
21+
/** @var GuzzleHttp\Client */
22+
private $http;
23+
24+
protected array $users = [
25+
'test' => 'Test user',
26+
];
27+
28+
/**
29+
* Store Test Forms Array.
30+
* Necessary as function due to object type-casting.
31+
*/
32+
private function setTestForms() {
33+
$this->testForms = [
34+
[
35+
'hash' => 'abcdefghij123456',
36+
'title' => 'Title of a test Form',
37+
'description' => '',
38+
'owner_id' => 'test',
39+
'access_enum' => 0,
40+
'created' => 12345,
41+
'expires' => 0,
42+
'state' => 0,
43+
'is_anonymous' => false,
44+
'submit_multiple' => false,
45+
'show_expiration' => false,
46+
'last_updated' => 123456789,
47+
'submission_message' => '',
48+
'file_id' => null,
49+
'file_format' => null,
50+
'questions' => [],
51+
'shares' => [],
52+
'submissions' => [],
53+
],
54+
[
55+
'hash' => '1234567890abcdef',
56+
'title' => 'Title of a second globally shared Form',
57+
'description' => '',
58+
'owner_id' => 'test1',
59+
'access_enum' => 2,
60+
'created' => 12345,
61+
'expires' => 0,
62+
'state' => 0,
63+
'is_anonymous' => false,
64+
'submit_multiple' => false,
65+
'show_expiration' => false,
66+
'last_updated' => 123456789,
67+
'submission_message' => '',
68+
'file_id' => null,
69+
'file_format' => null,
70+
'questions' => [],
71+
'shares' => [],
72+
'submissions' => [],
73+
],
74+
[
75+
'hash' => 'bcdf011899881',
76+
'title' => 'Title of a directly shared Form',
77+
'description' => '',
78+
'owner_id' => 'test1',
79+
'access_enum' => 0,
80+
'created' => 12345,
81+
'expires' => 0,
82+
'state' => 0,
83+
'is_anonymous' => false,
84+
'submit_multiple' => false,
85+
'show_expiration' => false,
86+
'last_updated' => 123456789,
87+
'submission_message' => '',
88+
'file_id' => null,
89+
'file_format' => null,
90+
'questions' => [],
91+
'shares' => [
92+
[
93+
'shareType' => 0,
94+
'shareWith' => 'test',
95+
'permissions' => ['submit'],
96+
],
97+
],
98+
'submissions' => [],
99+
],
100+
];
101+
}
102+
103+
private static function sharedTestForms(): array {
104+
return [
105+
[
106+
'hash' => 'abcdefghij123456',
107+
'title' => 'Title of a test Form',
108+
'description' => '',
109+
'created' => 12345,
110+
'expires' => 0,
111+
'state' => 0,
112+
'questions' => [],
113+
'shares' => [],
114+
'ownerId' => 'test',
115+
'fileId' => null,
116+
'fileFormat' => null,
117+
'access' => [],
118+
'isAnonymous' => false,
119+
'submitMultiple' => false,
120+
'showExpiration' => false,
121+
'submissionMessage' => '',
122+
'permissions' => [],
123+
'canSubmit' => true,
124+
'submissionCount' => 0,
125+
],
126+
];
127+
}
128+
129+
/**
130+
* Set up test environment.
131+
* Writing testforms into db, preparing http request
132+
*/
133+
public function setUp(): void {
134+
$this->setTestForms();
135+
$this->users = [
136+
'test' => 'Test Displayname',
137+
'user1' => 'User No. 1',
138+
];
139+
140+
parent::setUp();
141+
142+
// Set up http Client
143+
$this->http = new Client([
144+
'base_uri' => 'http://localhost:8080/ocs/v2.php/apps/forms/',
145+
'auth' => ['test', 'test'],
146+
'headers' => [
147+
'OCS-ApiRequest' => 'true',
148+
'Accept' => 'application/json'
149+
],
150+
]);
151+
}
152+
153+
public function tearDown(): void {
154+
parent::tearDown();
155+
}
156+
157+
// Small Wrapper for OCS-Response
158+
private function OcsResponse2Data($resp) {
159+
$arr = json_decode($resp->getBody()->getContents(), true);
160+
return $arr['ocs']['data'];
161+
}
162+
163+
// Unset Id, as we can not control it on the tests.
164+
private function arrayUnsetId(array $arr): array {
165+
foreach ($arr as $index => $elem) {
166+
unset($arr[$index]['id']);
167+
}
168+
return $arr;
169+
}
170+
171+
/**
172+
* Allow to update form if there are no admin settings
173+
*/
174+
public function testAllowUpdate(): void {
175+
$resp = $this->http->request(
176+
'PATCH',
177+
"api/v3/forms/{$this->testForms[0]['id']}",
178+
[
179+
'json' => [
180+
'keyValuePairs' => ['access' => ['permitAllUsers' => true, 'showToAllUsers' => true]],
181+
],
182+
],
183+
);
184+
$this->assertEquals(200, $resp->getStatusCode());
185+
186+
$resp = $this->http->request(
187+
'GET',
188+
"api/v3/forms/{$this->testForms[0]['id']}",
189+
);
190+
$data = $this->OcsResponse2Data($resp);
191+
192+
$expected = self::sharedTestForms()[0];
193+
$expected['access'] = ['permitAllUsers' => true, 'showToAllUsers' => true];
194+
195+
$this->assertEquals(200, $resp->getStatusCode());
196+
$this->assertEquals($expected, $this->arrayUnsetId($data));
197+
}
198+
199+
/**
200+
* Forbid to update form if there are admin settings
201+
* @dataProvider forbidUpdateAdminSettingsData
202+
*/
203+
public function testForbidUpdate(array $accessValue, array $adminConfigKeys): void {
204+
$config = \OCP\Server::get(IConfig::class);
205+
foreach ($adminConfigKeys as $key => $value) {
206+
$config->setAppValue(Application::APP_ID, $key, $value);
207+
}
208+
209+
$resp = $this->http->request(
210+
'PATCH',
211+
"api/v3/forms/{$this->testForms[0]['id']}",
212+
[
213+
'json' => [
214+
'keyValuePairs' => ['access' => $accessValue],
215+
],
216+
],
217+
);
218+
$this->assertEquals(401, $resp->getStatusCode());
219+
220+
$resp = $this->http->request(
221+
'GET',
222+
"api/v3/forms/{$this->testForms[0]['id']}",
223+
);
224+
$data = $this->OcsResponse2Data($resp);
225+
226+
$this->assertEquals(200, $resp->getStatusCode());
227+
$this->assertEquals(self::sharedTestForms()[0], $this->arrayUnsetId($data));
228+
}
229+
230+
public static function forbidUpdateAdminSettingsData(): array {
231+
return [
232+
'set both without show-to-all permission' => [
233+
[
234+
'permitAllUsers' => true,
235+
'showToAllUsers' => true,
236+
],
237+
[
238+
Constants::CONFIG_KEY_ALLOWSHOWTOALL => 'false',
239+
Constants::CONFIG_KEY_ALLOWPERMITALL => 'true',
240+
],
241+
],
242+
'set both without permit-all permission' => [
243+
[
244+
'permitAllUsers' => true,
245+
'showToAllUsers' => true,
246+
],
247+
[
248+
Constants::CONFIG_KEY_ALLOWSHOWTOALL => 'true',
249+
Constants::CONFIG_KEY_ALLOWPERMITALL => 'false',
250+
],
251+
],
252+
'set show-to-all without permission' => [
253+
[
254+
'showToAllUsers' => true,
255+
],
256+
[
257+
Constants::CONFIG_KEY_ALLOWSHOWTOALL => 'false',
258+
Constants::CONFIG_KEY_ALLOWPERMITALL => 'true',
259+
],
260+
],
261+
'set permit-all without permission' => [
262+
[
263+
'permitAllUsers' => true,
264+
],
265+
[
266+
Constants::CONFIG_KEY_ALLOWSHOWTOALL => 'true',
267+
Constants::CONFIG_KEY_ALLOWPERMITALL => 'false',
268+
],
269+
],
270+
];
271+
}
272+
273+
/**
274+
* Test that forms with public access are listed
275+
*/
276+
public function testListFormsAllowed(): void {
277+
$resp = $this->http->request(
278+
'GET',
279+
"api/v3/forms?type=shared",
280+
);
281+
$this->assertEquals(200, $resp->getStatusCode());
282+
283+
$data = $this->OcsResponse2Data($resp);
284+
$this->assertEquals(2, count($data));
285+
}
286+
287+
/**
288+
* Test that only forms directly shared are listed if the admin setting forbid access to the form.
289+
* Equivalent to creating form with "show to all" permission, but then the admin deactivates the "show all" globally.
290+
*/
291+
public function testListFormsNoAdminPermission(): void {
292+
$resp = $this->http->request(
293+
'GET',
294+
"api/v3/forms?type=shared",
295+
);
296+
$this->assertEquals(200, $resp->getStatusCode());
297+
298+
$data = $this->OcsResponse2Data($resp);
299+
$this->assertEquals(1, count($data));
300+
$this->assertEquals('Title of a directly shared Form', $data[0]['title']);
301+
}
302+
303+
};

tests/Integration/IntegrationBase.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
*/
88
namespace OCA\Forms\Tests\Integration;
99

10+
use OCA\Forms\AppInfo\Application;
11+
use OCA\Forms\Constants;
1012
use OCP\DB\QueryBuilder\IQueryBuilder;
13+
use OCP\IConfig;
14+
use OCP\IDBConnection;
15+
use OCP\IUserManager;
1116
use Test\TestCase;
1217

1318
/**
@@ -30,7 +35,12 @@ class IntegrationBase extends TestCase {
3035
public function setUp(): void {
3136
parent::setUp();
3237

33-
$userManager = \OC::$server->getUserManager();
38+
$config = \OCP\Server::get(IConfig::class);
39+
foreach (Constants::CONFIG_KEYS as $key) {
40+
$config->deleteAppValue(Application::APP_ID, $key);
41+
}
42+
43+
$userManager = \OCP\Server::get(IUserManager::class);
3444
foreach ($this->users as $userId => $displayName) {
3545
$user = $userManager->get($userId);
3646
if ($user === null) {
@@ -39,7 +49,7 @@ public function setUp(): void {
3949
$user->setDisplayName($displayName);
4050
}
4151

42-
$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
52+
$qb = \OCP\Server::get(IDBConnection::class)->getQueryBuilder();
4353

4454
// Write our test forms into db
4555
foreach ($this->testForms as $index => $form) {
@@ -136,7 +146,8 @@ public function setUp(): void {
136146

137147
/** Clean up database from testforms */
138148
public function tearDown(): void {
139-
$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
149+
$db = \OCP\Server::get(IDBConnection::class);
150+
$qb = $db->getQueryBuilder();
140151

141152
foreach ($this->testForms as $form) {
142153
$qb->delete('forms_v2_forms')

0 commit comments

Comments
 (0)