|
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\Auth; |
|
15
|
|
|
|
|
16
|
|
|
use Graze\Gigya\Auth\GigyaAuthInterface; |
|
17
|
|
|
use Graze\Gigya\Auth\GigyaHttpsAuth; |
|
18
|
|
|
use Graze\Gigya\Test\TestCase; |
|
19
|
|
|
use GuzzleHttp\Collection; |
|
20
|
|
|
use GuzzleHttp\Event\BeforeEvent; |
|
21
|
|
|
use GuzzleHttp\Event\RequestEvents; |
|
22
|
|
|
use GuzzleHttp\Event\SubscriberInterface; |
|
23
|
|
|
use GuzzleHttp\Message\RequestInterface; |
|
24
|
|
|
use GuzzleHttp\Query; |
|
25
|
|
|
use Mockery as m; |
|
26
|
|
|
|
|
27
|
|
|
class GigyaHttpsAuthTest extends TestCase |
|
28
|
|
|
{ |
|
29
|
|
|
public function testInstanceOf() |
|
30
|
|
|
{ |
|
31
|
|
|
$auth = new GigyaHttpsAuth('key', 'secret'); |
|
32
|
|
|
static::assertInstanceOf(GigyaAuthInterface::class, $auth); |
|
33
|
|
|
static::assertInstanceOf(SubscriberInterface::class, $auth); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testGetEventsHandlesBeforeAndSignsRequest() |
|
37
|
|
|
{ |
|
38
|
|
|
$auth = new GigyaHttpsAuth('key', 'secret'); |
|
39
|
|
|
static::assertEquals( |
|
40
|
|
|
['before' => ['sign', RequestEvents::SIGN_REQUEST]], |
|
41
|
|
|
$auth->getEvents() |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testKeyAndSecretIsPassedToParams() |
|
46
|
|
|
{ |
|
47
|
|
|
$request = m::mock(RequestInterface::class); |
|
48
|
|
|
$event = m::mock(BeforeEvent::class); |
|
49
|
|
|
$event->shouldReceive('getRequest') |
|
50
|
|
|
->andReturn($request); |
|
51
|
|
|
|
|
52
|
|
|
$query = m::mock(Query::class); |
|
53
|
|
|
$config = m::mock(Collection::class); |
|
54
|
|
|
|
|
55
|
|
|
$request->shouldReceive('getScheme') |
|
56
|
|
|
->andReturn('https'); |
|
57
|
|
|
$request->shouldReceive('getConfig') |
|
58
|
|
|
->andReturn($config); |
|
59
|
|
|
$request->shouldReceive('getQuery') |
|
60
|
|
|
->andReturn($query); |
|
61
|
|
|
|
|
62
|
|
|
$query->shouldReceive('offsetSet') |
|
63
|
|
|
->with('apiKey', 'key'); |
|
64
|
|
|
$query->shouldReceive('offsetSet') |
|
65
|
|
|
->with('secret', 'secret'); |
|
66
|
|
|
$config->shouldReceive('get') |
|
67
|
|
|
->with('auth') |
|
68
|
|
|
->andReturn('gigya'); |
|
69
|
|
|
|
|
70
|
|
|
$auth = new GigyaHttpsAuth('key', 'secret'); |
|
71
|
|
|
$auth->sign($event); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testKeySecretAndUserKeyIsPassedToParams() |
|
75
|
|
|
{ |
|
76
|
|
|
$request = m::mock(RequestInterface::class); |
|
77
|
|
|
$event = m::mock(BeforeEvent::class); |
|
78
|
|
|
$event->shouldReceive('getRequest') |
|
79
|
|
|
->andReturn($request); |
|
80
|
|
|
|
|
81
|
|
|
$query = m::mock(Query::class); |
|
82
|
|
|
$config = m::mock(Collection::class); |
|
83
|
|
|
|
|
84
|
|
|
$request->shouldReceive('getScheme') |
|
85
|
|
|
->andReturn('https'); |
|
86
|
|
|
$request->shouldReceive('getConfig') |
|
87
|
|
|
->andReturn($config); |
|
88
|
|
|
$request->shouldReceive('getQuery') |
|
89
|
|
|
->andReturn($query); |
|
90
|
|
|
|
|
91
|
|
|
$query->shouldReceive('offsetSet') |
|
92
|
|
|
->with('apiKey', 'key'); |
|
93
|
|
|
$query->shouldReceive('offsetSet') |
|
94
|
|
|
->with('secret', 'secret'); |
|
95
|
|
|
$query->shouldReceive('offsetSet') |
|
96
|
|
|
->with('userKey', 'user'); |
|
97
|
|
|
$config->shouldReceive('get') |
|
98
|
|
|
->with('auth') |
|
99
|
|
|
->andReturn('gigya'); |
|
100
|
|
|
|
|
101
|
|
|
$auth = new GigyaHttpsAuth('key', 'secret', 'user'); |
|
102
|
|
|
$auth->sign($event); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function testAccessors() |
|
106
|
|
|
{ |
|
107
|
|
|
$auth = new GigyaHttpsAuth('key', 'secret', 'user'); |
|
108
|
|
|
static::assertEquals('key', $auth->getApiKey()); |
|
109
|
|
|
static::assertEquals('secret', $auth->getSecret()); |
|
110
|
|
|
static::assertEquals('user', $auth->getUserKey()); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function testSubscriberDoesNotDoAnythingForNonHttpsRequests() |
|
114
|
|
|
{ |
|
115
|
|
|
$request = m::mock(RequestInterface::class); |
|
116
|
|
|
$event = m::mock(BeforeEvent::class); |
|
117
|
|
|
$event->shouldReceive('getRequest') |
|
118
|
|
|
->andReturn($request); |
|
119
|
|
|
|
|
120
|
|
|
$request->shouldReceive('getScheme') |
|
121
|
|
|
->andReturn('http'); |
|
122
|
|
|
|
|
123
|
|
|
$auth = new GigyaHttpsAuth('key', 'secret', 'user'); |
|
124
|
|
|
$auth->sign($event); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function testSubscriberDoesNotDoAnythingForNonGigyaAuthRequests() |
|
128
|
|
|
{ |
|
129
|
|
|
$request = m::mock(RequestInterface::class); |
|
130
|
|
|
$event = m::mock(BeforeEvent::class); |
|
131
|
|
|
$event->shouldReceive('getRequest') |
|
132
|
|
|
->andReturn($request); |
|
133
|
|
|
|
|
134
|
|
|
$config = m::mock(Collection::class); |
|
135
|
|
|
$request->shouldReceive('getScheme') |
|
136
|
|
|
->andReturn('https'); |
|
137
|
|
|
$request->shouldReceive('getConfig') |
|
138
|
|
|
->andReturn($config); |
|
139
|
|
|
|
|
140
|
|
|
$config->shouldReceive('get') |
|
141
|
|
|
->with('auth') |
|
142
|
|
|
->andReturn('oauth'); |
|
143
|
|
|
|
|
144
|
|
|
$auth = new GigyaHttpsAuth('key', 'secret', 'user'); |
|
145
|
|
|
$auth->sign($event); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|