Skip to content

Commit

Permalink
Merge pull request #16 from vivait/fix/DOC-29
Browse files Browse the repository at this point in the history
[DOC-29] Fix client not refreshing tokens
  • Loading branch information
leightonthomas authored Dec 17, 2018
2 parents c8e4206 + 00fe496 commit d5bd533
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 73 deletions.
31 changes: 12 additions & 19 deletions src/Vivait/DocBuild/DocBuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vivait\DocBuild\Exception\CacheException;
use Vivait\DocBuild\Exception\FileException;
use Vivait\DocBuild\Exception\TokenExpiredException;
use Vivait\DocBuild\Exception\TokenInvalidException;
use Vivait\DocBuild\Exception\UnauthorizedException;
use Vivait\DocBuild\Http\Adapter;
use Vivait\DocBuild\Http\Response;
Expand Down Expand Up @@ -481,13 +479,11 @@ private function performRequest(
$headers
);
} catch (HttpException $e) {
$code = $e->getCode();

if ( ! \in_array($code, [400, 401, 403])) {
if ($e->getResponse() === null) {
throw $e;
}

if ($e->getResponse() === null) {
if ( ! \in_array($e->getResponse()->getStatusCode(), [400, 401, 403])) {
throw $e;
}

Expand All @@ -499,24 +495,21 @@ private function performRequest(

$message = $body['error_description'];

if( ! $this->cache->delete($this->options->getCacheKey())){
throw new CacheException('Could not delete the key in the cache. Do you have permission?');
}

switch ($message) {
// We're unauthorised, so get the token again if we need to
case self::TOKEN_EXPIRED:
throw new TokenExpiredException;
case self::TOKEN_INVALID:
throw new TokenInvalidException;
default:
// We're unauthorised, so get the token again if we need to or re-throw
if( ! $this->cache->delete($this->options->getCacheKey())){
throw new CacheException('Could not delete the key in the cache. Do you have permission?');
}

if ($e instanceof TokenExpiredException || $e instanceof TokenInvalidException) {
if ($this->options->shouldTokenRefresh()) {
return $this->$method($resource, $request, $headers);
}
if ($this->options->shouldTokenRefresh()) {
return $this->$method($resource, $request, $headers);
}

// Can't re-authenticate
break;
default:
// Can't re-authenticate, throw unauthorized
throw new UnauthorizedException(\is_string($message) ? $message : null);
}
}
Expand Down
17 changes: 0 additions & 17 deletions src/Vivait/DocBuild/Exception/TokenExpiredException.php

This file was deleted.

17 changes: 0 additions & 17 deletions src/Vivait/DocBuild/Exception/TokenInvalidException.php

This file was deleted.

68 changes: 48 additions & 20 deletions tests/Vivait/DocBuild/DocBuildTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use Vivait\DocBuild\DocBuild;
use Vivait\DocBuild\Exception\CacheException;
use Vivait\DocBuild\Exception\FileException;
use Vivait\DocBuild\Exception\TokenExpiredException;
use Vivait\DocBuild\Exception\TokenInvalidException;
use Vivait\DocBuild\Exception\UnauthorizedException;
use Vivait\DocBuild\Http\Adapter;
use Vivait\DocBuild\Http\Response;
Expand Down Expand Up @@ -170,7 +168,7 @@ public function itErrorsWithInvalidCredentials(int $status): void
$status,
"Test",
$req1 = new Response(
200,
$status,
$this->asStream(\json_encode(['error_description' => 'unrecognised error']))
)
);
Expand Down Expand Up @@ -215,7 +213,7 @@ public function itWillThrowAnExceptionIfTheCacheKeyCannotBeDeleted(int $status):
$status,
"Test",
$req1 = new Response(
200,
$status,
$this->asStream(\json_encode(['error_description' => 'unrecognised error']))
)
);
Expand All @@ -234,34 +232,49 @@ public function itWillThrowAnExceptionIfTheCacheKeyCannotBeDeleted(int $status):
*
* @param int $status
*/
public function itWillThrowAnExceptionIfTheTokenIsExpired(int $status): void
public function itWillRetryARequestIfTheTokenIsExpired(int $status): void
{
$this->expectException(TokenExpiredException::class);

$this->cache->expects(self::once())
$this->cache->expects(self::exactly(2))
->method('contains')
->with('token')
->willReturn(true)
;

$this->cache->expects(self::once())
$this->cache->expects(self::exactly(2))
->method('fetch')
->with('token')
->willReturn('badToken')
;

$this->cache->expects(self::once())
->method('delete')
->willReturn(true)
;

$exception = new HttpException(
$status,
"Test",
$req1 = new Response(
200,
$status,
$this->asStream(\json_encode(['error_description' => DocBuild::TOKEN_EXPIRED]))
)
);

$this->client->expects(self::once())
$count = 0;

$this->client->expects(self::exactly(2))
->method('sendRequest')
->willThrowException($exception)
->willReturnCallback(
function() use(&$count, $exception) {
if ($count === 0) {
$count++;

throw $exception;
}

return new Response(200, $this->asStream('a'));
}
)
;

$this->docBuild->getDocuments();
Expand All @@ -273,34 +286,49 @@ public function itWillThrowAnExceptionIfTheTokenIsExpired(int $status): void
*
* @param int $status
*/
public function itWillThrowAnExceptionIfTheTokenIsInvalid(int $status): void
public function itWillRetryARequestIfTheTokenIsInvalid(int $status): void
{
$this->expectException(TokenInvalidException::class);

$this->cache->expects(self::once())
$this->cache->expects(self::exactly(2))
->method('contains')
->with('token')
->willReturn(true)
;

$this->cache->expects(self::once())
$this->cache->expects(self::exactly(2))
->method('fetch')
->with('token')
->willReturn('badToken')
;

$this->cache->expects(self::once())
->method('delete')
->willReturn(true)
;

$exception = new HttpException(
$status,
"Test",
$req1 = new Response(
200,
$status,
$this->asStream(\json_encode(['error_description' => DocBuild::TOKEN_INVALID]))
)
);

$this->client->expects(self::once())
$count = 0;

$this->client->expects(self::exactly(2))
->method('sendRequest')
->willThrowException($exception)
->willReturnCallback(
function() use(&$count, $exception) {
if ($count === 0) {
$count++;

throw $exception;
}

return new Response(200, $this->asStream('a'));
}
)
;

$this->docBuild->getDocuments();
Expand Down

0 comments on commit d5bd533

Please sign in to comment.