From 77bbb3f7a73aead8ee6aabde07c210365c44f554 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Tue, 6 Oct 2020 14:26:36 +0200 Subject: [PATCH] Add Oauth2 tests --- Auth/OAuth2/Grant/AuthorizationCode.php | 1 + Auth/OAuth2/Grant/ClientCredentials.php | 1 + Auth/OAuth2/Grant/GrantAbstract.php | 1 + Auth/OAuth2/Grant/GrantFactory.php | 3 +- Auth/OAuth2/Grant/Password.php | 1 + Auth/OAuth2/Grant/RefreshToken.php | 1 + .../HttpBasicAuthOptionProvider.php | 5 + .../OptionProviderInterface.php | 1 + .../OptionProvider/PostAuthOptionProvider.php | 3 +- Auth/OAuth2/Provider/GeneralProvider.php | 1 + Auth/OAuth2/Provider/GeneralResourceOwner.php | 1 + Auth/OAuth2/Provider/ProviderAbstract.php | 1 + .../Provider/ResourceOwnerInterface.php | 1 + Auth/OAuth2/Token/AccessToken.php | 22 ++-- Auth/OAuth2/Token/AccessTokenInterface.php | 3 +- .../ResourceOwnerAccessTokenInterface.php | 1 + Math/Matrix/CholeskyDecomposition.php | 3 +- Math/Matrix/EigenvalueDecomposition.php | 3 +- Math/Matrix/LUDecomposition.php | 3 +- Math/Matrix/QRDecomposition.php | 3 +- .../OAuth2/Grant/AuthorizationCodeTest.php | 65 ++++++++++ .../OAuth2/Grant/ClientCredentialsTest.php | 51 ++++++++ tests/Auth/OAuth2/Grant/GrantAbstractTest.php | 73 +++++++++++ tests/Auth/OAuth2/Grant/GrantFactoryTest.php | 62 +++++++++ tests/Auth/OAuth2/Grant/PasswordTest.php | 65 ++++++++++ tests/Auth/OAuth2/Grant/RefreshTokenTest.php | 65 ++++++++++ .../HttpBasicAuthOptionProviderTest.php | 64 ++++++++++ .../PostAuthOptionProviderTest.php | 43 +++++++ tests/Auth/OAuth2/Token/AccessTokenTest.php | 119 ++++++++++++++++++ 29 files changed, 643 insertions(+), 23 deletions(-) create mode 100644 tests/Auth/OAuth2/Grant/AuthorizationCodeTest.php create mode 100644 tests/Auth/OAuth2/Grant/ClientCredentialsTest.php create mode 100644 tests/Auth/OAuth2/Grant/GrantAbstractTest.php create mode 100644 tests/Auth/OAuth2/Grant/GrantFactoryTest.php create mode 100644 tests/Auth/OAuth2/Grant/PasswordTest.php create mode 100644 tests/Auth/OAuth2/Grant/RefreshTokenTest.php create mode 100644 tests/Auth/OAuth2/OptionProvider/HttpBasicAuthOptionProviderTest.php create mode 100644 tests/Auth/OAuth2/OptionProvider/PostAuthOptionProviderTest.php create mode 100644 tests/Auth/OAuth2/Token/AccessTokenTest.php diff --git a/Auth/OAuth2/Grant/AuthorizationCode.php b/Auth/OAuth2/Grant/AuthorizationCode.php index ec58339b4..ac68db44a 100644 --- a/Auth/OAuth2/Grant/AuthorizationCode.php +++ b/Auth/OAuth2/Grant/AuthorizationCode.php @@ -6,6 +6,7 @@ * * @package phpOMS\Auth\OAuth2\Grant * @copyright Dennis Eichhorn + * @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie - thephpleague/oauth2-client * @license OMS License 1.0 * @version 1.0.0 * @link https://orange-management.org diff --git a/Auth/OAuth2/Grant/ClientCredentials.php b/Auth/OAuth2/Grant/ClientCredentials.php index 075bac0fe..f67503d1b 100644 --- a/Auth/OAuth2/Grant/ClientCredentials.php +++ b/Auth/OAuth2/Grant/ClientCredentials.php @@ -6,6 +6,7 @@ * * @package phpOMS\Auth\OAuth2\Grant * @copyright Dennis Eichhorn + * @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie - thephpleague/oauth2-client * @license OMS License 1.0 * @version 1.0.0 * @link https://orange-management.org diff --git a/Auth/OAuth2/Grant/GrantAbstract.php b/Auth/OAuth2/Grant/GrantAbstract.php index fbe906c94..027cb8c96 100644 --- a/Auth/OAuth2/Grant/GrantAbstract.php +++ b/Auth/OAuth2/Grant/GrantAbstract.php @@ -6,6 +6,7 @@ * * @package phpOMS\Auth\OAuth2\Grant * @copyright Dennis Eichhorn + * @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie - thephpleague/oauth2-client * @license OMS License 1.0 * @version 1.0.0 * @link https://orange-management.org diff --git a/Auth/OAuth2/Grant/GrantFactory.php b/Auth/OAuth2/Grant/GrantFactory.php index 240ff99a4..04df30295 100644 --- a/Auth/OAuth2/Grant/GrantFactory.php +++ b/Auth/OAuth2/Grant/GrantFactory.php @@ -6,6 +6,7 @@ * * @package phpOMS\Auth\OAuth2\Grant * @copyright Dennis Eichhorn + * @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie - thephpleague/oauth2-client * @license OMS License 1.0 * @version 1.0.0 * @link https://orange-management.org @@ -46,7 +47,7 @@ class GrantFactory protected function registerDefaultGrant(string $name) : self { $class = \str_replace(' ', '', \ucwords(\str_replace(['-', '_'], ' ', $name))); - $class = 'phpOMS\\OAuth2\\Grant\\' . $class; + $class = 'phpOMS\\Auth\\OAuth2\\Grant\\' . $class; if (!\is_subclass_of($class, GrantAbstract::class)) { throw new \Exception(); diff --git a/Auth/OAuth2/Grant/Password.php b/Auth/OAuth2/Grant/Password.php index d260390d8..a9ee6a424 100644 --- a/Auth/OAuth2/Grant/Password.php +++ b/Auth/OAuth2/Grant/Password.php @@ -6,6 +6,7 @@ * * @package phpOMS\Auth\OAuth2\Grant * @copyright Dennis Eichhorn + * @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie - thephpleague/oauth2-client * @license OMS License 1.0 * @version 1.0.0 * @link https://orange-management.org diff --git a/Auth/OAuth2/Grant/RefreshToken.php b/Auth/OAuth2/Grant/RefreshToken.php index 1ed3347f5..e937ead38 100644 --- a/Auth/OAuth2/Grant/RefreshToken.php +++ b/Auth/OAuth2/Grant/RefreshToken.php @@ -6,6 +6,7 @@ * * @package phpOMS\Auth\OAuth2\Grant * @copyright Dennis Eichhorn + * @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie - thephpleague/oauth2-client * @license OMS License 1.0 * @version 1.0.0 * @link https://orange-management.org diff --git a/Auth/OAuth2/OptionProvider/HttpBasicAuthOptionProvider.php b/Auth/OAuth2/OptionProvider/HttpBasicAuthOptionProvider.php index 10bcb4ac5..c9455f8ec 100644 --- a/Auth/OAuth2/OptionProvider/HttpBasicAuthOptionProvider.php +++ b/Auth/OAuth2/OptionProvider/HttpBasicAuthOptionProvider.php @@ -6,6 +6,7 @@ * * @package phpOMS\Auth\OAuth2\OptionProvider * @copyright Dennis Eichhorn + * @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie - thephpleague/oauth2-client * @license OMS License 1.0 * @version 1.0.0 * @link https://orange-management.org @@ -27,6 +28,10 @@ class HttpBasicAuthOptionProvider extends PostAuthOptionProvider { public function getAccessTokenOptions(string $method, array $params) : array { + if (!isset($params['client_id'], $params['client_secret'])) { + return []; + } + $encoded = \base64_encode($params['client_id'] . ':' . $params['client_secret']); unset($params['client_id'], $params['client_secret']); diff --git a/Auth/OAuth2/OptionProvider/OptionProviderInterface.php b/Auth/OAuth2/OptionProvider/OptionProviderInterface.php index e843c5f46..5bce1ac2e 100644 --- a/Auth/OAuth2/OptionProvider/OptionProviderInterface.php +++ b/Auth/OAuth2/OptionProvider/OptionProviderInterface.php @@ -6,6 +6,7 @@ * * @package phpOMS\Auth\OAuth2\OptionProvider * @copyright Dennis Eichhorn + * @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie - thephpleague/oauth2-client * @license OMS License 1.0 * @version 1.0.0 * @link https://orange-management.org diff --git a/Auth/OAuth2/OptionProvider/PostAuthOptionProvider.php b/Auth/OAuth2/OptionProvider/PostAuthOptionProvider.php index 1830c377f..24eadcbf0 100644 --- a/Auth/OAuth2/OptionProvider/PostAuthOptionProvider.php +++ b/Auth/OAuth2/OptionProvider/PostAuthOptionProvider.php @@ -6,6 +6,7 @@ * * @package phpOMS\Auth\OAuth2\OptionProvider * @copyright Dennis Eichhorn + * @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie - thephpleague/oauth2-client * @license OMS License 1.0 * @version 1.0.0 * @link https://orange-management.org @@ -43,6 +44,6 @@ class PostAuthOptionProvider implements OptionProviderInterface protected function getAccessTokenBody(array $params) : string { - return \http_build_query($params, null, '&', \PHP_QUERY_RFC3986); + return \http_build_query($params, '', '&', \PHP_QUERY_RFC3986); } } diff --git a/Auth/OAuth2/Provider/GeneralProvider.php b/Auth/OAuth2/Provider/GeneralProvider.php index caa7351a7..bf1e65d27 100644 --- a/Auth/OAuth2/Provider/GeneralProvider.php +++ b/Auth/OAuth2/Provider/GeneralProvider.php @@ -6,6 +6,7 @@ * * @package phpOMS\Auth\OAuth2\Provider * @copyright Dennis Eichhorn + * @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie - thephpleague/oauth2-client * @license OMS License 1.0 * @version 1.0.0 * @link https://orange-management.org diff --git a/Auth/OAuth2/Provider/GeneralResourceOwner.php b/Auth/OAuth2/Provider/GeneralResourceOwner.php index ef76453ef..d6623b37a 100644 --- a/Auth/OAuth2/Provider/GeneralResourceOwner.php +++ b/Auth/OAuth2/Provider/GeneralResourceOwner.php @@ -6,6 +6,7 @@ * * @package phpOMS\Auth\OAuth2\Provider * @copyright Dennis Eichhorn + * @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie - thephpleague/oauth2-client * @license OMS License 1.0 * @version 1.0.0 * @link https://orange-management.org diff --git a/Auth/OAuth2/Provider/ProviderAbstract.php b/Auth/OAuth2/Provider/ProviderAbstract.php index b96641381..82f81d4b8 100644 --- a/Auth/OAuth2/Provider/ProviderAbstract.php +++ b/Auth/OAuth2/Provider/ProviderAbstract.php @@ -6,6 +6,7 @@ * * @package phpOMS\Auth\OAuth2\Provider * @copyright Dennis Eichhorn + * @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie - thephpleague/oauth2-client * @license OMS License 1.0 * @version 1.0.0 * @link https://orange-management.org diff --git a/Auth/OAuth2/Provider/ResourceOwnerInterface.php b/Auth/OAuth2/Provider/ResourceOwnerInterface.php index f0d265de1..57126234f 100644 --- a/Auth/OAuth2/Provider/ResourceOwnerInterface.php +++ b/Auth/OAuth2/Provider/ResourceOwnerInterface.php @@ -6,6 +6,7 @@ * * @package phpOMS\Auth\OAuth2\Provider * @copyright Dennis Eichhorn + * @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie - thephpleague/oauth2-client * @license OMS License 1.0 * @version 1.0.0 * @link https://orange-management.org diff --git a/Auth/OAuth2/Token/AccessToken.php b/Auth/OAuth2/Token/AccessToken.php index 0b6af1051..2ff8e6615 100644 --- a/Auth/OAuth2/Token/AccessToken.php +++ b/Auth/OAuth2/Token/AccessToken.php @@ -6,6 +6,7 @@ * * @package phpOMS\Auth\OAuth2\Token * @copyright Dennis Eichhorn + * @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie - thephpleague/oauth2-client * @license OMS License 1.0 * @version 1.0.0 * @link https://orange-management.org @@ -26,7 +27,7 @@ class AccessToken implements AccessTokenInterface, ResourceOwnerAccessTokenInter { protected string $accessToken; - protected ?int $expires = null; + protected int $expires = -1; protected ?string $refreshToken = null; @@ -70,7 +71,7 @@ class AccessToken implements AccessTokenInterface, ResourceOwnerAccessTokenInter return $this->accessToken; } - public function getExpires() : ?int + public function getExpires() : int { return $this->expires; } @@ -87,12 +88,12 @@ class AccessToken implements AccessTokenInterface, ResourceOwnerAccessTokenInter public function hasExpired() : bool { - return $this->expires < \time(); + return $this->expires > 0 && $this->expires < \time(); } public function getValues() : array { - return $this->vallues; + return $this->values; } public function __toString() @@ -102,21 +103,18 @@ class AccessToken implements AccessTokenInterface, ResourceOwnerAccessTokenInter public function jsonSerialize() { - $params = $this->values; + $params = $this->values; + $params['access_token'] = $this->accessToken; - if (isset($this->accessToken)) { - $params['access_token'] = $this->accessToken; - } - - if (isset($this->refreshToken)) { + if ($this->refreshToken !== null) { $params['refresh_token'] = $this->refreshToken; } - if (isset($this->expires)) { + if ($this->expires > 0) { $params['expires'] = $this->expires; } - if (isset($this->resourceOwnerId)) { + if ($this->resourceOwnerId !== null) { $params['resource_owner_id'] = $this->resourceOwnerId; } diff --git a/Auth/OAuth2/Token/AccessTokenInterface.php b/Auth/OAuth2/Token/AccessTokenInterface.php index 188a4a429..ad491d0f8 100644 --- a/Auth/OAuth2/Token/AccessTokenInterface.php +++ b/Auth/OAuth2/Token/AccessTokenInterface.php @@ -6,6 +6,7 @@ * * @package phpOMS\Auth\OAuth2\Token * @copyright Dennis Eichhorn + * @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie - thephpleague/oauth2-client * @license OMS License 1.0 * @version 1.0.0 * @link https://orange-management.org @@ -29,7 +30,7 @@ interface AccessTokenInterface extends \JsonSerializable public function getRefreshToken() : ?string; - public function getExpires() : ?int; + public function getExpires() : int; public function hasExpired() : bool; diff --git a/Auth/OAuth2/Token/ResourceOwnerAccessTokenInterface.php b/Auth/OAuth2/Token/ResourceOwnerAccessTokenInterface.php index d08b7f977..456ce4c4b 100644 --- a/Auth/OAuth2/Token/ResourceOwnerAccessTokenInterface.php +++ b/Auth/OAuth2/Token/ResourceOwnerAccessTokenInterface.php @@ -6,6 +6,7 @@ * * @package phpOMS\Auth\OAuth2\Token * @copyright Dennis Eichhorn + * @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie - thephpleague/oauth2-client * @license OMS License 1.0 * @version 1.0.0 * @link https://orange-management.org diff --git a/Math/Matrix/CholeskyDecomposition.php b/Math/Matrix/CholeskyDecomposition.php index deb25f092..44c6526e5 100644 --- a/Math/Matrix/CholeskyDecomposition.php +++ b/Math/Matrix/CholeskyDecomposition.php @@ -6,8 +6,8 @@ * * @package phpOMS\Math\Matrix * @copyright Dennis Eichhorn + * @copyright JAMA - https://math.nist.gov/javanumerics/jama/ * @license OMS License 1.0 - * @license JAMA - https://math.nist.gov/javanumerics/jama/ * @version 1.0.0 * @link https://orange-management.org */ @@ -24,7 +24,6 @@ use phpOMS\Math\Matrix\Exception\InvalidDimensionException; * * @package phpOMS\Math\Matrix * @license OMS License 1.0 - * @license JAMA - https://math.nist.gov/javanumerics/jama/ * @link https://orange-management.org * @since 1.0.0 */ diff --git a/Math/Matrix/EigenvalueDecomposition.php b/Math/Matrix/EigenvalueDecomposition.php index 32124a6c1..2ff478603 100644 --- a/Math/Matrix/EigenvalueDecomposition.php +++ b/Math/Matrix/EigenvalueDecomposition.php @@ -6,8 +6,8 @@ * * @package phpOMS\Math\Matrix * @copyright Dennis Eichhorn + * @copyright JAMA - https://math.nist.gov/javanumerics/jama/ * @license OMS License 1.0 - * @license JAMA - https://math.nist.gov/javanumerics/jama/ * @version 1.0.0 * @link https://orange-management.org */ @@ -25,7 +25,6 @@ use phpOMS\Math\Geometry\Shape\D2\Triangle; * * @package phpOMS\Math\Matrix * @license OMS License 1.0 - * @license JAMA - https://math.nist.gov/javanumerics/jama/ * @link https://orange-management.org * @since 1.0.0 */ diff --git a/Math/Matrix/LUDecomposition.php b/Math/Matrix/LUDecomposition.php index 4944a57f0..7293072eb 100644 --- a/Math/Matrix/LUDecomposition.php +++ b/Math/Matrix/LUDecomposition.php @@ -6,8 +6,8 @@ * * @package phpOMS\Math\Matrix * @copyright Dennis Eichhorn + * @copyright JAMA - https://math.nist.gov/javanumerics/jama/ * @license OMS License 1.0 - * @license JAMA - https://math.nist.gov/javanumerics/jama/ * @version 1.0.0 * @link https://orange-management.org */ @@ -24,7 +24,6 @@ use phpOMS\Math\Matrix\Exception\InvalidDimensionException; * * @package phpOMS\Math\Matrix * @license OMS License 1.0 - * @license JAMA - https://math.nist.gov/javanumerics/jama/ * @link https://orange-management.org * @since 1.0.0 */ diff --git a/Math/Matrix/QRDecomposition.php b/Math/Matrix/QRDecomposition.php index 8f7e592fc..e603c1552 100644 --- a/Math/Matrix/QRDecomposition.php +++ b/Math/Matrix/QRDecomposition.php @@ -6,8 +6,8 @@ * * @package phpOMS\Math\Matrix * @copyright Dennis Eichhorn + * @copyright JAMA - https://math.nist.gov/javanumerics/jama/ * @license OMS License 1.0 - * @license JAMA - https://math.nist.gov/javanumerics/jama/ * @version 1.0.0 * @link https://orange-management.org */ @@ -25,7 +25,6 @@ use phpOMS\Math\Matrix\Exception\InvalidDimensionException; * * @package phpOMS\Math\Matrix * @license OMS License 1.0 - * @license JAMA - https://math.nist.gov/javanumerics/jama/ * @link https://orange-management.org * @since 1.0.0 */ diff --git a/tests/Auth/OAuth2/Grant/AuthorizationCodeTest.php b/tests/Auth/OAuth2/Grant/AuthorizationCodeTest.php new file mode 100644 index 000000000..ff478530c --- /dev/null +++ b/tests/Auth/OAuth2/Grant/AuthorizationCodeTest.php @@ -0,0 +1,65 @@ +grant = new AuthorizationCode(); + } + + public function testDefault() : void + { + self::assertEquals('authorization_code', $this->grant->__toString()); + self::assertEquals( + [ + 'code' => 'value', + 'option' => '2', + 'test' => 'value2', + ], + $this->grant->prepareRequestParamters( + [ + 'code' => 'value' + ], + [ + 'option' => '2', + 'test' => 'value2' + ] + ) + ); + } + + public function testMissingDefaultOption() : void + { + $this->expectException(\Exception::class); + $this->grant->prepareRequestParamters( + [ + 'test' => 'value' + ], + [ + 'option' => '2', + 'test' => 'value2' + ] + ); + } +} diff --git a/tests/Auth/OAuth2/Grant/ClientCredentialsTest.php b/tests/Auth/OAuth2/Grant/ClientCredentialsTest.php new file mode 100644 index 000000000..bbbb71eac --- /dev/null +++ b/tests/Auth/OAuth2/Grant/ClientCredentialsTest.php @@ -0,0 +1,51 @@ +grant = new ClientCredentials(); + } + + public function testDefault() : void + { + self::assertEquals('client_credentials', $this->grant->__toString()); + self::assertEquals( + [ + 'code' => 'value', + 'option' => '2', + 'test' => 'value2', + ], + $this->grant->prepareRequestParamters( + [ + 'code' => 'value' + ], + [ + 'option' => '2', + 'test' => 'value2' + ] + ) + ); + } +} diff --git a/tests/Auth/OAuth2/Grant/GrantAbstractTest.php b/tests/Auth/OAuth2/Grant/GrantAbstractTest.php new file mode 100644 index 000000000..4f5614b6c --- /dev/null +++ b/tests/Auth/OAuth2/Grant/GrantAbstractTest.php @@ -0,0 +1,73 @@ +grant = new class extends GrantAbstract { + protected function getName() : string + { + return 'TestGrant'; + } + + protected function getRequiredRequestParameters() : array + { + return ['test']; + } + }; + } + + public function testDefault() : void + { + self::assertEquals('TestGrant', $this->grant->__toString()); + self::assertEquals( + [ + 'test' => 'value2', + 'option' => '2' + ], + $this->grant->prepareRequestParamters( + [ + 'test' => 'value' + ], + [ + 'option' => '2', + 'test' => 'value2' + ] + ) + ); + } + + public function testMissingDefaultOption() : void + { + $this->expectException(\Exception::class); + $this->grant->prepareRequestParamters( + [ + 'something' => 'value' + ], + [ + 'option' => '2', + ] + ); + } +} diff --git a/tests/Auth/OAuth2/Grant/GrantFactoryTest.php b/tests/Auth/OAuth2/Grant/GrantFactoryTest.php new file mode 100644 index 000000000..405f6e3ff --- /dev/null +++ b/tests/Auth/OAuth2/Grant/GrantFactoryTest.php @@ -0,0 +1,62 @@ +factory = new GrantFactory(); + } + + public function testGrantGet() : void + { + $grant = $this->factory->getGrant('AuthorizationCode'); + self::assertInstanceOf(AuthorizationCode::class, $grant); + } + + public function testGrantInputOutput() : void + { + $grant = new class extends GrantAbstract { + protected function getName() : string + { + return 'TestGrant'; + } + + protected function getRequiredRequestParameters() : array + { + return ['test']; + } + }; + $this->factory->setGrant('test', $grant); + + self::assertInstanceOf(\get_class($grant), $this->factory->getGrant('test')); + } + + public function testInvalidGrantGet() : void + { + $this->expectException(\Exception::class); + $this->factory->getGrant('invalid'); + } +} diff --git a/tests/Auth/OAuth2/Grant/PasswordTest.php b/tests/Auth/OAuth2/Grant/PasswordTest.php new file mode 100644 index 000000000..ecbe9d36a --- /dev/null +++ b/tests/Auth/OAuth2/Grant/PasswordTest.php @@ -0,0 +1,65 @@ +grant = new Password(); + } + + public function testDefault() : void + { + self::assertEquals('password', $this->grant->__toString()); + self::assertEquals( + [ + 'username' => 'value', + 'password' => '2', + 'test' => 'value2', + ], + $this->grant->prepareRequestParamters( + [ + 'username' => 'value' + ], + [ + 'password' => '2', + 'test' => 'value2' + ] + ) + ); + } + + public function testMissingDefaultOption() : void + { + $this->expectException(\Exception::class); + $this->grant->prepareRequestParamters( + [ + 'username' => 'value' + ], + [ + 'option' => '2', + 'test' => 'value2' + ] + ); + } +} diff --git a/tests/Auth/OAuth2/Grant/RefreshTokenTest.php b/tests/Auth/OAuth2/Grant/RefreshTokenTest.php new file mode 100644 index 000000000..4fc5989d9 --- /dev/null +++ b/tests/Auth/OAuth2/Grant/RefreshTokenTest.php @@ -0,0 +1,65 @@ +grant = new RefreshToken(); + } + + public function testDefault() : void + { + self::assertEquals('refresh_token', $this->grant->__toString()); + self::assertEquals( + [ + 'refresh_token' => 'value', + 'option' => '2', + 'test' => 'value2', + ], + $this->grant->prepareRequestParamters( + [ + 'refresh_token' => 'value' + ], + [ + 'option' => '2', + 'test' => 'value2' + ] + ) + ); + } + + public function testMissingDefaultOption() : void + { + $this->expectException(\Exception::class); + $this->grant->prepareRequestParamters( + [ + 'test' => 'value' + ], + [ + 'option' => '2', + 'test' => 'value2' + ] + ); + } +} diff --git a/tests/Auth/OAuth2/OptionProvider/HttpBasicAuthOptionProviderTest.php b/tests/Auth/OAuth2/OptionProvider/HttpBasicAuthOptionProviderTest.php new file mode 100644 index 000000000..66eb95918 --- /dev/null +++ b/tests/Auth/OAuth2/OptionProvider/HttpBasicAuthOptionProviderTest.php @@ -0,0 +1,64 @@ +provider = new HttpBasicAuthOptionProvider(); + } + + public function testDefault() : void + { + self::assertEquals( + [ + 'headers' => [ + 'content-type' => MimeType::M_POST, + 'Authorization' => 'Basic aWQ6c2VjcmV0' + ], + 'body' => 'para=test¶2=test2', + ], + $this->provider->getAccessTokenOptions(RequestMethod::POST, [ + 'para' => 'test', + 'para2' => 'test2', + 'client_id' => 'id', + 'client_secret' => 'secret' + ] + ) + ); + } + + public function testInvalidParams() : void + { + self::assertEquals([], + $this->provider->getAccessTokenOptions(RequestMethod::POST, [ + 'para' => 'test', + 'para2' => 'test2', + 'client_id' => 'id', + ] + ) + ); + } +} diff --git a/tests/Auth/OAuth2/OptionProvider/PostAuthOptionProviderTest.php b/tests/Auth/OAuth2/OptionProvider/PostAuthOptionProviderTest.php new file mode 100644 index 000000000..2584313ab --- /dev/null +++ b/tests/Auth/OAuth2/OptionProvider/PostAuthOptionProviderTest.php @@ -0,0 +1,43 @@ +provider = new PostAuthOptionProvider(); + } + + public function testDefault() : void + { + self::assertEquals( + [ + 'headers' => ['content-type' => MimeType::M_POST], + 'body' => 'para=test¶2=test2', + ], + $this->provider->getAccessTokenOptions(RequestMethod::POST, ['para' => 'test', 'para2' => 'test2']) + ); + } +} diff --git a/tests/Auth/OAuth2/Token/AccessTokenTest.php b/tests/Auth/OAuth2/Token/AccessTokenTest.php new file mode 100644 index 000000000..fa9d76176 --- /dev/null +++ b/tests/Auth/OAuth2/Token/AccessTokenTest.php @@ -0,0 +1,119 @@ + 'token']); + self::assertEquals('token', $token->getToken()); + self::assertEquals('token', $token->__toString()); + self::assertEquals(-1, $token->getExpires()); + self::assertEquals(null, $token->getRefreshToken()); + self::assertEquals(null, $token->getResourceOwnerId()); + self::assertEquals(false, $token->hasExpired()); + self::assertEquals([], $token->getValues()); + self::assertEquals(['access_token' => 'token'], $token->jsonSerialize()); + } + + public function testExpiresInputOutput() : void + { + $expires = \time(); + $token = new AccessToken(['access_token' => 'token', 'expires' => $expires]); + self::assertEquals($expires, $token->getExpires()); + } + + public function testExpiresInInputOutput() : void + { + $expires = \time(); + $token = new AccessToken(['access_token' => 'token', 'expires_in' => 10]); + self::assertFalse($token->hasExpired()); + self::assertTrue($expires < $token->getExpires() && $token->getExpires() < $expires + 20); + } + + public function testHasExpired() : void + { + $token = new AccessToken(['access_token' => 'token', 'expires_in' => -5]); + self::assertTrue($token->hasExpired()); + + $expires = \time(); + $token = new AccessToken(['access_token' => 'token', 'expires' => $expires - 5]); + self::assertTrue($token->hasExpired()); + } + + public function testResourceOwnerIdInputOutput() : void + { + $token = new AccessToken(['access_token' => 'token', 'resource_owner_id' => 'owner']); + self::assertEquals('owner', $token->getResourceOwnerId()); + } + + public function testRefreshTokenInputOutput() : void + { + $token = new AccessToken(['access_token' => 'token', 'refresh_token' => 'refresh']); + self::assertEquals('refresh', $token->getRefreshToken()); + } + + public function testValuesInputOutput() : void + { + $token = new AccessToken([ + 'access_token' => 'token', + 'resource_owner_id' => 'owner', + 'expires_in' => 10, + 'refresh_token' => 'refresh', + 'more' => 'values' + ]); + + self::assertEquals( + ['more' => 'values'], + $token->getValues() + ); + } + + public function testJsonSeriaize() : void + { + $expires = \time() + 10; + + $token = new AccessToken([ + 'access_token' => 'token', + 'resource_owner_id' => 'owner', + 'expires' => $expires, + 'refresh_token' => 'refresh', + 'more' => 'values' + ]); + + self::assertEquals( + [ + 'access_token' => 'token', + 'resource_owner_id' => 'owner', + 'expires' => $expires, + 'refresh_token' => 'refresh', + 'more' => 'values' + ], + $token->jsonSerialize() + ); + } + + public function testMissingAccessToken() : void + { + $this->expectException(\InvalidArgumentException::class); + $token = new AccessToken([]); + } +}