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\Validation; |
15
|
|
|
|
16
|
|
|
use Graze\Gigya\Response\ResponseInterface; |
17
|
|
|
use Graze\Gigya\Test\TestCase; |
18
|
|
|
use Graze\Gigya\Validation\ResponseValidatorInterface; |
19
|
|
|
use Graze\Gigya\Validation\Signature; |
20
|
|
|
use Graze\Gigya\Validation\UidSignatureValidator; |
21
|
|
|
use Illuminate\Support\Collection; |
22
|
|
|
use Mockery as m; |
23
|
|
|
|
24
|
|
|
class UidSignatureValidatorTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
const SECRET = '8j9h0g-opko;dk]=id0f[sjo'; |
27
|
|
|
|
28
|
|
|
/** @var mixed */ |
29
|
|
|
private $signature; |
30
|
|
|
/** @var UidSignatureValidator */ |
31
|
|
|
private $validator; |
32
|
|
|
|
33
|
|
|
public function setUp() |
34
|
|
|
{ |
35
|
|
|
$this->signature = m::mock(Signature::class); |
36
|
|
|
$this->validator = new UidSignatureValidator($this->signature, static::SECRET); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function tearDown() |
40
|
|
|
{ |
41
|
|
|
$this->signature = $this->validator = null; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testInstanceOf() |
45
|
|
|
{ |
46
|
|
|
static::assertInstanceOf(ResponseValidatorInterface::class, $this->validator); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testUidSignature() |
50
|
|
|
{ |
51
|
|
|
$response = m::mock(ResponseInterface::class); |
52
|
|
|
$collection = m::mock(Collection::class); |
53
|
|
|
$response->shouldReceive('getData') |
|
|
|
|
54
|
|
|
->andReturn($collection); |
55
|
|
|
$collection->shouldReceive('has') |
56
|
|
|
->with('UID') |
57
|
|
|
->andReturn(true); |
58
|
|
|
$collection->shouldReceive('has') |
59
|
|
|
->with('UIDSignature') |
60
|
|
|
->andReturn(true); |
61
|
|
|
$collection->shouldReceive('has') |
62
|
|
|
->with('signatureTimestamp') |
63
|
|
|
->andReturn(true); |
64
|
|
|
$collection->shouldReceive('get') |
65
|
|
|
->with('UID') |
66
|
|
|
->andReturn('some_uid'); |
67
|
|
|
$collection->shouldReceive('get') |
68
|
|
|
->with('UIDSignature') |
69
|
|
|
->andReturn('some_signature'); |
70
|
|
|
$collection->shouldReceive('get') |
71
|
|
|
->with('signatureTimestamp') |
72
|
|
|
->andReturn(12345667); |
73
|
|
|
static::assertTrue($this->validator->canValidate($response)); |
|
|
|
|
74
|
|
|
|
75
|
|
|
$this->signature->shouldReceive('checkTimestamp') |
76
|
|
|
->with(12345667) |
77
|
|
|
->andReturn(true); |
78
|
|
|
$this->signature->shouldReceive('getUidSignature') |
79
|
|
|
->with('some_uid', 12345667, static::SECRET) |
80
|
|
|
->andReturn('some_signature'); |
81
|
|
|
static::assertTrue($this->validator->validate($response)); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @expectedException \Graze\Gigya\Exception\InvalidUidSignatureException |
86
|
|
|
*/ |
87
|
|
|
public function testInvalidUidSignature() |
88
|
|
|
{ |
89
|
|
|
$response = m::mock(ResponseInterface::class); |
90
|
|
|
$collection = m::mock(Collection::class); |
91
|
|
|
$response->shouldReceive('getData') |
|
|
|
|
92
|
|
|
->andReturn($collection); |
93
|
|
|
$collection->shouldReceive('has') |
94
|
|
|
->with('UID') |
95
|
|
|
->andReturn(true); |
96
|
|
|
$collection->shouldReceive('has') |
97
|
|
|
->with('UIDSignature') |
98
|
|
|
->andReturn(true); |
99
|
|
|
$collection->shouldReceive('has') |
100
|
|
|
->with('signatureTimestamp') |
101
|
|
|
->andReturn(true); |
102
|
|
|
$collection->shouldReceive('get') |
103
|
|
|
->with('UID') |
104
|
|
|
->andReturn('some_uid'); |
105
|
|
|
$collection->shouldReceive('get') |
106
|
|
|
->with('UIDSignature') |
107
|
|
|
->andReturn('some_signature'); |
108
|
|
|
$collection->shouldReceive('get') |
109
|
|
|
->with('signatureTimestamp') |
110
|
|
|
->andReturn(12345667); |
111
|
|
|
static::assertTrue($this->validator->canValidate($response)); |
|
|
|
|
112
|
|
|
|
113
|
|
|
$this->signature->shouldReceive('checkTimestamp') |
114
|
|
|
->with(12345667) |
115
|
|
|
->andReturn(true); |
116
|
|
|
$this->signature->shouldReceive('getUidSignature') |
117
|
|
|
->with('some_uid', 12345667, static::SECRET) |
118
|
|
|
->andReturn('incorrect_signature'); |
119
|
|
|
static::assertFalse($this->validator->validate($response)); |
|
|
|
|
120
|
|
|
|
121
|
|
|
$response->shouldReceive('getErrorCode') |
122
|
|
|
->andReturn(0); |
123
|
|
|
|
124
|
|
|
$this->validator->assert($response); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @expectedException \Graze\Gigya\Exception\InvalidTimestampException |
129
|
|
|
*/ |
130
|
|
|
public function testInvalidTimestamp() |
131
|
|
|
{ |
132
|
|
|
$response = m::mock(ResponseInterface::class); |
133
|
|
|
$collection = m::mock(Collection::class); |
134
|
|
|
$response->shouldReceive('getData') |
|
|
|
|
135
|
|
|
->andReturn($collection); |
136
|
|
|
$collection->shouldReceive('has') |
137
|
|
|
->with('UID') |
138
|
|
|
->andReturn(true); |
139
|
|
|
$collection->shouldReceive('has') |
140
|
|
|
->with('UIDSignature') |
141
|
|
|
->andReturn(true); |
142
|
|
|
$collection->shouldReceive('has') |
143
|
|
|
->with('signatureTimestamp') |
144
|
|
|
->andReturn(true); |
145
|
|
|
$collection->shouldReceive('get') |
146
|
|
|
->with('UID') |
147
|
|
|
->andReturn('some_uid'); |
148
|
|
|
$collection->shouldReceive('get') |
149
|
|
|
->with('UIDSignature') |
150
|
|
|
->andReturn('some_signature'); |
151
|
|
|
$collection->shouldReceive('get') |
152
|
|
|
->with('signatureTimestamp') |
153
|
|
|
->andReturn(12345667); |
154
|
|
|
static::assertTrue($this->validator->canValidate($response)); |
|
|
|
|
155
|
|
|
|
156
|
|
|
$this->signature->shouldReceive('checkTimestamp') |
157
|
|
|
->with(12345667) |
158
|
|
|
->andReturn(false); |
159
|
|
|
static::assertFalse($this->validator->validate($response)); |
|
|
|
|
160
|
|
|
|
161
|
|
|
$response->shouldReceive('getErrorCode') |
162
|
|
|
->andReturn(0); |
163
|
|
|
|
164
|
|
|
$this->validator->assert($response); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testCanValidationReturnsFalseWhenTheRequiredFieldsAreNotPresent() |
168
|
|
|
{ |
169
|
|
|
$response = m::mock(ResponseInterface::class); |
170
|
|
|
$collection = m::mock(Collection::class); |
171
|
|
|
$response->shouldReceive('getData') |
|
|
|
|
172
|
|
|
->andReturn($collection); |
173
|
|
|
$collection->shouldReceive('has') |
174
|
|
|
->with('UID') |
175
|
|
|
->andReturn(true, true, true, false); |
176
|
|
|
$collection->shouldReceive('has') |
177
|
|
|
->with('UIDSignature') |
178
|
|
|
->andReturn(true, true, false, true); |
179
|
|
|
$collection->shouldReceive('has') |
180
|
|
|
->with('signatureTimestamp') |
181
|
|
|
->andReturn(true, false, true, true); |
182
|
|
|
static::assertTrue($this->validator->canValidate($response)); |
|
|
|
|
183
|
|
|
static::assertFalse($this->validator->canValidate($response)); |
184
|
|
|
static::assertFalse($this->validator->canValidate($response)); |
185
|
|
|
static::assertFalse($this->validator->canValidate($response)); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|