From 8ea9ced8184eee77c19b5d1332b7de5888534a8a Mon Sep 17 00:00:00 2001 From: Pe Ell Date: Sun, 21 May 2017 20:18:36 +0300 Subject: [PATCH] Add Authenticators (#19) * Remove authentication responsibility from authorizers * Updated Response interface naming * Add isStatusCode assert to Response --- CHANGELOG.md | 19 ++++- README.md | 29 ++++--- examples/cookie-authorization.php | 46 ++++++++++ ...oject-list.php => token-authorization.php} | 6 +- src/Authenticator/Contracts/Authenticator.php | 32 +++++++ src/Authenticator/CookieAuthenticator.php | 85 +++++++++++++++++++ .../Exceptions/AuthenticationException.php | 6 +- src/Authorizer/Contracts/Authorizer.php | 9 +- src/Authorizer/CookieAuthorizer.php | 57 ++----------- src/Authorizer/TokenAuthorizer.php | 33 ++----- src/Client/Contracts/Client.php | 10 ++- src/Client/YouTrackClient.php | 47 ++++++++-- src/Response/Contracts/Response.php | 22 +++-- src/Response/YouTrackResponse.php | 25 ++++-- .../Authenticator/CookieAuthorizerTest.php | 18 ++-- .../Authenticator/TokenAuthorizerTest.php | 6 +- 16 files changed, 310 insertions(+), 140 deletions(-) create mode 100644 examples/cookie-authorization.php rename examples/{project-list.php => token-authorization.php} (93%) create mode 100644 src/Authenticator/Contracts/Authenticator.php create mode 100644 src/Authenticator/CookieAuthenticator.php rename src/{Authorizer => Authenticator}/Exceptions/AuthenticationException.php (68%) diff --git a/CHANGELOG.md b/CHANGELOG.md index f518ae7..55fbea0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,12 +2,29 @@ All notable changes to `youtrack-rest-php` will be documented in this file. +## [3.0.0] - 2017-05-22 + +### Added + +- `Authenticator` contract and `CookieAuthenticator` implementation. +- `isStatusCode` assert in `Response` contract. + +### Updated + +- `CookieAuthorizer` constructor accepts `Authenticator` instead of credentials. +- `TokenAuthorizer` constructor accepts string token instead of array. +- `Authorizer` delegates authentication to `Authenticator`. +- Changed namespace of `AuthenticationException`. +- `getHeaders` method was dropped from `Authorizer` contract. +- `Response` interface methods `getResponse`, `getStatusCode`, `getCookie`, `getLocation` were renamed to `httpResponse`, `statusCode`, `cookie`, `location` respectively. + ## [2.0.1] - 2017-05-21 - Dropped Client `getAuthorizer` & `setAuthorizer` rudiment methods. ## 1.0.0 - 2017-05-12 -- Initial release +- Initial release. +[3.0.0]: https://github.com/cybercog/youtrack-rest-php/compare/2.0.1...3.0.0 [2.0.1]: https://github.com/cybercog/youtrack-rest-php/compare/1.0.0...2.0.1 \ No newline at end of file diff --git a/README.md b/README.md index 60fd91e..3e06d34 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ Part of the [YouTrack PHP SDK](https://github.com/cybercog/youtrack-php-sdk#read - Using contracts to keep high customization capabilities. - Multiple authorization strategies: Token, Cookie. - Following PHP Standard Recommendations: + - [PSR-1 (Basic Coding Standard)](http://www.php-fig.org/psr/psr-1/). - [PSR-2 (Coding Style Guide)](http://www.php-fig.org/psr/psr-2/). - [PSR-4 (Autoloading Standard)](http://www.php-fig.org/psr/psr-4/). - [PSR-7 (HTTP Message Interface)](http://www.php-fig.org/psr/psr-7/). @@ -98,29 +99,33 @@ require_once '/path/to/your-project/vendor/autoload.php'; Starting with YouTrack 2017.1 release [authorization based on permanent tokens](https://www.jetbrains.com/help/youtrack/standalone/2017.2/Manage-Permanent-Token.html) is recommended as the main approach for the authorization in your REST API calls. ```php +// Instantiate HTTP Client $http = new \GuzzleHttp\Client([ 'base_uri' => 'https://example.com', ]); -$authorizer = new \Cog\YouTrack\Rest\Authorizer\TokenAuthorizer([ - 'token' => 'YOUTRACK_API_TOKEN', -]); +// Instantiate YouTrack API Token Authorizer +$authorizer = new \Cog\YouTrack\Rest\Authorizer\TokenAuthorizer('YOUTRACK_API_TOKEN'); +// Instantiate YouTrack API Client $youtrack = new \Cog\YouTrack\Rest\YouTrackClient($http, $authorizer); ``` #### Cookie Authorizer ```php +// Instantiate HTTP Client $http = new \GuzzleHttp\Client([ 'base_uri' => 'https://example.com', ]); -$authorizer = new \Cog\YouTrack\Rest\Authorizer\CookieAuthorizer([ - 'username' => 'YOUTRACK_USERNAME', - 'password' => 'YOUTRACK_PASSWORD', -]); +// Instantiate YouTrack API Cookie Authenticator +$authenticator = new \Cog\YouTrack\Rest\Authenticator\CookieAuthenticator('YOUTRACK_USERNAME', 'YOUTRACK_PASSWORD'); + +// Instantiate YouTrack API Cookie Authorizer +$authorizer = new \Cog\YouTrack\Rest\Authorizer\CookieAuthorizer($authenticator); +// Instantiate YouTrack API Client $youtrack = new \Cog\YouTrack\Rest\YouTrackClient($http, $authorizer); ``` @@ -164,25 +169,25 @@ Each successful request to the API returns instance of `\Cog\YouTrack\Rest\Respo #### Get PSR HTTP response -PSR HTTP response could be accessed by calling `getResponse` method on API Response. +PSR HTTP response could be accessed by calling `httpResponse` method on API Response. ```php $youtrackResponse = $youtrack->get('/issue/TEST-1'); -$psrResponse = $youtrackResponse->getResponse(); +$psrResponse = $youtrackResponse->httpResponse(); ``` #### Get response Cookie ```php $apiResponse = $youtrack->get('/issue/TEST-1'); -$cookieString = $apiResponse->getCookie(); +$cookieString = $apiResponse->cookie(); ``` #### Get response Location ```php $apiResponse = $youtrack->get('/issue/TEST-1'); -$location = $apiResponse->getLocation(); +$location = $apiResponse->location(); ``` #### Transform response to array @@ -196,7 +201,7 @@ $location = $apiResponse->toArray(); ```php $apiResponse = $youtrack->get('/issue/TEST-1'); -$location = $apiResponse->getStatusCode(); +$location = $apiResponse->statusCode(); ``` ## Change log diff --git a/examples/cookie-authorization.php b/examples/cookie-authorization.php new file mode 100644 index 0000000..9ab60f0 --- /dev/null +++ b/examples/cookie-authorization.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +// Boot third party libraries +require_once __DIR__ . '/../vendor/autoload.php'; + +// Application configuration (replace with your YouTrack server values) +$apiBaseUri = 'https://write-youtrack-domain.here'; +$apiUsername = 'YOUR_USERNAME'; +$apiPassword = 'YOUR_PASSWORD'; + +// Instantiate HTTP Client +$http = new \GuzzleHttp\Client([ + 'base_uri' => $apiBaseUri, +]); + +// Instantiate YouTrack API Cookie Authenticator +$authenticator = new \Cog\YouTrack\Rest\Authenticator\CookieAuthenticator($apiUsername, $apiPassword); + +// Instantiate YouTrack API Cookie Authorizer +$authorizer = new \Cog\YouTrack\Rest\Authorizer\CookieAuthorizer($authenticator); + +// Instantiate YouTrack API Client +$client = new \Cog\YouTrack\Rest\Client\YouTrackClient($http, $authorizer); + +// Do request to the API +$response = $client->get('/admin/project'); + +// Convert response to array +$projects = $response->toArray(); + +// Render projects one by one +echo 'Project list:'; +foreach ($projects as $project) { + echo ' #' . $project['id']; +} diff --git a/examples/project-list.php b/examples/token-authorization.php similarity index 93% rename from examples/project-list.php rename to examples/token-authorization.php index 5205cdc..7d9e987 100644 --- a/examples/project-list.php +++ b/examples/token-authorization.php @@ -23,10 +23,8 @@ 'base_uri' => $apiBaseUri, ]); -// Instantiate YouTrack API Authorizer -$authorizer = new \Cog\YouTrack\Rest\Authorizer\TokenAuthorizer([ - 'token' => $apiAuthToken, -]); +// Instantiate YouTrack API Token Authorizer +$authorizer = new \Cog\YouTrack\Rest\Authorizer\TokenAuthorizer($apiAuthToken); // Instantiate YouTrack API Client $client = new \Cog\YouTrack\Rest\Client\YouTrackClient($http, $authorizer); diff --git a/src/Authenticator/Contracts/Authenticator.php b/src/Authenticator/Contracts/Authenticator.php new file mode 100644 index 0000000..e987599 --- /dev/null +++ b/src/Authenticator/Contracts/Authenticator.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\YouTrack\Rest\Authenticator\Contracts; + +use Cog\YouTrack\Rest\Client\Contracts\Client as ClientContract; + +/** + * Interface Authorizer. + * + * @package Cog\YouTrack\Rest\Authenticator\Contracts + */ +interface Authenticator +{ + /** + * Authenticate API Client. + * + * @param \Cog\YouTrack\Rest\Client\Contracts\Client $client + * @return void + */ + public function authenticate(ClientContract $client): void; +} diff --git a/src/Authenticator/CookieAuthenticator.php b/src/Authenticator/CookieAuthenticator.php new file mode 100644 index 0000000..5d5db83 --- /dev/null +++ b/src/Authenticator/CookieAuthenticator.php @@ -0,0 +1,85 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\YouTrack\Rest\Authenticator; + +use Cog\YouTrack\Rest\Authenticator\Contracts\Authenticator as AuthenticatorContract; +use Cog\YouTrack\Rest\Client\Contracts\Client as ClientContract; + +/** + * Class CookieAuthenticator. + * + * @package Cog\YouTrack\Rest\Authenticator + */ +class CookieAuthenticator implements AuthenticatorContract +{ + /** + * @var string + */ + private $username = ''; + + /** + * @var string + */ + private $password = ''; + + /** + * @var string + */ + private $cookie = ''; + + /** + * Determine is trying to authenticate. + * + * @var bool + */ + private $isAuthenticating = false; + + /** + * CookieAuthenticator constructor. + * + * @param string $username + * @param string $password + */ + public function __construct(string $username, string $password) + { + $this->username = $username; + $this->password = $password; + } + + /** + * Authenticate client and returns cookie on success login. + * + * @param \Cog\YouTrack\Rest\Client\Contracts\Client $client + * @return void + * + * @throws \Cog\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException + */ + public function authenticate(ClientContract $client): void + { + if ($this->cookie === '' && !$this->isAuthenticating) { + $this->isAuthenticating = true; + $response = $client->request('POST', '/user/login', [ + 'login' => $this->username, + 'password' => $this->password, + ]); + $this->isAuthenticating = false; + + if ($response->isStatusCode(200)) { + $this->cookie = $response->cookie(); + } + } + + $client->putHeader('Cookie', $this->cookie); + } +} diff --git a/src/Authorizer/Exceptions/AuthenticationException.php b/src/Authenticator/Exceptions/AuthenticationException.php similarity index 68% rename from src/Authorizer/Exceptions/AuthenticationException.php rename to src/Authenticator/Exceptions/AuthenticationException.php index 12d4173..4f62b88 100644 --- a/src/Authorizer/Exceptions/AuthenticationException.php +++ b/src/Authenticator/Exceptions/AuthenticationException.php @@ -11,12 +11,14 @@ * file that was distributed with this source code. */ -namespace Cog\YouTrack\Rest\Authorizer\Exceptions; +namespace Cog\YouTrack\Rest\Authenticator\Exceptions; + +use Cog\YouTrack\Rest\Authorizer\Exceptions\AuthorizationException; /** * Class AuthenticationException. * - * @package Cog\YouTrack\Rest\Authorizer\Exceptions + * @package Cog\YouTrack\Rest\Authenticator\Exceptions */ class AuthenticationException extends AuthorizationException { diff --git a/src/Authorizer/Contracts/Authorizer.php b/src/Authorizer/Contracts/Authorizer.php index b5afe04..7736ddf 100644 --- a/src/Authorizer/Contracts/Authorizer.php +++ b/src/Authorizer/Contracts/Authorizer.php @@ -25,15 +25,8 @@ interface Authorizer /** * Returns authorization headers. * - * @return array - */ - public function getHeaders(): array; - - /** - * Authenticate API Client. - * * @param \Cog\YouTrack\Rest\Client\Contracts\Client $client * @return void */ - public function authenticate(ClientContract $client): void; + public function appendHeadersTo(ClientContract $client): void; } diff --git a/src/Authorizer/CookieAuthorizer.php b/src/Authorizer/CookieAuthorizer.php index ccf9d69..20fa479 100644 --- a/src/Authorizer/CookieAuthorizer.php +++ b/src/Authorizer/CookieAuthorizer.php @@ -13,6 +13,7 @@ namespace Cog\YouTrack\Rest\Authorizer; +use Cog\YouTrack\Rest\Authenticator\Contracts\Authenticator as AuthenticatorContract; use Cog\YouTrack\Rest\Authorizer\Contracts\Authorizer as AuthorizerContract; use Cog\YouTrack\Rest\Client\Contracts\Client as ClientContract; @@ -24,70 +25,28 @@ class CookieAuthorizer implements AuthorizerContract { /** - * @var string + * @var \Cog\YouTrack\Rest\Authenticator\Contracts\Authenticator */ - private $cookie; - - /** - * @var string - */ - private $username; - - /** - * @var string - */ - private $password; + private $authenticator; /** * CookieAuthorizer constructor. * - * @param array $options + * @param \Cog\YouTrack\Rest\Authenticator\Contracts\Authenticator $authenticator */ - public function __construct(array $options) + public function __construct(AuthenticatorContract $authenticator) { - $this->setCredentials($options); + $this->authenticator = $authenticator; } /** * Returns authorization headers. * - * @return array - */ - public function getHeaders(): array - { - return [ - 'Cookie' => $this->cookie, - ]; - } - - /** - * Authenticate API Client. - * Stores cookie on success login. - * * @param \Cog\YouTrack\Rest\Client\Contracts\Client $client * @return void - * - * @throws \Cog\YouTrack\Rest\Authorizer\Exceptions\AuthenticationException - */ - public function authenticate(ClientContract $client): void - { - $response = $client->post('/user/login', [ - 'login' => $this->username, - 'password' => $this->password, - ]); - - $this->cookie = $response->getCookie(); - } - - /** - * Set authentication credentials. - * - * @param array $credentials - * @return void */ - protected function setCredentials(array $credentials): void + public function appendHeadersTo(ClientContract $client): void { - $this->username = $credentials['username']; - $this->password = $credentials['password']; + $this->authenticator->authenticate($client); } } diff --git a/src/Authorizer/TokenAuthorizer.php b/src/Authorizer/TokenAuthorizer.php index 9bccda8..74d1fe6 100644 --- a/src/Authorizer/TokenAuthorizer.php +++ b/src/Authorizer/TokenAuthorizer.php @@ -33,44 +33,21 @@ class TokenAuthorizer implements AuthorizerContract /** * TokenAuthorizer constructor. * - * @param array $options + * @param string $token */ - public function __construct(array $options) + public function __construct(string $token) { - $this->setToken($options); + $this->token = $token; } /** * Returns authorization headers. * - * @return array - */ - public function getHeaders(): array - { - return [ - 'Authorization' => "Bearer {$this->token}", - ]; - } - - /** - * Authenticate API Client. - * * @param \Cog\YouTrack\Rest\Client\Contracts\Client $client * @return void */ - public function authenticate(ClientContract $client): void - { - // Nothing to do - } - - /** - * Set authorization token. - * - * @param array $credentials - * @return void - */ - protected function setToken(array $credentials): void + public function appendHeadersTo(ClientContract $client): void { - $this->token = $credentials['token']; + $client->putHeader('Authorization', "Bearer {$this->token}"); } } diff --git a/src/Client/Contracts/Client.php b/src/Client/Contracts/Client.php index 73d53da..6dc2aa6 100644 --- a/src/Client/Contracts/Client.php +++ b/src/Client/Contracts/Client.php @@ -30,7 +30,7 @@ interface Client * @param array $formData * @return \Cog\YouTrack\Rest\Response\Contracts\Response * - * @throws \Cog\YouTrack\Rest\Authorizer\Exceptions\AuthenticationException + * @throws \Cog\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException * @throws \Cog\YouTrack\Rest\Authorizer\Exceptions\InvalidTokenException */ public function request(string $method, string $uri, array $formData = []) : ResponseContract; @@ -70,4 +70,12 @@ public function put(string $uri, array $formData = []): ResponseContract; * @return \Cog\YouTrack\Rest\Response\Contracts\Response */ public function delete(string $uri, array $formData = []): ResponseContract; + + /** + * Write header value. + * + * @param string $key + * @param string $value + */ + public function putHeader(string $key, string $value): void; } diff --git a/src/Client/YouTrackClient.php b/src/Client/YouTrackClient.php index 51b902e..c54225f 100644 --- a/src/Client/YouTrackClient.php +++ b/src/Client/YouTrackClient.php @@ -13,8 +13,8 @@ namespace Cog\YouTrack\Rest\Client; +use Cog\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException; use Cog\YouTrack\Rest\Authorizer\Contracts\Authorizer as AuthorizerContract; -use Cog\YouTrack\Rest\Authorizer\Exceptions\AuthenticationException; use Cog\YouTrack\Rest\Authorizer\Exceptions\InvalidTokenException; use Cog\YouTrack\Rest\Client\Contracts\Client as RestClientContract; use Cog\YouTrack\Rest\Response\Contracts\Response as ResponseContract; @@ -25,7 +25,7 @@ /** * Class YouTrackRestClient. * - * @see https://www.jetbrains.com/help/youtrack/standalone/2017.2/YouTrack-REST-API-Reference.html * + * @see https://www.jetbrains.com/help/youtrack/standalone/2017.2/YouTrack-REST-API-Reference.html * * @package Cog\YouTrack\Rest\Client */ @@ -34,19 +34,25 @@ class YouTrackClient implements RestClientContract /** * Version of YouTrack REST PHP client. */ - const CLIENT_VERSION = '2.0.1'; + const CLIENT_VERSION = '3.0.0'; /** + * HTTP Client. + * * @var \GuzzleHttp\ClientInterface */ private $http; /** + * Authorization driver. + * * @var \Cog\YouTrack\Rest\Authorizer\Contracts\Authorizer */ private $authorizer; /** + * Endpoint path prefix. + * * @todo make configurable * @todo test it * @todo choose good name @@ -54,6 +60,13 @@ class YouTrackClient implements RestClientContract */ private $endpointPathPrefix = '/rest'; + /** + * Request headers. + * + * @var array + */ + private $headers = []; + /** * YouTrackClient constructor. * @@ -64,7 +77,6 @@ public function __construct(GuzzleClientContract $http, AuthorizerContract $auth { $this->http = $http; $this->authorizer = $authorizer; - $this->authorizer->authenticate($this); } /** @@ -75,7 +87,7 @@ public function __construct(GuzzleClientContract $http, AuthorizerContract $auth * @param array $formData * @return \Cog\YouTrack\Rest\Response\Contracts\Response * - * @throws \Cog\YouTrack\Rest\Authorizer\Exceptions\AuthenticationException + * @throws \Cog\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException * @throws \Cog\YouTrack\Rest\Authorizer\Exceptions\InvalidTokenException */ public function request(string $method, string $uri, array $formData = []) : ResponseContract @@ -148,6 +160,19 @@ public function delete(string $uri, array $formData = []): ResponseContract } /** + * Write header value. + * + * @param string $key + * @param string $value + */ + public function putHeader(string $key, string $value): void + { + $this->headers[$key] = $value; + } + + /** + * Build endpoint URI. + * * @param string $uri * @return string */ @@ -157,6 +182,8 @@ protected function buildUri(string $uri): string } /** + * Build request options. + * * @param array $formData * @return array */ @@ -169,14 +196,20 @@ protected function buildOptions(array $formData = []): array } /** + * Build request headers. + * * @param array $headers * @return array */ protected function buildHeaders(array $headers = []): array { - return array_merge([ + $this->headers = [ 'User-Agent' => 'Cog-YouTrack-REST-PHP/' . self::CLIENT_VERSION, 'Accept' => 'application/json', - ], $this->authorizer->getHeaders(), $headers); + ]; + + $this->authorizer->appendHeadersTo($this); + + return array_merge($this->headers, $headers); } } diff --git a/src/Response/Contracts/Response.php b/src/Response/Contracts/Response.php index 98c93e7..1e0a78b 100644 --- a/src/Response/Contracts/Response.php +++ b/src/Response/Contracts/Response.php @@ -23,35 +23,35 @@ interface Response { /** - * Get original HTTP client response. + * Returns original HTTP client response. * * @return \Psr\Http\Message\ResponseInterface */ - public function getResponse(): ResponseInterface; + public function httpResponse(): ResponseInterface; /** - * Gets the response status code. + * Returns the response status code. * * The status code is a 3-digit integer result code of the server's attempt * to understand and satisfy the request. * * @return int */ - public function getStatusCode(): int; + public function statusCode(): int; /** * Transform response cookie headers to string. * * @return string */ - public function getCookie(): string; + public function cookie(): string; /** - * Gets the response Location header. + * Returns the response Location header. * * @return string */ - public function getLocation(): string; + public function location(): string; /** * Transform response body to array. @@ -59,4 +59,12 @@ public function getLocation(): string; * @return array */ public function toArray(): array; + + /** + * Assert the status code of the response. + * + * @param int $code + * @return bool + */ + public function isStatusCode(int $code): bool; } diff --git a/src/Response/YouTrackResponse.php b/src/Response/YouTrackResponse.php index df35605..a9cf9a5 100644 --- a/src/Response/YouTrackResponse.php +++ b/src/Response/YouTrackResponse.php @@ -41,24 +41,24 @@ public function __construct(ResponseInterface $response) } /** - * Get original HTTP client response. + * Returns original HTTP client response. * * @return \Psr\Http\Message\ResponseInterface */ - public function getResponse(): ResponseInterface + public function httpResponse(): ResponseInterface { return $this->response; } /** - * Gets the response status code. + * Returns the response status code. * * The status code is a 3-digit integer result code of the server's attempt * to understand and satisfy the request. * * @return int */ - public function getStatusCode(): int + public function statusCode(): int { return $this->response->getStatusCode(); } @@ -68,17 +68,17 @@ public function getStatusCode(): int * * @return string */ - public function getCookie(): string + public function cookie(): string { return $this->response->getHeaderLine('Set-Cookie'); } /** - * Gets the response Location header. + * Returns the response Location header. * * @return string */ - public function getLocation(): string + public function location(): string { return $this->response->getHeaderLine('Location'); } @@ -92,4 +92,15 @@ public function toArray(): array { return json_decode($this->response->getBody()->getContents(), true); } + + /** + * Assert the status code of the response. + * + * @param int $code + * @return bool + */ + public function isStatusCode(int $code): bool + { + return $this->response->getStatusCode() === $code; + } } diff --git a/tests/Feature/Authenticator/CookieAuthorizerTest.php b/tests/Feature/Authenticator/CookieAuthorizerTest.php index 9a73d03..bb385a3 100644 --- a/tests/Feature/Authenticator/CookieAuthorizerTest.php +++ b/tests/Feature/Authenticator/CookieAuthorizerTest.php @@ -13,8 +13,9 @@ namespace Cog\YouTrack\Rest\Tests\Feature\Authorizer; +use Cog\YouTrack\Rest\Authenticator\CookieAuthenticator; +use Cog\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException; use Cog\YouTrack\Rest\Authorizer\CookieAuthorizer; -use Cog\YouTrack\Rest\Authorizer\Exceptions\AuthenticationException; use Cog\YouTrack\Rest\Client\YouTrackClient; use Cog\YouTrack\Rest\Tests\FeatureTestCase; use GuzzleHttp\Client as HttpClient; @@ -36,14 +37,13 @@ public function it_throws_exception_on_failed_cookie_authentication() ]); $handler = HandlerStack::create($mock); $http = new HttpClient(['handler' => $handler]); - $authorizer = new CookieAuthorizer([ - 'username' => 'invalid-user', - 'password' => 'invalid-pass', - ]); + $authenticator = new CookieAuthenticator('invalid-user', 'invalid-pass'); + $authorizer = new CookieAuthorizer($authenticator); + $client = new YouTrackClient($http, $authorizer); $this->expectException(AuthenticationException::class); - new YouTrackClient($http, $authorizer); + $client->get('/admin/project'); } /** @todo test */ @@ -52,10 +52,8 @@ public function it_can_successfully_authenticate() $http = new HttpClient([ 'base_uri' => 'http://localhost', ]); - $authorizer = new CookieAuthorizer([ - 'username' => 'invalid-user', - 'password' => 'invalid-pass', - ]); + $authenticator = new CookieAuthenticator('valid-user', 'valid-pass'); + $authorizer = new CookieAuthorizer($authenticator); $this->expectException(AuthenticationException::class); diff --git a/tests/Feature/Authenticator/TokenAuthorizerTest.php b/tests/Feature/Authenticator/TokenAuthorizerTest.php index 9cd944b..8af46e5 100644 --- a/tests/Feature/Authenticator/TokenAuthorizerTest.php +++ b/tests/Feature/Authenticator/TokenAuthorizerTest.php @@ -36,13 +36,11 @@ public function it_throws_exception_on_failed_token_authorization() ]); $handler = HandlerStack::create($mock); $http = new HttpClient(['handler' => $handler]); - $authorizer = new TokenAuthorizer([ - 'token' => 'invalid-token', - ]); + $authorizer = new TokenAuthorizer('invalid-token'); + $client = new YouTrackClient($http, $authorizer); $this->expectException(InvalidTokenException::class); - $client = new YouTrackClient($http, $authorizer); $client->get('/admin/project'); } }