Completed
Push — master ( e13577...86f857 )
by Harry
15s
created

UidSignatureValidatorTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 3 Features 0
Metric Value
wmc 7
c 4
b 3
f 0
lcom 1
cbo 5
dl 0
loc 160
rs 10

7 Methods

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