1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\Gigya\Test\Unit\Auth\OAuth2; |
4
|
|
|
|
5
|
|
|
use DateInterval; |
6
|
|
|
use DateTime; |
7
|
|
|
use Graze\Gigya\Auth\OAuth2\GigyaCodeGrant; |
8
|
|
|
use Graze\Gigya\Auth\OAuth2\GrantInterface; |
9
|
|
|
use Graze\Gigya\Gigya; |
10
|
|
|
use Graze\Gigya\Response\ErrorCode; |
11
|
|
|
use Graze\Gigya\Response\ResponseInterface; |
12
|
|
|
use Graze\Gigya\Test\TestCase; |
13
|
|
|
use Illuminate\Support\Collection; |
14
|
|
|
use Mockery as m; |
15
|
|
|
|
16
|
|
|
class GigyaCodeGrantTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** @var mixed */ |
19
|
|
|
private $gigya; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $code |
23
|
|
|
* @param string $uri |
24
|
|
|
* |
25
|
|
|
* @return GigyaCodeGrant |
26
|
|
|
*/ |
27
|
|
|
private function getGrant($code, $uri) |
28
|
|
|
{ |
29
|
|
|
$this->gigya = m::mock(Gigya::class); |
30
|
|
|
return new GigyaCodeGrant($this->gigya, $code, $uri); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testInstanceOf() |
34
|
|
|
{ |
35
|
|
|
$grant = $this->getGrant('code', 'uri'); |
36
|
|
|
static::assertInstanceOf(GrantInterface::class, $grant); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
View Code Duplication |
public function testGetToken() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$grant = $this->getGrant('code', 'uri'); |
42
|
|
|
|
43
|
|
|
$response = m::mock(ResponseInterface::class); |
44
|
|
|
$this->gigya->shouldReceive('socialize->getToken') |
45
|
|
|
->with([ |
46
|
|
|
'authorization_code' => 'code', |
47
|
|
|
'redirect_uri' => 'uri', |
48
|
|
|
'grant_type' => 'code', |
49
|
|
|
], ['auth' => 'none']) |
50
|
|
|
->andReturn($response); |
51
|
|
|
|
52
|
|
|
$response->shouldReceive('getErrorCode') |
53
|
|
|
->andReturn(ErrorCode::OK); |
54
|
|
|
$data = m::mock(Collection::class); |
55
|
|
|
$response->shouldReceive('getData') |
56
|
|
|
->andReturn($data); |
57
|
|
|
|
58
|
|
|
$data->shouldReceive('get') |
59
|
|
|
->with('access_token') |
60
|
|
|
->andReturn('some_access_token'); |
61
|
|
|
$data->shouldReceive('get') |
62
|
|
|
->with('expires_in', null) |
63
|
|
|
->andReturn(3600); |
64
|
|
|
|
65
|
|
|
$token = $grant->getToken(); |
66
|
|
|
|
67
|
|
|
$expires = (new DateTime())->add(new DateInterval(sprintf('PT%dS', 3600))); |
68
|
|
|
|
69
|
|
|
static::assertEquals('some_access_token', $token->getToken()); |
70
|
|
|
static::assertLessThanOrEqual($expires, $token->getExpires()); |
71
|
|
|
static::assertFalse($token->isExpired()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testGetTokenWithNoExpiresReturnsAnUnExpiredToken() |
75
|
|
|
{ |
76
|
|
|
$grant = $this->getGrant('code', 'uri'); |
77
|
|
|
|
78
|
|
|
$response = m::mock(ResponseInterface::class); |
79
|
|
|
$this->gigya->shouldReceive('socialize->getToken') |
80
|
|
|
->with([ |
81
|
|
|
'authorization_code' => 'code', |
82
|
|
|
'redirect_uri' => 'uri', |
83
|
|
|
'grant_type' => 'code', |
84
|
|
|
], ['auth' => 'none']) |
85
|
|
|
->andReturn($response); |
86
|
|
|
|
87
|
|
|
$response->shouldReceive('getErrorCode') |
88
|
|
|
->andReturn(ErrorCode::OK); |
89
|
|
|
$data = m::mock(Collection::class); |
90
|
|
|
$response->shouldReceive('getData') |
91
|
|
|
->andReturn($data); |
92
|
|
|
|
93
|
|
|
$data->shouldReceive('get') |
94
|
|
|
->with('access_token') |
95
|
|
|
->andReturn('some_access_token'); |
96
|
|
|
$data->shouldReceive('get') |
97
|
|
|
->with('expires_in', null) |
98
|
|
|
->andReturn(null); |
99
|
|
|
|
100
|
|
|
$token = $grant->getToken(); |
101
|
|
|
|
102
|
|
|
static::assertEquals('some_access_token', $token->getToken()); |
103
|
|
|
static::assertNull($token->getExpires()); |
104
|
|
|
static::assertFalse($token->isExpired()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
public function testGetTokenCalledTwiceWillAlwaysReturnTheOriginalToken() |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$grant = $this->getGrant('code', 'uri'); |
110
|
|
|
|
111
|
|
|
$response = m::mock(ResponseInterface::class); |
112
|
|
|
$this->gigya->shouldReceive('socialize->getToken') |
113
|
|
|
->with([ |
114
|
|
|
'authorization_code' => 'code', |
115
|
|
|
'redirect_uri' => 'uri', |
116
|
|
|
'grant_type' => 'code', |
117
|
|
|
], ['auth' => 'none']) |
118
|
|
|
->andReturn($response); |
119
|
|
|
|
120
|
|
|
$response->shouldReceive('getErrorCode') |
121
|
|
|
->andReturn(ErrorCode::OK); |
122
|
|
|
$data = m::mock(Collection::class); |
123
|
|
|
$response->shouldReceive('getData') |
124
|
|
|
->andReturn($data); |
125
|
|
|
|
126
|
|
|
$data->shouldReceive('get') |
127
|
|
|
->with('access_token') |
128
|
|
|
->andReturn('some_access_token'); |
129
|
|
|
$data->shouldReceive('get') |
130
|
|
|
->with('expires_in', null) |
131
|
|
|
->andReturn(0); |
132
|
|
|
|
133
|
|
|
$token = $grant->getToken(); |
134
|
|
|
|
135
|
|
|
$expires = new DateTime(); |
136
|
|
|
|
137
|
|
|
static::assertEquals('some_access_token', $token->getToken()); |
138
|
|
|
static::assertLessThanOrEqual($expires, $token->getExpires()); |
139
|
|
|
static::assertTrue($token->isExpired()); |
140
|
|
|
|
141
|
|
|
static::assertSame($token, $grant->getToken()); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
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.