Some refactor to add ApiAccount version

This commit is contained in:
Benoît Burnichon
2015-12-16 17:10:31 +01:00
parent b2ee2668a1
commit 7d33b824d7
4 changed files with 128 additions and 33 deletions

View File

@@ -2,7 +2,9 @@
namespace Alchemy\Tests\Phrasea\Model\Manipulator;
use Alchemy\Phrasea\ControllerProvider\Api\V2;
use Alchemy\Phrasea\Exception\InvalidArgumentException;
use Alchemy\Phrasea\Model\Manipulator\ApiAccountManipulator;
use Alchemy\Phrasea\Model\Manipulator\ApiApplicationManipulator;
use Alchemy\Phrasea\Model\Entities\ApiApplication;
@@ -57,7 +59,8 @@ class ApiApplicationManipulatorTest extends \PhraseanetTestCase
public function testDelete()
{
$manipulator = new ApiApplicationManipulator(self::$DI['app']['orm.em'], self::$DI['app']['repo.api-applications'], self::$DI['app']['random.medium']);
$app = $this->getApplication();
$manipulator = new ApiApplicationManipulator($app['orm.em'], $app['repo.api-applications'], $app['random.medium']);
$application = $manipulator->create(
'desktop-app2',
ApiApplication::DESKTOP_TYPE,
@@ -65,15 +68,17 @@ class ApiApplicationManipulatorTest extends \PhraseanetTestCase
'http://desktop-app2-url.net'
);
$applicationSave = clone $application;
$countBefore = count(self::$DI['app']['repo.api-applications']->findAll());
$account = self::$DI['app']['manipulator.api-account']->create($application, self::$DI['user']);
$countBefore = count($app['repo.api-applications']->findAll());
/** @var ApiAccountManipulator $apiAccountManipulator */
$apiAccountManipulator = $app['manipulator.api-account'];
$account = $apiAccountManipulator->create($application, self::$DI['user'], V2::VERSION);
$accountMem = clone $account;
self::$DI['app']['manipulator.api-oauth-token']->create($account);
$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->assertGreaterThan(count($app['repo.api-applications']->findAll()), $countBefore);
$accounts = $app['repo.api-accounts']->findByUserAndApplication(self::$DI['user'], $applicationSave);
$this->assertEquals(0, count($accounts));
$tokens = self::$DI['app']['repo.api-oauth-tokens']->findOauthTokens($accountMem);
$tokens = $app['repo.api-oauth-tokens']->findOauthTokens($accountMem);
$this->assertEquals(0, count($tokens));
}

View File

@@ -2,8 +2,13 @@
namespace Alchemy\Tests\Phrasea\Model\Manipulator;
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\ControllerProvider\Api\V2;
use Alchemy\Phrasea\Exception\InvalidArgumentException;
use Alchemy\Phrasea\Model\Entities\ApiAccount;
use Alchemy\Phrasea\Model\Manipulator\ApiAccountManipulator;
use Alchemy\Phrasea\Model\Manipulator\ApiOauthCodeManipulator;
use Alchemy\Phrasea\Model\Repositories\ApiOauthCodeRepository;
/**
* @group functional
@@ -13,32 +18,40 @@ class ApiOauthCodeManipulatorTest extends \PhraseanetTestCase
{
public function testCreate()
{
$manipulator = new ApiOauthCodeManipulator(self::$DI['app']['orm.em'], self::$DI['app']['repo.api-oauth-codes'], self::$DI['app']['random.medium']);
$nbCodes = count(self::$DI['app']['repo.api-oauth-codes']->findAll());
$account = self::$DI['app']['manipulator.api-account']->create(self::$DI['oauth2-app-user'], self::$DI['user']);
$app = $this->getApplication();
$oauthCodesRepository = $this->getOAuthCodesRepository($app);
$manipulator = new ApiOauthCodeManipulator($app['orm.em'], $oauthCodesRepository, $app['random.medium']);
$nbCodes = count($oauthCodesRepository->findAll());
$account = $this->getApiAccount($app);
$manipulator->create($account, 'http://www.redirect.url', time() + 30);
$this->assertGreaterThan($nbCodes, count(self::$DI['app']['repo.api-oauth-codes']->findAll()));
$this->assertGreaterThan($nbCodes, count($oauthCodesRepository->findAll()));
}
public function testDelete()
{
$manipulator = new ApiOauthCodeManipulator(self::$DI['app']['orm.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']);
$app = $this->getApplication();
$oauthCodesRepository = $this->getOAuthCodesRepository($app);
$manipulator = new ApiOauthCodeManipulator($app['orm.em'], $oauthCodesRepository, $app['random.medium']);
$account = $this->getApiAccount($app);
$code = $manipulator->create($account, 'http://www.redirect.url', time() + 30);
$countBefore = count(self::$DI['app']['repo.api-oauth-codes']->findAll());
$countBefore = count($oauthCodesRepository->findAll());
$manipulator->delete($code);
$this->assertGreaterThan(count(self::$DI['app']['repo.api-oauth-codes']->findAll()), $countBefore);
$this->assertGreaterThan(count($oauthCodesRepository->findAll()), $countBefore);
}
public function testUpdate()
{
$app = $this->getApplication();
$oauthCodesRepository = $this->getOAuthCodesRepository($app);
$manipulator = new ApiOauthCodeManipulator(self::$DI['app']['orm.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']);
$manipulator = new ApiOauthCodeManipulator($app['orm.em'], $oauthCodesRepository, $app['random.medium']);
$account = $this->getApiAccount($app);
$code = $manipulator->create($account, 'http://www.redirect.url', $t = time() + 30);
$code->setExpires(time() + 40);
$manipulator->update($code);
$code = self::$DI['app']['repo.api-oauth-codes']->find($code->getCode());
$code = $oauthCodesRepository->find($code->getCode());
$this->assertGreaterThan($t, $code->getExpires());
}
@@ -47,8 +60,11 @@ class ApiOauthCodeManipulatorTest extends \PhraseanetTestCase
*/
public function testSetRedirectUriBadArgumentException()
{
$manipulator = new ApiOauthCodeManipulator(self::$DI['app']['orm.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']);
$app = $this->getApplication();
$oauthCodesRepository = $this->getOAuthCodesRepository($app);
$manipulator = new ApiOauthCodeManipulator($app['orm.em'], $oauthCodesRepository, $app['random.medium']);
$account = $this->getApiAccount($app);
$code = $manipulator->create($account, 'http://www.redirect.url', time() + 30);
try {
$manipulator->setRedirectUri($code, 'bad-url');
@@ -57,4 +73,32 @@ class ApiOauthCodeManipulatorTest extends \PhraseanetTestCase
}
}
/**
* @param Application $app
* @return ApiOauthCodeRepository
*/
private function getOAuthCodesRepository(Application $app)
{
return $app['repo.api-oauth-codes'];
}
/**
* @param Application $app
* @return ApiAccountManipulator
*/
private function getApiAccountManipulator(Application $app)
{
return $app['manipulator.api-account'];
}
/**
* @param Application $app
* @return ApiAccount
*/
private function getApiAccount(Application $app)
{
return $this->getApiAccountManipulator($app)
->create(self::$DI['oauth2-app-user'], self::$DI['user'], V2::VERSION);
}
}

View File

@@ -2,7 +2,12 @@
namespace Alchemy\Tests\Phrasea\Model\Manipulator;
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\ControllerProvider\Api\V2;
use Alchemy\Phrasea\Model\Entities\ApiAccount;
use Alchemy\Phrasea\Model\Manipulator\ApiAccountManipulator;
use Alchemy\Phrasea\Model\Manipulator\ApiOauthTokenManipulator;
use Alchemy\Phrasea\Model\Repositories\ApiOauthTokenRepository;
/**
* @group functional
@@ -12,42 +17,82 @@ class ApiOauthTokenManipulatorTest extends \PhraseanetTestCase
{
public function testCreate()
{
$manipulator = new ApiOauthTokenManipulator(self::$DI['app']['orm.em'], self::$DI['app']['repo.api-oauth-tokens'], self::$DI['app']['random.medium']);
$nbTokens = count(self::$DI['app']['repo.api-oauth-tokens']->findAll());
$account = self::$DI['app']['manipulator.api-account']->create(self::$DI['oauth2-app-user'], self::$DI['user']);
$app = $this->getApplication();
$tokenRepository = $this->getTokenRepository($app);
$manipulator = new ApiOauthTokenManipulator($app['orm.em'], $tokenRepository, $app['random.medium']);
$nbTokens = count($tokenRepository->findAll());
$account = $this->createAccount($app);
$manipulator->create($account);
$this->assertGreaterThan($nbTokens, count(self::$DI['app']['repo.api-oauth-tokens']->findAll()));
$this->assertGreaterThan($nbTokens, count($tokenRepository->findAll()));
}
public function testDelete()
{
$manipulator = new ApiOauthTokenManipulator(self::$DI['app']['orm.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']);
$app = $this->getApplication();
$tokenRepository = $this->getTokenRepository($app);
$manipulator = new ApiOauthTokenManipulator($app['orm.em'], $tokenRepository, $app['random.medium']);
$account = $this->createAccount($app);
$token = $manipulator->create($account);
$countBefore = count(self::$DI['app']['repo.api-oauth-tokens']->findAll());
$countBefore = count($tokenRepository->findAll());
$manipulator->delete($token);
$this->assertGreaterThan(count(self::$DI['app']['repo.api-oauth-tokens']->findAll()), $countBefore);
$this->assertGreaterThan(count($tokenRepository->findAll()), $countBefore);
}
public function testUpdate()
{
$app = $this->getApplication();
$tokenRepository = $this->getTokenRepository($app);
$manipulator = new ApiOauthTokenManipulator(self::$DI['app']['orm.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']);
$manipulator = new ApiOauthTokenManipulator($app['orm.em'], $tokenRepository, $app['random.medium']);
$account = $this->createAccount($app);
$token = $manipulator->create($account);
$token->setSessionId(123456);
$manipulator->update($token);
$token = self::$DI['app']['repo.api-oauth-tokens']->find($token->getOauthToken());
$token = $tokenRepository->find($token->getOauthToken());
$this->assertEquals(123456, $token->getSessionId());
}
public function testRenew()
{
$manipulator = new ApiOauthTokenManipulator(self::$DI['app']['orm.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']);
$app = $this->getApplication();
$tokenRepository = $this->getTokenRepository($app);
$manipulator = new ApiOauthTokenManipulator($app['orm.em'], $tokenRepository, $app['random.medium']);
$account = $this->createAccount($app);
$token = $manipulator->create($account);
$oauthTokenBefore = $token->getOauthToken();
$manipulator->renew($token);
$this->assertNotEquals($oauthTokenBefore, $token->getOauthToken());
}
/**
* @param Application $app
* @return ApiAccountManipulator
*/
private function getApiAccountManipulator(Application $app)
{
return $app['manipulator.api-account'];
}
/**
* @param Application $app
* @return ApiOauthTokenRepository
*/
private function getTokenRepository(Application $app)
{
$tokenRepository = $app['repo.api-oauth-tokens'];
return $tokenRepository;
}
/**
* @param Application $app
* @return ApiAccount
*/
private function createAccount(Application $app)
{
return $this->getApiAccountManipulator($app)
->create(self::$DI['oauth2-app-user'], self::$DI['user'], V2::VERSION);
}
}