From c4474fa0117b7d29c5ee1aea3137dd3c3b7be295 Mon Sep 17 00:00:00 2001 From: Jonas Rittershofer Date: Sun, 8 May 2022 18:06:58 +0200 Subject: [PATCH] Fix Tests Signed-off-by: Jonas Rittershofer --- .../Unit/Controller/ConfigControllerTest.php | 130 ++++++++++ .../Controller/ShareApiControllerTest.php | 35 ++- tests/Unit/Service/ConfigServiceTest.php | 228 ++++++++++++++++++ tests/Unit/Service/FormsServiceTest.php | 73 +++++- 4 files changed, 462 insertions(+), 4 deletions(-) create mode 100644 tests/Unit/Controller/ConfigControllerTest.php create mode 100644 tests/Unit/Service/ConfigServiceTest.php diff --git a/tests/Unit/Controller/ConfigControllerTest.php b/tests/Unit/Controller/ConfigControllerTest.php new file mode 100644 index 000000000..d8f384458 --- /dev/null +++ b/tests/Unit/Controller/ConfigControllerTest.php @@ -0,0 +1,130 @@ + + * + * @author Jonas Rittershofer + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OCA\Forms\Tests\Unit\Controller; + +use OCA\Forms\Controller\ConfigController; + +use OCA\Forms\Service\ConfigService; +use OCP\AppFramework\Http\DataResponse; +use OCP\IConfig; +use OCP\ILogger; +use OCP\IRequest; + +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +class ConfigControllerTest extends TestCase { + + /** @var ConfigController */ + private $configController; + + /** @var ConfigService */ + private $configService; + + /** @var IConfig|MockObject */ + private $config; + + /** @var ILogger|MockObject */ + private $logger; + + /** @var IRequest|MockObject */ + private $request; + + public function setUp(): void { + parent::setUp(); + + $this->configService = $this->createMock(ConfigService::class); + $this->config = $this->createMock(IConfig::class); + $this->logger = $this->createMock(ILogger::class); + $this->request = $this->createMock(IRequest::class); + + $this->configController = new ConfigController( + 'forms', + $this->configService, + $this->config, + $this->logger, + $this->request + ); + } + + public function testGetAppConfig() { + $this->configService->expects($this->once()) + ->method('getAppConfig') + ->willReturn([ + 'allow' => 'someConfig', + 'permit' => 'all' + ]); + + $this->assertEquals(new DataResponse([ + 'allow' => 'someConfig', + 'permit' => 'all' + ]), $this->configController->getAppConfig()); + } + + public function dataUpdateAppConfig() { + return [ + 'booleanConfig' => [ + 'configKey' => 'allowPermitAll', + 'configValue' => true, + 'strConfig' => "true" + ], + 'arrayConfig' => [ + 'configKey' => 'allowPermitAll', + 'configValue' => [ + "admin", + "group1" + ], + 'strConfig' => '["admin","group1"]' + ] + ]; + } + /** + * @dataProvider dataUpdateAppConfig + * + * @param string $configKey + * @param mixed $configValue + * @param string $strConfig The configValue as json-string + */ + public function testUpdateAppConfig(string $configKey, $configValue, string $strConfig) { + $this->logger->expects($this->once()) + ->method('debug'); + + $this->config->expects($this->once()) + ->method('setAppValue') + ->with('forms', $configKey, $strConfig); + + $this->assertEquals(new DataResponse(), $this->configController->updateAppConfig($configKey, $configValue)); + } + + public function testUpdateAppConfig_unknownKey() { + $this->logger->expects($this->once()) + ->method('debug'); + + $this->config->expects($this->never()) + ->method('setAppValue'); + + $this->assertEquals(new DataResponse('Unknown appConfig key: someUnknownKey', 400), $this->configController->updateAppConfig('someUnknownKey', 'storeThisValue!')); + } +} diff --git a/tests/Unit/Controller/ShareApiControllerTest.php b/tests/Unit/Controller/ShareApiControllerTest.php index 65e8053e3..ca5cdf6ff 100644 --- a/tests/Unit/Controller/ShareApiControllerTest.php +++ b/tests/Unit/Controller/ShareApiControllerTest.php @@ -30,6 +30,7 @@ use OCA\Forms\Db\FormMapper; use OCA\Forms\Db\Share; use OCA\Forms\Db\ShareMapper; +use OCA\Forms\Service\ConfigService; use OCA\Forms\Service\FormsService; use OCP\AppFramework\Db\DoesNotExistException; @@ -61,6 +62,9 @@ class ShareApiControllerTest extends TestCase { /** @var ShareMapper|MockObject */ private $shareMapper; + /** @var ConfigService|MockObject */ + private $configService; + /** @var FormsService|MockObject */ private $formsService; @@ -82,6 +86,7 @@ class ShareApiControllerTest extends TestCase { public function setUp(): void { $this->formMapper = $this->createMock(FormMapper::class); $this->shareMapper = $this->createMock(ShareMapper::class); + $this->configService = $this->createMock(ConfigService::class); $this->formsService = $this->createMock(FormsService::class); $this->groupManager = $this->createMock(IGroupManager::class); $this->logger = $this->createMock(ILogger::class); @@ -102,6 +107,7 @@ public function setUp(): void { 'forms', $this->formMapper, $this->shareMapper, + $this->configService, $this->formsService, $this->groupManager, $this->logger, @@ -238,6 +244,10 @@ public function testNewLinkShare(int $shareType, string $shareWith, array $expec ->with($share) ->willReturn($shareWithId); + $this->configService->expects($this->once()) + ->method('getAllowPublicLink') + ->willReturn(true); + $this->formsService->expects($this->once()) ->method('getShareDisplayName') ->with($shareWithId->read()) @@ -255,7 +265,7 @@ public function testNewLinkShare(int $shareType, string $shareWith, array $expec /** * Test a random link hash, that is already existing. */ - public function testExistingLinkHash() { + public function testNewLinkShare_ExistingHash() { $form = new Form(); $form->setId('5'); $form->setOwnerId('currentUser'); @@ -265,6 +275,10 @@ public function testExistingLinkHash() { ->with('5') ->willReturn($form); + $this->configService->expects($this->once()) + ->method('getAllowPublicLink') + ->willReturn(true); + $this->secureRandom->expects($this->any()) ->method('generate') ->willReturn('abcdefgh'); @@ -281,6 +295,25 @@ public function testExistingLinkHash() { $this->shareApiController->newShare(5, IShare::TYPE_LINK, ''); } + /** + * Test a random link hash, that is already existing. + */ + public function testNewLinkShare_PublicLinkNotAllowed() { + $form = new Form(); + $form->setId('5'); + $form->setOwnerId('currentUser'); + + $this->configService->expects($this->once()) + ->method('getAllowPublicLink') + ->willReturn(false); + + $this->shareMapper->expects($this->never()) + ->method('insert'); + + $this->expectException(OCSException::class); + $this->shareApiController->newShare(5, IShare::TYPE_LINK, ''); + } + /** * Test an unused (but existing) Share-Type */ diff --git a/tests/Unit/Service/ConfigServiceTest.php b/tests/Unit/Service/ConfigServiceTest.php new file mode 100644 index 000000000..d5cc2fc05 --- /dev/null +++ b/tests/Unit/Service/ConfigServiceTest.php @@ -0,0 +1,228 @@ + + * + * @author Jonas Rittershofer + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OCA\Forms\Tests\Unit\Service; + +use OCA\Forms\Service\ConfigService; + +use OCP\IConfig; +use OCP\IGroup; +use OCP\IGroupManager; +use OCP\ILogger; +use OCP\IUser; +use OCP\IUserSession; + +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +class ConfigServiceTest extends TestCase { + + /** @var ConfigService */ + private $configService; + + /** @var IConfig|MockObject */ + private $config; + + /** @var IGroupManager|MockObject */ + private $groupManager; + + /** @var ILogger|MockObject */ + private $logger; + + /** @var IUser|MockObject */ + private $currentUser; + + public function setUp(): void { + parent::setUp(); + + $this->config = $this->createMock(IConfig::class); + $this->groupManager = $this->createMock(IGroupManager::class); + $this->logger = $this->createMock(ILogger::class); + $userSession = $this->createMock(IUserSession::class); + + $this->currentUser = $this->createMock(IUser::class); + $this->currentUser->expects($this->any()) + ->method('getUID') + ->willReturn('currentUser'); + $userSession->expects($this->any()) + ->method('getUser') + ->willReturn($this->currentUser); + + $this->configService = new ConfigService( + 'forms', + $this->config, + $this->groupManager, + $this->logger, + $userSession + ); + } + + public function dataGetAppConfig() { + return [ + 'oneFullConfig' => [ + 'strConfig' => [ + 'allowPermitAll' => 'false', + 'allowPublicLink' => 'false', + 'creationAllowedGroups' => '["group1", "group2", "nonExisting"]', + 'restrictCreation' => 'true', + ], + 'groupDisplayNames' => [ + 'group1' => 'Group No. 1', + 'group2' => 'Group No. 2' + ], + 'expected' => [ + 'allowPermitAll' => false, + 'allowPublicLink' => false, + 'creationAllowedGroups' => [ + [ + 'groupId' => 'group1', + 'displayName' => 'Group No. 1' + ], + [ + 'groupId' => 'group2', + 'displayName' => 'Group No. 2' + ] + ], + 'restrictCreation' => true, + + 'canCreateForms' => false + ] + ] + ]; + } + /** + * @dataProvider dataGetAppConfig + * + * @param array $strConfig JSON Config Strings as stored in AppConfig + * @param array $groupDisplayNames DisplayNames for Testing + * @param array $expected + */ + public function testGetAppConfig(array $strConfig, array $groupDisplayNames, array $expected) { + // Default Values are set within getAppValue, thus returning this one. + $this->config->expects($this->any()) + ->method('getAppValue') + ->will($this->returnCallback(function ($appName, $configKey, $defaultVal) use ($strConfig) { + return $strConfig[$configKey]; + })); + + + // Mock Group formatting + $this->groupManager->expects($this->any()) + ->method('get') + ->will($this->returnCallback(function ($groupId) use ($groupDisplayNames) { + if (!array_key_exists($groupId, $groupDisplayNames)) { + return []; + } + $group = $this->createMock(IGroup::class); + $group->expects($this->once()) + ->method('getDisplayName') + ->willReturn($groupDisplayNames[$groupId]); + return $group; + })); + + // Return currentUser Groups + $this->groupManager->expects($this->once()) + ->method('getUserGroupIds') + ->with($this->currentUser) + ->willReturn([]); + + $this->assertEquals($expected, $this->configService->getAppConfig()); + } + + public function dataGetAppConfig_Default() { + return [ + 'defaultValues' => [ + 'expected' => [ + 'allowPermitAll' => true, + 'allowPublicLink' => true, + 'creationAllowedGroups' => [], + 'restrictCreation' => false, + + 'canCreateForms' => true + ] + ] + ]; + } + /** + * @dataProvider dataGetAppConfig_Default + * + * @param array $expected + */ + public function testGetAppConfig_Default(array $expected) { + // Default Values are set within getAppValue, thus returning this one. + $this->config->expects($this->any()) + ->method('getAppValue') + ->will($this->returnCallback(function ($appName, $configKey, $defaultVal) { + return $defaultVal; + })); + + $this->assertEquals($expected, $this->configService->getAppConfig()); + } + + public function dataCanCreateForms() { + return [ + 'notRestriced' => [ + 'config' => [ + 'restrictCreation' => 'false', + ], + 'expected' => true + ], + 'restrictedGroupAllowed' => [ + 'config' => [ + 'restrictCreation' => 'true', + 'creationAllowedGroups' => '["usersGroup","notUsersGroup"]' + ], + 'expected' => true + ], + 'restrictedNotInGroup' => [ + 'config' => [ + 'restrictCreation' => 'true', + 'creationAllowedGroups' => '["notUsersGroup"]' + ], + 'expected' => false + ] + ]; + } + + /** + * @dataProvider dataCanCreateForms + * + * @param array $config AppConfig with necessary values + * @param bool $expected + */ + public function testCanCreateForms(array $config, bool $expected) { + $this->config->expects($this->any()) + ->method('getAppValue') + ->will($this->returnCallback(function ($appName, $configKey, $defaultVal) use ($config) { + return $config[$configKey]; + })); + + $this->groupManager->expects($this->any()) + ->method('getUserGroupIds') + ->with($this->currentUser) + ->willReturn(["usersGroup"]); + + $this->assertEquals($expected, $this->configService->canCreateForms()); + } +} diff --git a/tests/Unit/Service/FormsServiceTest.php b/tests/Unit/Service/FormsServiceTest.php index 80585a83f..b8e1efbc7 100644 --- a/tests/Unit/Service/FormsServiceTest.php +++ b/tests/Unit/Service/FormsServiceTest.php @@ -36,6 +36,7 @@ use OCA\Forms\Db\Share; use OCA\Forms\Db\ShareMapper; use OCA\Forms\Db\SubmissionMapper; +use OCA\Forms\Service\ConfigService; use OCP\IGroup; use OCP\IGroupManager; @@ -71,6 +72,9 @@ class FormsServiceTest extends TestCase { /** @var SubmissionMapper|MockObject */ private $submissionMapper; + /** @var ConfigService|MockObject */ + private $configService; + /** @var IGroupManager|MockObject */ private $groupManager; @@ -88,6 +92,7 @@ public function setUp(): void { $this->questionMapper = $this->createMock(QuestionMapper::class); $this->shareMapper = $this->createMock(ShareMapper::class); $this->submissionMapper = $this->createMock(SubmissionMapper::class); + $this->configService = $this->createMock(ConfigService::class); $this->groupManager = $this->createMock(IGroupManager::class); $this->logger = $this->createMock(ILogger::class); @@ -109,6 +114,7 @@ public function setUp(): void { $this->questionMapper, $this->shareMapper, $this->submissionMapper, + $this->configService, $this->groupManager, $this->logger, $this->userManager, @@ -435,6 +441,10 @@ public function testGetPermissions(string $ownerId, array $access, array $expect ->with(42) ->willReturn([]); + $this->configService->expects($this->any()) + ->method('getAllowPermitAll') + ->willReturn(true); + $this->assertEquals($expected, $this->formsService->getPermissions(42)); } @@ -536,6 +546,7 @@ public function testPublicCanSubmit() { $this->questionMapper, $this->shareMapper, $this->submissionMapper, + $this->configService, $this->groupManager, $this->logger, $this->userManager, @@ -646,6 +657,10 @@ public function testHasUserAccess(array $accessArray, string $ownerId, bool $exp ->with(42) ->willReturn($form); + $this->configService->expects($this->any()) + ->method('getAllowPermitAll') + ->willReturn(true); + $this->assertEquals($expected, $this->formsService->hasUserAccess(42)); } @@ -673,6 +688,26 @@ public function testHasUserAccess_DirectShare() { $this->assertEquals(true, $this->formsService->hasUserAccess(42)); } + public function testHasUserAccess_PermitAllNotAllowed() { + $form = new Form(); + $form->setAccess([ + 'permitAllUsers' => true, + 'showToAllUsers' => true, + ]); + $form->setOwnerId('notCurrentUser'); + + $this->formMapper->expects($this->once()) + ->method('findById') + ->with(42) + ->willReturn($form); + + $this->configService->expects($this->once()) + ->method('getAllowPermitAll') + ->willReturn(false); + + $this->assertEquals(false, $this->formsService->hasUserAccess(42)); + } + public function testHasUserAccess_NotLoggedIn() { $userSession = $this->createMock(IUserSession::class); $userSession->expects($this->once()) @@ -686,6 +721,7 @@ public function testHasUserAccess_NotLoggedIn() { $this->questionMapper, $this->shareMapper, $this->submissionMapper, + $this->configService, $this->groupManager, $this->logger, $this->userManager, @@ -778,9 +814,13 @@ public function testIsSharedFormShown(string $ownerId, int $expires, array $acce $form->setAccess($access); $this->formMapper->expects($this->any()) - ->method('findById') - ->with(42) - ->willReturn($form); + ->method('findById') + ->with(42) + ->willReturn($form); + + $this->configService->expects($this->any()) + ->method('getAllowPermitAll') + ->willReturn(true); $share = new Share(); $share->setShareType($shareType); @@ -793,6 +833,33 @@ public function testIsSharedFormShown(string $ownerId, int $expires, array $acce $this->assertEquals($expected, $this->formsService->isSharedFormShown(42)); } + public function testIsSharedFormShown_PermitAllNotAllowed() { + $form = new Form(); + $form->setId(42); + $form->setOwnerId('notCurrentUser'); + $form->setExpires(false); + $form->setAccess([ + 'permitAllUsers' => true, + 'showToAllUsers' => true, + ]); + + $this->formMapper->expects($this->any()) + ->method('findById') + ->with(42) + ->willReturn($form); + + $this->configService->expects($this->any()) + ->method('getAllowPermitAll') + ->willReturn(false); + + $this->shareMapper->expects($this->any()) + ->method('findByForm') + ->with(42) + ->willReturn([]); + + $this->assertEquals(false, $this->formsService->isSharedFormShown(42)); + } + public function dataIsSharedToUser() { return [ 'sharedToUser' => [