OAuthTest.php
8.4 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/*
* This file is part of the overtrue/socialite.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
use Mockery as m;
use Overtrue\Socialite\AccessTokenInterface;
use Overtrue\Socialite\Providers\AbstractProvider;
use Overtrue\Socialite\User;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
class OAuthTest extends TestCase
{
public function tearDown()
{
m::close();
}
public function testAbstractProviderBackwardCompatible()
{
$request = Request::create('foo');
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
$session->shouldReceive('put')->once();
$provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect');
$this->assertSame('client_id', $provider->getConfig()['client_id']);
$this->assertSame('client_secret', $provider->getConfig()['client_secret']);
$this->assertSame('redirect', $provider->getConfig()['redirect']);
$response = $provider->redirect();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
$this->assertSame('http://auth.url', $response->getTargetUrl());
}
public function testRedirectGeneratesTheProperSymfonyRedirectResponse()
{
$request = Request::create('foo');
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
$session->shouldReceive('put')->once();
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect',
]
);
$response = $provider->redirect();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
$this->assertSame('http://auth.url', $response->getTargetUrl());
}
public function testRedirectUrl()
{
$request = Request::create('foo', 'GET', ['state' => str_repeat('A', 40), 'code' => 'code']);
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
]
);
$this->assertNull($provider->getRedirectUrl());
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect_uri',
]
);
$this->assertSame('redirect_uri', $provider->getRedirectUrl());
$provider->setRedirectUrl('overtrue.me');
$this->assertSame('overtrue.me', $provider->getRedirectUrl());
$provider->withRedirectUrl('http://overtrue.me');
$this->assertSame('http://overtrue.me', $provider->getRedirectUrl());
}
public function testUserReturnsAUserInstanceForTheAuthenticatedRequest()
{
$request = Request::create('foo', 'GET', ['state' => str_repeat('A', 40), 'code' => 'code']);
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
$session->shouldReceive('get')->once()->with('state')->andReturn(str_repeat('A', 40));
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect_uri',
]
);
$provider->http = m::mock('StdClass');
$provider->http->shouldReceive('post')->once()->with(
'http://token.url',
[
'headers' => ['Accept' => 'application/json'],
'form_params' => [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'code' => 'code',
'redirect_uri' => 'redirect_uri',
],
]
)->andReturn($response = m::mock('StdClass'));
$response->shouldReceive('getBody')->once()->andReturn('{"access_token":"access_token"}');
$user = $provider->user();
$this->assertInstanceOf('Overtrue\Socialite\User', $user);
$this->assertSame('foo', $user->getId());
}
/**
* @expectedException \Overtrue\Socialite\InvalidStateException
*/
public function testExceptionIsThrownIfStateIsInvalid()
{
$request = Request::create('foo', 'GET', ['state' => str_repeat('B', 40), 'code' => 'code']);
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
$session->shouldReceive('get')->once()->with('state')->andReturn(str_repeat('A', 40));
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect',
]
);
$user = $provider->user();
}
/**
* @expectedException \Overtrue\Socialite\AuthorizeFailedException
* @expectedExceptionMessage Authorize Failed: {"error":"scope is invalid"}
*/
public function testExceptionisThrownIfAuthorizeFailed()
{
$request = Request::create('foo', 'GET', ['state' => str_repeat('A', 40), 'code' => 'code']);
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
$session->shouldReceive('get')->once()->with('state')->andReturn(str_repeat('A', 40));
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect_uri',
]
);
$provider->http = m::mock('StdClass');
$provider->http->shouldReceive('post')->once()->with(
'http://token.url',
[
'headers' => ['Accept' => 'application/json'],
'form_params' => [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'code' => 'code',
'redirect_uri' => 'redirect_uri',
],
]
)->andReturn($response = m::mock('StdClass'));
$response->shouldReceive('getBody')->once()->andReturn('{"error":"scope is invalid"}');
$user = $provider->user();
}
/**
* @expectedException \Overtrue\Socialite\InvalidStateException
*/
public function testExceptionIsThrownIfStateIsNotSet()
{
$request = Request::create('foo', 'GET', ['state' => 'state', 'code' => 'code']);
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
$session->shouldReceive('get')->once()->with('state');
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect',
]
);
$user = $provider->user();
}
public function testDriverName()
{
$request = Request::create('foo', 'GET', ['state' => 'state', 'code' => 'code']);
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect',
]
);
$this->assertSame('OAuthTwoTest', $provider->getName());
}
}
class OAuthTwoTestProviderStub extends AbstractProvider
{
public $http;
protected function getAuthUrl($state)
{
return 'http://auth.url';
}
protected function getTokenUrl()
{
return 'http://token.url';
}
protected function getUserByToken(AccessTokenInterface $token)
{
return ['id' => 'foo'];
}
protected function mapUserToObject(array $user)
{
return new User(['id' => $user['id']]);
}
/**
* Get a fresh instance of the Guzzle HTTP client.
*
* @return \GuzzleHttp\Client
*/
protected function getHttpClient()
{
if ($this->http) {
return $this->http;
}
return $this->http = m::mock('StdClass');
}
}