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 DateInterval; |
17
|
|
|
use DateTime; |
18
|
|
|
use Graze\Gigya\Auth\OAuth2\GigyaGrant; |
19
|
|
|
use Graze\Gigya\Auth\OAuth2\GrantInterface; |
20
|
|
|
use Graze\Gigya\Gigya; |
21
|
|
|
use Graze\Gigya\Response\ErrorCode; |
22
|
|
|
use Graze\Gigya\Response\ResponseInterface; |
23
|
|
|
use Graze\Gigya\Test\TestCase; |
24
|
|
|
use Illuminate\Support\Collection; |
25
|
|
|
use Mockery as m; |
26
|
|
|
|
27
|
|
|
class GigyaGrantTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
/** @var mixed */ |
30
|
|
|
private $gigya; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $apiKey |
34
|
|
|
* @param string $secret |
35
|
|
|
* @param string|null $userKey |
36
|
|
|
* |
37
|
|
|
* @return GigyaGrant |
38
|
|
|
*/ |
39
|
|
|
public function getGrant($apiKey, $secret, $userKey = null) |
40
|
|
|
{ |
41
|
|
|
$this->gigya = m::mock(Gigya::class); |
42
|
|
|
return new GigyaGrant($this->gigya, $apiKey, $secret, $userKey); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testInstanceOf() |
46
|
|
|
{ |
47
|
|
|
$grant = $this->getGrant('key', 'secret'); |
48
|
|
|
static::assertInstanceOf(GrantInterface::class, $grant); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testGetTokenWhenOneHasNotBeenSet() |
52
|
|
|
{ |
53
|
|
|
$grant = $this->getGrant('key', 'secret'); |
54
|
|
|
|
55
|
|
|
$response = m::mock(ResponseInterface::class); |
56
|
|
|
|
57
|
|
|
$this->gigya->shouldReceive('socialize->getToken') |
58
|
|
|
->with([ |
59
|
|
|
'client_id' => 'key', |
60
|
|
|
'client_secret' => 'secret', |
61
|
|
|
'grant_type' => 'none', |
62
|
|
|
], ['auth' => 'none']) |
63
|
|
|
->andReturn($response); |
64
|
|
|
|
65
|
|
|
$response->shouldReceive('getErrorCode') |
66
|
|
|
->andReturn(ErrorCode::OK); |
67
|
|
|
$data = m::mock(Collection::class); |
68
|
|
|
$response->shouldReceive('getData') |
69
|
|
|
->andReturn($data); |
70
|
|
|
$data->shouldReceive('get') |
71
|
|
|
->with('access_token') |
72
|
|
|
->andReturn('some_token'); |
73
|
|
|
$data->shouldReceive('get') |
74
|
|
|
->with('expires_in', null) |
75
|
|
|
->andReturn(3600); |
76
|
|
|
|
77
|
|
|
$token = $grant->getToken(); |
78
|
|
|
|
79
|
|
|
$expires = (new DateTime())->add(new DateInterval(sprintf('PT%dS', 3600))); |
80
|
|
|
|
81
|
|
|
static::assertEquals('some_token', $token->getToken()); |
82
|
|
|
static::assertLessThanOrEqual($expires, $token->getExpires()); |
83
|
|
|
static::assertFalse($token->isExpired()); |
84
|
|
|
|
85
|
|
|
static::assertSame($token, $grant->getToken(), 'Calling getToken again, should return the same token'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testGetTokenWithNoExpiry() |
89
|
|
|
{ |
90
|
|
|
$grant = $this->getGrant('key', 'secret'); |
91
|
|
|
|
92
|
|
|
$response = m::mock(ResponseInterface::class); |
93
|
|
|
|
94
|
|
|
$this->gigya->shouldReceive('socialize->getToken') |
95
|
|
|
->with([ |
96
|
|
|
'client_id' => 'key', |
97
|
|
|
'client_secret' => 'secret', |
98
|
|
|
'grant_type' => 'none', |
99
|
|
|
], ['auth' => 'none']) |
100
|
|
|
->andReturn($response); |
101
|
|
|
|
102
|
|
|
$response->shouldReceive('getErrorCode') |
103
|
|
|
->andReturn(ErrorCode::OK); |
104
|
|
|
$data = m::mock(Collection::class); |
105
|
|
|
$response->shouldReceive('getData') |
106
|
|
|
->andReturn($data); |
107
|
|
|
$data->shouldReceive('get') |
108
|
|
|
->with('access_token') |
109
|
|
|
->andReturn('some_token'); |
110
|
|
|
$data->shouldReceive('get') |
111
|
|
|
->with('expires_in', null) |
112
|
|
|
->andReturn(null); |
113
|
|
|
|
114
|
|
|
$token = $grant->getToken(); |
115
|
|
|
|
116
|
|
|
static::assertEquals('some_token', $token->getToken()); |
117
|
|
|
static::assertNull($token->getExpires()); |
118
|
|
|
static::assertFalse($token->isExpired()); |
119
|
|
|
|
120
|
|
|
static::assertSame($token, $grant->getToken(), 'Calling getToken again, should return the same token'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testGetTokenThatExpiresCallGetTokenAgain() |
124
|
|
|
{ |
125
|
|
|
$grant = $this->getGrant('key', 'secret'); |
126
|
|
|
|
127
|
|
|
$response = m::mock(ResponseInterface::class); |
128
|
|
|
|
129
|
|
|
$this->gigya->shouldReceive('socialize->getToken') |
130
|
|
|
->with([ |
131
|
|
|
'client_id' => 'key', |
132
|
|
|
'client_secret' => 'secret', |
133
|
|
|
'grant_type' => 'none', |
134
|
|
|
], ['auth' => 'none']) |
135
|
|
|
->twice() |
136
|
|
|
->andReturn($response); |
137
|
|
|
|
138
|
|
|
$response->shouldReceive('getErrorCode') |
139
|
|
|
->twice() |
140
|
|
|
->andReturn(ErrorCode::OK); |
141
|
|
|
$data = m::mock(Collection::class); |
142
|
|
|
$response->shouldReceive('getData') |
143
|
|
|
->andReturn($data); |
144
|
|
|
$data->shouldReceive('get') |
145
|
|
|
->with('access_token') |
146
|
|
|
->twice() |
147
|
|
|
->andReturn('some_token'); |
148
|
|
|
$data->shouldReceive('get') |
149
|
|
|
->with('expires_in', null) |
150
|
|
|
->twice() |
151
|
|
|
->andReturn(0); |
152
|
|
|
|
153
|
|
|
$token = $grant->getToken(); |
154
|
|
|
|
155
|
|
|
$expires = new DateTime(); |
156
|
|
|
|
157
|
|
|
static::assertEquals('some_token', $token->getToken()); |
158
|
|
|
static::assertLessThanOrEqual($expires, $token->getExpires()); |
159
|
|
|
static::assertTrue($token->isExpired()); |
160
|
|
|
|
161
|
|
|
$newToken = $grant->getToken(); |
162
|
|
|
|
163
|
|
|
static::assertNotSame($token, $newToken, 'Calling getToken again, should return the same token'); |
164
|
|
|
|
165
|
|
|
static::assertEquals('some_token', $token->getToken()); |
166
|
|
|
static::assertLessThanOrEqual($expires, $token->getExpires()); |
167
|
|
|
static::assertTrue($token->isExpired()); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function testNonOkResponseReturnsANullToken() |
171
|
|
|
{ |
172
|
|
|
$grant = $this->getGrant('key', 'secret'); |
173
|
|
|
|
174
|
|
|
$response = m::mock(ResponseInterface::class); |
175
|
|
|
|
176
|
|
|
$this->gigya->shouldReceive('socialize->getToken') |
177
|
|
|
->with([ |
178
|
|
|
'client_id' => 'key', |
179
|
|
|
'client_secret' => 'secret', |
180
|
|
|
'grant_type' => 'none', |
181
|
|
|
], ['auth' => 'none']) |
182
|
|
|
->andReturn($response); |
183
|
|
|
|
184
|
|
|
$response->shouldReceive('getErrorCode') |
185
|
|
|
->andReturn(ErrorCode::ERROR_GENERAL_SERVER_ERROR); |
186
|
|
|
|
187
|
|
|
$token = $grant->getToken(); |
188
|
|
|
|
189
|
|
|
static::assertNull($token); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|