Commit 9b3619af authored by Laurent's avatar Laurent
Browse files

Adding more Test Failures on Electre service management

parent 38f3f9e5
Pipeline #9176 passed with stage
in 1 minute and 26 seconds
......@@ -38,19 +38,65 @@ class Electre extends Provider {
public function fetchRecord($search_request) {
$token = $this->_getToken($this->_client_id, $this->_client_secret);
try{
$response = $this->_getToken($this->_client_id, $this->_client_secret);
} catch(\Exception $exception) {
return (new FetchRecordResult())->beError(StatusCodeInterface::STATUS_GATEWAY_TIMEOUT
,
'no_answer_from_gateway');
}
if ($response->getStatusCode() == StatusCodeInterface::STATUS_BAD_REQUEST)
return (new FetchRecordResult())->beError(StatusCodeInterface::STATUS_UNAUTHORIZED
,
'invalid_credentials');
if ($response->getStatusCode() != StatusCodeInterface::STATUS_OK)
return (new FetchRecordResult())->beError(StatusCodeInterface::STATUS_BAD_GATEWAY
,
'provider_error');
$json_response = $this->_parseJsonResponse($response);
if (! $json_response)
return (new FetchRecordResult())->beError(StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY
,
'cannot_parse_response');
if (!isset($json_response->access_token))
return (new FetchRecordResult())->beError(StatusCodeInterface::STATUS_NOT_ACCEPTABLE
,
'no_expected_attribute_in_data');
$token = $json_response->access_token;
if (!$token) {
return (new FetchRecordResult())->beError(401, 'invalid_credentials');
try{
$response = $this->_getAllLinks($token, $this->_getIsbnOrEan($search_request));
} catch(\Exception $exception) {
return (new FetchRecordResult())->beError(StatusCodeInterface::STATUS_GATEWAY_TIMEOUT
,
'no_answer_from_gateway');
}
$links = $this->_getAllLinks($token, $this->_getIsbnOrEan($search_request));
if ($links->count()==0) {
if ($response->getStatusCode() != StatusCodeInterface::STATUS_OK)
return (new FetchRecordResult())->beError(StatusCodeInterface::STATUS_NOT_FOUND
,
'ean_not_found');
}
$json_response = $this->_parseJsonResponse($response);
if (! $json_response)
return (new FetchRecordResult())->beError(StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY
,
'cannot_parse_response');
if (!isset($json_response->_links))
return (new FetchRecordResult())->beError(StatusCodeInterface::STATUS_NOT_ACCEPTABLE
,
'no_expected_attribute_in_data');
$links = new \Storm\Model\Collection($json_response->_links);
$medialist =
$links->select(
......@@ -93,21 +139,14 @@ class Electre extends Provider {
'client_secret' =>$secret,
'grant_type'=> 'client_credentials',
'scope'=>'webapi']));
if ($response->getStatusCode() != StatusCodeInterface::STATUS_OK)
return '';
return $this->_parseJsonResponse($response)->access_token;
return $response;
}
public function _getAllLinks($token, $ean){
public function _getAllLinks($token, $ean) {
$browser = static::newHttpClient();
$response = $browser->get(static::API_END_POINT . '/v1.0/eans/' . $ean,
['Authorization'=>'Bearer '. $token]);
if ($response->getStatusCode() != StatusCodeInterface::STATUS_OK)
return new \Storm\Model\Collection();
return new \Storm\Model\Collection( $this->_parseJsonResponse($response)->_links );
return $response;
}
}
......@@ -105,18 +105,19 @@ class MediaGetTest extends TestCase {
class MediaWithErrorOnAuthenticationTest extends TestCase {
/** @test */
public function withAuthorizationHeaderEmptyResponseStatusShouldBe407() {
$this->assertEquals(407,$this->dispatch('/1.0/media/isbn/2259228180',
['Authorization' => ""])->getStatusCode());
$this->assertEquals(407,
$this->dispatch('/1.0/media/isbn/2259228180',
['Authorization' => ""])->getStatusCode());
}
/** @test */
public function withAuthorizationHeaderEmptyWithPelliculeEmptyResponseStatusShouldBe407() {
$this->assertEquals(407,$this->dispatch('/1.0/media/isbn/2259228180',
['Authorization' => "Pellicule "])->getStatusCode());
$this->assertEquals(407,
$this->dispatch('/1.0/media/isbn/2259228180',
['Authorization' => "Pellicule "])->getStatusCode());
}
}
......@@ -281,4 +282,166 @@ class MediaWithElectreWithInvalidCredentialsTest extends TestCase {
$this->assertEquals('{"error":"invalid_credentials"}', $this->_response->getBody()->getContents());
}
}
\ No newline at end of file
}
class MediaWithElectreWithServiceInErrorTest extends TestCase {
protected $_mock_browser;
public function setUp() {
parent::setUp();
$this->_mock_browser = new BrowserMockBuilder();
Electre::setDefaultHttpClient($this->_mock_browser->getMock());
}
/** @test */
public function withError500OnElectreResponseShouldHaveStatus502() {
$this->_mock_browser
->expectPost(['url' => 'https://electre3staging-idp.bvdep.com/connect/token',
'body' => ['client_id' => 'multipass',
'client_secret' => '1314',
'grant_type'=> 'client_credentials',
'scope'=>'webapi']],
['status'=> 500,
'content' => '']);
$response = $this->dispatch('/1.0/media/ean/123456789',
['Authorization' => ['Pellicule ' . base64_encode(json_encode(['provider'=> 'electre',
'client_secret'=>'1314',
'client_id'=>'multipass']))]]);
$this->assertEquals(502, $response->getStatusCode());
return $response;
}
/**
* @depends withError500OnElectreResponseShouldHaveStatus502
* @test
*/
public function withError500OnElectreResponseShouldHaveErrorProviderError($response) {
$this->assertEquals('{"error":"provider_error"}', $response->getBody()->getContents());
}
}
class MediaWithElectreWithServiceInTimeOutTest extends TestCase {
protected $_mock_browser;
public function setUp() {
parent::setUp();
$this->_mock_browser = new BrowserMockBuilder();
Electre::setDefaultHttpClient($this->_mock_browser->getMock());
}
/** @test */
public function withTimeOutOnElectreResponseShouldHaveStatus504() {
$this->_mock_browser
->getMock()
->whenCalled('post')
->with('https://electre3staging-idp.bvdep.com/connect/token',
[],
http_build_query( ['client_id' => 'multipass',
'client_secret' => '1314',
'grant_type'=> 'client_credentials',
'scope'=>'webapi']))
->willDo(function() { throw new \Exception('timeout !!');});
$response = $this->dispatch('/1.0/media/ean/123456789',
['Authorization' => ['Pellicule ' . base64_encode(json_encode(['provider'=> 'electre',
'client_secret'=>'1314',
'client_id'=>'multipass']))]]);
$this->assertEquals(504, $response->getStatusCode());
return $response;
}
/**
* @depends withTimeOutOnElectreResponseShouldHaveStatus504
* @test
*/
public function withTimeOutOnElectreResponseContentShouldHaveErrorNoAnswerFromGateway($response) {
$this->assertEquals('{"error":"no_answer_from_gateway"}', $response->getBody()->getContents());
}
}
class MediaWithElectreWithIncorrectJSONError extends TestCase {
public function setUp(){
parent::setUp();
$mock_browser = (new BrowserMockBuilder())
->expectPost(['url' => 'https://electre3staging-idp.bvdep.com/connect/token',
'body' => ['client_id' => 'multipass',
'client_secret' => 'leeloo',
'grant_type'=> 'client_credentials',
'scope'=>'webapi']],
['content' => '{"access_token":"MyPr3ttyT0k3n","expi}'])
->getMock();
Electre::setDefaultHttpClient($mock_browser);
$this->_response = $this->dispatch('/1.0/media/ean/123456789',
['Authorization' => ['Pellicule ' . base64_encode(json_encode(['provider'=> 'electre',
'client_secret'=>'leeloo',
'client_id'=>'multipass']))]]);
}
/** @test */
public function responseStatusShouldBe422() {
$this->assertEquals('422',$this->_response->getStatusCode());
}
/** @test */
public function responseJSONErrorShouldBeCannotParseResponse() {
$this->_response->getBody()->rewind();
$this->assertEquals('{"error":"cannot_parse_response"}', $this->_response->getBody()->getContents());
}
}
class MediaWithElectreWithNoAccessTokenError extends TestCase {
public function setUp(){
parent::setUp();
$mock_browser = (new BrowserMockBuilder())
->expectPost(['url' => 'https://electre3staging-idp.bvdep.com/connect/token',
'body' => ['client_id' => 'multipass',
'client_secret' => 'leeloo',
'grant_type'=> 'client_credentials',
'scope'=>'webapi']],
['content' => '{"expires_in":3600}'])
->getMock();
Electre::setDefaultHttpClient($mock_browser);
$this->_response = $this->dispatch('/1.0/media/ean/123456789',
['Authorization' => ['Pellicule ' . base64_encode(json_encode(['provider'=> 'electre',
'client_secret'=>'leeloo',
'client_id'=>'multipass']))]]);
}
/** @test */
public function responseStatusShouldBe406() {
$this->assertEquals('406',$this->_response->getStatusCode());
}
/** @test */
public function responseJSONShouldBeErrorNoExpectedAttributeInData() {
$this->_response->getBody()->rewind();
$this->assertEquals('{"error":"no_expected_attribute_in_data"}', $this->_response->getBody()->getContents());
}
}
......@@ -8,6 +8,12 @@ use \Slim\Psr7\Headers;
use \Storm\Test\THelpers;
class TestCase extends \PHPUnit\Framework\TestCase {
public function setUp() {
parent::setUp();
\Storm\Model\ModelAbstract::unsetLoaders();
}
public function dispatch($url, $headers = []){
$app = \Pellicule\AppFactory::create();
$request = new Request('GET',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment