|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace spec\Diglin\Sylius\ApiClient\Client; |
| 4 | + |
| 5 | +use Diglin\Sylius\ApiClient\Api\Legacy\AuthenticationApiInterface; |
| 6 | +use Diglin\Sylius\ApiClient\Client\HttpClient; |
| 7 | +use Diglin\Sylius\ApiClient\Client\HttpClientInterface; |
| 8 | +use Diglin\Sylius\ApiClient\Client\LegacyAuthenticatedHttpClient; |
| 9 | +use Diglin\Sylius\ApiClient\Exception\UnauthorizedHttpException; |
| 10 | +use Diglin\Sylius\ApiClient\Security\LegacyAuthentication; |
| 11 | +use PhpSpec\ObjectBehavior; |
| 12 | +use Psr\Http\Message\ResponseInterface; |
| 13 | + |
| 14 | +class LegacyAuthenticatedHttpClientSpec extends ObjectBehavior |
| 15 | +{ |
| 16 | + public function let( |
| 17 | + HttpClient $httpClient, |
| 18 | + AuthenticationApiInterface $authenticationApi, |
| 19 | + LegacyAuthentication $authentication |
| 20 | + ) { |
| 21 | + $this->beConstructedWith($httpClient, $authenticationApi, $authentication); |
| 22 | + } |
| 23 | + |
| 24 | + public function it_is_initializable() |
| 25 | + { |
| 26 | + $this->shouldHaveType(LegacyAuthenticatedHttpClient::class); |
| 27 | + $this->shouldImplement(HttpClientInterface::class); |
| 28 | + } |
| 29 | + |
| 30 | + public function it_sends_an_authenticated_and_successful_request_when_access_token_is_defined( |
| 31 | + $httpClient, |
| 32 | + $authentication, |
| 33 | + ResponseInterface $response |
| 34 | + ) { |
| 35 | + $authentication->getAccessToken()->willReturn('bar'); |
| 36 | + |
| 37 | + $httpClient->sendRequest( |
| 38 | + 'POST', |
| 39 | + 'http://diglin.com/api/rest/v1/products/foo', |
| 40 | + ['Content-Type' => 'application/json', 'Authorization' => 'Bearer bar'], |
| 41 | + '{"identifier": "foo"}' |
| 42 | + )->willReturn($response); |
| 43 | + |
| 44 | + $this->sendRequest( |
| 45 | + 'POST', |
| 46 | + 'http://diglin.com/api/rest/v1/products/foo', |
| 47 | + ['Content-Type' => 'application/json'], |
| 48 | + '{"identifier": "foo"}' |
| 49 | + )->shouldReturn($response); |
| 50 | + } |
| 51 | + |
| 52 | + public function it_sends_an_authenticated_and_successful_request_at_first_call( |
| 53 | + $httpClient, |
| 54 | + $authenticationApi, |
| 55 | + $authentication, |
| 56 | + ResponseInterface $response |
| 57 | + ) { |
| 58 | + $authentication->getClientId()->willReturn('client_id'); |
| 59 | + $authentication->getSecret()->willReturn('secret'); |
| 60 | + $authentication->getUsername()->willReturn('julia'); |
| 61 | + $authentication->getPassword()->willReturn('julia_pwd'); |
| 62 | + $authentication->getAccessToken()->willReturn(null, 'foo'); |
| 63 | + |
| 64 | + $authenticationApi |
| 65 | + ->authenticateByPassword('client_id', 'secret', 'julia', 'julia_pwd') |
| 66 | + ->willReturn([ |
| 67 | + 'access_token' => 'foo', |
| 68 | + 'expires_in' => 3600, |
| 69 | + 'token_type' => 'bearer', |
| 70 | + 'scope' => null, |
| 71 | + 'refresh_token' => 'bar', |
| 72 | + ]) |
| 73 | + ; |
| 74 | + |
| 75 | + $authentication |
| 76 | + ->setAccessToken('foo') |
| 77 | + ->shouldBeCalled() |
| 78 | + ->willReturn($authentication) |
| 79 | + ; |
| 80 | + |
| 81 | + $authentication |
| 82 | + ->setRefreshToken('bar') |
| 83 | + ->shouldBeCalled() |
| 84 | + ->willReturn($authentication) |
| 85 | + ; |
| 86 | + |
| 87 | + $httpClient->sendRequest( |
| 88 | + 'POST', |
| 89 | + 'http://diglin.com/api/rest/v1/products/foo', |
| 90 | + ['Content-Type' => 'application/json', 'Authorization' => 'Bearer foo'], |
| 91 | + '{"identifier": "foo"}' |
| 92 | + )->willReturn($response); |
| 93 | + |
| 94 | + $this->sendRequest( |
| 95 | + 'POST', |
| 96 | + 'http://diglin.com/api/rest/v1/products/foo', |
| 97 | + ['Content-Type' => 'application/json'], |
| 98 | + '{"identifier": "foo"}' |
| 99 | + )->shouldReturn($response); |
| 100 | + } |
| 101 | + |
| 102 | + public function it_sends_an_authenticated_and_successful_request_when_access_token_expired( |
| 103 | + $httpClient, |
| 104 | + $authenticationApi, |
| 105 | + $authentication, |
| 106 | + ResponseInterface $response |
| 107 | + ) { |
| 108 | + $authentication->getClientId()->willReturn('client_id'); |
| 109 | + $authentication->getSecret()->willReturn('secret'); |
| 110 | + $authentication->getUsername()->willReturn('julia'); |
| 111 | + $authentication->getPassword()->willReturn('julia_pwd'); |
| 112 | + $authentication->getAccessToken()->willReturn('foo', 'foo', 'baz'); |
| 113 | + $authentication->getRefreshToken()->willReturn('bar'); |
| 114 | + |
| 115 | + $httpClient->sendRequest( |
| 116 | + 'POST', |
| 117 | + 'http://diglin.com/api/rest/v1/products/foo', |
| 118 | + ['Content-Type' => 'application/json', 'Authorization' => 'Bearer foo'], |
| 119 | + '{"identifier": "foo"}' |
| 120 | + )->willThrow(UnauthorizedHttpException::class); |
| 121 | + |
| 122 | + $authenticationApi |
| 123 | + ->authenticateByRefreshToken('client_id', 'secret', 'bar') |
| 124 | + ->willReturn([ |
| 125 | + 'access_token' => 'baz', |
| 126 | + 'expires_in' => 3600, |
| 127 | + 'token_type' => 'bearer', |
| 128 | + 'scope' => null, |
| 129 | + 'refresh_token' => 'foz', |
| 130 | + ]) |
| 131 | + ; |
| 132 | + |
| 133 | + $authentication |
| 134 | + ->setAccessToken('baz') |
| 135 | + ->shouldBeCalled() |
| 136 | + ->willReturn($authentication) |
| 137 | + ; |
| 138 | + |
| 139 | + $authentication |
| 140 | + ->setRefreshToken('foz') |
| 141 | + ->shouldBeCalled() |
| 142 | + ->willReturn($authentication) |
| 143 | + ; |
| 144 | + |
| 145 | + $httpClient->sendRequest( |
| 146 | + 'POST', |
| 147 | + 'http://diglin.com/api/rest/v1/products/foo', |
| 148 | + ['Content-Type' => 'application/json', 'Authorization' => 'Bearer baz'], |
| 149 | + '{"identifier": "foo"}' |
| 150 | + )->willReturn($response); |
| 151 | + |
| 152 | + $this->sendRequest( |
| 153 | + 'POST', |
| 154 | + 'http://diglin.com/api/rest/v1/products/foo', |
| 155 | + ['Content-Type' => 'application/json'], |
| 156 | + '{"identifier": "foo"}' |
| 157 | + ); |
| 158 | + } |
| 159 | +} |
0 commit comments