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 Mockery as m; |
24
|
|
|
use Psr\Http\Message\RequestInterface; |
25
|
|
|
|
26
|
|
|
class CredentialsAuthMiddlewareTest extends TestCase |
27
|
|
|
{ |
28
|
|
View Code Duplication |
public function testKeyAndSecretIsPassedToParams() |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$handler = new MockHandler([ |
31
|
|
|
function (RequestInterface $request, array $options) { |
32
|
|
|
$this->assertEquals('key', $options['query']['client_id']); |
33
|
|
|
$this->assertEquals('client_secret', $options['query']['client_secret']); |
34
|
|
|
return new Response(200); |
35
|
|
|
}, |
36
|
|
|
]); |
37
|
|
|
|
38
|
|
|
$stack = new HandlerStack($handler); |
39
|
|
|
$stack->push(CredentialsAuthMiddleware::middleware('key', 'client_secret')); |
40
|
|
|
|
41
|
|
|
$comp = $stack->resolve(); |
42
|
|
|
|
43
|
|
|
$promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'credentials']); |
44
|
|
|
$this->assertInstanceOf(PromiseInterface::class, $promise); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
public function testKeySecretAndUserKeyIsPassedToParams() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$handler = new MockHandler([ |
50
|
|
|
function (RequestInterface $request, array $options) { |
51
|
|
|
$this->assertEquals('user', $options['query']['client_id']); |
52
|
|
|
$this->assertEquals('client_secret', $options['query']['client_secret']); |
53
|
|
|
return new Response(200); |
54
|
|
|
}, |
55
|
|
|
]); |
56
|
|
|
|
57
|
|
|
$stack = new HandlerStack($handler); |
58
|
|
|
$stack->push(CredentialsAuthMiddleware::middleware('key', 'client_secret', 'user')); |
59
|
|
|
|
60
|
|
|
$comp = $stack->resolve(); |
61
|
|
|
|
62
|
|
|
$promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'credentials']); |
63
|
|
|
$this->assertInstanceOf(PromiseInterface::class, $promise); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
public function testAccessors() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$auth = new CredentialsAuthMiddleware(function () { |
69
|
|
|
}, 'key', 'client_secret', 'user'); |
70
|
|
|
static::assertEquals('key', $auth->getApiKey()); |
71
|
|
|
static::assertEquals('client_secret', $auth->getSecret()); |
72
|
|
|
static::assertEquals('user', $auth->getUserKey()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
public function testSubscriberDoesNotDoAnythingForNonHttpsRequests() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$handler = new MockHandler([ |
78
|
|
|
function (RequestInterface $request, array $options) { |
79
|
|
|
if (isset($options['query'])) { |
80
|
|
|
$this->assertNotContains('client_id', $options['query']); |
81
|
|
|
$this->assertNotContains('client_secret', $options['query']); |
82
|
|
|
} |
83
|
|
|
return new Response(200); |
84
|
|
|
}, |
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
$stack = new HandlerStack($handler); |
88
|
|
|
$stack->push(CredentialsAuthMiddleware::middleware('key', 'secret', 'user')); |
89
|
|
|
|
90
|
|
|
$comp = $stack->resolve(); |
91
|
|
|
|
92
|
|
|
$promise = $comp(new Request('GET', 'http://example.com'), ['auth' => 'credentials']); |
93
|
|
|
$this->assertInstanceOf(PromiseInterface::class, $promise); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
public function testSubscriberDoesNotDoAnythingForNonGigyaAuthRequests() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$handler = new MockHandler([ |
99
|
|
|
function (RequestInterface $request, array $options) { |
100
|
|
|
if (isset($options['query'])) { |
101
|
|
|
$this->assertNotContains('client_id', $options['query']); |
102
|
|
|
$this->assertNotContains('client_secret', $options['query']); |
103
|
|
|
} |
104
|
|
|
return new Response(200); |
105
|
|
|
}, |
106
|
|
|
]); |
107
|
|
|
|
108
|
|
|
$stack = new HandlerStack($handler); |
109
|
|
|
$stack->push(CredentialsAuthMiddleware::middleware('key', 'secret', 'user')); |
110
|
|
|
|
111
|
|
|
$comp = $stack->resolve(); |
112
|
|
|
|
113
|
|
|
$promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'oauth']); |
114
|
|
|
$this->assertInstanceOf(PromiseInterface::class, $promise); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
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.