Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pelmered committed Sep 16, 2024
1 parent 2320d92 commit 194286f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tests/Unit/MacroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function (Request $request) {
),
[
'scopes' => ['scope1', 'scope2'],
'tokenType' => AccessToken::TYPE_BEARER,
'tokenType' => AccessToken::TOKEN_TYPE_BEARER,
'authType' => Credentials::AUTH_TYPE_BASIC,
'cacheDriver' => 'file',
'cacheKey' => 'my_cache_key',
Expand Down
42 changes: 40 additions & 2 deletions tests/Unit/RefreshTokenTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

uses(\Pelmered\LaravelHttpOAuthHelper\Tests\TestCase::class);

use Carbon\Carbon;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Cache;
Expand Down Expand Up @@ -85,6 +87,9 @@

test('refresh token custom', function () {
Cache::clear();

$callback = fn (PendingRequest $httpClient) => $httpClient->withHeader('Authorization', 'my_custom_token');

$accessToken = app(RefreshToken::class)(
'https://example.com/oauth/token',
new Credentials(
Expand All @@ -94,11 +99,12 @@
scopes: ['scope1', 'scope2'],
grantType: 'password_credentials',
tokenType: AccessToken::TOKEN_TYPE_CUSTOM,
tokenTypeCustomCallback: fn (PendingRequest $httpClient) => $httpClient->withHeader('Authorization', 'my_custom_token'),
tokenTypeCustomCallback: $callback,
),
);

expect($accessToken->getAccessToken())->toEqual('this_is_my_access_token_from_body_refresh_token');
expect($accessToken->getAccessToken())->toEqual('this_is_my_access_token_from_body_refresh_token')
->and($accessToken->getCustomCallback())->toEqual($callback);
Http::assertSent(static function (Request $request) {
return $request->url() === 'https://example.com/oauth/token'
&& $request->hasHeader('Authorization', 'my_custom_token')
Expand Down Expand Up @@ -209,4 +215,36 @@
);
});
*/

test('throws exception with an invalid auth type', function () {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('customCallback must be set when using AUTH_TYPE_CUSTOM');

app(RefreshToken::class)(
'https://example.com/oauth/token',
new Credentials(['my_token']),
new Options(
scopes: ['scope1', 'scope2'],
tokenType: AccessToken::TOKEN_TYPE_CUSTOM,
),
);
});


test('access token getters', function () {

$accessToken = app(RefreshToken::class)(
'https://example.com/oauth/token',
new Credentials(['my_token']),
new Options(
scopes: ['scope1', 'scope2'],
),
);

expect($accessToken->getAccessToken())->toBe('this_is_my_access_token_from_body_refresh_token')
->and($accessToken->getExpiresIn())->toBe(3540)
->and($accessToken->getExpiresAt())->toBeInstanceOf(Carbon::class)
->and($accessToken->getCustomCallback())->toBeNull();
});

})->done(assignee: 'pelmered');

0 comments on commit 194286f

Please sign in to comment.