diff --git a/.gitignore b/.gitignore index 57872d0..337a38d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /vendor/ +/.idea +composer.lock diff --git a/composer.json b/composer.json index 9d98427..3a1a50d 100644 --- a/composer.json +++ b/composer.json @@ -12,19 +12,25 @@ "require": { "php": ">=5.5.0", "guzzlehttp/guzzle": "~5.3|~6.0|~6.2", - "illuminate/support": "~5.0|~5.1|~5.2" + "illuminate/support": "~5.0|~5.1|~5.2", + "nesbot/carbon": "^1.21" }, "require-dev": { "fzaninotto/faker": "~1.4", "phpunit/phpunit": "~4.0", - "mockery/mockery": "0.9.*" + "mockery/mockery": "0.9.*", + "symfony/var-dumper": "^3.1" }, "autoload": { - "classmap": [ - "tests/TestCase.php" - ], "psr-4": { "FindBrok\\WatsonBridge\\" : "src/" } - } + }, + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "minimum-stability": "dev", + "prefer-stable": true } diff --git a/src/Bridge.php b/src/Bridge.php index 56d8099..cabf86b 100644 --- a/src/Bridge.php +++ b/src/Bridge.php @@ -41,6 +41,28 @@ class Bridge */ protected $client; + /** + * The WatsonToken + * + * @var \FindBrok\WatsonBridge\Token + */ + protected $token; + + /** + * Decide which method to use when sending request + * + * @var string + */ + protected $authMethod = 'credentials'; + + /** + * The limit for which we can re request token, + * when performing request + * + * @var int + */ + protected $exceptionThrottle = 0; + /** * Default headers * @@ -58,14 +80,18 @@ class Bridge * @param string $password * @param string $endpoint */ - public function __construct($username = null, $password = null, $endpoint = null) + public function __construct($username, $password, $endpoint) { //Set Username, Password and Endpoint $this->username = $username; $this->password = $password; $this->endpoint = $endpoint; + //Set HttpClient - $this->setClient(); + $this->setClient($endpoint); + + //Set Token + $this->token = new Token($this->username); } /** @@ -107,16 +133,73 @@ public function getHeaders() return $this->headers; } + /** + * Fetch token from Watson and Save it locally + * + * @param bool $incrementThrottle + * @return void + */ + public function fetchToken($incrementThrottle = false) + { + //Increment throttle if needed + if($incrementThrottle) { + $this->incrementThrottle(); + } + //Reset Client + $this->setClient($this->getAuthorizationEndpoint()); + //Get the token response + $response = $this->get('v1/token', [ + 'url' => $this->endpoint + ]); + //Extract + $token = json_decode($response->getBody()->getContents(), true); + //Reset client + $this->setClient($this->endpoint); + //Update token + $this->token->updateToken($token['token']); + } + + /** + * Get a token for authorization from Watson or Storage + * + * @return string + */ + public function getToken() + { + //Token is not valid + if (! $this->token->isValid()) { + //Fetch from Watson + $this->fetchToken(); + } + + //Return token + return $this->token->getToken(); + } + + /** + * Get the authorization endpoint for getting tokens + * + * @return string + */ + public function getAuthorizationEndpoint() + { + //Parse the endpoint + $parsedEndpoint = collect(parse_url($this->endpoint)); + //Return auth url + return $parsedEndpoint->get('scheme').'://'.$parsedEndpoint->get('host').'/authorization/api/'; + } + /** * Creates the http client * + * @param string $endpoint * @return void */ - private function setClient() + public function setClient($endpoint = null) { //Create client using API endpoint $this->client = new Client([ - 'base_uri' => $this->endpoint, + 'base_uri' => ! is_null($endpoint) ? $endpoint : $this->endpoint, ]); } @@ -164,7 +247,7 @@ public function failedRequest($response) } /** - * Make a Request to Watson + * Make a Request to Watson with credentials Auth * * @param string $method * @param string $uri @@ -175,8 +258,17 @@ public function request($method = 'GET', $uri = '', $options = []) { try { //Make the request - return $this->getClient()->request($method, $uri, $options); + return $this->getClient()->request($method, $uri, $this->getRequestOptions($options)); } catch (ClientException $e) { + //We are using token auth and probably token expired + if($this->authMethod == 'token' && $e->getCode() == 401 && ! $this->isThrottledReached()) { + //Try refresh token + $this->fetchToken(true); + //Try requesting again + return $this->request($method, $uri, $options); + } + //Clear throttle for this request + $this->clearThrottle(); //Call Failed Request $this->failedRequest($e->getResponse()); } @@ -193,12 +285,8 @@ public function request($method = 'GET', $uri = '', $options = []) */ private function send($method = 'POST', $uri, $data, $type = 'json') { - //Make a Post Request - $response = $this->request($method, $uri, $this->cleanOptions([ - 'headers' => $this->getHeaders(), - 'auth' => $this->getAuth(), - $type => $data - ])); + //Make the Request to Watson + $response = $this->request($method, $uri, [$type => $data]); //Request Failed if ($response->getStatusCode() != 200) { //Throw Watson Bridge Exception @@ -208,6 +296,78 @@ private function send($method = 'POST', $uri, $data, $type = 'json') return $response; } + /** + * Get Request options to pass along + * + * @param array $initial + * @return array + */ + public function getRequestOptions($initial = []) + { + //Define options + $options = collect($initial); + //Define an auth option + if($this->authMethod == 'credentials') { + $options = $options->merge([ + 'auth' => $this->getAuth(), + ]); + } elseif ($this->authMethod == 'token') { + $this->appendHeaders([ + 'X-Watson-Authorization-Token' => $this->getToken() + ]); + } + //Put Headers in options + $options = $options->merge([ + 'headers' => $this->getHeaders(), + ]); + //Clean and return + return $this->cleanOptions($options->all()); + } + + /** + * Change the auth method + * + * @param string $method + * @return self + */ + public function useAuthMethodAs($method = 'credentials') + { + //Change auth method + $this->authMethod = $method; + //Return object + return $this; + } + + /** + * Checks if throttle is reached + * + * @return bool + */ + public function isThrottledReached() + { + return $this->exceptionThrottle >= 2; + } + + /** + * Increment throttle + * + * @return void + */ + public function incrementThrottle() + { + $this->exceptionThrottle++; + } + + /** + * Clears throttle counter + * + * @return void + */ + public function clearThrottle() + { + $this->exceptionThrottle = 0; + } + /** * Make a GET Request to Watson * diff --git a/src/Storage/.gitignore b/src/Storage/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/src/Storage/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/src/Token.php b/src/Token.php new file mode 100644 index 0000000..bbfb492 --- /dev/null +++ b/src/Token.php @@ -0,0 +1,175 @@ +username = $username; + //Have payload to set + if(! empty($payLoad)) { + $this->payLoad = $payLoad; + } else { + //Load from file + $this->payLoad = $this->loadPayLoadFromFile(); + } + } + + /** + * Check if token is loaded in class + * + * @return bool + */ + public function hasPayLoad() + { + return ! empty($this->payLoad); + } + + /** + * Check that token file exists + * + * @return bool + */ + public function exists() + { + return file_exists(__DIR__.'/Storage/'.'token-'.$this->username.'.json'); + } + + /** + * Check that token is expired + * + * @return bool + */ + public function isExpired() + { + return ($this->hasPayLoad() && ($this->payLoad['created'] + $this->payLoad['expires_in']) < Carbon::now()->format('U')); + } + + /** + * Check that the token is not expired + * + * @return bool + */ + public function isNotExpired() + { + return ! $this->isExpired(); + } + + /** + * Check if token is valid + * + * @return bool + */ + public function isValid() + { + return $this->exists() && $this->isNotExpired(); + } + + /** + * Saves a token + * + * @return bool + */ + public function save() + { + //No payload to save + if(! $this->hasPayLoad()) { + return false; + } + //Save the token + return (bool) file_put_contents($this->getFilePath(), collect($this->payLoad)->toJson(), LOCK_EX); + } + + /** + * Get the token file path + * + * @return string + */ + public function getFilePath() + { + return __DIR__.'/Storage/token-'.$this->username.'.json'; + } + + /** + * Load payload from file + * + * @return array + */ + public function loadPayLoadFromFile() + { + //Not found + if(! $this->exists()) { + //We return empty array + return []; + } + //Load content from file + return json_decode(file_get_contents($this->getFilePath()), true); + } + + /** + * Get the payload + * + * @return array + */ + public function getPayLoad() + { + return $this->payLoad; + } + + /** + * Get the token + * + * @return string|null + */ + public function getToken() + { + return collect($this->payLoad)->get('token'); + } + + /** + * Update the token + * + * @param string $token + * @return bool + */ + public function updateToken($token) + { + //Update Payload + $this->payLoad = [ + 'token' => $token, + 'expires_in' => 3600, + 'created' => Carbon::now()->format('U'), + ]; + //Save token + return $this->save(); + } +} diff --git a/tests/TestBridge.php b/tests/TestBridge.php new file mode 100644 index 0000000..64cb5e3 --- /dev/null +++ b/tests/TestBridge.php @@ -0,0 +1,331 @@ +bridge = $this->getMockBuilder('FindBrok\WatsonBridge\Bridge') + ->disableOriginalConstructor() + ->setMethods(['getClient']) + ->getMock(); + + $this->createTestTokenFile('token-foo', [ + 'token' => 'someToken', + 'expires_in' => 3600, + 'created' => Carbon::now()->format('U') + ]); + + $reflected = new ReflectionClass(Bridge::class); + $constructor = $reflected->getConstructor(); + $constructor->invoke($this->bridge, 'foo', 'password', 'url'); + } + + /** + * TearDown Test + */ + public function tearDown() + { + unset($this->bridge); + $this->deleteTestTokenFile('token-foo'); + } + + /** + * Return Token Storage Folder + * + * @param string $file + * @return string + */ + public function getTokenStoragePath($file = '') + { + return __DIR__.'/../src/Storage/'.$file; + } + + /** + * Get response body for a token + * + * @return string + */ + public function getTokenResponseBody() + { + return file_get_contents(__DIR__.'/fixtures/raw-token.json'); + } + + /** + * Creates a test token file + * + * @param string $name + * @param array $data + * @return void + */ + public function createTestTokenFile($name = '', $data = []) + { + file_put_contents( + $this->getTokenStoragePath($name.'.json'), + collect($data)->toJson(), + LOCK_EX + ); + } + + /** + * Delete a test token file + * + * @param string $name + * @return void + */ + public function deleteTestTokenFile($name = '') + { + unlink($this->getTokenStoragePath($name.'.json')); + } + + /** + * Test that we are able to create bridge object + * + * @return voids + */ + public function testBridgeObjectCanBeConstructed() + { + $bridge = new Bridge('username', 'password', 'endpoint'); + $this->assertInstanceOf(Bridge::class, $bridge); + } + + /** + * Test that we get the correct Auth endpoint for getting token + * + * @return void + */ + public function testGetAuthEndpointMethodCorrectEndpointReturned() + { + $bridge = new Bridge('username', 'password', 'https://gateway.watsonplatform.net/service/api/'); + $this->assertEquals('https://gateway.watsonplatform.net/authorization/api/', $bridge->getAuthorizationEndpoint()); + + $bridge2 = new Bridge('username', 'password', 'https://stream.watsonplatform.net/service/api/'); + $this->assertEquals('https://stream.watsonplatform.net/authorization/api/', $bridge2->getAuthorizationEndpoint()); + } + + /** + * Test that we can set and reset the client correctly + * + * @return void + */ + public function testSetClientMethodClientCorrectlySet() + { + $bridge = new Bridge('username', 'password', 'https://gateway.watsonplatform.net/service/api/'); + $this->assertInstanceOf(Client::class, $bridge->getClient()); + + $client = new Client([ + 'base_uri' => 'https://gateway.watsonplatform.net/service/api/' + ]); + $this->assertEquals($client->getConfig('base_uri'), $bridge->getClient()->getConfig('base_uri')); + + $bridge->setClient($bridge->getAuthorizationEndpoint()); + $this->assertNotEquals($client->getConfig('base_uri'), $bridge->getClient()->getConfig('base_uri')); + } + + /** + * Test that the getRequestOptions method works correctly + * + * @return void + */ + public function testGetRequestOptionsMethodWillReturnCorrectOptions() + { + // Create a mock and queue one response + $mock = new MockHandler([ + new Response(200, ['X-Foo' => 'Bar'], $this->getTokenResponseBody()), + ]); + $handler = HandlerStack::create($mock); + $client = new Client(['handler' => $handler]); + $this->bridge->method('getClient')->willReturn($client); + + $data = [ + 'foo' => 'bar', + ]; + + $this->assertEquals([ + 'json' => ['foo' => 'bar'], + 'auth' => ['foo', 'password'], + 'headers' => [ + 'Accept' => 'application/json', + 'X-Watson-Learning-Opt-Out' => false + ] + ], $this->bridge->useAuthMethodAs('credentials')->getRequestOptions(['json' => $data])); + + $this->assertEquals([ + 'json' => ['foo' => 'bar'], + 'headers' => [ + 'Accept' => 'application/json', + 'X-Watson-Learning-Opt-Out' => false, + 'X-Watson-Authorization-Token' => 'someToken' + ] + ], $this->bridge->useAuthMethodAs('token')->getRequestOptions(['json' => $data])); + } + + /** + * Test a Successful Get request + * + * @return void + */ + public function testGetRequestResponseOk() + { + // Create a mock and queue one response + $mock = new MockHandler([ + new Response(200, ['X-Foo' => 'Bar']), + ]); + $handler = HandlerStack::create($mock); + $client = new Client(['handler' => $handler]); + + $this->bridge->method('getClient')->willReturn($client); + + $this->assertEquals(200, $this->bridge->get('version/watson-api-method', ['foo' => 'bar'])->getStatusCode()); + } + + /** + * Test that the getToken method works + * + * @return void + */ + public function testGetTokenMethodWhenTokenNotValidAndFetchedFromWatsonWithOkToken() + { + // Create a mock and queue one response + $mock = new MockHandler([ + new Response(200, ['X-Foo' => 'Bar'], $this->getTokenResponseBody()), + ]); + $handler = HandlerStack::create($mock); + $client = new Client(['handler' => $handler]); + $this->bridge->method('getClient')->willReturn($client); + + $reflected = new ReflectionClass(Bridge::class); + $constructor = $reflected->getConstructor(); + $constructor->invoke($this->bridge, 'username', 'password', 'url'); + + $this->assertEquals('someToken', $this->bridge->getToken()); + } + + /** + * Test that the getToken method works when token is saved + * + * @return void + */ + public function testGetTokenMethodWhenTokenIsAlreadyInCache() + { + $this->assertEquals('someToken', $this->bridge->getToken()); + } + + /** + * Test the getToken method with an expired token, we fetch the token from + * Watson again + * + * @return void + */ + public function testGetTokenMethodWhenTokenExpiredAndFetchTokenAgain() + { + $this->createTestTokenFile('token-foofoo', [ + 'token' => 'oldToken', + 'expires_in' => 3600, + 'created' => 1463977413 + ]); + + // Create a mock and queue one response + $mock = new MockHandler([ + new Response(200, ['X-Foo' => 'Bar'], $this->getTokenResponseBody()), + ]); + $handler = HandlerStack::create($mock); + $client = new Client(['handler' => $handler]); + $this->bridge->method('getClient')->willReturn($client); + + $reflected = new ReflectionClass(Bridge::class); + $constructor = $reflected->getConstructor(); + $constructor->invoke($this->bridge, 'foofoo', 'password', 'url'); + + $this->assertEquals('someToken', $this->bridge->getToken()); + + $this->deleteTestTokenFile('token-foofoo'); + } + + /** + * Test a Get request which fails + * + * @expectedException \FindBrok\WatsonBridge\Exceptions\WatsonBridgeException + * + public function testGetRequestWithException() + { + // Create a mock and queue one response + $mock = new MockHandler([ + new ClientException( + 'Watson Error', + new Request('GET', 'version/watson-api-method'), + new Response(400, ['X-Foo' => 'Bar'], collect(['error_code' => 400, 'error_message' => 'Watson Error'])->toJson()) + ) + ]); + $handler = HandlerStack::create($mock); + $client = new Client(['handler' => $handler]); + $this->bridge->method('getClient')->willReturn($client); + + $this->bridge->get('version/watson-api-method', ['foo' => 'bar']); + }*/ + + /** + * Test that when the token is expired we refresh the token and try again + * + * @return void + */ + public function testTokenExpiredWhenMakingRequestWeRefreshTokenAndTryAgain() + { + $this->createTestTokenFile('token-foobar', [ + 'token' => 'oldToken', + 'expires_in' => 3600, + 'created' => Carbon::now()->format('U') + ]); + + $expectedResponseBody = collect(['someData' => 'data'])->toJson(); + + // Create a mock and queue responses + $mock = new MockHandler([ + new ClientException( + 'Watson Error', + new Request('GET', 'version/watson-api-method'), + new Response(401, ['X-Foo' => 'Bar'], collect(['error_code' => 401, 'error_message' => 'unauthorized access'])->toJson()) + ), + new Response(200, ['X-Foo' => 'Bar'], $this->getTokenResponseBody()), + new Response(200, ['X-Foo' => 'Bar'], $expectedResponseBody), + ]); + $handler = HandlerStack::create($mock); + $client = new Client(['handler' => $handler]); + $this->bridge->method('getClient')->willReturn($client); + + $reflected = new ReflectionClass(Bridge::class); + $constructor = $reflected->getConstructor(); + $constructor->invoke($this->bridge, 'foobar', 'password', 'url'); + + $this->bridge->useAuthMethodAs('token'); + $this->assertEquals('oldToken', $this->bridge->getToken()); + + $this->assertEquals( + $expectedResponseBody, + $this->bridge->useAuthMethodAs('token')->get('version/watson-api-method')->getBody()->getContents() + ); + + $this->deleteTestTokenFile('token-foobar'); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php deleted file mode 100644 index bf83f70..0000000 --- a/tests/TestCase.php +++ /dev/null @@ -1,74 +0,0 @@ -bridge = $this->getMockBuilder('FindBrok\WatsonBridge\Bridge') - ->disableOriginalConstructor() - ->setMethods(['getClient']) - ->getMock(); - } - - /** - * Test a Successful Get request - */ - public function testGetRequestResponseOk() - { - // Create a mock and queue one response - $mock = new MockHandler([ - new Response(200, ['X-Foo' => 'Bar']), - ]); - $handler = HandlerStack::create($mock); - $client = new Client(['handler' => $handler]); - - $this->bridge->method('getClient')->willReturn($client); - - $this->assertEquals(200, $this->bridge->get('version/watson-api-method', ['foo' => 'bar'])->getStatusCode()); - } - - /** - * Test a Get request which fails - * - * @expectedException \FindBrok\WatsonBridge\Exceptions\WatsonBridgeException - */ - public function testGetRequestWithException() - { - // Create a mock and queue one response - $mock = new MockHandler([ - new ClientException( - 'Watson Error', - new Request('GET', 'version/watson-api-method'), - new Response(400, ['X-Foo' => 'Bar'], collect(['error_code' => 400, 'error_message' => 'Watson Error'])->toJson()) - ) - ]); - $handler = HandlerStack::create($mock); - $client = new Client(['handler' => $handler]); - - $this->bridge->method('getClient')->willReturn($client); - - $this->bridge->get('version/watson-api-method', ['foo' => 'bar']); - - $this->setExpectedException('\FindBrok\WatsonBridge\Exceptions\WatsonBridgeException'); - } -} diff --git a/tests/TestToken.php b/tests/TestToken.php new file mode 100644 index 0000000..f93baea --- /dev/null +++ b/tests/TestToken.php @@ -0,0 +1,239 @@ +token = new Token('username'); + + file_put_contents($this->getTokenStoragePath('token-username.json'), collect([ + 'token' => 'sometoken', + 'expires_in' => 3600, + 'created' => Carbon::now()->format('U') + ])->toJson(), LOCK_EX); + } + + /** + * Tear down test + */ + public function tearDown() + { + unset($this->token); + + unlink($this->getTokenStoragePath('token-username.json')); + } + + /** + * Return Token Storage Folder + * + * @param string $file + * @return string + */ + public function getTokenStoragePath($file = '') + { + return __DIR__.'/../src/Storage/'.$file; + } + + /** + * Test that we can create the token object + * + * @return void + */ + public function testTokenObjectCanBeConstructed() + { + $token = new Token('username'); + $this->assertInstanceOf(Token::class, $token); + } + + /** + * Test that the method hasPayload works + * + * @return void + */ + public function testHasPayLoadMethod() + { + $token = new Token('username', [ + 'token' => 'sometoken', + 'expires_in' => 3600, + 'created' => Carbon::now()->format('U') + ]); + $this->assertTrue($token->hasPayLoad()); + + $token2 = new Token('username2'); + $this->assertFalse($token2->hasPayLoad()); + } + + /** + * Test that the method isExpired works + * + * @return void + */ + public function testIsExpiredAndIsNotExpiredMethod() + { + $token = new Token('username', [ + 'token' => 'sometoken', + 'expires_in' => '3600', + 'created' => Carbon::now()->format('U') + ]); + $this->assertTrue($token->isNotExpired()); + + $token2 = new Token('username2', [ + 'token' => 'sometoken', + 'expires_in' => 3600, + 'created' => Carbon::createFromFormat('Y-m-d H:i:s', '2016-06-02 00:00:00')->format('U') + ]); + $this->assertTrue($token2->isExpired()); + } + + /** + * Test that the getFilePath method works + * + * @return void + */ + public function testGetFilePathMethod() + { + $this->assertEquals( + realpath($this->getTokenStoragePath('token-username.json')), + realpath($this->token->getFilePath()) + ); + } + + /** + * Test that the exists method works + * + * @return void + */ + public function testExistsMethod() + { + $this->assertTrue($this->token->exists()); + $token2 = new Token('username2'); + $this->assertFalse($token2->exists()); + } + + /** + * Test the save method to see if it works + * + * @return void + */ + public function testSaveMethod() + { + $payload = [ + 'token' => 'sometoken', + 'expires_in' => 3600, + 'created' => Carbon::now()->format('U') + ]; + $token = new Token('username2', $payload); + + $this->assertTrue($token->save()); + $this->assertFileExists($this->getTokenStoragePath('token-username2.json')); + $this->assertJsonStringEqualsJsonFile($this->getTokenStoragePath('token-username2.json'), collect($payload)->toJson()); + + unlink($this->getTokenStoragePath('token-username2.json')); + } + + /** + * Test that we can load a token from file and get its payload + * + * @return void + */ + public function testLoadFromFileMethodAndGetPayLoadMethod() + { + file_put_contents($this->getTokenStoragePath('token-username3.json'), collect([ + 'token' => 'sometoken', + 'expires_in' => 3600, + 'created' => 1463977413 + ])->toJson(), LOCK_EX); + + $token = new Token('username3'); + $this->assertEquals([ + 'token' => 'sometoken', + 'expires_in' => 3600, + 'created' => 1463977413 + ], $token->getPayload()); + + unlink($this->getTokenStoragePath('token-username3.json')); + } + + /** + * Test to see if the isValid method works + * + * @return void + */ + public function testIsValidMethod() + { + $this->assertTrue($this->token->isValid()); + $token2 = new Token('username2'); + $this->assertFalse($token2->isValid()); + + file_put_contents($this->getTokenStoragePath('token-username3.json'), collect([ + 'token' => 'sometoken', + 'expires_in' => 3600, + 'created' => 1463977413 + ])->toJson(), LOCK_EX); + + $token3 = new Token('username3'); + $this->assertFalse($token3->isValid()); + + unlink($this->getTokenStoragePath('token-username3.json')); + } + + /** + * Test the getToken method to see if it works + * + * @return void + */ + public function testGetTokenMethod() + { + file_put_contents($this->getTokenStoragePath('token-username3.json'), collect([ + 'token' => 'sometoken', + 'expires_in' => 3600, + 'created' => Carbon::now()->format('U') + ])->toJson(), LOCK_EX); + + $token3 = new Token('username3'); + $this->assertEquals('sometoken', $token3->getToken()); + + $token2 = new Token('username2'); + $this->assertNull($token2->getToken()); + + unlink($this->getTokenStoragePath('token-username3.json')); + } + + /** + * Test to see if the Update token method works + * + * @return void + */ + public function testUpdateTokenMethod() + { + $payload = [ + 'token' => 'sometoken', + 'expires_in' => 3600, + 'created' => Carbon::now()->format('U') + ]; + $token = new Token('username3', $payload); + $this->assertEquals('sometoken', $token->getToken()); + + $this->assertTrue($token->updateToken('newToken')); + $this->assertEquals('newToken', $token->getToken()); + $this->assertFileExists($this->getTokenStoragePath('token-username3.json')); + + unlink($this->getTokenStoragePath('token-username3.json')); + } +} diff --git a/tests/fixtures/raw-token.json b/tests/fixtures/raw-token.json new file mode 100644 index 0000000..7507070 --- /dev/null +++ b/tests/fixtures/raw-token.json @@ -0,0 +1,3 @@ +{ + "token": "someToken" +} diff --git a/tests/fixtures/token.json b/tests/fixtures/token.json new file mode 100644 index 0000000..e21943c --- /dev/null +++ b/tests/fixtures/token.json @@ -0,0 +1,5 @@ +{ + "token": "someToken", + "expires_in": 3600, + "created": 1463977413 +}