-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MacroTest.php
131 lines (113 loc) · 4.61 KB
/
MacroTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
uses(\Pelmered\LaravelHttpOAuthHelper\Tests\TestCase::class);
use Illuminate\Cache\FileStore;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Pelmered\LaravelHttpOAuthHelper\AccessToken;
use Pelmered\LaravelHttpOAuthHelper\Credentials;
beforeEach(function () {});
test('macro with shorthand refresh token', function () {
$response = Http::withRefreshToken(
'https://example.com/oauth/token',
['my_refresh_token'],
['scopes' => ['scope1', 'scope2']],
)->get('https://example.com/api');
Cache::shouldReceive('get')
->with('test', '', \Closure::class)
->andReturn('');
Http::assertSent(static function (Request $request) {
return $request->hasHeader('Authorization', 'Bearer this_is_my_access_token_from_body_refresh_token') && $request->url() === 'https://example.com/api';
});
});
test('macro with shorthand client credentials', function () {
$response = Http::withRefreshToken(
'https://example.com/oauth/token',
[
'my_client_id', 'my_client_secret',
],
[
'scopes' => ['scope1', 'scope2'],
'authType' => 'basic',
],
)->get('https://example.com/api');
expect($response->json()['data'])->toBe('some data with bearer token');
Http::assertSentInOrder([
function (Request $request) {
return $request->hasHeader('Authorization', 'Basic bXlfY2xpZW50X2lkOm15X2NsaWVudF9zZWNyZXQ=')
&& $request->url() === 'https://example.com/oauth/token';
},
function (Request $request) {
return $request->hasHeader('Authorization', 'Bearer this_is_my_access_token_from_body_refresh_token')
&& $request->url() === 'https://example.com/api';
},
]);
});
test('macro with refresh token in credentials object', function () {
$response = Http::withRefreshToken(
'https://example.com/oauth/token',
new Credentials(
token: 'this_is_my_refresh_token',
),
[
'scopes' => ['scope1', 'scope2'],
'authType' => Credentials::AUTH_TYPE_BEARER,
]
)->get('https://example.com/api');
expect($response->json()['data'])->toBe('some data with bearer token');
Http::assertSentInOrder([
function (Request $request) {
return $request->hasHeader('Authorization', 'Bearer this_is_my_refresh_token') && $request->url() === 'https://example.com/oauth/token';
},
function (Request $request) {
return $request->hasHeader('Authorization', 'Bearer this_is_my_access_token_from_body_refresh_token') && $request->url() === 'https://example.com/api';
},
]);
});
test('macro with client credentials in credentials object', function () {
$response = Http::withRefreshToken(
'https://example.com/oauth/token',
new Credentials(
clientId: 'this_is_my_client_id',
clientSecret: 'this_is_my_client_secret',
),
[
'scopes' => ['scope1', 'scope2'],
'tokenType' => AccessToken::TOKEN_TYPE_QUERY,
'authType' => Credentials::AUTH_TYPE_BASIC,
],
)->get('https://example.com/api');
Http::assertSentInOrder([
function (Request $request) {
return $request->hasHeader('Authorization', 'Basic dGhpc19pc19teV9jbGllbnRfaWQ6dGhpc19pc19teV9jbGllbnRfc2VjcmV0')
&& $request->url() === 'https://example.com/oauth/token';
},
function (Request $request) {
return $request->url() === 'https://example.com/api?token=this_is_my_access_token_from_body_refresh_token';
},
]);
});
test('macro with custom cache store', function () {
Cache::clear();
Cache::spy();
Cache::shouldReceive('store')
->with('file')
->andReturn(new FileStore(app()['files'], 'tests/cache'));
$response = Http::withRefreshToken(
'https://example.com/oauth/token',
new Credentials(
clientId: 'this_is_my_client_id',
clientSecret: 'this_is_my_client_secret',
),
[
'scopes' => ['scope1', 'scope2'],
'tokenType' => AccessToken::TOKEN_TYPE_BEARER,
'authType' => Credentials::AUTH_TYPE_BASIC,
'cacheDriver' => 'file',
'cacheKey' => 'my_cache_key',
],
)->get('https://example.com/api');
$data = $response->json();
expect($data['data'])->toBe('some data with bearer token')
->and($data['token'])->toBe('Bearer this_is_my_access_token_from_body_refresh_token');
});