diff --git a/controller/internalcontroller.php b/controller/internalcontroller.php index 74a98ada..5bbad891 100644 --- a/controller/internalcontroller.php +++ b/controller/internalcontroller.php @@ -40,15 +40,17 @@ class InternalController extends ApiController { */ public function remind($credential_id) { $credential = $this->credentialService->getCredentialById($credential_id, $this->userId); - $credential->setExpireTime(time() + (24 * 60 * 60)); - $this->credentialService->upd($credential); + if($credential) { + $credential->setExpireTime(time() + (24 * 60 * 60)); + $this->credentialService->upd($credential); - $manager = \OC::$server->getNotificationManager(); - $notification = $manager->createNotification(); - $notification->setApp('passman') - ->setObject('credential', $credential_id) - ->setUser($this->userId); - $manager->markProcessed($notification); + $manager = \OC::$server->getNotificationManager(); + $notification = $manager->createNotification(); + $notification->setApp('passman') + ->setObject('credential', $credential_id) + ->setUser($this->userId); + $manager->markProcessed($notification); + } } /** @@ -57,15 +59,17 @@ class InternalController extends ApiController { public function read($credential_id) { $credential = $this->credentialService->getCredentialById($credential_id, $this->userId); - $credential->setExpireTime(0); - $this->credentialService->upd($credential); + if($credential) { + $credential->setExpireTime(0); + $this->credentialService->upd($credential); - $manager = \OC::$server->getNotificationManager(); - $notification = $manager->createNotification(); - $notification->setApp('passman') - ->setObject('credential', $credential_id) - ->setUser($this->userId); - $manager->markProcessed($notification); + $manager = \OC::$server->getNotificationManager(); + $notification = $manager->createNotification(); + $notification->setApp('passman') + ->setObject('credential', $credential_id) + ->setUser($this->userId); + $manager->markProcessed($notification); + } } /** diff --git a/controller/vaultcontroller.php b/controller/vaultcontroller.php index 4891aa6c..93df13b2 100644 --- a/controller/vaultcontroller.php +++ b/controller/vaultcontroller.php @@ -50,19 +50,20 @@ class VaultController extends ApiController { $vaults = $this->vaultService->getByUser($this->userId); $protected_credential_fields = array('getDescription', 'getEmail', 'getUsername', 'getPassword'); - - foreach ($vaults as $vault) { - $credential = $this->credentialService->getRandomCredentialByVaultId($vault->getId(), $this->userId); - $secret_field = $protected_credential_fields[array_rand($protected_credential_fields)]; - array_push($result, array( - 'vault_id' => $vault->getId(), - 'guid' => $vault->getGuid(), - 'name' => $vault->getName(), - 'created' => $vault->getCreated(), - 'public_sharing_key' => $vault->getPublicSharingKey(), - 'last_access' => $vault->getlastAccess(), - 'challenge_password' => $credential->{$secret_field}() - )); + if ($vaults) { + foreach ($vaults as $vault) { + $credential = $this->credentialService->getRandomCredentialByVaultId($vault->getId(), $this->userId); + $secret_field = $protected_credential_fields[array_rand($protected_credential_fields)]; + array_push($result, array( + 'vault_id' => $vault->getId(), + 'guid' => $vault->getGuid(), + 'name' => $vault->getName(), + 'created' => $vault->getCreated(), + 'public_sharing_key' => $vault->getPublicSharingKey(), + 'last_access' => $vault->getlastAccess(), + 'challenge_password' => $credential->{$secret_field}() + )); + } } return new JSONResponse($result); @@ -87,7 +88,7 @@ class VaultController extends ApiController { try { $vault = $this->vaultService->getByGuid($vault_guid, $this->userId); } catch (\Exception $e) { - return new NotFoundJSONResponse(); + return new NotFoundJSONResponse(); } $result = array(); if ($vault) { @@ -119,10 +120,10 @@ class VaultController extends ApiController { */ public function update($vault_guid, $name, $vault_settings) { $vault = $this->vaultService->getByGuid($vault_guid, $this->userId); - if ($name) { + if ($name && $vault) { $vault->setName($name); } - if ($vault_settings) { + if ($vault_settings && $vault) { $vault->setVaultSettings($vault_settings); } $this->vaultService->updateVault($vault); @@ -137,10 +138,13 @@ class VaultController extends ApiController { try { $vault = $this->vaultService->getByGuid($vault_guid, $this->userId); } catch (\Exception $e) { - + // No need to catch the execption + } + + if ($vault) { + $this->vaultService->updateSharingKeys($vault->getId(), $private_sharing_key, $public_sharing_key); } - $this->vaultService->updateSharingKeys($vault->getId(), $private_sharing_key, $public_sharing_key); return; } diff --git a/tests/unit/controller/FileControllerTest.php b/tests/unit/controller/FileControllerTest.php new file mode 100644 index 00000000..9e426c5c --- /dev/null +++ b/tests/unit/controller/FileControllerTest.php @@ -0,0 +1,99 @@ +. + * + */ + +namespace OCA\Passman\Controller; + +use OCA\Comments\Activity\Setting; +use OCA\Passman\Service\ActivityService; +use OCA\Passman\Service\CredentialService; +use OCA\Passman\Service\FileService; +use OCA\Passman\Service\NotificationService; +use OCA\Passman\Service\SettingsService; +use OCA\Passman\Service\ShareService; +use OCA\Passman\Service\VaultService; +use OCP\IGroupManager; +use OCP\IUserManager; +use PHPUnit_Framework_TestCase; + +use OCP\AppFramework\Http\JSONResponse; + +/** + * Class InternalControllerTest + * + * @package OCA\Passman\Controller + * @coversDefaultClass \OCA\Passman\Controller\FileController + */ +class FileControllerTest extends PHPUnit_Framework_TestCase { + + private $controller; + private $userId = 'example'; + private $credentialService; + private $vaultService; + private $groupManager; + private $userManager; + private $activityService; + private $shareService; + private $notificationService; + private $fileService; + private $settings; + + public function setUp() { + $request = $this->getMockBuilder('OCP\IRequest')->getMock(); + $this->fileService = $this->createMock(FileService::class); + $this->controller = new FileController( + 'passman', $request, $this->userId, $this->fileService + ); + } + + /** + * @covers ::uploadFile + */ + public function testUploadFile() { + $result = $this->controller->uploadFile('000', '0.png', 'image/png', 3); + $this->assertTrue($result instanceof JSONResponse); + } + + /** + * @covers ::getFile + */ + public function testGetFile() { + $result = $this->controller->getFile(null); + $this->assertTrue($result instanceof JSONResponse); + } + + /** + * @covers ::deleteFile + */ + public function testDeleteFile() { + $result = $this->controller->deleteFile(null); + $this->assertTrue($result instanceof JSONResponse); + } + + /** + * @covers ::updateFile + */ + public function testUpdateFile() { + $this->controller->updateFile('6AD30804-BFFC-4EFC-97F8-20A126FA1709', '0' , '0.jpg'); + $this->assertTrue(true); + } +} \ No newline at end of file diff --git a/tests/unit/controller/InternalControllerTest.php b/tests/unit/controller/InternalControllerTest.php index 5326b0db..0c62b6c7 100644 --- a/tests/unit/controller/InternalControllerTest.php +++ b/tests/unit/controller/InternalControllerTest.php @@ -50,6 +50,21 @@ class InternalControllerTest extends PHPUnit_Framework_TestCase { ); } + /** + * @covers ::remind + */ + public function testRemind() { + $this->controller->remind(null); + $this->assertTrue(true); + } + /** + * @covers ::read + */ + public function testRead() { + $this->controller->read(null); + $this->assertTrue(true); + } + /** * @covers ::getAppVersion */ @@ -65,4 +80,21 @@ class InternalControllerTest extends PHPUnit_Framework_TestCase { $result = $this->controller->generatePerson(); $this->assertTrue($result instanceof JSONResponse); } + + /** + * @covers ::getSettings + */ + public function testGetSettings() { + $result = $this->controller->getSettings(); + $this->assertTrue($result instanceof JSONResponse); + } + + /** + * @covers ::saveSettings + */ + public function testSaveSettings() { + $result = $this->controller->saveSettings('test', 'test'); + $this->assertTrue(true); + } + } \ No newline at end of file diff --git a/tests/unit/controller/SettingsControllerTest.php b/tests/unit/controller/SettingsControllerTest.php index 808e7b89..1f16af7c 100644 --- a/tests/unit/controller/SettingsControllerTest.php +++ b/tests/unit/controller/SettingsControllerTest.php @@ -58,13 +58,20 @@ class SettingsControllerTest extends PHPUnit_Framework_TestCase { $result = $this->controller->getForm(); $this->assertTrue($result instanceof TemplateResponse); } + /** + * @covers ::getSection + */ + public function testGetSection() { + $result = $this->controller->getSection(); + $this->assertTrue(is_string($result)); + } /** * @covers ::getPriority */ public function testGetPriority() { $result = $this->controller->getPriority(); - $this->assertTrue($result === 0); + $this->assertTrue(is_numeric($result)); } /** diff --git a/tests/unit/controller/VaultControllerTest.php b/tests/unit/controller/VaultControllerTest.php new file mode 100644 index 00000000..f44b8cb6 --- /dev/null +++ b/tests/unit/controller/VaultControllerTest.php @@ -0,0 +1,108 @@ +. + * + */ + +namespace OCA\Passman\Controller; + +use OCA\Passman\Service\CredentialService; +use OCA\Passman\Service\VaultService; +use PHPUnit_Framework_TestCase; + +use OCP\AppFramework\Http\JSONResponse; + +/** + * Class InternalControllerTest + * + * @package OCA\Passman\Controller + * @coversDefaultClass \OCA\Passman\Controller\VaultController + */ +class VaultControllerTest extends PHPUnit_Framework_TestCase { + + private $controller; + private $userId = 'example'; + private $credentialService; + private $vaultService; + + public function setUp() { + $request = $this->getMockBuilder('OCP\IRequest')->getMock(); + $this->vaultService = $this->createMock(VaultService::class); + $this->credentialService = $this->createMock(CredentialService::class); + + $this->controller = new VaultController( + 'passman', $request, $this->userId, $this->vaultService, $this->credentialService + ); + } + + /** + * @covers ::listVaults + */ + public function testListVaults() { + $result = $this->controller->listVaults(); + $this->assertTrue($result instanceof JSONResponse); + } + + /** + * @covers ::create + */ + public function testCreate() { + $result = $this->controller->create('My test vault'); + $this->assertTrue($result instanceof JSONResponse); + } + + /** + * @covers ::get + */ + public function testGet() { + $result = $this->controller->get(''); + $this->assertTrue($result instanceof JSONResponse); + } + + /** + * Just test the method + * @see http://stackoverflow.com/questions/27511593/how-to-phpunit-test-a-method-with-no-return-value + * + * @covers ::update + */ + public function testUpdate() { + $this->controller->update('6AD30804-BFFC-4EFC-97F8-20A126FA1709', 'testname' ,null); + $this->assertTrue(true); + } + + /** + * Just test the method + * @see http://stackoverflow.com/questions/27511593/how-to-phpunit-test-a-method-with-no-return-value + * + * @covers ::updateSharingKeys + */ + public function testUpdateSharingKeys() { + $this->controller->updateSharingKeys('6AD30804-BFFC-4EFC-97F8-20A126FA1709', null ,null); + $this->assertTrue(true); + } + + /** + * @covers ::delete + */ + public function testDelete() { + $result = $this->controller->delete(''); + $this->assertTrue($result instanceof JSONResponse); + } +} \ No newline at end of file diff --git a/tests/unit/lib/Service/SettingsServiceTest.php b/tests/unit/lib/Service/SettingsServiceTest.php index 1444263e..d3d3d1d3 100644 --- a/tests/unit/lib/Service/SettingsServiceTest.php +++ b/tests/unit/lib/Service/SettingsServiceTest.php @@ -33,7 +33,7 @@ use OCA\Passman\Service\SettingsService; * Class PageControllerTest * * @package OCA\Passman\Controller - * @coversDefaultClass \OCA\Passman\Service\SettingsServiceTest + * @coversDefaultClass \OCA\Passman\Service\SettingsService */ class SettingsServiceTest extends PHPUnit_Framework_TestCase {