| @@ 28-46 (lines=19) @@ | ||
| 25 | ||
| 26 | class CredentialsAuthMiddlewareTest extends TestCase |
|
| 27 | { |
|
| 28 | public function testKeyAndSecretIsPassedToParams() |
|
| 29 | { |
|
| 30 | $handler = new MockHandler([ |
|
| 31 | function (RequestInterface $request) { |
|
| 32 | $query = $request->getUri()->getQuery(); |
|
| 33 | $this->assertRegExp('/client_id=key/', $query); |
|
| 34 | $this->assertRegExp('/client_secret=client_secret/', $query); |
|
| 35 | return new Response(200); |
|
| 36 | }, |
|
| 37 | ]); |
|
| 38 | ||
| 39 | $stack = new HandlerStack($handler); |
|
| 40 | $stack->push(CredentialsAuthMiddleware::middleware('key', 'client_secret')); |
|
| 41 | ||
| 42 | $comp = $stack->resolve(); |
|
| 43 | ||
| 44 | $promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'credentials']); |
|
| 45 | $this->assertInstanceOf(PromiseInterface::class, $promise); |
|
| 46 | } |
|
| 47 | ||
| 48 | public function testKeySecretAndUserKeyIsPassedToParams() |
|
| 49 | { |
|
| @@ 48-66 (lines=19) @@ | ||
| 45 | $this->assertInstanceOf(PromiseInterface::class, $promise); |
|
| 46 | } |
|
| 47 | ||
| 48 | public function testKeySecretAndUserKeyIsPassedToParams() |
|
| 49 | { |
|
| 50 | $handler = new MockHandler([ |
|
| 51 | function (RequestInterface $request) { |
|
| 52 | $query = $request->getUri()->getQuery(); |
|
| 53 | $this->assertRegExp('/client_id=user/', $query); |
|
| 54 | $this->assertRegExp('/client_secret=client_secret/', $query); |
|
| 55 | return new Response(200); |
|
| 56 | }, |
|
| 57 | ]); |
|
| 58 | ||
| 59 | $stack = new HandlerStack($handler); |
|
| 60 | $stack->push(CredentialsAuthMiddleware::middleware('key', 'client_secret', 'user')); |
|
| 61 | ||
| 62 | $comp = $stack->resolve(); |
|
| 63 | ||
| 64 | $promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'credentials']); |
|
| 65 | $this->assertInstanceOf(PromiseInterface::class, $promise); |
|
| 66 | } |
|
| 67 | ||
| 68 | public function testAccessors() |
|
| 69 | { |
|
| @@ 77-95 (lines=19) @@ | ||
| 74 | static::assertEquals('user', $auth->getUserKey()); |
|
| 75 | } |
|
| 76 | ||
| 77 | public function testSubscriberDoesNotDoAnythingForNonHttpsRequests() |
|
| 78 | { |
|
| 79 | $handler = new MockHandler([ |
|
| 80 | function (RequestInterface $request) { |
|
| 81 | $query = $request->getUri()->getQuery(); |
|
| 82 | $this->assertNotRegExp('/client_id=/', $query); |
|
| 83 | $this->assertNotRegExp('/client_secret=/', $query); |
|
| 84 | return new Response(200); |
|
| 85 | }, |
|
| 86 | ]); |
|
| 87 | ||
| 88 | $stack = new HandlerStack($handler); |
|
| 89 | $stack->push(CredentialsAuthMiddleware::middleware('key', 'secret', 'user')); |
|
| 90 | ||
| 91 | $comp = $stack->resolve(); |
|
| 92 | ||
| 93 | $promise = $comp(new Request('GET', 'http://example.com'), ['auth' => 'credentials']); |
|
| 94 | $this->assertInstanceOf(PromiseInterface::class, $promise); |
|
| 95 | } |
|
| 96 | ||
| 97 | public function testSubscriberDoesNotDoAnythingForNonGigyaAuthRequests() |
|
| 98 | { |
|
| @@ 97-115 (lines=19) @@ | ||
| 94 | $this->assertInstanceOf(PromiseInterface::class, $promise); |
|
| 95 | } |
|
| 96 | ||
| 97 | public function testSubscriberDoesNotDoAnythingForNonGigyaAuthRequests() |
|
| 98 | { |
|
| 99 | $handler = new MockHandler([ |
|
| 100 | function (RequestInterface $request) { |
|
| 101 | $query = $request->getUri()->getQuery(); |
|
| 102 | $this->assertNotRegExp('/client_id=/', $query); |
|
| 103 | $this->assertNotRegExp('/client_secret=/', $query); |
|
| 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 | ||
| @@ 28-46 (lines=19) @@ | ||
| 25 | ||
| 26 | class HttpsAuthMiddlewareTest extends TestCase |
|
| 27 | { |
|
| 28 | public function testKeyAndSecretIsPassedToParams() |
|
| 29 | { |
|
| 30 | $handler = new MockHandler([ |
|
| 31 | function (RequestInterface $request) { |
|
| 32 | $query = $request->getUri()->getQuery(); |
|
| 33 | $this->assertRegExp('/apiKey=key/', $query); |
|
| 34 | $this->assertRegExp('/secret=secret/', $query); |
|
| 35 | return new Response(200); |
|
| 36 | }, |
|
| 37 | ]); |
|
| 38 | ||
| 39 | $stack = new HandlerStack($handler); |
|
| 40 | $stack->push(HttpsAuthMiddleware::middleware('key', 'secret')); |
|
| 41 | ||
| 42 | $comp = $stack->resolve(); |
|
| 43 | ||
| 44 | $promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya']); |
|
| 45 | $this->assertInstanceOf(PromiseInterface::class, $promise); |
|
| 46 | } |
|
| 47 | ||
| 48 | public function testKeySecretAndUserKeyIsPassedToParams() |
|
| 49 | { |
|
| @@ 48-67 (lines=20) @@ | ||
| 45 | $this->assertInstanceOf(PromiseInterface::class, $promise); |
|
| 46 | } |
|
| 47 | ||
| 48 | public function testKeySecretAndUserKeyIsPassedToParams() |
|
| 49 | { |
|
| 50 | $handler = new MockHandler([ |
|
| 51 | function (RequestInterface $request) { |
|
| 52 | $query = $request->getUri()->getQuery(); |
|
| 53 | $this->assertRegExp('/apiKey=key/', $query); |
|
| 54 | $this->assertRegExp('/secret=secret/', $query); |
|
| 55 | $this->assertRegExp('/userKey=user/', $query); |
|
| 56 | return new Response(200); |
|
| 57 | }, |
|
| 58 | ]); |
|
| 59 | ||
| 60 | $stack = new HandlerStack($handler); |
|
| 61 | $stack->push(HttpsAuthMiddleware::middleware('key', 'secret', 'user')); |
|
| 62 | ||
| 63 | $comp = $stack->resolve(); |
|
| 64 | ||
| 65 | $promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya']); |
|
| 66 | $this->assertInstanceOf(PromiseInterface::class, $promise); |
|
| 67 | } |
|
| 68 | ||
| 69 | public function testAccessors() |
|
| 70 | { |
|
| @@ 78-97 (lines=20) @@ | ||
| 75 | static::assertEquals('user', $auth->getUserKey()); |
|
| 76 | } |
|
| 77 | ||
| 78 | public function testSubscriberDoesNotDoAnythingForNonHttpsRequests() |
|
| 79 | { |
|
| 80 | $handler = new MockHandler([ |
|
| 81 | function (RequestInterface $request) { |
|
| 82 | $query = $request->getUri()->getQuery(); |
|
| 83 | $this->assertNotRegExp('/apiKey=/', $query); |
|
| 84 | $this->assertNotRegExp('/secret=/', $query); |
|
| 85 | $this->assertNotRegExp('/userKey=/', $query); |
|
| 86 | return new Response(200); |
|
| 87 | }, |
|
| 88 | ]); |
|
| 89 | ||
| 90 | $stack = new HandlerStack($handler); |
|
| 91 | $stack->push(HttpsAuthMiddleware::middleware('key', 'secret', 'user')); |
|
| 92 | ||
| 93 | $comp = $stack->resolve(); |
|
| 94 | ||
| 95 | $promise = $comp(new Request('GET', 'http://example.com'), ['auth' => 'gigya']); |
|
| 96 | $this->assertInstanceOf(PromiseInterface::class, $promise); |
|
| 97 | } |
|
| 98 | ||
| 99 | public function testSubscriberDoesNotDoAnythingForNonGigyaAuthRequests() |
|
| 100 | { |
|
| @@ 99-118 (lines=20) @@ | ||
| 96 | $this->assertInstanceOf(PromiseInterface::class, $promise); |
|
| 97 | } |
|
| 98 | ||
| 99 | public function testSubscriberDoesNotDoAnythingForNonGigyaAuthRequests() |
|
| 100 | { |
|
| 101 | $handler = new MockHandler([ |
|
| 102 | function (RequestInterface $request, array $options) { |
|
| 103 | $query = $request->getUri()->getQuery(); |
|
| 104 | $this->assertNotRegExp('/apiKey=/', $query); |
|
| 105 | $this->assertNotRegExp('/secret=/', $query); |
|
| 106 | $this->assertNotRegExp('/userKey=/', $query); |
|
| 107 | return new Response(200); |
|
| 108 | }, |
|
| 109 | ]); |
|
| 110 | ||
| 111 | $stack = new HandlerStack($handler); |
|
| 112 | $stack->push(HttpsAuthMiddleware::middleware('key', 'secret', 'user')); |
|
| 113 | ||
| 114 | $comp = $stack->resolve(); |
|
| 115 | ||
| 116 | $promise = $comp(new Request('GET', 'http://example.com'), ['auth' => 'oauth']); |
|
| 117 | $this->assertInstanceOf(PromiseInterface::class, $promise); |
|
| 118 | } |
|
| 119 | } |
|
| 120 | ||
| @@ 39-59 (lines=21) @@ | ||
| 36 | $this->grant = m::mock(GrantInterface::class); |
|
| 37 | } |
|
| 38 | ||
| 39 | public function testSign() |
|
| 40 | { |
|
| 41 | $handler = new MockHandler([ |
|
| 42 | function (RequestInterface $request) { |
|
| 43 | $this->assertEquals('OAuth test', $request->getHeaderLine('Authorization')); |
|
| 44 | return new Response(200); |
|
| 45 | }, |
|
| 46 | ]); |
|
| 47 | ||
| 48 | $token = new AccessToken('test'); |
|
| 49 | $this->grant->shouldReceive('getToken') |
|
| 50 | ->andReturn($token); |
|
| 51 | ||
| 52 | $stack = new HandlerStack($handler); |
|
| 53 | $stack->push(OAuth2Middleware::middleware($this->grant)); |
|
| 54 | ||
| 55 | $comp = $stack->resolve(); |
|
| 56 | ||
| 57 | $promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya-oauth2']); |
|
| 58 | $this->assertInstanceOf(PromiseInterface::class, $promise); |
|
| 59 | } |
|
| 60 | ||
| 61 | public function testErrorThatIsNot401() |
|
| 62 | { |
|
| @@ 61-81 (lines=21) @@ | ||
| 58 | $this->assertInstanceOf(PromiseInterface::class, $promise); |
|
| 59 | } |
|
| 60 | ||
| 61 | public function testErrorThatIsNot401() |
|
| 62 | { |
|
| 63 | $handler = new MockHandler([ |
|
| 64 | function (RequestInterface $request) { |
|
| 65 | $this->assertEquals('OAuth test', $request->getHeaderLine('Authorization')); |
|
| 66 | return new Response(503); |
|
| 67 | }, |
|
| 68 | ]); |
|
| 69 | ||
| 70 | $token = new AccessToken('test'); |
|
| 71 | $this->grant->shouldReceive('getToken') |
|
| 72 | ->andReturn($token); |
|
| 73 | ||
| 74 | $stack = new HandlerStack($handler); |
|
| 75 | $stack->push(OAuth2Middleware::middleware($this->grant)); |
|
| 76 | ||
| 77 | $comp = $stack->resolve(); |
|
| 78 | ||
| 79 | $promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya-oauth2']); |
|
| 80 | $this->assertInstanceOf(PromiseInterface::class, $promise); |
|
| 81 | } |
|
| 82 | ||
| 83 | public function testErrorThatIsNotRetried() |
|
| 84 | { |
|
| @@ 145-165 (lines=21) @@ | ||
| 142 | $this->assertEquals(200, $response->getStatusCode()); |
|
| 143 | } |
|
| 144 | ||
| 145 | public function testErrorThatIsNotOauthAuth() |
|
| 146 | { |
|
| 147 | $handler = new MockHandler([ |
|
| 148 | function (RequestInterface $request) { |
|
| 149 | $this->assertEquals('', $request->getHeaderLine('Authorization')); |
|
| 150 | return new Response(401); |
|
| 151 | }, |
|
| 152 | ]); |
|
| 153 | ||
| 154 | $stack = new HandlerStack($handler); |
|
| 155 | $stack->push(OAuth2Middleware::middleware($this->grant)); |
|
| 156 | ||
| 157 | $comp = $stack->resolve(); |
|
| 158 | ||
| 159 | /** @var PromiseInterface $promise */ |
|
| 160 | $promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'none']); |
|
| 161 | $this->assertInstanceOf(PromiseInterface::class, $promise); |
|
| 162 | /** @var ResponseInterface $response */ |
|
| 163 | $response = $promise->wait(true); |
|
| 164 | $this->assertEquals(401, $response->getStatusCode()); |
|
| 165 | } |
|
| 166 | ||
| 167 | public function testErrorWhenNoTokenIsReturnedWillNotIntercept() |
|
| 168 | { |
|