diff --git a/lib/Alchemy/Phrasea/Model/Manipulator/ApiOauthCodeManipulator.php b/lib/Alchemy/Phrasea/Model/Manipulator/ApiOauthCodeManipulator.php index 12b84012f9..b8252202fd 100644 --- a/lib/Alchemy/Phrasea/Model/Manipulator/ApiOauthCodeManipulator.php +++ b/lib/Alchemy/Phrasea/Model/Manipulator/ApiOauthCodeManipulator.php @@ -13,6 +13,7 @@ namespace Alchemy\Phrasea\Model\Manipulator; use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Authentication\ACLProvider; +use Alchemy\Phrasea\Exception\InvalidArgumentException; use Alchemy\Phrasea\Model\Entities\ApiAccount; use Alchemy\Phrasea\Model\Entities\ApiOauthCode; use Alchemy\Phrasea\Model\Entities\User; diff --git a/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiAccountManipulatorTest.php b/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiAccountManipulatorTest.php index ae4dafb9af..90e7da671f 100644 --- a/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiAccountManipulatorTest.php +++ b/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiAccountManipulatorTest.php @@ -23,12 +23,13 @@ class ApiAccountManipulatorTest extends \PhraseanetTestCase { $manipulator = new ApiAccountManipulator(self::$DI['app']['EM'], self::$DI['app']['repo.api-accounts']); $account = $manipulator->create(self::$DI['oauth2-app-user'], self::$DI['user']); + $accountMem = clone $account; $countBefore = count(self::$DI['app']['repo.api-accounts']->findAll()); - /** - * @todo Link token and tests if token is deleted too - */ + self::$DI['app']['manipulator.api-oauth-token']->create($account); $manipulator->delete($account); $this->assertGreaterThan(count(self::$DI['app']['repo.api-accounts']->findAll()), $countBefore); + $tokens = self::$DI['app']['repo.api-oauth-tokens']->findOauthTokens($accountMem); + $this->assertEquals(0, count($tokens)); } public function testUpdate() diff --git a/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiApplicationManipulatorTest.php b/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiApplicationManipulatorTest.php index 167d6577e5..2e6c64977a 100644 --- a/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiApplicationManipulatorTest.php +++ b/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiApplicationManipulatorTest.php @@ -60,12 +60,16 @@ class ApiApplicationManipulatorTest extends \PhraseanetTestCase 'Desktop application description', 'http://desktop-app-url.net' ); + $applicationSave = clone $application; $countBefore = count(self::$DI['app']['repo.api-applications']->findAll()); - /** - * @todo Link accounts and tokens to application and tests if everything is deleted - */ + $account = self::$DI['app']['manipulator.api-account']->create($application, self::$DI['user']); + self::$DI['app']['manipulator.api-oauth-token']->create($account); $manipulator->delete($application); $this->assertGreaterThan(count(self::$DI['app']['repo.api-applications']->findAll()), $countBefore); + $accounts = self::$DI['app']['repo.api-accounts']->findByUserAndApplication(self::$DI['user'], $applicationSave); + $this->assertEquals(0, count($accounts)); + $tokens = self::$DI['app']['repo.api-oauth-tokens']->findOauthTokens($account); + $this->assertEquals(0, count($tokens)); } public function testUpdate() diff --git a/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiOauthCodeManipulatorTest.php b/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiOauthCodeManipulatorTest.php new file mode 100644 index 0000000000..faea618fb9 --- /dev/null +++ b/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiOauthCodeManipulatorTest.php @@ -0,0 +1,59 @@ +findAll()); + $account = self::$DI['app']['manipulator.api-account']->create(self::$DI['oauth2-app-user'], self::$DI['user']); + $manipulator->create($account, 'http://www.redirect.url'); + $this->assertGreaterThan($nbCodes, count(self::$DI['app']['repo.api-oauth-codes']->findAll())); + } + + public function testDelete() + { + $manipulator = new ApiOauthCodeManipulator(self::$DI['app']['EM'], self::$DI['app']['repo.api-oauth-codes'], self::$DI['app']['random.medium']); + $account = self::$DI['app']['manipulator.api-account']->create(self::$DI['oauth2-app-user'], self::$DI['user']); + $code = $manipulator->create($account, 'http://www.redirect.url'); + $countBefore = count(self::$DI['app']['repo.api-oauth-codes']->findAll()); + $manipulator->delete($code); + $this->assertGreaterThan(count(self::$DI['app']['repo.api-oauth-codes']->findAll()), $countBefore); + } + + public function testUpdate() + { + + $manipulator = new ApiOauthCodeManipulator(self::$DI['app']['EM'], self::$DI['app']['repo.api-oauth-codes'], self::$DI['app']['random.medium']); + $account = self::$DI['app']['manipulator.api-account']->create(self::$DI['oauth2-app-user'], self::$DI['user']); + $code = $manipulator->create($account, 'http://www.redirect.url'); + $code->setExpires(new \DateTime()); + $manipulator->update($code); + $code = self::$DI['app']['repo.api-oauth-codes']->find($code->getCode()); + $this->assertNotNull($code->getExpires()); + } + + /** + * @setExpectedException Alchemy\Phrasea\Exception\InvalidArgumentException + */ + public function testSetRedirectUriBadArgumentException() + { + $manipulator = new ApiOauthCodeManipulator(self::$DI['app']['EM'], self::$DI['app']['repo.api-oauth-codes'], self::$DI['app']['random.medium']); + $account = self::$DI['app']['manipulator.api-account']->create(self::$DI['oauth2-app-user'], self::$DI['user']); + $code = $manipulator->create($account, 'http://www.redirect.url'); + try { + $manipulator->setRedirectUri($code, 'bad-url'); + $this->fail('Invalid argument exception should be raised'); + } catch (InvalidArgumentException $e) { + + } + } +} diff --git a/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiOauthTokenManipulatorTest.php b/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiOauthTokenManipulatorTest.php new file mode 100644 index 0000000000..6096b6ae66 --- /dev/null +++ b/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiOauthTokenManipulatorTest.php @@ -0,0 +1,52 @@ +findAll()); + $account = self::$DI['app']['manipulator.api-account']->create(self::$DI['oauth2-app-user'], self::$DI['user']); + $manipulator->create($account); + $this->assertGreaterThan($nbTokens, count(self::$DI['app']['repo.api-oauth-tokens']->findAll())); + } + + public function testDelete() + { + $manipulator = new ApiOauthTokenManipulator(self::$DI['app']['EM'], self::$DI['app']['repo.api-oauth-tokens'], self::$DI['app']['random.medium']); + $account = self::$DI['app']['manipulator.api-account']->create(self::$DI['oauth2-app-user'], self::$DI['user']); + $token = $manipulator->create($account); + $countBefore = count(self::$DI['app']['repo.api-oauth-tokens']->findAll()); + $manipulator->delete($token); + $this->assertGreaterThan(count(self::$DI['app']['repo.api-oauth-tokens']->findAll()), $countBefore); + } + + public function testUpdate() + { + + $manipulator = new ApiOauthTokenManipulator(self::$DI['app']['EM'], self::$DI['app']['repo.api-oauth-tokens'], self::$DI['app']['random.medium']); + $account = self::$DI['app']['manipulator.api-account']->create(self::$DI['oauth2-app-user'], self::$DI['user']); + $token = $manipulator->create($account); + $token->setSessionId(123456); + $manipulator->update($token); + $token = self::$DI['app']['repo.api-oauth-tokens']->find($token->getOauthToken()); + $this->assertEquals(123456, $token->getSessionId()); + } + + public function testRenew() + { + $manipulator = new ApiOauthTokenManipulator(self::$DI['app']['EM'], self::$DI['app']['repo.api-oauth-tokens'], self::$DI['app']['random.medium']); + $account = self::$DI['app']['manipulator.api-account']->create(self::$DI['oauth2-app-user'], self::$DI['user']); + $token = $manipulator->create($account); + $oauthTokenBefore = $token->getOauthToken(); + $manipulator->renew($token); + $this->assertNotEquals($oauthTokenBefore, $token->getOauthToken()); + } +}