@@ 29-48 (lines=20) @@ | ||
26 | $this->beConstructedWith('<api key>', self::LOGIN_ENDPOINT, $httpClient, $messageFactory); |
|
27 | } |
|
28 | ||
29 | public function it_should_authenticate_user( |
|
30 | HttpClient $httpClient, |
|
31 | MessageFactory $messageFactory, |
|
32 | RequestInterface $tokenRequest, |
|
33 | ResponseInterface $tokenResponse, |
|
34 | StreamInterface $tokenStream |
|
35 | ) { |
|
36 | $headers = ['Content-Type' => 'application/x-www-form-urlencoded']; |
|
37 | $body = http_build_query(['api_key' => '<api key>']); |
|
38 | ||
39 | $messageFactory->createRequest('POST', self::LOGIN_ENDPOINT, $headers, $body)->willReturn($tokenRequest); |
|
40 | $httpClient->sendRequest($tokenRequest)->willReturn($tokenResponse); |
|
41 | $tokenResponse->getStatusCode()->willReturn(200); |
|
42 | $tokenResponse->getBody()->willReturn($tokenStream); |
|
43 | $tokenStream->__toString()->willReturn('{"token": "<jwt_token>"}'); |
|
44 | ||
45 | $this->auth(); |
|
46 | $this->getApiToken()->shouldReturn('<jwt_token>'); |
|
47 | $this->isAuthenticated()->shouldReturn(true); |
|
48 | } |
|
49 | ||
50 | public function it_should_throw_authentication_exception_if_api_key_is_invalid( |
|
51 | HttpClient $httpClient, |
|
@@ 50-69 (lines=20) @@ | ||
47 | $this->isAuthenticated()->shouldReturn(true); |
|
48 | } |
|
49 | ||
50 | public function it_should_throw_authentication_exception_if_api_key_is_invalid( |
|
51 | HttpClient $httpClient, |
|
52 | MessageFactory $messageFactory, |
|
53 | RequestInterface $tokenRequest, |
|
54 | ResponseInterface $tokenResponse, |
|
55 | StreamInterface $tokenStream |
|
56 | ) { |
|
57 | $headers = ['Content-Type' => 'application/x-www-form-urlencoded']; |
|
58 | $body = http_build_query(['api_key' => '<api key>']); |
|
59 | ||
60 | $messageFactory->createRequest('POST', self::LOGIN_ENDPOINT, $headers, $body)->willReturn($tokenRequest); |
|
61 | $httpClient->sendRequest($tokenRequest)->willReturn($tokenResponse); |
|
62 | $tokenResponse->getStatusCode()->willReturn(401); |
|
63 | $tokenResponse->getBody()->willReturn($tokenStream); |
|
64 | $tokenStream->__toString()->willReturn('{"message": "Bad credentials", "code": 401}'); |
|
65 | ||
66 | $this->shouldThrow(AuthenticationException::class)->during('auth'); |
|
67 | $this->getApiToken()->shouldBeNull(); |
|
68 | $this->isAuthenticated()->shouldReturn(false); |
|
69 | } |
|
70 | ||
71 | public function it_should_throw_invalid_response_exception_if_invalid_json( |
|
72 | HttpClient $httpClient, |
|
@@ 71-90 (lines=20) @@ | ||
68 | $this->isAuthenticated()->shouldReturn(false); |
|
69 | } |
|
70 | ||
71 | public function it_should_throw_invalid_response_exception_if_invalid_json( |
|
72 | HttpClient $httpClient, |
|
73 | MessageFactory $messageFactory, |
|
74 | RequestInterface $tokenRequest, |
|
75 | ResponseInterface $tokenResponse, |
|
76 | StreamInterface $tokenStream |
|
77 | ) { |
|
78 | $headers = ['Content-Type' => 'application/x-www-form-urlencoded']; |
|
79 | $body = http_build_query(['api_key' => '<api key>']); |
|
80 | ||
81 | $messageFactory->createRequest('POST', self::LOGIN_ENDPOINT, $headers, $body)->willReturn($tokenRequest); |
|
82 | $httpClient->sendRequest($tokenRequest)->willReturn($tokenResponse); |
|
83 | $tokenResponse->getStatusCode()->willReturn(200); |
|
84 | $tokenResponse->getBody()->willReturn($tokenStream); |
|
85 | $tokenStream->__toString()->willReturn('{"invalid JSON",}'); |
|
86 | ||
87 | $this->shouldThrow(InvalidResponseException::class)->during('auth'); |
|
88 | $this->getApiToken()->shouldBeNull(); |
|
89 | $this->isAuthenticated()->shouldReturn(false); |
|
90 | } |
|
91 | } |
|
92 |