Completed
Pull Request — master (#18)
by Harry
03:48
created

testUidSignatureWhenIncorrectTimestampThrowsAnException()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
rs 8.8571
cc 1
eloc 15
nc 1
nop 0
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\Exception\InvalidTimestampException;
17
use Graze\Gigya\Exception\InvalidUidSignatureException;
18
use Graze\Gigya\Gigya;
19
use Graze\Gigya\Test\TestCase;
20
use Graze\Gigya\Test\TestFixtures;
21
use Graze\Gigya\Validation\Signature;
22
use GuzzleHttp\Event\SubscriberInterface;
23
use GuzzleHttp\Message\Response;
24
use GuzzleHttp\Stream\Stream;
25
use GuzzleHttp\Subscriber\History;
26
use GuzzleHttp\Subscriber\Mock;
27
use Mockery as m;
28
29
class GigyaTest extends TestCase
30
{
31
    /**
32
     * @param Gigya       $gigya
33
     * @param string|null $body Optional body text
34
     *
35
     * @return History
36
     */
37
    public function setUpGigyaHistory(Gigya $gigya, $body = null)
38
    {
39
        $history = new History();
40
        $mock = new Mock(array_pad(
41
            [],
42
            3,
43
            new Response(
44
                '200',
45
                [],
46
                Stream::factory(
47
                    $body ?: TestFixtures::getFixture('basic')
48
                )
49
            )
50
        ));
51
        $gigya->addSubscriber($history);
52
        $gigya->addSubscriber($mock);
53
54
        return $history;
55
    }
56
57
    public function testAuthInjectsKeyAndSecretIntoParams()
58
    {
59
        $client = new Gigya('key', 'secret');
60
        $history = $this->setUpGigyaHistory($client);
61
62
        $response = $client->accounts()->getAccountInfo();
63
64
        static::assertEquals(0, $response->getErrorCode());
65
        static::assertEquals(1, $history->count());
66
        $request = $history->getLastRequest();
67
        static::assertEquals(
68
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo?apiKey=key&secret=secret',
69
            $request->getUrl()
70
        );
71
        $query = $request->getQuery();
72
        static::assertArrayHasKey('apiKey', $query);
73
        static::assertArrayHasKey('secret', $query);
74
        static::assertEquals('key', $query['apiKey']);
75
        static::assertEquals('secret', $query['secret']);
76
    }
77
78
    public function testAuthInjectsKeySecretAndUserKeyIntoParams()
79
    {
80
        $client = new Gigya('key', 'secret', null, 'userKey');
81
        $history = $this->setUpGigyaHistory($client);
82
83
        $response = $client->accounts()->getAccountInfo();
84
85
        static::assertEquals(0, $response->getErrorCode());
86
        static::assertEquals(1, $history->count());
87
        $request = $history->getLastRequest();
88
        static::assertEquals(
89
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo?apiKey=key&secret=secret&userKey=userKey',
90
            $request->getUrl()
91
        );
92
        $query = $request->getQuery();
93
        static::assertArrayHasKey('apiKey', $query);
94
        static::assertArrayHasKey('secret', $query);
95
        static::assertArrayHasKey('userKey', $query);
96
        static::assertEquals('key', $query['apiKey']);
97
        static::assertEquals('secret', $query['secret']);
98
        static::assertEquals('userKey', $query['userKey']);
99
    }
100
101
    public function testUidSignatureWhenValidDoesNotThrowException()
102
    {
103
        $uid = 'diofu90ifgdf';
104
        $timestamp = time();
105
106
        $signatureValidator = new Signature();
107
        $signature = $signatureValidator->calculateSignature($timestamp . '_' . $uid, 'secret');
108
109
        $body = sprintf(
110
            '{
111
            "UID": "%s",
112
            "UIDSignature": "%s",
113
            "signatureTimestamp": "%d",
114
            "statusCode": 200,
115
            "errorCode": 0,
116
            "statusReason": "OK",
117
            "callId": "123456",
118
            "time": "2015-03-22T11:42:25.943Z"
119
        }',
120
            $uid,
121
            $signature,
122
            $timestamp
123
        );
124
125
        $client = new Gigya('key', 'secret');
126
        $history = $this->setUpGigyaHistory($client, $body);
127
128
        $response = $client->accounts()->getAccountInfo(['uid' => $uid]);
129
        static::assertEquals(0, $response->getErrorCode());
130
        static::assertEquals(1, $history->count());
131
        $request = $history->getLastRequest();
132
        static::assertEquals(
133
            "https://accounts.eu1.gigya.com/accounts.getAccountInfo?uid=$uid&apiKey=key&secret=secret",
134
            $request->getUrl()
135
        );
136
        $query = $request->getQuery();
137
        static::assertArrayHasKey('apiKey', $query);
138
        static::assertArrayHasKey('secret', $query);
139
        static::assertArrayHasKey('uid', $query);
140
        static::assertEquals('key', $query['apiKey']);
141
        static::assertEquals('secret', $query['secret']);
142
        static::assertEquals($uid, $query['uid']);
143
144
        $data = $response->getData();
145
        static::assertEquals($uid, $data->get('UID'));
146
        static::assertEquals($signature, $data->get('UIDSignature'));
147
        static::assertEquals($timestamp, $data->get('signatureTimestamp'));
148
    }
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
        $client = new Gigya('key', 'secret');
175
        $this->setUpGigyaHistory($client, $body);
176
177
        static::setExpectedException(
178
            InvalidTimestampException::class
179
        );
180
181
        $client->accounts()->getAccountInfo(['uid' => $uid]);
182
    }
183
184 View Code Duplication
    public function testUidSignatureWhenInvalidSignatureThrowsAnException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
185
    {
186
        $uid = 'diofu90ifgdf';
187
        $timestamp = time();
188
189
        $body = sprintf(
190
            '{
191
            "UID": "%s",
192
            "UIDSignature": "%s",
193
            "signatureTimestamp": "%d",
194
            "statusCode": 200,
195
            "errorCode": 0,
196
            "statusReason": "OK",
197
            "callId": "123456",
198
            "time": "2015-03-22T11:42:25.943Z"
199
        }',
200
            $uid,
201
            'invalid',
202
            $timestamp
203
        );
204
205
        $client = new Gigya('key', 'secret');
206
        $this->setUpGigyaHistory($client, $body);
207
208
        static::setExpectedException(
209
            InvalidUidSignatureException::class
210
        );
211
212
        $client->accounts()->getAccountInfo(['uid' => $uid]);
213
    }
214
215 View Code Duplication
    public function testRequestWillThrowTimestampExceptionWhenBothTimestampAndSignatureAreInvalid()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
216
    {
217
        $uid = 'diofu90ifgdf';
218
        $timestamp = time() - 181;
219
220
        $body = sprintf(
221
            '{
222
            "UID": "%s",
223
            "UIDSignature": "%s",
224
            "signatureTimestamp": "%d",
225
            "statusCode": 200,
226
            "errorCode": 0,
227
            "statusReason": "OK",
228
            "callId": "123456",
229
            "time": "2015-03-22T11:42:25.943Z"
230
        }',
231
            $uid,
232
            'invalid',
233
            $timestamp
234
        );
235
236
        $client = new Gigya('key', 'secret');
237
        $this->setUpGigyaHistory($client, $body);
238
239
        static::setExpectedException(
240
            InvalidTimestampException::class
241
        );
242
243
        $client->accounts()->getAccountInfo(['uid' => $uid]);
244
    }
245
246
    public function testGigyaWillTriggerSubscriberOnlyWhenItIsAddedInARequest()
247
    {
248
        $client = new Gigya('key', 'secret');
249
        $this->setUpGigyaHistory($client);
250
251
        $client->accounts()->getAccountInfo();
252
253
        $subscriber = m::mock(SubscriberInterface::class);
254
        $subscriber->shouldReceive('getEvents')
255
                   ->andReturn([
256
                       'complete' => ['someMethod'],
257
                   ]);
258
259
        $client->addSubscriber($subscriber);
260
261
        $subscriber->shouldReceive('someMethod')
262
                   ->once();
263
264
        $client->accounts()->getAccountInfo();
265
266
        $client->removeSubscriber($subscriber);
267
268
        $client->accounts()->getAccountInfo();
269
    }
270
}
271