1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/gigya-client |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/gigya-client/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/gigya-client |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\Gigya\Test\Unit\Auth\OAuth2; |
15
|
|
|
|
16
|
|
|
use Graze\Gigya\Auth\OAuth2\AccessToken; |
17
|
|
|
use Graze\Gigya\Auth\OAuth2\GrantInterface; |
18
|
|
|
use Graze\Gigya\Auth\OAuth2\OAuth2Subscriber; |
19
|
|
|
use Graze\Gigya\Test\TestCase; |
20
|
|
|
use GuzzleHttp\Collection; |
21
|
|
|
use GuzzleHttp\Event\BeforeEvent; |
22
|
|
|
use GuzzleHttp\Event\ErrorEvent; |
23
|
|
|
use GuzzleHttp\Event\RequestEvents; |
24
|
|
|
use GuzzleHttp\Event\SubscriberInterface; |
25
|
|
|
use GuzzleHttp\Message\RequestInterface; |
26
|
|
|
use GuzzleHttp\Message\ResponseInterface; |
27
|
|
|
use Mockery as m; |
28
|
|
|
|
29
|
|
|
class OAuth2SubscriberTest extends TestCase |
30
|
|
|
{ |
31
|
|
|
/** @var mixed */ |
32
|
|
|
private $grant; |
33
|
|
|
/** @var OAuth2Subscriber */ |
34
|
|
|
private $subscriber; |
35
|
|
|
|
36
|
|
|
public function setUp() |
37
|
|
|
{ |
38
|
|
|
$this->grant = m::mock(GrantInterface::class); |
39
|
|
|
$this->subscriber = new OAuth2Subscriber($this->grant); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testInstanceOf() |
43
|
|
|
{ |
44
|
|
|
static::assertInstanceOf(SubscriberInterface::class, $this->subscriber); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testGetEvents() |
48
|
|
|
{ |
49
|
|
|
static::assertEquals([ |
50
|
|
|
'before' => ['sign', RequestEvents::SIGN_REQUEST], |
51
|
|
|
'error' => ['error', RequestEvents::EARLY], |
52
|
|
|
], $this->subscriber->getEvents()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testSign() |
56
|
|
|
{ |
57
|
|
|
$event = m::mock(BeforeEvent::class); |
58
|
|
|
$request = m::mock(RequestInterface::class); |
59
|
|
|
$event->shouldReceive('getRequest') |
60
|
|
|
->andReturn($request); |
61
|
|
|
$request->shouldReceive('getScheme') |
62
|
|
|
->andReturn('https'); |
63
|
|
|
$request->shouldReceive('getConfig->get') |
64
|
|
|
->with('auth') |
65
|
|
|
->andReturn('gigya-oauth2'); |
66
|
|
|
|
67
|
|
|
$token = new AccessToken('test'); |
68
|
|
|
|
69
|
|
|
$this->grant->shouldReceive('getToken') |
70
|
|
|
->andReturn($token); |
71
|
|
|
|
72
|
|
|
$request->shouldReceive('addHeader') |
73
|
|
|
->with('Authorization', 'OAuth test') |
74
|
|
|
->once(); |
75
|
|
|
|
76
|
|
|
$this->subscriber->sign($event); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testErrorThatIsNot401() |
80
|
|
|
{ |
81
|
|
|
$event = m::mock(ErrorEvent::class); |
82
|
|
|
$response = m::mock(ResponseInterface::class); |
83
|
|
|
$event->shouldReceive('getResponse') |
84
|
|
|
->andReturn($response); |
85
|
|
|
$response->shouldReceive('getStatusCode') |
86
|
|
|
->andReturn(503); |
87
|
|
|
$this->subscriber->error($event); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testErrorThatIsNotRetried() |
91
|
|
|
{ |
92
|
|
|
$event = m::mock(ErrorEvent::class); |
93
|
|
|
|
94
|
|
|
$response = m::mock(ResponseInterface::class); |
95
|
|
|
$event->shouldReceive('getResponse') |
96
|
|
|
->atLeast() |
97
|
|
|
->once() |
98
|
|
|
->andReturn($response); |
99
|
|
|
$response->shouldReceive('getStatusCode') |
100
|
|
|
->atLeast() |
101
|
|
|
->once() |
102
|
|
|
->andReturn(401); |
103
|
|
|
|
104
|
|
|
$request = m::mock(RequestInterface::class); |
105
|
|
|
$event->shouldReceive('getRequest') |
106
|
|
|
->atLeast() |
107
|
|
|
->once() |
108
|
|
|
->andReturn($request); |
109
|
|
|
$request->shouldReceive('getScheme') |
110
|
|
|
->atLeast() |
111
|
|
|
->once() |
112
|
|
|
->andReturn('https'); |
113
|
|
|
$config = m::mock(Collection::class); |
114
|
|
|
$request->shouldReceive('getConfig') |
115
|
|
|
->atLeast() |
116
|
|
|
->once() |
117
|
|
|
->andReturn($config); |
118
|
|
|
$config->shouldReceive('get') |
119
|
|
|
->with('auth') |
120
|
|
|
->atLeast() |
121
|
|
|
->once() |
122
|
|
|
->andReturn('gigya-oauth2'); |
123
|
|
|
$config->shouldReceive('get') |
124
|
|
|
->with('retried') |
125
|
|
|
->atLeast() |
126
|
|
|
->once() |
127
|
|
|
->andReturn(false); |
128
|
|
|
|
129
|
|
|
$token = new AccessToken('test2'); |
130
|
|
|
$this->grant->shouldReceive('getToken') |
131
|
|
|
->andReturn($token); |
132
|
|
|
|
133
|
|
|
$config->shouldReceive('set') |
134
|
|
|
->with('retried', true) |
135
|
|
|
->atLeast() |
136
|
|
|
->once(); |
137
|
|
|
|
138
|
|
|
$newResponse = m::mock(ResponseInterface::class); |
139
|
|
|
|
140
|
|
|
$event->shouldReceive('getClient->send') |
141
|
|
|
->with($request) |
142
|
|
|
->atLeast() |
143
|
|
|
->once() |
144
|
|
|
->andReturn($newResponse); |
145
|
|
|
$event->shouldReceive('intercept') |
146
|
|
|
->atLeast() |
147
|
|
|
->once() |
148
|
|
|
->with($newResponse); |
149
|
|
|
|
150
|
|
|
$this->subscriber->error($event); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testErrorThatIsNotOauthAuth() |
154
|
|
|
{ |
155
|
|
|
$event = m::mock(ErrorEvent::class); |
156
|
|
|
|
157
|
|
|
$response = m::mock(ResponseInterface::class); |
158
|
|
|
$event->shouldReceive('getResponse') |
159
|
|
|
->atLeast() |
160
|
|
|
->once() |
161
|
|
|
->andReturn($response); |
162
|
|
|
$response->shouldReceive('getStatusCode') |
163
|
|
|
->atLeast() |
164
|
|
|
->once() |
165
|
|
|
->andReturn(401); |
166
|
|
|
|
167
|
|
|
$request = m::mock(RequestInterface::class); |
168
|
|
|
$event->shouldReceive('getRequest') |
169
|
|
|
->atLeast() |
170
|
|
|
->once() |
171
|
|
|
->andReturn($request); |
172
|
|
|
$request->shouldReceive('getScheme') |
173
|
|
|
->atLeast() |
174
|
|
|
->once() |
175
|
|
|
->andReturn('https'); |
176
|
|
|
$request->shouldReceive('getConfig->get') |
177
|
|
|
->with('auth') |
178
|
|
|
->atLeast() |
179
|
|
|
->once() |
180
|
|
|
->andReturn('none'); |
181
|
|
|
|
182
|
|
|
$this->subscriber->error($event); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function testErrorWhenNoTokenIsReturnedWillNotIntercept() |
186
|
|
|
{ |
187
|
|
|
$event = m::mock(ErrorEvent::class); |
188
|
|
|
|
189
|
|
|
$response = m::mock(ResponseInterface::class); |
190
|
|
|
$event->shouldReceive('getResponse') |
191
|
|
|
->atLeast() |
192
|
|
|
->once() |
193
|
|
|
->andReturn($response); |
194
|
|
|
$response->shouldReceive('getStatusCode') |
195
|
|
|
->atLeast() |
196
|
|
|
->once() |
197
|
|
|
->andReturn(401); |
198
|
|
|
|
199
|
|
|
$request = m::mock(RequestInterface::class); |
200
|
|
|
$event->shouldReceive('getRequest') |
201
|
|
|
->atLeast() |
202
|
|
|
->once() |
203
|
|
|
->andReturn($request); |
204
|
|
|
$request->shouldReceive('getScheme') |
205
|
|
|
->atLeast() |
206
|
|
|
->once() |
207
|
|
|
->andReturn('https'); |
208
|
|
|
$config = m::mock(Collection::class); |
209
|
|
|
$request->shouldReceive('getConfig') |
210
|
|
|
->atLeast() |
211
|
|
|
->once() |
212
|
|
|
->andReturn($config); |
213
|
|
|
$config->shouldReceive('get') |
214
|
|
|
->with('auth') |
215
|
|
|
->atLeast() |
216
|
|
|
->once() |
217
|
|
|
->andReturn('gigya-oauth2'); |
218
|
|
|
$config->shouldReceive('get') |
219
|
|
|
->with('retried') |
220
|
|
|
->atLeast() |
221
|
|
|
->once() |
222
|
|
|
->andReturn(false); |
223
|
|
|
|
224
|
|
|
$this->grant->shouldReceive('getToken') |
225
|
|
|
->andReturn(null); |
226
|
|
|
|
227
|
|
|
$this->subscriber->error($event); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|