Add tests

This commit is contained in:
Nicolas Le Goff
2014-03-12 20:36:39 +01:00
parent 137cacea0b
commit 6b50ee15a7
5 changed files with 123 additions and 6 deletions

View File

@@ -13,6 +13,7 @@ namespace Alchemy\Phrasea\Model\Manipulator;
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Authentication\ACLProvider; use Alchemy\Phrasea\Authentication\ACLProvider;
use Alchemy\Phrasea\Exception\InvalidArgumentException;
use Alchemy\Phrasea\Model\Entities\ApiAccount; use Alchemy\Phrasea\Model\Entities\ApiAccount;
use Alchemy\Phrasea\Model\Entities\ApiOauthCode; use Alchemy\Phrasea\Model\Entities\ApiOauthCode;
use Alchemy\Phrasea\Model\Entities\User; use Alchemy\Phrasea\Model\Entities\User;

View File

@@ -23,12 +23,13 @@ class ApiAccountManipulatorTest extends \PhraseanetTestCase
{ {
$manipulator = new ApiAccountManipulator(self::$DI['app']['EM'], self::$DI['app']['repo.api-accounts']); $manipulator = new ApiAccountManipulator(self::$DI['app']['EM'], self::$DI['app']['repo.api-accounts']);
$account = $manipulator->create(self::$DI['oauth2-app-user'], self::$DI['user']); $account = $manipulator->create(self::$DI['oauth2-app-user'], self::$DI['user']);
$accountMem = clone $account;
$countBefore = count(self::$DI['app']['repo.api-accounts']->findAll()); $countBefore = count(self::$DI['app']['repo.api-accounts']->findAll());
/** self::$DI['app']['manipulator.api-oauth-token']->create($account);
* @todo Link token and tests if token is deleted too
*/
$manipulator->delete($account); $manipulator->delete($account);
$this->assertGreaterThan(count(self::$DI['app']['repo.api-accounts']->findAll()), $countBefore); $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() public function testUpdate()

View File

@@ -60,12 +60,16 @@ class ApiApplicationManipulatorTest extends \PhraseanetTestCase
'Desktop application description', 'Desktop application description',
'http://desktop-app-url.net' 'http://desktop-app-url.net'
); );
$applicationSave = clone $application;
$countBefore = count(self::$DI['app']['repo.api-applications']->findAll()); $countBefore = count(self::$DI['app']['repo.api-applications']->findAll());
/** $account = self::$DI['app']['manipulator.api-account']->create($application, self::$DI['user']);
* @todo Link accounts and tokens to application and tests if everything is deleted self::$DI['app']['manipulator.api-oauth-token']->create($account);
*/
$manipulator->delete($application); $manipulator->delete($application);
$this->assertGreaterThan(count(self::$DI['app']['repo.api-applications']->findAll()), $countBefore); $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() public function testUpdate()

View File

@@ -0,0 +1,59 @@
<?php
namespace Alchemy\Tests\Phrasea\Model\Manipulator;
use Alchemy\Phrasea\Exception\InvalidArgumentException;
use Alchemy\Phrasea\Model\Manipulator\ApiOauthCodeManipulator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ApiOauthCodeManipulatorTest extends \PhraseanetTestCase
{
public function testCreate()
{
$manipulator = new ApiOauthCodeManipulator(self::$DI['app']['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']);
$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) {
}
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace Alchemy\Tests\Phrasea\Model\Manipulator;
use Alchemy\Phrasea\Model\Manipulator\ApiLogManipulator;
use Alchemy\Phrasea\Model\Manipulator\ApiOauthTokenManipulator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ApiOauthTokenManipulatorTest extends \PhraseanetTestCase
{
public function testCreate()
{
$manipulator = new ApiOauthTokenManipulator(self::$DI['app']['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']);
$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());
}
}