graze /
gigya-client
| 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\HttpsAuthMiddleware; |
||
| 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 HttpsAuthMiddlewareTest extends TestCase |
||
| 26 | { |
||
| 27 | public function testKeyAndSecretIsPassedToParams() |
||
| 28 | { |
||
| 29 | $handler = new MockHandler( |
||
| 30 | [ |
||
| 31 | function (RequestInterface $request) { |
||
| 32 | $params = \GuzzleHttp\Psr7\parse_query($request->getBody()); |
||
| 33 | $this->assertArrayHasKey('apiKey', $params); |
||
| 34 | $this->assertEquals('key', $params['apiKey']); |
||
| 35 | $this->assertArrayHasKey('secret', $params); |
||
| 36 | $this->assertEquals('secret', $params['secret']); |
||
| 37 | return new Response(200); |
||
| 38 | }, |
||
| 39 | ] |
||
| 40 | ); |
||
| 41 | |||
| 42 | $stack = new HandlerStack($handler); |
||
| 43 | $stack->push(HttpsAuthMiddleware::middleware('key', 'secret')); |
||
| 44 | |||
| 45 | $comp = $stack->resolve(); |
||
| 46 | |||
| 47 | $promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya']); |
||
| 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($request->getBody()); |
||
| 57 | $this->assertArrayHasKey('apiKey', $params); |
||
| 58 | $this->assertEquals('key', $params['apiKey']); |
||
| 59 | $this->assertArrayHasKey('secret', $params); |
||
| 60 | $this->assertEquals('secret', $params['secret']); |
||
| 61 | $this->assertArrayHasKey('userKey', $params); |
||
| 62 | $this->assertEquals('user', $params['userKey']); |
||
| 63 | return new Response(200); |
||
| 64 | }, |
||
| 65 | ] |
||
| 66 | ); |
||
| 67 | |||
| 68 | $stack = new HandlerStack($handler); |
||
| 69 | $stack->push(HttpsAuthMiddleware::middleware('key', 'secret', 'user')); |
||
| 70 | |||
| 71 | $comp = $stack->resolve(); |
||
| 72 | |||
| 73 | $promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya']); |
||
| 74 | $this->assertInstanceOf(PromiseInterface::class, $promise); |
||
| 75 | } |
||
| 76 | |||
| 77 | public function testAccessors() |
||
| 78 | { |
||
| 79 | $auth = new HttpsAuthMiddleware( |
||
| 80 | function () { |
||
| 81 | }, |
||
| 82 | 'key', |
||
| 83 | 'secret', |
||
| 84 | 'user' |
||
| 85 | ); |
||
| 86 | static::assertEquals('key', $auth->getApiKey()); |
||
| 87 | static::assertEquals('secret', $auth->getSecret()); |
||
| 88 | static::assertEquals('user', $auth->getUserKey()); |
||
| 89 | } |
||
| 90 | |||
| 91 | public function testSubscriberDoesNotDoAnythingForNonHttpsRequests() |
||
| 92 | { |
||
| 93 | $handler = new MockHandler( |
||
| 94 | [ |
||
| 95 | function (RequestInterface $request) { |
||
| 96 | $params = \GuzzleHttp\Psr7\parse_query($request->getBody()); |
||
| 97 | $this->assertArrayNotHasKey('apiKey', $params); |
||
| 98 | $this->assertArrayNotHasKey('secret', $params); |
||
| 99 | $this->assertArrayNotHasKey('userKey', $params); |
||
| 100 | return new Response(200); |
||
| 101 | }, |
||
| 102 | ] |
||
| 103 | ); |
||
| 104 | |||
| 105 | $stack = new HandlerStack($handler); |
||
| 106 | $stack->push(HttpsAuthMiddleware::middleware('key', 'secret', 'user')); |
||
| 107 | |||
| 108 | $comp = $stack->resolve(); |
||
| 109 | |||
| 110 | $promise = $comp(new Request('GET', 'http://example.com'), ['auth' => 'gigya']); |
||
| 111 | $this->assertInstanceOf(PromiseInterface::class, $promise); |
||
| 112 | } |
||
| 113 | |||
| 114 | public function testSubscriberDoesNotDoAnythingForNonGigyaAuthRequests() |
||
| 115 | { |
||
| 116 | $handler = new MockHandler( |
||
| 117 | [ |
||
| 118 | function (RequestInterface $request, array $options) { |
||
|
0 ignored issues
–
show
|
|||
| 119 | $params = \GuzzleHttp\Psr7\parse_query($request->getBody()); |
||
| 120 | $this->assertArrayNotHasKey('apiKey', $params); |
||
| 121 | $this->assertArrayNotHasKey('secret', $params); |
||
| 122 | $this->assertArrayNotHasKey('userKey', $params); |
||
| 123 | return new Response(200); |
||
| 124 | }, |
||
| 125 | ] |
||
| 126 | ); |
||
| 127 | |||
| 128 | $stack = new HandlerStack($handler); |
||
| 129 | $stack->push(HttpsAuthMiddleware::middleware('key', 'secret', 'user')); |
||
| 130 | |||
| 131 | $comp = $stack->resolve(); |
||
| 132 | |||
| 133 | $promise = $comp(new Request('GET', 'http://example.com'), ['auth' => 'oauth']); |
||
| 134 | $this->assertInstanceOf(PromiseInterface::class, $promise); |
||
| 135 | } |
||
| 136 | } |
||
| 137 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.