|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SumUp\Tests; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use SumUp\Application\ApplicationConfiguration; |
| 7 | +use SumUp\HttpClients\Response; |
| 8 | +use SumUp\Services\Authorization; |
| 9 | +use SumUp\Tests\Doubles\FakeHttpClient; |
| 10 | + |
| 11 | +class AuthorizationTest extends TestCase |
| 12 | +{ |
| 13 | + public function testApiKeyShortCircuitsHttpCalls() |
| 14 | + { |
| 15 | + $config = new ApplicationConfiguration([ |
| 16 | + 'api_key' => 'secret-api-key', |
| 17 | + ]); |
| 18 | + |
| 19 | + $httpClient = new FakeHttpClient($this->createResponse([]), true); |
| 20 | + $authorization = new Authorization($httpClient, $config); |
| 21 | + |
| 22 | + $token = $authorization->getToken(); |
| 23 | + |
| 24 | + $this->assertSame('secret-api-key', $token->getValue()); |
| 25 | + $this->assertSame('Bearer', $token->getType()); |
| 26 | + } |
| 27 | + |
| 28 | + public function testClientCredentialsFlowMakesHttpRequest() |
| 29 | + { |
| 30 | + $config = new ApplicationConfiguration([ |
| 31 | + 'app_id' => 'app-id', |
| 32 | + 'app_secret' => 'app-secret', |
| 33 | + 'grant_type' => 'client_credentials', |
| 34 | + ]); |
| 35 | + |
| 36 | + $httpClient = new FakeHttpClient($this->createResponse([ |
| 37 | + 'access_token' => 'token-from-http', |
| 38 | + 'token_type' => 'Bearer', |
| 39 | + 'expires_in' => 3600, |
| 40 | + ])); |
| 41 | + |
| 42 | + $authorization = new Authorization($httpClient, $config); |
| 43 | + $token = $authorization->getToken(); |
| 44 | + |
| 45 | + $this->assertSame('token-from-http', $token->getValue()); |
| 46 | + $this->assertSame('Bearer', $token->getType()); |
| 47 | + $this->assertSame(3600, $token->getExpiresIn()); |
| 48 | + |
| 49 | + $requests = $httpClient->getRequests(); |
| 50 | + $this->assertCount(1, $requests); |
| 51 | + $this->assertSame('POST', $requests[0]['method']); |
| 52 | + $this->assertSame('/token', $requests[0]['url']); |
| 53 | + $this->assertSame('client_credentials', $requests[0]['body']['grant_type']); |
| 54 | + $this->assertSame('app-id', $requests[0]['body']['client_id']); |
| 55 | + $this->assertSame('app-secret', $requests[0]['body']['client_secret']); |
| 56 | + $this->assertArrayHasKey('Content-Type', $requests[0]['headers']); |
| 57 | + } |
| 58 | + |
| 59 | + private function createResponse(array $body) |
| 60 | + { |
| 61 | + return new Response(200, json_decode(json_encode($body))); |
| 62 | + } |
| 63 | +} |
0 commit comments