Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
feat(Error): add oauth error classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jens Schulze committed May 2, 2016
1 parent 68d263a commit 92eec57
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/Error/AccessDeniedError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* @author @jayS-de <jens.schulze@commercetools.de>
*/

namespace Commercetools\Core\Error;

/**
* @package Commercetools\Core\Error
*
* @method string getCode()
* @method AccessDeniedError setCode(string $code = null)
* @method string getMessage()
* @method AccessDeniedError setMessage(string $message = null)
*/
class AccessDeniedError extends ApiError
{
const CODE = 'access_denied';
}
19 changes: 19 additions & 0 deletions src/Error/InvalidTokenError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* @author @jayS-de <jens.schulze@commercetools.de>
*/

namespace Commercetools\Core\Error;

/**
* @package Commercetools\Core\Error
*
* @method string getCode()
* @method AccessDeniedError setCode(string $code = null)
* @method string getMessage()
* @method AccessDeniedError setMessage(string $message = null)
*/
class InvalidTokenError extends ApiError
{
const CODE = 'invalid_token';
}
57 changes: 55 additions & 2 deletions tests/integration/Errors/ErrorResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
namespace Commercetools\Core\Errors;

use Commercetools\Core\ApiTestCase;
use Commercetools\Core\Client\OAuth\Manager;
use Commercetools\Core\Error\AccessDeniedError;
use Commercetools\Core\Error\ApiException;
use Commercetools\Core\Error\ConcurrentModificationError;
use Commercetools\Core\Error\DuplicateAttributeValueError;
use Commercetools\Core\Error\DuplicateAttributeValuesError;
Expand All @@ -18,6 +21,7 @@
use Commercetools\Core\Error\InvalidCurrentPasswordError;
use Commercetools\Core\Error\InvalidFieldError;
use Commercetools\Core\Error\InvalidOperationError;
use Commercetools\Core\Error\InvalidTokenError;
use Commercetools\Core\Error\RequiredFieldError;
use Commercetools\Core\Error\ResourceNotFoundError;
use Commercetools\Core\Model\Common\Attribute;
Expand All @@ -44,6 +48,7 @@
use Commercetools\Core\Request\Products\ProductUpdateRequest;
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeAddAttributeDefinitionAction;
use Commercetools\Core\Request\ProductTypes\ProductTypeUpdateRequest;
use Commercetools\Core\Response\ErrorResponse;

class ErrorResponseTest extends ApiTestCase
{
Expand Down Expand Up @@ -117,7 +122,6 @@ public function testConcurrentModification()
$this->assertTrue($response->isError());
$this->assertInstanceOf('\Commercetools\Core\Response\ErrorResponse', $response);
$error = $response->getErrors()->current();
var_dump((string)$response->getBody());
$this->assertInstanceOf(
'\Commercetools\Core\Error\ConcurrentModificationError',
$error
Expand Down Expand Up @@ -585,7 +589,7 @@ public function testInvalidField()
$this->assertSame(['test'], $error->getAllowedValues());
}

public function testInvalidClientScope()
public function testInsufficientScope()
{
$draft = $this->getProductDraft();

Expand All @@ -603,4 +607,53 @@ public function testInvalidClientScope()
);
$this->assertSame(InsufficientScopeError::CODE, $error->getCode());
}

public function testInvalidToken()
{
$client = $this->getClient('manage_project');
$cacheScope = $client->getConfig()->getScope() . '-' . $client->getConfig()->getGrantType();
$cacheKey = Manager::TOKEN_CACHE_KEY . '-' . sha1($cacheScope);
$client->getOauthManager()->getCacheAdapter()->store($cacheKey, '1234');

$request = ProductQueryRequest::of();
$client->addBatchRequest($request);

$responses = $client->executeBatch();
$response = current($responses);
$this->assertTrue($response->isError());
$this->assertInstanceOf('\Commercetools\Core\Response\ErrorResponse', $response);
$this->assertSame(401, $response->getStatusCode());

$error = $response->getErrors()->current();
$this->assertInstanceOf(
'\Commercetools\Core\Error\InvalidTokenError',
$error
);
$this->assertSame(InvalidTokenError::CODE, $error->getCode());
}

public function testAccessDenied()
{
$request = ProductQueryRequest::of();
$httpRequest = $request->httpRequest();

$client = $this->getClient('manage_project');
$httpClient = $client->getHttpClient();
try {
$httpResponse = $httpClient->execute($httpRequest);
} catch (ApiException $exception) {
$httpResponse = $exception->getResponse();
$response = new ErrorResponse($exception, $request, $httpResponse);
}
$this->assertTrue($response->isError());
$this->assertInstanceOf('\Commercetools\Core\Response\ErrorResponse', $response);
$this->assertSame(401, $response->getStatusCode());

$error = $response->getErrors()->current();
$this->assertInstanceOf(
'\Commercetools\Core\Error\AccessDeniedError',
$error
);
$this->assertSame(AccessDeniedError::CODE, $error->getCode());
}
}

0 comments on commit 92eec57

Please sign in to comment.