|
| 1 | +<?php namespace tests; |
| 2 | + |
| 3 | +use Atomx\AccountStore; |
| 4 | +use Atomx\ApiException; |
| 5 | +use Atomx\Resources\Advertiser; |
| 6 | +use Atomx\Resources\Domain; |
| 7 | +use GuzzleHttp\Message\Response; |
| 8 | +use GuzzleHttp\Stream\Stream; |
| 9 | +use GuzzleHttp\Subscriber\History; |
| 10 | +use GuzzleHttp\Subscriber\Mock; |
| 11 | + |
| 12 | +class MemoryAccountStore implements AccountStore { |
| 13 | + private $token = 'TEST_TOKEN'; |
| 14 | + |
| 15 | + public function getToken() { return $this->token; } |
| 16 | + public function storeToken($token) { $this->token = $token; } |
| 17 | + public function getUsername() { return ''; } |
| 18 | + public function getPassword() { return ''; } |
| 19 | + public function getApiBase() { return 'https://api.atomx.com/v3/'; } |
| 20 | +} |
| 21 | + |
| 22 | +class ClientTest extends \PHPUnit_Framework_TestCase { |
| 23 | + public function testDiscardInvalidToken() |
| 24 | + { |
| 25 | + $store = new MemoryAccountStore(); |
| 26 | + $advertiser = new Advertiser($store); |
| 27 | + |
| 28 | + $mock = new Mock([ |
| 29 | + "HTTP/1.1 401 Unknown Error\r\nContent-Length: 55\r\n\r\n{\"success\":false,\"error\":\"User unauthorized\",\"errno\":1}" |
| 30 | + ]); |
| 31 | + |
| 32 | + $advertiser->getClient()->getEmitter()->attach($mock); |
| 33 | + |
| 34 | + try { |
| 35 | + $advertiser->get(['limit' => 1, 'depth' => 0]); |
| 36 | + } catch (ApiException $e) {} |
| 37 | + |
| 38 | + $this->assertNull($store->getToken()); |
| 39 | + } |
| 40 | + |
| 41 | + public function testRequestWithoutLogin() |
| 42 | + { |
| 43 | + $store = new MemoryAccountStore(); |
| 44 | + $domain = new Domain($store); |
| 45 | + |
| 46 | + $history = new History(); |
| 47 | + $mock = new Mock([ $this->getValidEmptyResponse() ]); |
| 48 | + |
| 49 | + $domain->getClient()->getEmitter()->attach($mock); |
| 50 | + $domain->getClient()->getEmitter()->attach($history); |
| 51 | + |
| 52 | + $domain->get(['limit' => 1, 'depth' => 0]); |
| 53 | + |
| 54 | + $this->assertArrayNotHasKey('Authorization', $history->getLastRequest()->getHeaders()); |
| 55 | + } |
| 56 | + |
| 57 | + public function testRequestWithAuthToken() |
| 58 | + { |
| 59 | + $store = new MemoryAccountStore(); |
| 60 | + $advertiser = new Advertiser($store); |
| 61 | + |
| 62 | + $history = new History(); |
| 63 | + $mock = new Mock([ $this->getValidEmptyResponse() ]); |
| 64 | + |
| 65 | + $advertiser->getClient()->getEmitter()->attach($mock); |
| 66 | + $advertiser->getClient()->getEmitter()->attach($history); |
| 67 | + |
| 68 | + $advertiser->get(['limit' => 1, 'depth' => 0]); |
| 69 | + |
| 70 | + $this->assertArraySubset(['Authorization' => ['Bearer TEST_TOKEN']], $history->getLastRequest()->getHeaders()); |
| 71 | + } |
| 72 | + |
| 73 | + public function testLogin() |
| 74 | + { |
| 75 | + $store = new MemoryAccountStore(); |
| 76 | + $store->storeToken(null); |
| 77 | + |
| 78 | + $advertiser = new Advertiser($store); |
| 79 | + |
| 80 | + $history = new History(); |
| 81 | + $mock = new Mock([ |
| 82 | + $this->getValidLoginResponse(), |
| 83 | + $this->getValidEmptyResponse() |
| 84 | + ]); |
| 85 | + |
| 86 | + $advertiser->getClient()->getEmitter()->attach($mock); |
| 87 | + $advertiser->getClient()->getEmitter()->attach($history); |
| 88 | + |
| 89 | + $advertiser->get(['limit' => 1, 'depth' => 0]); |
| 90 | + |
| 91 | + |
| 92 | + $this->assertEquals(2, count($history)); |
| 93 | + $loginRequest = $history->getIterator()[0]['request']; |
| 94 | + $this->assertArrayNotHasKey('Authorization', $loginRequest->getHeaders()); |
| 95 | + $this->assertArraySubset(['Authorization' => ['Bearer LOGIN_TOKEN']], $history->getLastRequest()->getHeaders()); |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + private function getValidEmptyResponse() |
| 100 | + { |
| 101 | + return new Response(200, [], Stream::factory("[]")); |
| 102 | + } |
| 103 | + |
| 104 | + private function getValidLoginResponse() |
| 105 | + { |
| 106 | + $loginBody = '{"user":{"id":1},"resource":"auth_token","totp_required":false,' . |
| 107 | + '"message":"atomx (user id 1) logged in","success":true,"auth_token":"LOGIN_TOKEN"}'; |
| 108 | + |
| 109 | + return new Response(200, |
| 110 | + ['Content-Type' => 'application/json; charset=UTF-8'], |
| 111 | + Stream::factory($loginBody) |
| 112 | + ); |
| 113 | + } |
| 114 | +} |
0 commit comments