|
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
|
|
|
/** @var GigyaGrant */ |
|
32
|
|
|
private $grant; |
|
33
|
|
|
|
|
34
|
|
|
public function setUp() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->gigya = m::mock(Gigya::class); |
|
37
|
|
|
$this->grant = new GigyaGrant($this->gigya); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testInstanceOf() |
|
41
|
|
|
{ |
|
42
|
|
|
static::assertInstanceOf(GrantInterface::class, $this->grant); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testGetTokenWhenOneHasNotBeenSet() |
|
46
|
|
|
{ |
|
47
|
|
|
$response = m::mock(ResponseInterface::class); |
|
48
|
|
|
|
|
49
|
|
|
$this->gigya->shouldReceive('socialize->getToken') |
|
50
|
|
|
->with([ |
|
51
|
|
|
'grant_type' => 'none', |
|
52
|
|
|
], ['auth' => 'credentials']) |
|
53
|
|
|
->andReturn($response); |
|
54
|
|
|
|
|
55
|
|
|
$response->shouldReceive('getErrorCode') |
|
|
|
|
|
|
56
|
|
|
->andReturn(ErrorCode::OK); |
|
57
|
|
|
$data = m::mock(Collection::class); |
|
58
|
|
|
$response->shouldReceive('getData') |
|
59
|
|
|
->andReturn($data); |
|
60
|
|
|
$data->shouldReceive('get') |
|
61
|
|
|
->with('access_token') |
|
62
|
|
|
->andReturn('some_token'); |
|
63
|
|
|
$data->shouldReceive('has') |
|
64
|
|
|
->with('expires_in') |
|
65
|
|
|
->andReturn(true); |
|
66
|
|
|
$data->shouldReceive('get') |
|
67
|
|
|
->with('expires_in', 0) |
|
68
|
|
|
->andReturn(3600); |
|
69
|
|
|
|
|
70
|
|
|
$token = $this->grant->getToken(); |
|
71
|
|
|
|
|
72
|
|
|
$expires = (new DateTime())->add(new DateInterval(sprintf('PT%dS', 3600))); |
|
73
|
|
|
|
|
74
|
|
|
static::assertEquals('some_token', $token->getToken()); |
|
75
|
|
|
static::assertLessThanOrEqual($expires, $token->getExpires()); |
|
76
|
|
|
static::assertFalse($token->isExpired()); |
|
77
|
|
|
|
|
78
|
|
|
static::assertSame($token, $this->grant->getToken(), 'Calling getToken again, should return the same token'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testGetTokenWithNoExpiry() |
|
82
|
|
|
{ |
|
83
|
|
|
$response = m::mock(ResponseInterface::class); |
|
84
|
|
|
|
|
85
|
|
|
$this->gigya->shouldReceive('socialize->getToken') |
|
86
|
|
|
->with([ |
|
87
|
|
|
'grant_type' => 'none', |
|
88
|
|
|
], ['auth' => 'credentials']) |
|
89
|
|
|
->andReturn($response); |
|
90
|
|
|
|
|
91
|
|
|
$response->shouldReceive('getErrorCode') |
|
|
|
|
|
|
92
|
|
|
->andReturn(ErrorCode::OK); |
|
93
|
|
|
$data = m::mock(Collection::class); |
|
94
|
|
|
$response->shouldReceive('getData') |
|
95
|
|
|
->andReturn($data); |
|
96
|
|
|
$data->shouldReceive('get') |
|
97
|
|
|
->with('access_token') |
|
98
|
|
|
->andReturn('some_token'); |
|
99
|
|
|
$data->shouldReceive('has') |
|
100
|
|
|
->with('expires_in') |
|
101
|
|
|
->andReturn(false); |
|
102
|
|
|
|
|
103
|
|
|
$token = $this->grant->getToken(); |
|
104
|
|
|
|
|
105
|
|
|
static::assertEquals('some_token', $token->getToken()); |
|
106
|
|
|
static::assertNull($token->getExpires()); |
|
107
|
|
|
static::assertFalse($token->isExpired()); |
|
108
|
|
|
|
|
109
|
|
|
static::assertSame($token, $this->grant->getToken(), 'Calling getToken again, should return the same token'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function testGetTokenThatExpiresCallGetTokenAgain() |
|
113
|
|
|
{ |
|
114
|
|
|
$response = m::mock(ResponseInterface::class); |
|
115
|
|
|
|
|
116
|
|
|
$this->gigya->shouldReceive('socialize->getToken') |
|
117
|
|
|
->with([ |
|
118
|
|
|
'grant_type' => 'none', |
|
119
|
|
|
], ['auth' => 'credentials']) |
|
120
|
|
|
->twice() |
|
121
|
|
|
->andReturn($response); |
|
122
|
|
|
|
|
123
|
|
|
$response->shouldReceive('getErrorCode') |
|
|
|
|
|
|
124
|
|
|
->twice() |
|
125
|
|
|
->andReturn(ErrorCode::OK); |
|
126
|
|
|
$data = m::mock(Collection::class); |
|
127
|
|
|
$response->shouldReceive('getData') |
|
128
|
|
|
->andReturn($data); |
|
129
|
|
|
$data->shouldReceive('get') |
|
130
|
|
|
->with('access_token') |
|
131
|
|
|
->twice() |
|
132
|
|
|
->andReturn('some_token'); |
|
133
|
|
|
$data->shouldReceive('has') |
|
134
|
|
|
->with('expires_in') |
|
135
|
|
|
->twice() |
|
136
|
|
|
->andReturn(true); |
|
137
|
|
|
$data->shouldReceive('get') |
|
138
|
|
|
->with('expires_in', 0) |
|
139
|
|
|
->twice() |
|
140
|
|
|
->andReturn(0); |
|
141
|
|
|
|
|
142
|
|
|
$token = $this->grant->getToken(); |
|
143
|
|
|
|
|
144
|
|
|
$expires = new DateTime(); |
|
145
|
|
|
|
|
146
|
|
|
static::assertEquals('some_token', $token->getToken()); |
|
147
|
|
|
static::assertLessThanOrEqual($expires, $token->getExpires()); |
|
148
|
|
|
static::assertTrue($token->isExpired()); |
|
149
|
|
|
|
|
150
|
|
|
$newToken = $this->grant->getToken(); |
|
151
|
|
|
|
|
152
|
|
|
static::assertNotSame($token, $newToken, 'Calling getToken again, should return the same token'); |
|
153
|
|
|
|
|
154
|
|
|
static::assertEquals('some_token', $token->getToken()); |
|
155
|
|
|
static::assertLessThanOrEqual($expires, $token->getExpires()); |
|
156
|
|
|
static::assertTrue($token->isExpired()); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function testNonOkResponseReturnsANullToken() |
|
160
|
|
|
{ |
|
161
|
|
|
$response = m::mock(ResponseInterface::class); |
|
162
|
|
|
|
|
163
|
|
|
$this->gigya->shouldReceive('socialize->getToken') |
|
164
|
|
|
->with([ |
|
165
|
|
|
'grant_type' => 'none', |
|
166
|
|
|
], ['auth' => 'credentials']) |
|
167
|
|
|
->andReturn($response); |
|
168
|
|
|
|
|
169
|
|
|
$response->shouldReceive('getErrorCode') |
|
|
|
|
|
|
170
|
|
|
->andReturn(ErrorCode::ERROR_GENERAL_SERVER_ERROR); |
|
171
|
|
|
|
|
172
|
|
|
$token = $this->grant->getToken(); |
|
173
|
|
|
|
|
174
|
|
|
static::assertNull($token); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|