Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dcblogdev committed Aug 31, 2024
1 parent 58299d4 commit 700b1a0
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 61 deletions.
4 changes: 2 additions & 2 deletions src/Actions/tokenExpiredAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class tokenExpiredAction
/**
* @throws Exception
*/
public function handle(array $result, XeroToken $token): ?RedirectResponse
public function __invoke(array $result, XeroToken $token): ?RedirectResponse
{
if (isset($result['error']) && $result['error'] === 'invalid_grant') {
$token->delete();

if (app()->runningInConsole()) {
throw new Exception('Xero token has expired, please re-authenticate.');
} else {
throw new XeroTokenExpiredException('Xero token has expired, please re-authenticate.');
return redirect()->away(config('xero.redirectUri'));
}
}

Expand Down
14 changes: 0 additions & 14 deletions src/Exceptions/XeroTokenExpiredException.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Xero.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function getAccessToken(bool $redirectWhenNotConnected = true): string
/**
* @throws Exception
*/
public function renewExpiringToken(XeroToken $token, tokenExpiredAction $tokenExpiredAction): string
public function renewExpiringToken(XeroToken $token): string
{
$params = [
'grant_type' => 'refresh_token',
Expand All @@ -203,7 +203,7 @@ public function renewExpiringToken(XeroToken $token, tokenExpiredAction $tokenEx

$result = $this->sendPost(self::$tokenUrl, $params);

$tokenExpiredAction->handle($result, $token);
app(tokenExpiredAction::class)($result, $token);

$this->storeToken($result, ['tenant_id' => $token->tenant_id]);

Expand Down
45 changes: 2 additions & 43 deletions tests/Actions/HandleTokenExpiredActionTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php

use Dcblogdev\Xero\Actions\tokenExpiredAction;
use Dcblogdev\Xero\Exceptions\XeroTokenExpiredException;
use Dcblogdev\Xero\Models\XeroToken;
use Illuminate\Support\Facades\Route;
use function Pest\Laravel\assertDatabaseCount;

test('token refresh throws exception when expired and a refresh is attempted over cli', function(){
Expand All @@ -12,8 +10,7 @@

$result = ['error' => 'invalid_grant'];

$action = new tokenExpiredAction();
$action->handle($result, $token);
app(tokenExpiredAction::class)($result, $token);

assertDatabaseCount(XeroToken::class, 0);

Expand All @@ -25,47 +22,9 @@

$result = [];

$action = new tokenExpiredAction();
$response = $action->handle($result, $token);
$response = app(tokenExpiredAction::class)($result, $token);

expect($response)->toBeNull();

assertDatabaseCount(XeroToken::class, 1);

});

test('wip', function(){

$token = XeroToken::factory()->create();

$result = ['error' => 'invalid_grant'];

try {
$action = new TokenExpiredAction();
$action->handle($result, $token);
} catch (XeroTokenExpiredException $e) {
$this->assertEquals(config('xero.redirectUri'), $e->render()->getTargetUrl());
}


})->throws(XeroTokenExpiredException::class, 'Xero token has expired, please re-authenticate.');

test('token refresh redirects when expired and a refresh is attempted over HTTP', function () {

$this->withoutExceptionHandling();

$token = XeroToken::factory()->create();
assertDatabaseCount(XeroToken::class, 1);

// Define a temporary route in the test to handle the action.
Route::post('/test-endpoint', function () use ($token) {
$result = ['error' => 'invalid_grant'];
$action = new TokenExpiredAction();
$action->handle($result, $token);
});

$this->post('/test-endpoint')
->assertRedirect(config('xero.redirectUri'));

assertDatabaseCount(XeroToken::class, 0);
})->throws(XeroTokenExpiredException::class);

0 comments on commit 700b1a0

Please sign in to comment.