-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The commit updates the return type of filter methods in `Contacts.php` and `Invoices.php` from `static` to explicit class names, for better clarity and stability. It also changes some uses of fully qualified class names in `XeroAuthenticated.php` and `XeroShowAllCommand.php` to uses of `use` statements for better readability. Unnecessary namespace in `XeroShowAllCommand.php` was removed.
- Loading branch information
Showing
7 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Dcblogdev\Xero\Actions; | ||
|
||
use Dcblogdev\Xero\Exceptions\XeroTokenExpiredException; | ||
use Dcblogdev\Xero\Models\XeroToken; | ||
use Exception; | ||
use Illuminate\Http\RedirectResponse; | ||
|
||
class tokenExpiredAction | ||
{ | ||
/** | ||
* @throws Exception | ||
*/ | ||
public function handle(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 null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Dcblogdev\Xero\Exceptions; | ||
|
||
use Exception; | ||
use Illuminate\Http\RedirectResponse; | ||
|
||
class XeroTokenExpiredException extends Exception | ||
{ | ||
public function render(): RedirectResponse | ||
{ | ||
return redirect()->away(config('xero.redirectUri'))->with('error', $this->getMessage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Dcblogdev\Xero\database\factories; | ||
|
||
use Dcblogdev\Xero\Models\XeroToken; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class TokenFactory extends Factory | ||
{ | ||
protected $model = XeroToken::class; | ||
|
||
public function definition(): array | ||
{ | ||
return [ | ||
'tenant_id' => $this->faker->uuid, | ||
'tenant_name' => $this->faker->name, | ||
'access_token' => $this->faker->uuid, | ||
'refresh_token' => $this->faker->uuid, | ||
'expires_in' => $this->faker->randomNumber(), | ||
'created_at' => $this->faker->dateTime, | ||
'updated_at' => $this->faker->dateTime, | ||
'scopes' => config('xero.scopes'), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?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(){ | ||
|
||
$token = XeroToken::factory()->create(); | ||
|
||
$result = ['error' => 'invalid_grant']; | ||
|
||
$action = new tokenExpiredAction(); | ||
$action->handle($result, $token); | ||
|
||
assertDatabaseCount(XeroToken::class, 0); | ||
|
||
})->throws(Exception::class, 'Xero token has expired, please re-authenticate.'); | ||
|
||
test('token refresh does not throw an exception and token is not deleted', function(){ | ||
|
||
$token = XeroToken::factory()->create(); | ||
|
||
$result = []; | ||
|
||
$action = new tokenExpiredAction(); | ||
$response = $action->handle($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); |