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\OAuth2Middleware; |
19
|
|
|
use Graze\Gigya\Test\TestCase; |
20
|
|
|
use GuzzleHttp\Handler\MockHandler; |
21
|
|
|
use GuzzleHttp\HandlerStack; |
22
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
23
|
|
|
use GuzzleHttp\Psr7\Request; |
24
|
|
|
use GuzzleHttp\Psr7\Response; |
25
|
|
|
use Mockery as m; |
26
|
|
|
use Psr\Http\Message\RequestInterface; |
27
|
|
|
use Psr\Http\Message\ResponseInterface; |
28
|
|
|
|
29
|
|
|
class OAuth2MiddlewareTest extends TestCase |
30
|
|
|
{ |
31
|
|
|
/** @var mixed */ |
32
|
|
|
private $grant; |
33
|
|
|
|
34
|
|
|
public function setUp() |
35
|
|
|
{ |
36
|
|
|
$this->grant = m::mock(GrantInterface::class); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
View Code Duplication |
public function testSign() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$handler = new MockHandler([ |
42
|
|
|
function (RequestInterface $request) { |
43
|
|
|
$this->assertEquals('OAuth test', $request->getHeaderLine('Authorization')); |
44
|
|
|
return new Response(200); |
45
|
|
|
}, |
46
|
|
|
]); |
47
|
|
|
|
48
|
|
|
$token = new AccessToken('test'); |
49
|
|
|
$this->grant->shouldReceive('getToken') |
50
|
|
|
->andReturn($token); |
51
|
|
|
|
52
|
|
|
$stack = new HandlerStack($handler); |
53
|
|
|
$stack->push(OAuth2Middleware::middleware($this->grant)); |
54
|
|
|
|
55
|
|
|
$comp = $stack->resolve(); |
56
|
|
|
|
57
|
|
|
$promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya-oauth2']); |
58
|
|
|
$this->assertInstanceOf(PromiseInterface::class, $promise); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
public function testErrorThatIsNot401() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$handler = new MockHandler([ |
64
|
|
|
function (RequestInterface $request) { |
65
|
|
|
$this->assertEquals('OAuth test', $request->getHeaderLine('Authorization')); |
66
|
|
|
return new Response(503); |
67
|
|
|
}, |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
$token = new AccessToken('test'); |
71
|
|
|
$this->grant->shouldReceive('getToken') |
72
|
|
|
->andReturn($token); |
73
|
|
|
|
74
|
|
|
$stack = new HandlerStack($handler); |
75
|
|
|
$stack->push(OAuth2Middleware::middleware($this->grant)); |
76
|
|
|
|
77
|
|
|
$comp = $stack->resolve(); |
78
|
|
|
|
79
|
|
|
$promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya-oauth2']); |
80
|
|
|
$this->assertInstanceOf(PromiseInterface::class, $promise); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testErrorThatIsNotRetried() |
84
|
|
|
{ |
85
|
|
|
$handler = new MockHandler([ |
86
|
|
View Code Duplication |
function (RequestInterface $request, array $options) { |
|
|
|
|
87
|
|
|
$this->assertEquals('OAuth test', $request->getHeaderLine('Authorization')); |
88
|
|
|
$this->assertEquals(1, $options['retries']); |
89
|
|
|
return new Response(401); |
90
|
|
|
}, |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
$token = new AccessToken('test'); |
94
|
|
|
$this->grant->shouldReceive('getToken') |
95
|
|
|
->andReturn($token); |
96
|
|
|
|
97
|
|
|
$stack = new HandlerStack($handler); |
98
|
|
|
$stack->push(OAuth2Middleware::middleware($this->grant)); |
99
|
|
|
|
100
|
|
|
$comp = $stack->resolve(); |
101
|
|
|
|
102
|
|
|
/** @var PromiseInterface $promise */ |
103
|
|
|
$promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya-oauth2', 'retries' => 1]); |
104
|
|
|
$this->assertInstanceOf(PromiseInterface::class, $promise); |
105
|
|
|
/** @var ResponseInterface $response */ |
106
|
|
|
$response = $promise->wait(); |
107
|
|
|
$this->assertEquals(401, $response->getStatusCode()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testErrorThatIsRetried() |
111
|
|
|
{ |
112
|
|
|
$handler = new MockHandler([ |
113
|
|
|
function (RequestInterface $request, array $options) { |
114
|
|
|
$this->assertEquals('OAuth test', $request->getHeaderLine('Authorization')); |
115
|
|
|
if (isset($options['retries'])) { |
116
|
|
|
$this->assertEquals(0, $options['retries']); |
117
|
|
|
} |
118
|
|
|
return new Response(401); |
119
|
|
|
}, |
120
|
|
View Code Duplication |
function (RequestInterface $request, array $options) { |
|
|
|
|
121
|
|
|
$this->assertEquals('OAuth test2', $request->getHeaderLine('Authorization')); |
122
|
|
|
$this->assertEquals(1, $options['retries']); |
123
|
|
|
return new Response(200); |
124
|
|
|
}, |
125
|
|
|
]); |
126
|
|
|
|
127
|
|
|
$token1 = new AccessToken('test'); |
128
|
|
|
$token2 = new AccessToken('test2'); |
129
|
|
|
$this->grant->shouldReceive('getToken') |
130
|
|
|
->andReturn($token1, $token2); |
131
|
|
|
|
132
|
|
|
$stack = new HandlerStack($handler); |
133
|
|
|
$stack->push(OAuth2Middleware::middleware($this->grant)); |
134
|
|
|
|
135
|
|
|
$comp = $stack->resolve(); |
136
|
|
|
|
137
|
|
|
/** @var PromiseInterface $promise */ |
138
|
|
|
$promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya-oauth2']); |
139
|
|
|
$this->assertInstanceOf(PromiseInterface::class, $promise); |
140
|
|
|
/** @var ResponseInterface $response */ |
141
|
|
|
$response = $promise->wait(); |
142
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
View Code Duplication |
public function testErrorThatIsNotOauthAuth() |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
$handler = new MockHandler([ |
148
|
|
|
function (RequestInterface $request) { |
149
|
|
|
$this->assertEquals('', $request->getHeaderLine('Authorization')); |
150
|
|
|
return new Response(401); |
151
|
|
|
}, |
152
|
|
|
]); |
153
|
|
|
|
154
|
|
|
$stack = new HandlerStack($handler); |
155
|
|
|
$stack->push(OAuth2Middleware::middleware($this->grant)); |
156
|
|
|
|
157
|
|
|
$comp = $stack->resolve(); |
158
|
|
|
|
159
|
|
|
/** @var PromiseInterface $promise */ |
160
|
|
|
$promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'none']); |
161
|
|
|
$this->assertInstanceOf(PromiseInterface::class, $promise); |
162
|
|
|
/** @var ResponseInterface $response */ |
163
|
|
|
$response = $promise->wait(true); |
164
|
|
|
$this->assertEquals(401, $response->getStatusCode()); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testErrorWhenNoTokenIsReturnedWillNotIntercept() |
168
|
|
|
{ |
169
|
|
|
$handler = new MockHandler([ |
170
|
|
|
function (RequestInterface $request) { |
171
|
|
|
$this->assertEquals('', $request->getHeaderLine('Authorization')); |
172
|
|
|
return new Response(401); |
173
|
|
|
}, |
174
|
|
|
]); |
175
|
|
|
|
176
|
|
|
$this->grant->shouldReceive('getToken') |
177
|
|
|
->atLeast() |
178
|
|
|
->once() |
179
|
|
|
->andReturn(null); |
180
|
|
|
|
181
|
|
|
$stack = new HandlerStack($handler); |
182
|
|
|
$stack->push(OAuth2Middleware::middleware($this->grant)); |
183
|
|
|
|
184
|
|
|
$comp = $stack->resolve(); |
185
|
|
|
|
186
|
|
|
/** @var PromiseInterface $promise */ |
187
|
|
|
$promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya-oauth2']); |
188
|
|
|
$this->assertInstanceOf(PromiseInterface::class, $promise); |
189
|
|
|
/** @var ResponseInterface $response */ |
190
|
|
|
$response = $promise->wait(true); |
191
|
|
|
$this->assertEquals(401, $response->getStatusCode()); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.