Completed
Pull Request — master (#18)
by Harry
05:07
created

GigyaTest::setUpGigyaHistory()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 1
Metric Value
c 4
b 2
f 1
dl 0
loc 31
rs 8.5806
cc 4
eloc 18
nc 1
nop 2
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\Functional;
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([
41
            new Response(
42
                '200',
43
                [],
44
                Stream::factory(
45
                    $body ?: TestFixtures::getFixture('basic')
46
                )
47
            ),
48
            new Response(
49
                '200',
50
                [],
51
                Stream::factory(
52
                    $body ?: TestFixtures::getFixture('basic')
53
                )
54
            ),
55
            new Response(
56
                '200',
57
                [],
58
                Stream::factory(
59
                    $body ?: TestFixtures::getFixture('basic')
60
                )
61
            ),
62
        ]);
63
        $gigya->addSubscriber($history);
64
        $gigya->addSubscriber($mock);
65
66
        return $history;
67
    }
68
69
    public function testAuthInjectsKeyAndSecretIntoParams()
70
    {
71
        $client  = new Gigya('key', 'secret');
72
        $history = $this->setUpGigyaHistory($client);
73
74
        $response = $client->accounts()->getAccountInfo();
75
76
        static::assertEquals(0, $response->getErrorCode());
77
        static::assertEquals(1, $history->count());
78
        $request = $history->getLastRequest();
79
        static::assertEquals(
80
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo?apiKey=key&secret=secret',
81
            $request->getUrl()
82
        );
83
        $query = $request->getQuery();
84
        static::assertArrayHasKey('apiKey', $query);
85
        static::assertArrayHasKey('secret', $query);
86
        static::assertEquals('key', $query['apiKey']);
87
        static::assertEquals('secret', $query['secret']);
88
    }
89
90
    public function testAuthInjectsKeySecretAndUserKeyIntoParams()
91
    {
92
        $client  = new Gigya('key', 'secret', null, 'userKey');
93
        $history = $this->setUpGigyaHistory($client);
94
95
        $response = $client->accounts()->getAccountInfo();
96
97
        static::assertEquals(0, $response->getErrorCode());
98
        static::assertEquals(1, $history->count());
99
        $request = $history->getLastRequest();
100
        static::assertEquals(
101
            'https://accounts.eu1.gigya.com/accounts.getAccountInfo?apiKey=key&secret=secret&userKey=userKey',
102
            $request->getUrl()
103
        );
104
        $query = $request->getQuery();
105
        static::assertArrayHasKey('apiKey', $query);
106
        static::assertArrayHasKey('secret', $query);
107
        static::assertArrayHasKey('userKey', $query);
108
        static::assertEquals('key', $query['apiKey']);
109
        static::assertEquals('secret', $query['secret']);
110
        static::assertEquals('userKey', $query['userKey']);
111
    }
112
113
    public function testUidSignatureWhenValidDoesNotThrowException()
114
    {
115
        $uid       = 'diofu90ifgdf';
116
        $timestamp = time();
117
118
        $signatureValidator = new Signature();
119
        $signature          = $signatureValidator->calculateSignature($timestamp . '_' . $uid, 'secret');
120
121
        $body = sprintf(
122
            '{
123
            "UID": "%s",
124
            "UIDSignature": "%s",
125
            "signatureTimestamp": "%d",
126
            "statusCode": 200,
127
            "errorCode": 0,
128
            "statusReason": "OK",
129
            "callId": "123456",
130
            "time": "2015-03-22T11:42:25.943Z"
131
        }',
132
            $uid,
133
            $signature,
134
            $timestamp
135
        );
136
137
        $client  = new Gigya('key', 'secret');
138
        $history = $this->setUpGigyaHistory($client, $body);
139
140
        $response = $client->accounts()->getAccountInfo(['uid' => $uid]);
141
        static::assertEquals(0, $response->getErrorCode());
142
        static::assertEquals(1, $history->count());
143
        $request = $history->getLastRequest();
144
        static::assertEquals(
145
            "https://accounts.eu1.gigya.com/accounts.getAccountInfo?uid=$uid&apiKey=key&secret=secret",
146
            $request->getUrl()
147
        );
148
        $query = $request->getQuery();
149
        static::assertArrayHasKey('apiKey', $query);
150
        static::assertArrayHasKey('secret', $query);
151
        static::assertArrayHasKey('uid', $query);
152
        static::assertEquals('key', $query['apiKey']);
153
        static::assertEquals('secret', $query['secret']);
154
        static::assertEquals($uid, $query['uid']);
155
156
        $data = $response->getData();
157
        static::assertEquals($uid, $data->get('UID'));
158
        static::assertEquals($signature, $data->get('UIDSignature'));
159
        static::assertEquals($timestamp, $data->get('signatureTimestamp'));
160
    }
161
162
    public function testUidSignatureWhenIncorrectTimestampThrowsAnException()
163
    {
164
        $uid       = 'diofu90ifgdf';
165
        $timestamp = time() - 181;
166
167
        $signatureValidator = new Signature();
168
        $signature          = $signatureValidator->calculateSignature($timestamp . '_' . $uid, 'secret');
169
170
        $body = sprintf(
171
            '{
172
            "UID": "%s",
173
            "UIDSignature": "%s",
174
            "signatureTimestamp": "%d",
175
            "statusCode": 200,
176
            "errorCode": 0,
177
            "statusReason": "OK",
178
            "callId": "123456",
179
            "time": "2015-03-22T11:42:25.943Z"
180
        }',
181
            $uid,
182
            $signature,
183
            $timestamp
184
        );
185
186
        $client = new Gigya('key', 'secret');
187
        $this->setUpGigyaHistory($client, $body);
188
189
        static::setExpectedException(
190
            InvalidTimestampException::class
191
        );
192
193
        $client->accounts()->getAccountInfo(['uid' => $uid]);
194
    }
195
196 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...
197
    {
198
        $uid       = 'diofu90ifgdf';
199
        $timestamp = time();
200
201
        $body = sprintf(
202
            '{
203
            "UID": "%s",
204
            "UIDSignature": "%s",
205
            "signatureTimestamp": "%d",
206
            "statusCode": 200,
207
            "errorCode": 0,
208
            "statusReason": "OK",
209
            "callId": "123456",
210
            "time": "2015-03-22T11:42:25.943Z"
211
        }',
212
            $uid,
213
            'invalid',
214
            $timestamp
215
        );
216
217
        $client = new Gigya('key', 'secret');
218
        $this->setUpGigyaHistory($client, $body);
219
220
        static::setExpectedException(
221
            InvalidUidSignatureException::class
222
        );
223
224
        $client->accounts()->getAccountInfo(['uid' => $uid]);
225
    }
226
227 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...
228
    {
229
        $uid       = 'diofu90ifgdf';
230
        $timestamp = time() - 181;
231
232
        $body = sprintf(
233
            '{
234
            "UID": "%s",
235
            "UIDSignature": "%s",
236
            "signatureTimestamp": "%d",
237
            "statusCode": 200,
238
            "errorCode": 0,
239
            "statusReason": "OK",
240
            "callId": "123456",
241
            "time": "2015-03-22T11:42:25.943Z"
242
        }',
243
            $uid,
244
            'invalid',
245
            $timestamp
246
        );
247
248
        $client = new Gigya('key', 'secret');
249
        $this->setUpGigyaHistory($client, $body);
250
251
        static::setExpectedException(
252
            InvalidTimestampException::class
253
        );
254
255
        $client->accounts()->getAccountInfo(['uid' => $uid]);
256
    }
257
258
    public function testGigyaWillTriggerSubscriberOnlyWhenItIsAddedInARequest()
259
    {
260
        $client = new Gigya('key', 'secret');
261
        $this->setUpGigyaHistory($client);
262
263
        $client->accounts()->getAccountInfo();
264
265
        $subscriber = m::mock(SubscriberInterface::class);
266
        $subscriber->shouldReceive('getEvents')
267
                   ->andReturn([
268
                       'complete' => ['someMethod'],
269
                   ]);
270
271
        $client->addSubscriber($subscriber);
272
273
        $subscriber->shouldReceive('someMethod')
274
                   ->once();
275
276
        $client->accounts()->getAccountInfo();
277
278
        $client->removeSubscriber($subscriber);
279
280
        $client->accounts()->getAccountInfo();
281
    }
282
}
283