|
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\CredentialsAuthMiddleware; |
|
17
|
|
|
use Graze\Gigya\Test\TestCase; |
|
18
|
|
|
use GuzzleHttp\Handler\MockHandler; |
|
19
|
|
|
use GuzzleHttp\HandlerStack; |
|
20
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
|
21
|
|
|
use GuzzleHttp\Psr7\Request; |
|
22
|
|
|
use GuzzleHttp\Psr7\Response; |
|
23
|
|
|
use Psr\Http\Message\RequestInterface; |
|
24
|
|
|
|
|
25
|
|
|
class CredentialsAuthMiddlewareTest extends TestCase |
|
26
|
|
|
{ |
|
27
|
|
|
public function testKeyAndSecretIsPassedToParams() |
|
28
|
|
|
{ |
|
29
|
|
|
$handler = new MockHandler( |
|
30
|
|
|
[ |
|
31
|
|
|
function (RequestInterface $request) { |
|
32
|
|
|
$params = \GuzzleHttp\Psr7\parse_query((string)$request->getBody()); |
|
33
|
|
|
$this->assertArrayHasKey('client_id', $params); |
|
34
|
|
|
$this->assertEquals('key', $params['client_id']); |
|
35
|
|
|
$this->assertArrayHasKey('client_secret', $params); |
|
36
|
|
|
$this->assertEquals('client_secret', $params['client_secret']); |
|
37
|
|
|
return new Response(200); |
|
38
|
|
|
}, |
|
39
|
|
|
] |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
$stack = new HandlerStack($handler); |
|
43
|
|
|
$stack->push(CredentialsAuthMiddleware::middleware('key', 'client_secret')); |
|
44
|
|
|
|
|
45
|
|
|
$comp = $stack->resolve(); |
|
46
|
|
|
|
|
47
|
|
|
$promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'credentials']); |
|
48
|
|
|
$this->assertInstanceOf(PromiseInterface::class, $promise); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testKeySecretAndUserKeyIsPassedToParams() |
|
52
|
|
|
{ |
|
53
|
|
|
$handler = new MockHandler( |
|
54
|
|
|
[ |
|
55
|
|
|
function (RequestInterface $request) { |
|
56
|
|
|
$params = \GuzzleHttp\Psr7\parse_query((string)$request->getBody()); |
|
57
|
|
|
$this->assertArrayHasKey('client_id', $params); |
|
58
|
|
|
$this->assertEquals('user', $params['client_id']); |
|
59
|
|
|
$this->assertArrayHasKey('client_secret', $params); |
|
60
|
|
|
$this->assertEquals('client_secret', $params['client_secret']); |
|
61
|
|
|
return new Response(200); |
|
62
|
|
|
}, |
|
63
|
|
|
] |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
$stack = new HandlerStack($handler); |
|
67
|
|
|
$stack->push(CredentialsAuthMiddleware::middleware('key', 'client_secret', 'user')); |
|
68
|
|
|
|
|
69
|
|
|
$comp = $stack->resolve(); |
|
70
|
|
|
|
|
71
|
|
|
$promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'credentials']); |
|
72
|
|
|
$this->assertInstanceOf(PromiseInterface::class, $promise); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testAccessors() |
|
76
|
|
|
{ |
|
77
|
|
|
$auth = new CredentialsAuthMiddleware( |
|
78
|
|
|
function () { |
|
79
|
|
|
}, |
|
80
|
|
|
'key', |
|
81
|
|
|
'client_secret', |
|
82
|
|
|
'user' |
|
83
|
|
|
); |
|
84
|
|
|
static::assertEquals('key', $auth->getApiKey()); |
|
85
|
|
|
static::assertEquals('client_secret', $auth->getSecret()); |
|
86
|
|
|
static::assertEquals('user', $auth->getUserKey()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testSubscriberDoesNotDoAnythingForNonHttpsRequests() |
|
90
|
|
|
{ |
|
91
|
|
|
$handler = new MockHandler( |
|
92
|
|
|
[ |
|
93
|
|
|
function (RequestInterface $request) { |
|
94
|
|
|
$params = \GuzzleHttp\Psr7\parse_query((string)$request->getBody()); |
|
95
|
|
|
$this->assertArrayNotHasKey('client_id', $params); |
|
96
|
|
|
$this->assertArrayNotHasKey('client_secret', $params); |
|
97
|
|
|
return new Response(200); |
|
98
|
|
|
}, |
|
99
|
|
|
] |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
$stack = new HandlerStack($handler); |
|
103
|
|
|
$stack->push(CredentialsAuthMiddleware::middleware('key', 'secret', 'user')); |
|
104
|
|
|
|
|
105
|
|
|
$comp = $stack->resolve(); |
|
106
|
|
|
|
|
107
|
|
|
$promise = $comp(new Request('GET', 'http://example.com'), ['auth' => 'credentials']); |
|
108
|
|
|
$this->assertInstanceOf(PromiseInterface::class, $promise); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function testSubscriberDoesNotDoAnythingForNonGigyaAuthRequests() |
|
112
|
|
|
{ |
|
113
|
|
|
$handler = new MockHandler( |
|
114
|
|
|
[ |
|
115
|
|
|
function (RequestInterface $request) { |
|
116
|
|
|
$params = \GuzzleHttp\Psr7\parse_query((string)$request->getBody()); |
|
117
|
|
|
$this->assertArrayNotHasKey('client_id', $params); |
|
118
|
|
|
$this->assertArrayNotHasKey('client_secret', $params); |
|
119
|
|
|
return new Response(200); |
|
120
|
|
|
}, |
|
121
|
|
|
] |
|
122
|
|
|
); |
|
123
|
|
|
|
|
124
|
|
|
$stack = new HandlerStack($handler); |
|
125
|
|
|
$stack->push(CredentialsAuthMiddleware::middleware('key', 'secret', 'user')); |
|
126
|
|
|
|
|
127
|
|
|
$comp = $stack->resolve(); |
|
128
|
|
|
|
|
129
|
|
|
$promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'oauth']); |
|
130
|
|
|
$this->assertInstanceOf(PromiseInterface::class, $promise); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|