-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ferdinand Thiessen <[email protected]>
- Loading branch information
Showing
2 changed files
with
266 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
<?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\IConfig; | ||
|
||
/** | ||
* This tests that the API respects all admin settings | ||
* @group DB | ||
*/ | ||
class RespectAdminSettingsTest extends IntegrationBase { | ||
/** @var GuzzleHttp\Client */ | ||
private $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 a test Form', | ||
'description' => '', | ||
'owner_id' => 'test', | ||
'access_enum' => 0, | ||
'created' => 12345, | ||
'expires' => 0, | ||
'state' => 0, | ||
'is_anonymous' => false, | ||
'submit_multiple' => false, | ||
'show_expiration' => false, | ||
'last_updated' => 123456789, | ||
'submission_message' => '', | ||
'file_id' => null, | ||
'file_format' => null, | ||
'questions' => [], | ||
'shares' => [], | ||
'submissions' => [], | ||
], | ||
[ | ||
'hash' => '1234567890abcdef', | ||
'title' => 'Title of a second globally shared Form', | ||
'description' => '', | ||
'owner_id' => 'test1', | ||
'access_enum' => 2, | ||
'created' => 12345, | ||
'expires' => 0, | ||
'state' => 0, | ||
'is_anonymous' => false, | ||
'submit_multiple' => 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, | ||
'is_anonymous' => false, | ||
'submit_multiple' => false, | ||
'show_expiration' => false, | ||
'last_updated' => 123456789, | ||
'submission_message' => '', | ||
'file_id' => null, | ||
'file_format' => null, | ||
'questions' => [], | ||
'shares' => [ | ||
[ | ||
'shareType' => 0, | ||
'shareWith' => 'test', | ||
'permissions' => ['submit'], | ||
], | ||
], | ||
'submissions' => [], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* 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']; | ||
} | ||
|
||
// Unset Id, as we can not control it on the tests. | ||
private function arrayUnsetId(array $arr): array { | ||
foreach ($arr as $index => $elem) { | ||
unset($arr[$index]['id']); | ||
} | ||
return $arr; | ||
} | ||
|
||
/** | ||
* 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); | ||
$this->assertEquals(200, $resp->getStatusCode()); | ||
$this->assertEquals($this->testForms[0], $data); | ||
} | ||
|
||
/** | ||
* Forbid to update form if there are admin settings | ||
* @dataProvider forbidUpdateAdminSettingsData | ||
*/ | ||
public function testForbidUpdate(array $accessValue, array $adminConfigKeys): void { | ||
$config = \OCP\Server::get(IConfig::class); | ||
foreach ($adminConfigKeys as $key) { | ||
$config->setAppValue(Application::APP_ID, $key, 'false'); | ||
} | ||
|
||
$resp = $this->http->request( | ||
'PATCH', | ||
"api/v3/forms/{$this->testForms[0]['id']}", | ||
[ | ||
'json' => [ | ||
'keyValuePairs' => ['access' => $accessValue], | ||
], | ||
], | ||
); | ||
$this->assertEquals(401, $resp->getStatusCode()); | ||
|
||
$resp = $this->http->request( | ||
'GET', | ||
"api/v3/forms/{$this->testForms[0]['id']}", | ||
); | ||
$data = $this->OcsResponse2Data($resp); | ||
$this->assertEquals(200, $resp->getStatusCode()); | ||
$this->assertEquals($this->testForms[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 {} | ||
|
||
/** | ||
* Test that only forms directly shared are listed if the admin setting forbid access to the form | ||
*/ | ||
public function testListFormsNoAdminPermission(): void {} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters