/**
  * @dataProvider provideExceptionsAndCode
  */
 public function testError($exception, $code)
 {
     $app = new Application('test');
     $app['dispatcher']->addSubscriber(new ApiExceptionHandlerSubscriber($app));
     $app->get('/', function () use($exception) {
         throw $exception;
     });
     $client = new Client($app);
     $client->request('GET', '/');
     $this->assertEquals($code, $client->getResponse()->getStatusCode());
 }
 private function request($accept)
 {
     $app = new Application(Application::ENV_TEST);
     $app['dispatcher']->addSubscriber(new ContentNegotiationSubscriber($app['negotiator'], $app['phraseanet.content-negotiation.priorities']));
     $app->get('/content/negociation', function () {
         return '';
     });
     $client = new Client($app);
     $client->request('GET', '/content/negociation', array(), array(), array('HTTP_Accept' => $accept));
     return $client->getResponse();
 }
 private function request($accept)
 {
     $app = new Application('test');
     $app['dispatcher']->addSubscriber(new ContentNegotiationSubscriber($app));
     $app->get('/content/negociation', function () {
         return '';
     });
     $client = new Client($app);
     $client->request('GET', '/content/negociation', array(), array(), array('HTTP_Accept' => $accept));
     return $client->getResponse();
 }
 /**
  * @dataProvider provideExceptionsAndCode
  */
 public function testErrorOnOtherRoutes($exception, $code, $contentType)
 {
     $app = new Application('test');
     unset($app['exception_handler']);
     $app['dispatcher']->addSubscriber(new ApiOauth2ErrorsSubscriber(PhraseaExceptionHandler::register(), $this->createTranslatorMock()));
     $app->get('/', function () use($exception) {
         throw $exception;
     });
     $client = new Client($app);
     $this->setExpectedException(get_class($exception));
     $client->request('GET', '/');
 }
 public function testItCanBeDisabled()
 {
     $app = new Application();
     $app['exception_handler'] = new PhraseaExceptionHandlerSubscriber(PhraseaExceptionHandler::register());
     $app->get('/', function () {
         throw new \Exception();
     });
     $app['exception_handler']->disable();
     $client = new Client($app);
     $this->setExpectedException('\\Exception');
     $client->request('GET', '/');
 }
 /**
  * @param array  $conf
  * @param string $method
  * @param array  $extraHeaders
  *
  * @return \Symfony\Component\HttpFoundation\Response
  */
 private function request(array $conf, $method = 'GET', array $extraHeaders = [])
 {
     $app = new Application('test');
     $app['phraseanet.configuration']['api_cors'] = $conf;
     $app['dispatcher']->addSubscriber(new ApiCorsSubscriber($app));
     $app->get('/api/v1/test-route', function () {
         return '';
     });
     $client = new Client($app);
     $client->request($method, '/api/v1/test-route', [], [], array_merge($extraHeaders, ['HTTP_Origin' => $this->origin]));
     return $client->getResponse();
 }
 public function testNoHeaderNoRedirection()
 {
     $app = new Application();
     unset($app['exception_handler']);
     $app['dispatcher']->addSubscriber(new FirewallSubscriber());
     $app->get('/', function () {
         throw new HttpException(500);
     });
     $client = new Client($app);
     $this->setExpectedException('Symfony\\Component\\HttpKernel\\Exception\\HttpException');
     $client->request('GET', '/');
 }
 public function testErrorOnOtherExceptions()
 {
     $app = new Application('test');
     $app['bridge.account'] = $this->getMockBuilder('Bridge_Account')->disableOriginalConstructor()->getMock();
     unset($app['exception_handler']);
     $app['dispatcher']->addSubscriber(new BridgeExceptionSubscriber($app));
     $app->get('/', function () {
         throw new \InvalidArgumentException();
     });
     $client = new Client($app);
     $this->setExpectedException('\\InvalidArgumentException');
     $client->request('GET', '/');
 }
 /**
  * @dataProvider provideExceptionsAndCode
  */
 public function testError($exception, $code)
 {
     $app = new Application('test');
     $app['api'] = function () use($app) {
         return new \API_V1_adapter($app);
     };
     $app->register(new \API_V1_Timer());
     $app['dispatcher']->addSubscriber(new ApiExceptionHandlerSubscriber($app));
     $app->get('/', function () use($exception) {
         throw $exception;
     });
     $client = new Client($app);
     $client->request('GET', '/');
     $this->assertEquals($code, $client->getResponse()->getStatusCode());
     $this->assertEquals('application/json', $client->getResponse()->headers->get('content-type'));
 }
 /**
  * @dataProvider provideVariousRoutes
  */
 public function testRoutes($disabled, $route)
 {
     $app = new Application();
     $app['dispatcher']->addSubscriber(new CookiesDisablerSubscriber($app));
     $app->get($route, function () {
         $response = new Response();
         $response->headers->setCookie(new Cookie('key', 'value'));
         return $response;
     });
     $client = $this->getClientWithCookie($app);
     $client->request('GET', $route);
     $this->assertSame($disabled, $app['session.test']);
     if ($disabled) {
         $this->assertCount(0, $client->getResponse()->headers->getCookies(ResponseHeaderBag::COOKIES_ARRAY));
     } else {
         $this->assertGreaterThanOrEqual(1, count($client->getResponse()->headers->getCookies(ResponseHeaderBag::COOKIES_ARRAY)));
     }
 }
 /**
  * @dataProvider provideIpsAndEnvironments
  */
 public function testIpsAndEnvironments($exceptionThrown, $env, $incomingIp, $authorized)
 {
     $app = new Application($env);
     unset($app['exception_handler']);
     $app['phraseanet.configuration.config-path'] = __DIR__ . '/Fixtures/configuration-debugger.yml';
     $app['phraseanet.configuration.config-compiled-path'] = __DIR__ . '/Fixtures/configuration-debugger.php';
     if (is_file($app['phraseanet.configuration.config-compiled-path'])) {
         unlink($app['phraseanet.configuration.config-compiled-path']);
     }
     $app['conf']->set(['debugger', 'allowed-ips'], $authorized);
     $app['dispatcher']->addSubscriber(new DebuggerSubscriber($app));
     $app->get('/', function () {
         return 'success';
     });
     $app->boot();
     if ($exceptionThrown) {
         $this->setExpectedException('Symfony\\Component\\HttpKernel\\Exception\\AccessDeniedHttpException');
     }
     $app->handle(new Request([], [], [], [], [], ['REMOTE_ADDR' => $incomingIp]));
 }
 public function testCheckPositive()
 {
     $app = new Application();
     $app['phraseanet.configuration.config-path'] = __DIR__ . '/Fixtures/configuration-maintenance.yml';
     $app['phraseanet.configuration.config-compiled-path'] = __DIR__ . '/Fixtures/configuration-maintenance.php';
     if (is_file($app['phraseanet.configuration.config-compiled-path'])) {
         unlink($app['phraseanet.configuration.config-compiled-path']);
     }
     unset($app['exception_handler']);
     $app['dispatcher']->addSubscriber(new MaintenanceSubscriber($app));
     $app->get('/', function () {
         return 'Hello';
     });
     $client = new Client($app);
     try {
         $client->request('GET', '/');
         $this->fail('An exception should have been raised');
     } catch (HttpException $e) {
         $this->assertEquals(503, $e->getStatusCode());
         $this->assertEquals(['Retry-After' => 3600], $e->getHeaders());
     }
 }
 /**
  * @dataProvider provideRouteParameters
  */
 public function testRoutes($route, $isJson, $exceptionExpected)
 {
     $app = new Application();
     unset($app['exception_handler']);
     $app['dispatcher']->addSubscriber(new JsonRequestSubscriber());
     $app->get($route, function () {
         throw new \Exception('I disagree');
     });
     $client = new Client($app);
     $headers = $isJson ? ['HTTP_ACCEPT' => 'application/json'] : [];
     if ($exceptionExpected) {
         $this->setExpectedException('Exception');
     }
     $client->request('GET', $route, [], [], $headers);
     if (!$exceptionExpected) {
         $this->assertEquals(200, $client->getResponse()->getStatusCode());
         $this->assertEquals('application/json', $client->getResponse()->headers->get('content-type'));
         $data = json_decode($client->getResponse()->getContent(), true);
         $this->assertArrayHasKey('success', $data);
         $this->assertArrayHasKey('message', $data);
         $this->assertFalse($data['success']);
     }
 }
Example #14
0
 private function getApp()
 {
     $app = new Application(Application::ENV_TEST);
     $app->get('/', function (Application $app, Request $request) {
         $app['session']->set('usr_id', 5);
         $response = new Response('hello');
         $response->headers->setCookie(new Cookie('key', 'value'));
         return $response;
     });
     unset($app['exception_handler']);
     return $app;
 }
 /**
  * @dataProvider forbiddenRouteProvider
  */
 public function testForbiddenRoutes($route)
 {
     $app = new Application('test');
     $app['dispatcher']->addSubscriber(new SessionManagerSubscriber($app));
     $app['authentication'] = $this->getMockBuilder('Alchemy\\Phrasea\\Authentication\\Authenticator')->disableOriginalConstructor()->getMock();
     $app['authentication']->expects($this->never())->method('isAuthenticated');
     $app['EM'] = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock();
     $app['EM']->expects($this->never())->method('flush');
     $app->get('/login', function () {
         return '';
     })->bind("homepage");
     $app->get($route, function () {
         return '';
     });
     $client = new Client($app);
     $client->request('GET', $route, [], [], ['HTTP_CONTENT-TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json', 'HTTP_X-Requested-With' => 'XMLHttpRequest']);
 }
Example #16
0
use Alchemy\Phrasea\Core\Event\ApiLoadEndEvent;
use Alchemy\Phrasea\Core\Event\ApiLoadStartEvent;
use Alchemy\Phrasea\Core\Event\Subscriber\ApiOauth2ErrorsSubscriber;
use Alchemy\Phrasea\Core\Event\Subscriber\ApiExceptionHandlerSubscriber;
use Monolog\Logger;
use Monolog\Processor\WebProcessor;
use Silex\Application as SilexApplication;
use Symfony\Component\HttpFoundation\Request;
return call_user_func(function ($environment = PhraseaApplication::ENV_PROD) {
    $app = new PhraseaApplication($environment);
    $app->loadPlugins();
    $app['exception_handler'] = $app->share(function ($app) {
        return new ApiExceptionHandlerSubscriber($app);
    });
    $app['monolog'] = $app->share($app->extend('monolog', function (Logger $monolog) {
        $monolog->pushProcessor(new WebProcessor());
        return $monolog;
    }));
    $app->register(new \API_V1_Timer());
    $app['dispatcher']->dispatch(PhraseaEvents::API_LOAD_START, new ApiLoadStartEvent());
    $app->get('/api/', function (Request $request, SilexApplication $app) {
        $apiAdapter = new \API_V1_adapter($app);
        $result = new \API_V1_result($app, $request, $apiAdapter);
        return $result->set_datas(['name' => $app['conf']->get(['registry', 'general', 'title']), 'type' => 'phraseanet', 'description' => $app['conf']->get(['registry', 'general', 'description']), 'documentation' => 'https://docs.phraseanet.com/Devel', 'versions' => ['1' => ['number' => $apiAdapter->get_version(), 'uri' => '/api/v1/', 'authenticationProtocol' => 'OAuth2', 'authenticationVersion' => 'draft#v9', 'authenticationEndPoints' => ['authorization_token' => '/api/oauthv2/authorize', 'access_token' => '/api/oauthv2/token']]]])->get_response();
    });
    $app->mount('/api/oauthv2', new Oauth2());
    $app->mount('/api/v1', new V1());
    $app['dispatcher']->addSubscriber(new ApiOauth2ErrorsSubscriber($app['phraseanet.exception_handler'], $app['translator']));
    $app['dispatcher']->dispatch(PhraseaEvents::API_LOAD_END, new ApiLoadEndEvent());
    return $app;
}, isset($environment) ? $environment : PhraseaApplication::ENV_PROD);
Example #17
0
            $request->setRequestFormat(Result::FORMAT_JSONP);
        } else {
            $request->setRequestFormat($request->getFormat($format->getValue()));
        }
        // tells whether asked format is extended or not
        $request->attributes->set('_extended', in_array($request->getRequestFormat(Result::FORMAT_JSON), array(Result::FORMAT_JSON_EXTENDED, Result::FORMAT_YAML_EXTENDED, Result::FORMAT_JSONP_EXTENDED)));
    }, PhraseaApplication::EARLY_EVENT);
    $app->after(function (Request $request, Response $response) use($app) {
        if ($request->getRequestFormat(Result::FORMAT_JSON) === Result::FORMAT_JSONP && !$response->isOk() && !$response->isServerError()) {
            $response->setStatusCode(200);
        }
        // set response content type
        if (!$response->headers->get('Content-Type')) {
            $response->headers->set('Content-Type', $request->getMimeType($request->getRequestFormat(Result::FORMAT_JSON)));
        }
    });
    $app->get('/api/', function (Request $request, SilexApplication $app) {
        return Result::create($request, ['name' => $app['conf']->get(['registry', 'general', 'title']), 'type' => 'phraseanet', 'description' => $app['conf']->get(['registry', 'general', 'description']), 'documentation' => 'https://docs.phraseanet.com/Devel', 'versions' => ['1' => ['number' => V1::VERSION, 'uri' => '/api/v1/', 'authenticationProtocol' => 'OAuth2', 'authenticationVersion' => 'draft#v9', 'authenticationEndPoints' => ['authorization_token' => '/api/oauthv2/authorize', 'access_token' => '/api/oauthv2/token']]]])->createResponse();
    });
    $app->mount('/api/oauthv2', new Oauth2());
    $app->mount('/api/v1', new V1());
    $app['dispatcher'] = $app->share($app->extend('dispatcher', function ($dispatcher, PhraseaApplication $app) {
        $dispatcher->addSubscriber(new ApiOauth2ErrorsSubscriber($app['phraseanet.exception_handler'], $app['translator']));
        return $dispatcher;
    }));
    $app->after(function (Request $request, Response $response) use($app) {
        $app['dispatcher']->dispatch(PhraseaEvents::API_RESULT, new ApiResultEvent($request, $response));
    });
    $app['dispatcher']->addSubscriber(new ApiCorsSubscriber($app));
    return $app;
}, isset($environment) ? $environment : PhraseaApplication::ENV_PROD);