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\Integration; |
15
|
|
|
|
16
|
|
|
use Graze\Gigya\Gigya; |
17
|
|
|
use Graze\Gigya\Test\TestCase; |
18
|
|
|
use Graze\Gigya\Test\TestFixtures; |
19
|
|
|
use Graze\Gigya\Validation\Signature; |
20
|
|
|
use GuzzleHttp\Handler\MockHandler; |
21
|
|
|
use GuzzleHttp\HandlerStack; |
22
|
|
|
use GuzzleHttp\Middleware; |
23
|
|
|
use GuzzleHttp\Psr7\Response; |
24
|
|
|
use Psr\Http\Message\RequestInterface; |
25
|
|
|
|
26
|
|
|
class GigyaTest extends TestCase |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @param string|null $body Optional body text |
30
|
|
|
* |
31
|
|
|
* @return HandlerStack |
32
|
|
|
*/ |
33
|
|
|
public function setupHandler($body = null) |
34
|
|
|
{ |
35
|
|
|
$mockHandler = new MockHandler(array_pad( |
36
|
|
|
[], |
37
|
|
|
3, |
38
|
|
|
new Response( |
39
|
|
|
'200', |
40
|
|
|
[], |
41
|
|
|
$body ?: TestFixtures::getFixture('basic') |
42
|
|
|
) |
43
|
|
|
)); |
44
|
|
|
|
45
|
|
|
return new HandlerStack($mockHandler); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testAuthInjectsKeyAndSecretIntoParams() |
49
|
|
|
{ |
50
|
|
|
$handler = $this->setupHandler(); |
51
|
|
|
$client = new Gigya('key', 'secret', null, null, ['guzzle' => ['handler' => $handler]]); |
52
|
|
|
$store = []; |
53
|
|
|
$handler->push(Middleware::history($store)); |
54
|
|
|
|
55
|
|
|
$response = $client->accounts()->getAccountInfo(); |
56
|
|
|
|
57
|
|
|
static::assertEquals(0, $response->getErrorCode()); |
58
|
|
|
static::assertCount(1, $store); |
59
|
|
|
$log = array_pop($store); |
60
|
|
|
static::assertEquals( |
61
|
|
|
'https://accounts.eu1.gigya.com/accounts.getAccountInfo', |
62
|
|
|
$log['request']->getUri()->__toString() |
63
|
|
|
); |
64
|
|
|
static::assertEquals( |
65
|
|
|
'apiKey=key&secret=secret', |
66
|
|
|
$log['request']->getBody()->__toString() |
67
|
|
|
); |
68
|
|
|
static::assertEquals(['application/x-www-form-urlencoded'], $log['request']->getHeader('Content-Type')); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testAuthInjectsKeySecretAndUserKeyIntoParams() |
72
|
|
|
{ |
73
|
|
|
$handler = $this->setupHandler(); |
74
|
|
|
$client = new Gigya('key', 'secret', null, 'userKey', ['guzzle' => ['handler' => $handler]]); |
75
|
|
|
$store = []; |
76
|
|
|
$handler->push(Middleware::history($store)); |
77
|
|
|
|
78
|
|
|
$response = $client->accounts()->getAccountInfo(); |
79
|
|
|
|
80
|
|
|
static::assertEquals(0, $response->getErrorCode()); |
81
|
|
|
static::assertCount(1, $store); |
82
|
|
|
$log = array_pop($store); |
83
|
|
|
static::assertArrayHasKey('request', $log); |
84
|
|
|
/** @var RequestInterface $request */ |
85
|
|
|
$request = $log['request']; |
86
|
|
|
static::assertInstanceOf(RequestInterface::class, $request); |
87
|
|
|
static::assertEquals( |
88
|
|
|
'https://accounts.eu1.gigya.com/accounts.getAccountInfo', |
89
|
|
|
$request->getUri()->__toString() |
90
|
|
|
); |
91
|
|
|
static::assertEquals( |
92
|
|
|
'apiKey=key&secret=secret&userKey=userKey', |
93
|
|
|
$log['request']->getBody()->__toString() |
94
|
|
|
); |
95
|
|
|
static::assertEquals(['application/x-www-form-urlencoded'], $log['request']->getHeader('Content-Type')); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testUidSignatureWhenValidDoesNotThrowException() |
99
|
|
|
{ |
100
|
|
|
$uid = 'diofu90ifgdf'; |
101
|
|
|
$timestamp = time(); |
102
|
|
|
|
103
|
|
|
$signatureValidator = new Signature(); |
104
|
|
|
$signature = $signatureValidator->calculateSignature($timestamp . '_' . $uid, 'secret'); |
105
|
|
|
|
106
|
|
|
$body = sprintf( |
107
|
|
|
'{ |
108
|
|
|
"UID": "%s", |
109
|
|
|
"UIDSignature": "%s", |
110
|
|
|
"signatureTimestamp": "%d", |
111
|
|
|
"statusCode": 200, |
112
|
|
|
"errorCode": 0, |
113
|
|
|
"statusReason": "OK", |
114
|
|
|
"callId": "123456", |
115
|
|
|
"time": "2015-03-22T11:42:25.943Z" |
116
|
|
|
}', |
117
|
|
|
$uid, |
118
|
|
|
$signature, |
119
|
|
|
$timestamp |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
$handler = $this->setupHandler($body); |
123
|
|
|
$client = new Gigya('key', 'secret', null, null, ['guzzle' => ['handler' => $handler]]); |
124
|
|
|
$store = []; |
125
|
|
|
$handler->push(Middleware::history($store)); |
126
|
|
|
|
127
|
|
|
$response = $client->accounts()->getAccountInfo(['uid' => $uid]); |
128
|
|
|
static::assertEquals(0, $response->getErrorCode()); |
129
|
|
|
static::assertCount(1, $store); |
130
|
|
|
$log = array_pop($store); |
131
|
|
|
static::assertEquals( |
132
|
|
|
"https://accounts.eu1.gigya.com/accounts.getAccountInfo", |
133
|
|
|
$log['request']->getUri()->__toString() |
134
|
|
|
); |
135
|
|
|
static::assertEquals( |
136
|
|
|
"uid=$uid&apiKey=key&secret=secret", |
137
|
|
|
$log['request']->getBody()->__toString() |
138
|
|
|
); |
139
|
|
|
static::assertEquals(['application/x-www-form-urlencoded'], $log['request']->getHeader('Content-Type')); |
140
|
|
|
|
141
|
|
|
$data = $response->getData(); |
142
|
|
|
static::assertEquals($uid, $data->get('UID')); |
143
|
|
|
static::assertEquals($signature, $data->get('UIDSignature')); |
144
|
|
|
static::assertEquals($timestamp, $data->get('signatureTimestamp')); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @expectedException \Graze\Gigya\Exception\InvalidTimestampException |
149
|
|
|
*/ |
150
|
|
|
public function testUidSignatureWhenIncorrectTimestampThrowsAnException() |
151
|
|
|
{ |
152
|
|
|
$uid = 'diofu90ifgdf'; |
153
|
|
|
$timestamp = time() - 181; |
154
|
|
|
|
155
|
|
|
$signatureValidator = new Signature(); |
156
|
|
|
$signature = $signatureValidator->calculateSignature($timestamp . '_' . $uid, 'secret'); |
157
|
|
|
|
158
|
|
|
$body = sprintf( |
159
|
|
|
'{ |
160
|
|
|
"UID": "%s", |
161
|
|
|
"UIDSignature": "%s", |
162
|
|
|
"signatureTimestamp": "%d", |
163
|
|
|
"statusCode": 200, |
164
|
|
|
"errorCode": 0, |
165
|
|
|
"statusReason": "OK", |
166
|
|
|
"callId": "123456", |
167
|
|
|
"time": "2015-03-22T11:42:25.943Z" |
168
|
|
|
}', |
169
|
|
|
$uid, |
170
|
|
|
$signature, |
171
|
|
|
$timestamp |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
$handler = $this->setupHandler($body); |
175
|
|
|
$client = new Gigya('key', 'secret', null, null, ['guzzle' => ['handler' => $handler]]); |
176
|
|
|
|
177
|
|
|
$client->accounts()->getAccountInfo(['uid' => $uid]); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @expectedException \Graze\Gigya\Exception\InvalidUidSignatureException |
182
|
|
|
*/ |
183
|
|
|
public function testUidSignatureWhenInvalidSignatureThrowsAnException() |
184
|
|
|
{ |
185
|
|
|
$uid = 'diofu90ifgdf'; |
186
|
|
|
$timestamp = time(); |
187
|
|
|
|
188
|
|
|
$body = sprintf( |
189
|
|
|
'{ |
190
|
|
|
"UID": "%s", |
191
|
|
|
"UIDSignature": "%s", |
192
|
|
|
"signatureTimestamp": "%d", |
193
|
|
|
"statusCode": 200, |
194
|
|
|
"errorCode": 0, |
195
|
|
|
"statusReason": "OK", |
196
|
|
|
"callId": "123456", |
197
|
|
|
"time": "2015-03-22T11:42:25.943Z" |
198
|
|
|
}', |
199
|
|
|
$uid, |
200
|
|
|
'invalid', |
201
|
|
|
$timestamp |
202
|
|
|
); |
203
|
|
|
|
204
|
|
|
$handler = $this->setupHandler($body); |
205
|
|
|
$client = new Gigya('key', 'secret', null, null, ['guzzle' => ['handler' => $handler]]); |
206
|
|
|
|
207
|
|
|
$client->accounts()->getAccountInfo(['uid' => $uid]); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @expectedException \Graze\Gigya\Exception\InvalidTimestampException |
212
|
|
|
*/ |
213
|
|
|
public function testRequestWillThrowTimestampExceptionWhenBothTimestampAndSignatureAreInvalid() |
214
|
|
|
{ |
215
|
|
|
$uid = 'diofu90ifgdf'; |
216
|
|
|
$timestamp = time() - 181; |
217
|
|
|
|
218
|
|
|
$body = sprintf( |
219
|
|
|
'{ |
220
|
|
|
"UID": "%s", |
221
|
|
|
"UIDSignature": "%s", |
222
|
|
|
"signatureTimestamp": "%d", |
223
|
|
|
"statusCode": 200, |
224
|
|
|
"errorCode": 0, |
225
|
|
|
"statusReason": "OK", |
226
|
|
|
"callId": "123456", |
227
|
|
|
"time": "2015-03-22T11:42:25.943Z" |
228
|
|
|
}', |
229
|
|
|
$uid, |
230
|
|
|
'invalid', |
231
|
|
|
$timestamp |
232
|
|
|
); |
233
|
|
|
|
234
|
|
|
$handler = $this->setupHandler($body); |
235
|
|
|
$client = new Gigya('key', 'secret', null, null, ['guzzle' => ['handler' => $handler]]); |
236
|
|
|
|
237
|
|
|
$client->accounts()->getAccountInfo(['uid' => $uid]); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function testGigyaWillTriggerSubscriberOnlyWhenItIsAddedInARequest() |
241
|
|
|
{ |
242
|
|
|
$handler = $this->setupHandler(); |
243
|
|
|
$client = new Gigya('key', 'secret', null, null, ['guzzle' => ['handler' => $handler]]); |
244
|
|
|
|
245
|
|
|
$client->accounts()->getAccountInfo(); |
246
|
|
|
|
247
|
|
|
$called = 0; |
248
|
|
|
|
249
|
|
|
$fn = function (callable $handler) use (&$called) { |
250
|
|
|
return function (RequestInterface $request, $options) use ($handler, &$called) { |
251
|
|
|
$called++; |
252
|
|
|
return $handler($request, $options); |
253
|
|
|
}; |
254
|
|
|
}; |
255
|
|
|
|
256
|
|
|
$client->addHandler($fn); |
257
|
|
|
|
258
|
|
|
$client->accounts()->getAccountInfo(); |
259
|
|
|
|
260
|
|
|
$this->assertEquals(1, $called); |
261
|
|
|
|
262
|
|
|
$client->removeHandler($fn); |
263
|
|
|
|
264
|
|
|
$client->accounts()->getAccountInfo(); |
265
|
|
|
|
266
|
|
|
$this->assertEquals(1, $called); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function testBodyIsEncodedWithChildren() |
270
|
|
|
{ |
271
|
|
|
$handler = $this->setupHandler(); |
272
|
|
|
$client = new Gigya('key', 'secret', null, 'userKey', ['guzzle' => ['handler' => $handler]]); |
273
|
|
|
$store = []; |
274
|
|
|
$handler->push(Middleware::history($store)); |
275
|
|
|
|
276
|
|
|
$response = $client->accounts()->register([ |
277
|
|
|
'email' => '[email protected]', |
278
|
|
|
'profile' => [ |
279
|
|
|
'email' => '[email protected]', |
280
|
|
|
'firstName' => 'foo', |
281
|
|
|
'lastName' => 'bar', |
282
|
|
|
'gender' => 'Magic', |
283
|
|
|
'UID' => 'ds8a9d8sa08d90as8d0', |
284
|
|
|
], |
285
|
|
|
]); |
286
|
|
|
|
287
|
|
|
static::assertEquals(0, $response->getErrorCode()); |
288
|
|
|
static::assertCount(1, $store); |
289
|
|
|
$log = array_pop($store); |
290
|
|
|
static::assertArrayHasKey('request', $log); |
291
|
|
|
/** @var RequestInterface $request */ |
292
|
|
|
$request = $log['request']; |
293
|
|
|
static::assertInstanceOf(RequestInterface::class, $request); |
294
|
|
|
static::assertEquals( |
295
|
|
|
'https://accounts.eu1.gigya.com/accounts.register', |
296
|
|
|
$request->getUri()->__toString() |
297
|
|
|
); |
298
|
|
|
static::assertEquals( |
299
|
|
|
'email=foo%40bar.com&profile%5Bemail%5D=foo%40bar.com&profile%5BfirstName%5D=foo&profile%5BlastName%5D=bar&profile%5Bgender%5D=Magic&profile%5BUID%5D=ds8a9d8sa08d90as8d0&apiKey=key&secret=secret&userKey=userKey', |
300
|
|
|
$log['request']->getBody()->__toString() |
301
|
|
|
); |
302
|
|
|
static::assertEquals(['application/x-www-form-urlencoded'], $log['request']->getHeader('Content-Type')); |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|