mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-18 15:33:15 +00:00
Change to be able to return called API version
This commit is contained in:
@@ -15,6 +15,7 @@ use Alchemy\Phrasea\Application as PhraseaApplication;
|
||||
use Alchemy\Phrasea\Controller\Api\Result;
|
||||
use Alchemy\Phrasea\ControllerProvider\Api\OAuth2;
|
||||
use Alchemy\Phrasea\ControllerProvider\Api\V1;
|
||||
use Alchemy\Phrasea\ControllerProvider\Api\V2;
|
||||
use Alchemy\Phrasea\ControllerProvider\Datafiles;
|
||||
use Alchemy\Phrasea\ControllerProvider\MediaAccessor;
|
||||
use Alchemy\Phrasea\ControllerProvider\Minifier;
|
||||
@@ -38,13 +39,11 @@ return call_user_func(function ($environment = PhraseaApplication::ENV_PROD) {
|
||||
|
||||
$app->register(new OAuth2());
|
||||
$app->register(new V1());
|
||||
$app->register(new V2());
|
||||
$app->loadPlugins();
|
||||
|
||||
$app['exception_handler'] = $app->share(function ($app) {
|
||||
$handler = new ApiExceptionHandlerSubscriber($app);
|
||||
$handler->setLogger($app['monolog']);
|
||||
|
||||
return $handler;
|
||||
return new ApiExceptionHandlerSubscriber($app['monolog']);
|
||||
});
|
||||
$app['monolog'] = $app->share($app->extend('monolog', function (Logger $monolog) {
|
||||
$monolog->pushProcessor(new WebProcessor());
|
||||
@@ -113,7 +112,17 @@ return call_user_func(function ($environment = PhraseaApplication::ENV_PROD) {
|
||||
'authorization_token' => '/api/oauthv2/authorize',
|
||||
'access_token' => '/api/oauthv2/token'
|
||||
]
|
||||
]
|
||||
],
|
||||
'2' => [
|
||||
'number' => V2::VERSION,
|
||||
'uri' => '/api/v2/',
|
||||
'authenticationProtocol' => 'OAuth2',
|
||||
'authenticationVersion' => 'draft#v9',
|
||||
'authenticationEndPoints' => [
|
||||
'authorization_token' => '/api/oauthv2/authorize',
|
||||
'access_token' => '/api/oauthv2/token'
|
||||
],
|
||||
],
|
||||
]
|
||||
])->createResponse();
|
||||
});
|
||||
@@ -129,6 +138,7 @@ return call_user_func(function ($environment = PhraseaApplication::ENV_PROD) {
|
||||
$app->mount('/api/oauthv2', new OAuth2());
|
||||
$app->mount('/datafiles/', new Datafiles());
|
||||
$app->mount('/api/v1', new V1());
|
||||
$app->mount('/api/v2', new V2());
|
||||
$app->mount('/permalink/', new Permalink());
|
||||
$app->mount($app['controller.media_accessor.route_prefix'], new MediaAccessor());
|
||||
$app->mount('/include/minify/', new Minifier());
|
||||
|
@@ -11,12 +11,14 @@
|
||||
|
||||
namespace Alchemy\Phrasea\Command\Developer;
|
||||
|
||||
use Alchemy\Phrasea\Border\File;
|
||||
use Alchemy\Phrasea\Border\Manager;
|
||||
use Alchemy\Phrasea\Command\Command;
|
||||
use Alchemy\Phrasea\ControllerProvider\Api\V2;
|
||||
use Alchemy\Phrasea\Media\SubdefSubstituer;
|
||||
use Alchemy\Phrasea\Model\Entities\AggregateToken;
|
||||
use Alchemy\Phrasea\Model\Entities\ApiApplication;
|
||||
use Alchemy\Phrasea\Model\Entities\AuthFailure;
|
||||
use Alchemy\Phrasea\Model\Entities\AggregateToken;
|
||||
use Alchemy\Phrasea\Model\Entities\Basket;
|
||||
use Alchemy\Phrasea\Model\Entities\BasketElement;
|
||||
use Alchemy\Phrasea\Model\Entities\Feed;
|
||||
@@ -26,18 +28,20 @@ use Alchemy\Phrasea\Model\Entities\FeedPublisher;
|
||||
use Alchemy\Phrasea\Model\Entities\FeedToken;
|
||||
use Alchemy\Phrasea\Model\Entities\LazaretSession;
|
||||
use Alchemy\Phrasea\Model\Entities\Registration;
|
||||
use Alchemy\Phrasea\Model\Entities\StoryWZ;
|
||||
use Alchemy\Phrasea\Model\Entities\Task;
|
||||
use Alchemy\Phrasea\Model\Entities\Token;
|
||||
use Alchemy\Phrasea\Model\Entities\User;
|
||||
use Alchemy\Phrasea\Model\Entities\ValidationData;
|
||||
use Alchemy\Phrasea\Model\Entities\ValidationSession;
|
||||
use Alchemy\Phrasea\Model\Entities\ValidationParticipant;
|
||||
use Alchemy\Phrasea\Model\Entities\UsrListOwner;
|
||||
use Alchemy\Phrasea\Model\Entities\UsrList;
|
||||
use Alchemy\Phrasea\Model\Entities\UsrListEntry;
|
||||
use Alchemy\Phrasea\Model\Entities\StoryWZ;
|
||||
use Alchemy\Phrasea\Model\Entities\UsrListOwner;
|
||||
use Alchemy\Phrasea\Model\Entities\ValidationData;
|
||||
use Alchemy\Phrasea\Model\Entities\ValidationParticipant;
|
||||
use Alchemy\Phrasea\Model\Entities\ValidationSession;
|
||||
use Alchemy\Phrasea\Model\Entities\WebhookEvent;
|
||||
use Alchemy\Phrasea\Model\Entities\WebhookEventDelivery;
|
||||
use Alchemy\Phrasea\Model\Manipulator\ApiAccountManipulator;
|
||||
use Alchemy\Phrasea\Model\Manipulator\ApiOauthTokenManipulator;
|
||||
use Alchemy\Phrasea\Model\Manipulator\TokenManipulator;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Doctrine\ORM\Tools\SchemaTool;
|
||||
@@ -45,7 +49,6 @@ use Gedmo\Timestampable\TimestampableListener;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Alchemy\Phrasea\Border\File;
|
||||
|
||||
class RegenerateSqliteDb extends Command
|
||||
{
|
||||
@@ -207,10 +210,14 @@ class RegenerateSqliteDb extends Command
|
||||
|
||||
public function insertOauthAccounts(\Pimple $DI)
|
||||
{
|
||||
$DI['api-app-acc-user'] = $this->container['manipulator.api-account']->create($DI['api-app-user'], $DI['user']);
|
||||
$this->container['manipulator.api-oauth-token']->create($DI['api-app-acc-user']);
|
||||
$DI['api-app-acc-user-not-admin'] = $this->container['manipulator.api-account']->create($DI['api-app-user-not-admin'], $DI['user_notAdmin']);
|
||||
$this->container['manipulator.api-oauth-token']->create($DI['api-app-acc-user-not-admin']);
|
||||
/** @var ApiAccountManipulator $apiAccountManipulator */
|
||||
$apiAccountManipulator = $this->container['manipulator.api-account'];
|
||||
/** @var ApiOauthTokenManipulator $apiOAuthTokenManipulator */
|
||||
$apiOAuthTokenManipulator = $this->container['manipulator.api-oauth-token'];
|
||||
$DI['api-app-acc-user'] = $apiAccountManipulator->create($DI['api-app-user'], $DI['user'], V2::VERSION);
|
||||
$apiOAuthTokenManipulator->create($DI['api-app-acc-user']);
|
||||
$DI['api-app-acc-user-not-admin'] = $apiAccountManipulator->create($DI['api-app-user-not-admin'], $DI['user_notAdmin'], V2::VERSION);
|
||||
$apiOAuthTokenManipulator->create($DI['api-app-acc-user-not-admin']);
|
||||
}
|
||||
|
||||
public function insertNativeApps()
|
||||
|
34
lib/Alchemy/Phrasea/Controller/Api/BasketController.php
Normal file
34
lib/Alchemy/Phrasea/Controller/Api/BasketController.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Phraseanet
|
||||
*
|
||||
* (c) 2005-2015 Alchemy
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace Alchemy\Phrasea\Controller\Api;
|
||||
|
||||
use Alchemy\Phrasea\Application\Helper\DataboxLoggerAware;
|
||||
use Alchemy\Phrasea\Application\Helper\DispatcherAware;
|
||||
use Alchemy\Phrasea\Controller\Controller;
|
||||
use Alchemy\Phrasea\Model\Entities\Basket;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
class BasketController extends Controller
|
||||
{
|
||||
use DataboxLoggerAware;
|
||||
use DispatcherAware;
|
||||
|
||||
public function addRecordsAction(Request $request, Basket $basket)
|
||||
{
|
||||
}
|
||||
|
||||
public function removeRecordsAction(Request $request, Basket $basket)
|
||||
{
|
||||
}
|
||||
|
||||
public function reorderRecordsAction(Request $request, Basket $basket)
|
||||
{
|
||||
}
|
||||
}
|
@@ -11,7 +11,6 @@
|
||||
|
||||
namespace Alchemy\Phrasea\Controller\Api;
|
||||
|
||||
use Alchemy\Phrasea\ControllerProvider\Api\V1;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
@@ -19,6 +18,8 @@ use Symfony\Component\Yaml\Dumper;
|
||||
|
||||
class Result
|
||||
{
|
||||
private static $defaultVersion = '1.0.0';
|
||||
|
||||
/** @var string */
|
||||
private $responseTime;
|
||||
|
||||
@@ -43,6 +44,9 @@ class Result
|
||||
/** @var string */
|
||||
private $responseType;
|
||||
|
||||
/** @var string */
|
||||
private $version;
|
||||
|
||||
const FORMAT_JSON = 'json';
|
||||
const FORMAT_YAML = 'yaml';
|
||||
const FORMAT_JSONP = 'jsonp';
|
||||
@@ -75,8 +79,6 @@ class Result
|
||||
$this->errorType = $errorType;
|
||||
$this->errorMessage = $errorMessage;
|
||||
$this->errorDetails = $errorDetails;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -211,7 +213,7 @@ class Result
|
||||
|
||||
$ret = [
|
||||
'meta' => [
|
||||
'api_version' => V1::VERSION,
|
||||
'api_version' => $this->getVersion(),
|
||||
'request' => $request_uri,
|
||||
'response_time' => $this->responseTime,
|
||||
'http_code' => $this->code,
|
||||
@@ -244,6 +246,28 @@ class Result
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $version
|
||||
*/
|
||||
public function setVersion($version)
|
||||
{
|
||||
$this->version = (string)$version;
|
||||
}
|
||||
|
||||
public static function setDefaultVersion($version)
|
||||
{
|
||||
self::$defaultVersion = $version;
|
||||
}
|
||||
|
||||
public function getVersion()
|
||||
{
|
||||
if (null === $this->version) {
|
||||
$this->version = $this->request->attributes->get('api_version') ?: self::$defaultVersion;
|
||||
}
|
||||
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns serialized data content type
|
||||
*
|
||||
|
@@ -10,6 +10,7 @@
|
||||
namespace Alchemy\Phrasea\Controller\Root;
|
||||
|
||||
use Alchemy\Phrasea\Controller\Controller;
|
||||
use Alchemy\Phrasea\ControllerProvider\Api\V2;
|
||||
use Alchemy\Phrasea\Exception\InvalidArgumentException;
|
||||
use Alchemy\Phrasea\Model\Entities\ApiApplication;
|
||||
use Alchemy\Phrasea\Model\Manipulator\ApiAccountManipulator;
|
||||
@@ -185,7 +186,7 @@ class DeveloperController extends Controller
|
||||
);
|
||||
|
||||
// create an account as well
|
||||
$this->getApiAccountManipulator()->create($application, $user);
|
||||
$this->getApiAccountManipulator()->create($application, $user, V2::VERSION);
|
||||
|
||||
return $this->app->redirectPath('developers_application', ['application' => $application->getId()]);
|
||||
}
|
||||
|
@@ -21,7 +21,7 @@ use Silex\ServiceProviderInterface;
|
||||
|
||||
class V1 implements ControllerProviderInterface, ServiceProviderInterface
|
||||
{
|
||||
const VERSION = '2.0.0';
|
||||
const VERSION = '1.5.0';
|
||||
|
||||
public static $extendedContentTypes = [
|
||||
'json' => ['application/vnd.phraseanet.record-extended+json'],
|
||||
|
77
lib/Alchemy/Phrasea/ControllerProvider/Api/V2.php
Normal file
77
lib/Alchemy/Phrasea/ControllerProvider/Api/V2.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Phraseanet
|
||||
*
|
||||
* (c) 2005-2015 Alchemy
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace Alchemy\Phrasea\ControllerProvider\Api;
|
||||
|
||||
use Alchemy\Phrasea\Application as PhraseaApplication;
|
||||
use Alchemy\Phrasea\Controller\Api\BasketController;
|
||||
use Alchemy\Phrasea\ControllerProvider\ControllerProviderTrait;
|
||||
use Alchemy\Phrasea\Core\Event\Listener\OAuthListener;
|
||||
use Silex\Application;
|
||||
use Silex\Controller;
|
||||
use Silex\ControllerProviderInterface;
|
||||
use Silex\ServiceProviderInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
class V2 implements ControllerProviderInterface, ServiceProviderInterface
|
||||
{
|
||||
use ControllerProviderTrait;
|
||||
|
||||
const VERSION = '2.0.0';
|
||||
|
||||
public function register(Application $app)
|
||||
{
|
||||
$app['controller.api.v2.baskets'] = $app->share(
|
||||
function (PhraseaApplication $app) {
|
||||
return (new BasketController($app))
|
||||
->setDataboxLoggerLocator($app['phraseanet.logger'])
|
||||
->setDispatcher($app['dispatcher']);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function boot(Application $app)
|
||||
{
|
||||
// Intentionally left empty
|
||||
}
|
||||
|
||||
public function connect(Application $app)
|
||||
{
|
||||
$controllers = $this->createCollection($app);
|
||||
|
||||
$controllers->before(new OAuthListener());
|
||||
|
||||
$controller = $controllers
|
||||
->post('/baskets/{basket}/records/', 'controller.api.v2.baskets:removeRecordsAction')
|
||||
->bind('api_v2_basket_records_add');
|
||||
$this->addBasketMiddleware($app, $controller);
|
||||
$controllers->post('/baskets/{wrong_basket}/records/', 'controller.api.v1:getBadRequestAction');
|
||||
|
||||
$controller = $controllers->delete('/baskets/{basket}/records/', 'controller.api.v2.baskets:addRecordsAction')
|
||||
->bind('api_v2_basket_records_remove');
|
||||
$this->addBasketMiddleware($app, $controller);
|
||||
$controllers->delete('/baskets/{wrong_basket}/records/', 'controller.api.v1:getBadRequestAction');
|
||||
|
||||
$controller = $controllers->put('/baskets/{basket}/records/reorder', 'controller.api.v2.baskets:reorderRecordsAction')
|
||||
->bind('api_v2_basket_records_reorder');
|
||||
$this->addBasketMiddleware($app, $controller);
|
||||
|
||||
return $controllers;
|
||||
}
|
||||
|
||||
private function addBasketMiddleware(Application $app, Controller $controller)
|
||||
{
|
||||
$controller
|
||||
->before($app['middleware.basket.converter'])
|
||||
->before($app['middleware.basket.user-access'])
|
||||
->assert('basket', '\d+');
|
||||
|
||||
return $controller;
|
||||
}
|
||||
}
|
@@ -19,8 +19,6 @@ use Alchemy\Phrasea\Core\Event\ApiOAuth2StartEvent;
|
||||
use Alchemy\Phrasea\Core\Event\PreAuthenticate;
|
||||
use Alchemy\Phrasea\Core\PhraseaEvents;
|
||||
use Alchemy\Phrasea\Model\Entities\ApiOauthToken;
|
||||
use Alchemy\Phrasea\Model\Manipulator\ApiLogManipulator;
|
||||
use Alchemy\Phrasea\Model\Manipulator\ApiOauthTokenManipulator;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
@@ -73,6 +71,8 @@ class OAuthListener
|
||||
$this->getSession($app)->set('token', $token);
|
||||
|
||||
$oAuth2Account = $token->getAccount();
|
||||
// Sets the Api Version
|
||||
$request->attributes->set('api_version', $oAuth2Account->getApiVersion());
|
||||
$oAuth2App = $oAuth2Account->getApplication();
|
||||
|
||||
/** @var PropertyAccess $conf */
|
||||
|
@@ -27,14 +27,11 @@ use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
||||
|
||||
class ApiExceptionHandlerSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
private $app;
|
||||
|
||||
private $logger;
|
||||
|
||||
public function __construct(Application $app)
|
||||
public function __construct(LoggerInterface $logger = null)
|
||||
{
|
||||
$this->app = $app;
|
||||
$this->logger = new NullLogger();
|
||||
$this->logger = $logger ?: new NullLogger();
|
||||
}
|
||||
|
||||
public function setLogger(LoggerInterface $logger)
|
||||
|
@@ -11,6 +11,8 @@
|
||||
|
||||
namespace Alchemy\Phrasea\Core\Provider;
|
||||
|
||||
use Alchemy\Phrasea\Controller\Api\Result;
|
||||
use Alchemy\Phrasea\ControllerProvider\Api\V2;
|
||||
use Silex\Application;
|
||||
use Silex\ServiceProviderInterface;
|
||||
|
||||
@@ -29,9 +31,12 @@ class APIServiceProvider implements ServiceProviderInterface
|
||||
|
||||
return $token ? $app['repo.api-oauth-tokens']->find($token) : null;
|
||||
});
|
||||
|
||||
$app['api.default_version'] = V2::VERSION;
|
||||
}
|
||||
|
||||
public function boot(Application $app)
|
||||
{
|
||||
Result::setDefaultVersion($app['api.default_version']);
|
||||
}
|
||||
}
|
||||
|
@@ -11,7 +11,6 @@
|
||||
|
||||
namespace Alchemy\Phrasea\Model\Manipulator;
|
||||
|
||||
use Alchemy\Phrasea\ControllerProvider\Api\V1;
|
||||
use Alchemy\Phrasea\Model\Entities\ApiAccount;
|
||||
use Alchemy\Phrasea\Model\Entities\ApiApplication;
|
||||
use Alchemy\Phrasea\Model\Entities\User;
|
||||
@@ -26,12 +25,18 @@ class ApiAccountManipulator implements ManipulatorInterface
|
||||
$this->om = $om;
|
||||
}
|
||||
|
||||
public function create(ApiApplication $application, User $user)
|
||||
/**
|
||||
* @param ApiApplication $application
|
||||
* @param User $user
|
||||
* @param string $version
|
||||
* @return ApiAccount
|
||||
*/
|
||||
public function create(ApiApplication $application, User $user, $version)
|
||||
{
|
||||
$account = new ApiAccount();
|
||||
$account->setUser($user);
|
||||
$account->setApplication($application);
|
||||
$account->setApiVersion(V1::VERSION);
|
||||
$account->setApiVersion($version);
|
||||
|
||||
$this->update($account);
|
||||
|
||||
|
@@ -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'));
|
||||
|
@@ -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']);
|
||||
}
|
||||
|
@@ -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"]);
|
||||
|
@@ -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;
|
||||
});
|
||||
|
@@ -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());
|
||||
|
@@ -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());
|
||||
}
|
||||
|
@@ -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');
|
||||
|
@@ -30,6 +30,7 @@ abstract class PhraseanetTestCase extends WebTestCase
|
||||
const USER_AGENT_IE6 = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)';
|
||||
const USER_AGENT_IPHONE = 'Mozilla/5.0 (iPod; U; CPU iPhone OS 2_1 like Mac OS X; fr-fr) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5F137 Safari/525.20';
|
||||
|
||||
/** @var Pimple */
|
||||
protected static $DI;
|
||||
|
||||
private static $recordsInitialized = false;
|
||||
|
Reference in New Issue
Block a user