Change to be able to return called API version

This commit is contained in:
Benoît Burnichon
2015-12-16 16:31:42 +01:00
parent 0dfe794145
commit b2ee2668a1
19 changed files with 247 additions and 73 deletions

View File

@@ -29,9 +29,10 @@ class ApiRootTest extends \PhraseanetWebTestCase
public function testRoot()
{
self::$DI['client']->request('GET', '/api/');
$client = $this->getClient();
$client->request('GET', '/api/');
$response = self::$DI['client']->getResponse();
$response = $client->getResponse();
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('application/json', $response->headers->get('content-type'));

View File

@@ -6,6 +6,8 @@ use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Authentication\Context;
use Alchemy\Phrasea\Border\File;
use Alchemy\Phrasea\Controller\Api\V1Controller;
use Alchemy\Phrasea\ControllerProvider\Api\V2;
use Alchemy\Phrasea\Core\Configuration\PropertyAccess;
use Alchemy\Phrasea\Core\PhraseaEvents;
use Alchemy\Phrasea\Model\Entities\ApiOauthToken;
use Alchemy\Phrasea\Model\Entities\LazaretSession;
@@ -243,10 +245,11 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase
{
$route = '/api/v1/nothinghere';
$this->setToken($this->userAccessToken);
self::$DI['client']->request('GET', $route, $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize(self::$DI['client']->getResponse()->getContent());
$client = $this->getClient();
$client->request('GET', $route, $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize($client->getResponse()->getContent());
$this->evaluateResponseNotFound(self::$DI['client']->getResponse());
$this->evaluateResponseNotFound($client->getResponse());
$this->evaluateMetaNotFound($content);
}
@@ -277,22 +280,27 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase
public function testCheckNativeApp()
{
$value = self::$DI['app']['conf']->get(['registry', 'api-clients', 'navigator-enabled']);
self::$DI['app']['conf']->set(['registry', 'api-clients', 'navigator-enabled'], false);
$app = $this->getApplication();
/** @var PropertyAccess $conf */
$conf = $app['conf'];
$value = $conf->get(['registry', 'api-clients', 'navigator-enabled']);
$conf->set(['registry', 'api-clients', 'navigator-enabled'], false);
$fail = null;
try {
$nativeApp = self::$DI['app']['repo.api-applications']->findByClientId(\API_OAuth2_Application_Navigator::CLIENT_ID);
$nativeApp = $app['repo.api-applications']->findByClientId(\API_OAuth2_Application_Navigator::CLIENT_ID);
if (null === $nativeApp) {
throw new \Exception(sprintf('%s not found', \API_OAuth2_Application_Navigator::CLIENT_ID));
}
$account = self::$DI['app']['manipulator.api-account']->create($nativeApp, self::$DI['user']);
$token = self::$DI['app']['manipulator.api-oauth-token']->create($account);
$account = $app['manipulator.api-account']->create($nativeApp, self::$DI['user'], V2::VERSION);
$token = $app['manipulator.api-oauth-token']->create($account);
$this->setToken($token);
self::$DI['client']->request('GET', '/api/v1/databoxes/list/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize(self::$DI['client']->getResponse()->getContent());
$client = $this->getClient();
$client->request('GET', '/api/v1/databoxes/list/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize(
$client->getResponse()->getContent());
if (403 != $content['meta']['http_code']) {
$fail = new \Exception('Result does not match expected 403, returns ' . $content['meta']['http_code']);
@@ -301,7 +309,7 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase
$fail = $e;
}
self::$DI['app']['conf']->set(['registry', 'api-clients', 'navigator-enabled'], $value);
$conf->set(['registry', 'api-clients', 'navigator-enabled'], $value);
if ($fail) {
throw $fail;
@@ -1381,10 +1389,11 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase
$this->evaluateMethodNotAllowedRoute($route, ['GET', 'PUT', 'DELETE']);
self::$DI['client']->request('POST', $route, $this->getParameters(['name' => 'un Joli Nom']), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize(self::$DI['client']->getResponse()->getContent());
$client = $this->getClient();
$client->request('POST', $route, $this->getParameters(['name' => 'un Joli Nom']), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize($client->getResponse()->getContent());
$this->evaluateResponse200(self::$DI['client']->getResponse());
$this->evaluateResponse200($client->getResponse());
$this->evaluateMeta200($content);
$this->assertEquals(1, count($content['response']));
@@ -2270,10 +2279,11 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase
protected function evaluateMethodNotAllowedRoute($route, $methods)
{
foreach ($methods as $method) {
self::$DI['client']->request($method, $route, $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize(self::$DI['client']->getResponse()->getContent());
$this->assertTrue(self::$DI['client']->getResponse()->headers->has('Allow'));
$this->evaluateResponseMethodNotAllowed(self::$DI['client']->getResponse());
$client = $this->getClient();
$client->request($method, $route, $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize($client->getResponse()->getContent());
$this->assertTrue($client->getResponse()->headers->has('Allow'));
$this->evaluateResponseMethodNotAllowed($client->getResponse());
$this->evaluateMetaMethodNotAllowed($content);
}
}
@@ -2281,21 +2291,22 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase
protected function evaluateBadRequestRoute($route, $methods)
{
foreach ($methods as $method) {
self::$DI['client']->request($method, $route, $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize(self::$DI['client']->getResponse()->getContent());
$this->evaluateResponseBadRequest(self::$DI['client']->getResponse());
$client = $this->getClient();
$client->request($method, $route, $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize($client->getResponse()->getContent());
$this->evaluateResponseBadRequest($client->getResponse());
$this->evaluateMetaBadRequest($content);
}
}
protected function evaluateMeta($content)
protected function evaluateMeta($content, $version = null)
{
$this->assertTrue(is_array($content), 'La reponse est un objet');
$this->assertArrayHasKey('meta', $content);
$this->assertArrayHasKey('response', $content);
$this->assertTrue(is_array($content['meta']), 'Le bloc meta est un array');
$this->assertTrue(is_array($content['response']), 'Le bloc reponse est un array');
$this->assertEquals('2.0.0', $content['meta']['api_version']);
$this->assertEquals($version ?: V2::VERSION, $content['meta']['api_version']);
$this->assertNotNull($content['meta']['response_time']);
$this->assertEquals('UTF-8', $content['meta']['charset']);
}

View File

@@ -3,7 +3,6 @@
namespace Alchemy\Tests\Phrasea\Controller\Api;
use Alchemy\Phrasea\Controller\Api\Result;
use Alchemy\Phrasea\ControllerProvider\Api\V1;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Yaml\Parser;
@@ -22,7 +21,7 @@ class ResultTest extends \PhraseanetTestCase
'REQUEST_URI' => 'my/base/path/my/request/uri',
'PHP_SELF' => 'my/base/path',
];
$request = new Request(["callback" => ""], [], [], [], [], $server);
$request = new Request(["callback" => ""], [], ['api_version' => '2.0.0'], [], [], $server);
$apiResult = new Result($request);
$return = $apiResult->createResponse()->getContent();
@@ -35,7 +34,7 @@ class ResultTest extends \PhraseanetTestCase
$this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_OBJECT, $response->response);
$this->assertEquals(0, sizeof(get_object_vars($response->response)));
$this->assertEquals(0, sizeof(get_class_methods($response->response)));
$this->checkResponseFieldMeta($response, "api_version", V1::VERSION, \PHPUnit_Framework_Constraint_IsType::TYPE_STRING);
$this->checkResponseFieldMeta($response, "api_version", '2.0.0', \PHPUnit_Framework_Constraint_IsType::TYPE_STRING);
$this->checkResponseFieldMeta($response, "request", "GET my/base/path/my/request/uri", \PHPUnit_Framework_Constraint_IsType::TYPE_STRING);
$date = new \DateTime();
@@ -72,6 +71,7 @@ class ResultTest extends \PhraseanetTestCase
'PHP_SELF' => 'my/base/path',
];
$request = new Request(["callback" => ""], [], [], [], [], $server);
Result::setDefaultVersion('1.0.0');
$apiResult = new Result($request);
$response = (new Parser())->parse($apiResult->createResponse()->getContent());
@@ -84,7 +84,7 @@ class ResultTest extends \PhraseanetTestCase
$this->assertEquals(0, count($response["response"]));
$this->assertArrayHasKey("api_version", $response["meta"]);
$this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING, $response["meta"]["api_version"]);
$this->assertEquals(V1::VERSION, $response["meta"]["api_version"]);
$this->assertEquals('1.0.0', $response["meta"]["api_version"]);
$this->assertArrayHasKey("request", $response["meta"]);
$this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING, $response["meta"]["request"]);
$this->assertEquals("GET my/base/path/my/request/uri", $response["meta"]["request"]);

View File

@@ -23,7 +23,7 @@ class ApiExceptionHandlerSubscriberTest extends \PhraseanetTestCase
public function testError($exception, $code)
{
$app = new Application(Application::ENV_TEST);
$app['dispatcher']->addSubscriber(new ApiExceptionHandlerSubscriber($app));
$app['dispatcher']->addSubscriber(new ApiExceptionHandlerSubscriber());
$app->get('/', function () use ($exception) {
throw $exception;
});

View File

@@ -72,7 +72,7 @@ class UserDeletionTest extends \PhraseanetAuthenticatedWebTestCase
/** @var ApiAccountManipulator $apiAccountManipulator */
$apiAccountManipulator = $app['manipulator.api-account'];
$account = $apiAccountManipulator->create($this->apiApplication, $this->user);
$account = $apiAccountManipulator->create($this->apiApplication, $this->user, V2::VERSION);
$this->assertInstanceOf(ApiAccount::class, $account);
$apiLog = $apiLogManipulator->create($account, new Request(), new Response());

View File

@@ -2,7 +2,7 @@
namespace Alchemy\Tests\Phrasea\Model\Manipulator;
use Alchemy\Phrasea\ControllerProvider\Api\V1;
use Alchemy\Phrasea\ControllerProvider\Api\V2;
use Alchemy\Phrasea\Model\Manipulator\ApiAccountManipulator;
/**
@@ -24,16 +24,16 @@ class ApiAccountManipulatorTest extends \PhraseanetTestCase
public function testCreate()
{
$nbApps = count(self::$DI['app']['repo.api-accounts']->findAll());
$account = $this->sut->create(self::$DI['oauth2-app-user'], self::$DI['user']);
$account = $this->sut->create(self::$DI['oauth2-app-user'], self::$DI['user'], V2::VERSION);
$this->assertGreaterThan($nbApps, count(self::$DI['app']['repo.api-accounts']->findAll()));
$this->assertFalse($account->isRevoked());
$this->assertEquals(V1::VERSION, $account->getApiVersion());
$this->assertEquals(V2::VERSION, $account->getApiVersion());
$this->assertGreaterThan($nbApps, count(self::$DI['app']['repo.api-accounts']->findAll()));
}
public function testDelete()
{
$account = $this->sut->create(self::$DI['oauth2-app-user'], self::$DI['user']);
$account = $this->sut->create(self::$DI['oauth2-app-user'], self::$DI['user'], V2::VERSION);
$accountMem = clone $account;
$countBefore = count(self::$DI['app']['repo.api-accounts']->findAll());
self::$DI['app']['manipulator.api-oauth-token']->create($account);
@@ -45,7 +45,7 @@ class ApiAccountManipulatorTest extends \PhraseanetTestCase
public function testUpdate()
{
$account = $this->sut->create(self::$DI['oauth2-app-user'], self::$DI['user']);
$account = $this->sut->create(self::$DI['oauth2-app-user'], self::$DI['user'], V2::VERSION);
$account->setApiVersion(24);
$this->sut->update($account);
$account = self::$DI['app']['repo.api-accounts']->find($account->getId());
@@ -54,14 +54,14 @@ class ApiAccountManipulatorTest extends \PhraseanetTestCase
public function testAuthorizeAccess()
{
$account = $this->sut->create(self::$DI['oauth2-app-user'], self::$DI['user']);
$account = $this->sut->create(self::$DI['oauth2-app-user'], self::$DI['user'], V2::VERSION);
$this->sut->authorizeAccess($account);
$this->assertFalse($account->isRevoked());
}
public function testRevokeAccess()
{
$account = $this->sut->create(self::$DI['oauth2-app-user'], self::$DI['user']);
$account = $this->sut->create(self::$DI['oauth2-app-user'], self::$DI['user'], V2::VERSION);
$this->sut->revokeAccess($account);
$this->assertTrue($account->isRevoked());
}

View File

@@ -2,6 +2,7 @@
namespace Alchemy\Tests\Phrasea\Model\Manipulator;
use Alchemy\Phrasea\ControllerProvider\Api\V2;
use Alchemy\Phrasea\Model\Manipulator\ApiLogManipulator;
use Alchemy\Phrasea\Model\Manipulator\ApiAccountManipulator;
use Symfony\Component\HttpFoundation\Request;
@@ -16,7 +17,7 @@ class ApiLogManipulatorTest extends \PhraseanetTestCase
public function testCreate()
{
$manipulator = new ApiAccountManipulator(self::$DI['app']['orm.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'], V2::VERSION);
$nbLogs = count(self::$DI['app']['repo.api-logs']->findAll());
$manipulator = new ApiLogManipulator(self::$DI['app']['orm.em'], self::$DI['app']['repo.api-logs']);
$manipulator->create($account, Request::create('/databoxes/list/', 'POST'), new Response());
@@ -41,7 +42,7 @@ class ApiLogManipulatorTest extends \PhraseanetTestCase
public function testDelete()
{
$manipulator = new ApiAccountManipulator(self::$DI['app']['orm.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'], V2::VERSION);
$manipulator = new ApiLogManipulator(self::$DI['app']['orm.em'], self::$DI['app']['repo.api-logs']);
$log = $manipulator->create($account, Request::create('/databoxes/list/', 'POST'), new Response());
$countBefore = count(self::$DI['app']['repo.api-logs']->findAll());
@@ -52,7 +53,7 @@ class ApiLogManipulatorTest extends \PhraseanetTestCase
public function testUpdate()
{
$manipulator = new ApiAccountManipulator(self::$DI['app']['orm.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'], V2::VERSION);
$manipulator = new ApiLogManipulator(self::$DI['app']['orm.em'], self::$DI['app']['repo.api-logs']);
$log = $manipulator->create($account, Request::create('/databoxes/list/', 'POST'), new Response());
$log->setAspect('a-new-aspect');