diff --git a/README.md b/README.md index 7126900ab01998e46757fd5dc75c4c734f26f972..ffbcf046d579a6b7e673aa23c4068718f9d630e4 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ if (!isset($_GET['code'])) { ## Testing ``` bash -$ ./vendor/bin/phpunit +$ ./vendor/bin/phpunit tests ``` ## License diff --git a/composer.json b/composer.json index c4a66383691e807b688ee28e5452b445e5884c6d..01c68582bc121d81b44773ed1127d5a74536bbec 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,6 @@ { "name": "libcast/oauth2-authot", - "description": "Authot Provider for OAuth 2.0 Client", - "homepage": "https://code.libcast.net/libcast/oauth2-authot", + "description": "Authôt OAuth 2.0 Client Provider for The PHP League OAuth2-Client", "license": "MIT", "authors": [ { @@ -17,9 +16,18 @@ "php": ">=5.5.0", "league/oauth2-client": "^1.4" }, + "require-dev": { + "phpunit/phpunit": "~4.0", + "mockery/mockery": "~0.9" + }, "autoload": { "psr-4": { "Libcast\\OAuth2\\Client\\": "src/" } + }, + "autoload-dev": { + "psr-4": { + "Libcast\\OAuth2\\Client\\Test\\": "tests/src/" + } } } \ No newline at end of file diff --git a/tests/src/Provider/AuthotTest.php b/tests/src/Provider/AuthotTest.php new file mode 100644 index 0000000000000000000000000000000000000000..45d471ca506f33d4c9d276430a0e49ed5c05bf5a --- /dev/null +++ b/tests/src/Provider/AuthotTest.php @@ -0,0 +1,137 @@ +provider = new \Libcast\OAuth2\Client\Provider\Authot([ + 'clientId' => 'mock_client_id', + 'clientSecret' => 'mock_secret', + 'redirectUri' => 'none', + ]); + } + + public function tearDown() + { + m::close(); + parent::tearDown(); + } + + public function testAuthorizationUrl() + { + $url = $this->provider->getAuthorizationUrl(); + $uri = parse_url($url); + parse_str($uri['query'], $query); + + self::assertArrayHasKey('client_id', $query); + self::assertArrayHasKey('redirect_uri', $query); + self::assertArrayHasKey('state', $query); + self::assertArrayHasKey('response_type', $query); + self::assertArrayHasKey('approval_prompt', $query); + self::assertNotNull($this->provider->getState()); + } + + public function testGetAuthorizationUrl() + { + $url = $this->provider->getAuthorizationUrl(); + $uri = parse_url($url); + + self::assertEquals('/oauth/authorize', $uri['path']); + } + + public function testGetBaseAccessTokenUrl() + { + $params = []; + + $url = $this->provider->getBaseAccessTokenUrl($params); + $uri = parse_url($url); + + self::assertEquals('/oauth/token', $uri['path']); + } + + public function testGetAccessToken() + { + $response = m::mock(\Psr\Http\Message\ResponseInterface::class); + $response->shouldReceive('getBody')->andReturn('{"access_token":"mock_access_token", "token_type":"bearer"}'); + $response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); + $response->shouldReceive('getStatusCode')->andReturn(200); + + $client = m::mock(\GuzzleHttp\ClientInterface::class); + $client->shouldReceive('send')->times(1)->andReturn($response); + $this->provider->setHttpClient($client); + + $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); + + self::assertEquals('mock_access_token', $token->getToken()); + self::assertNull($token->getExpires()); + self::assertNull($token->getRefreshToken()); + self::assertNull($token->getResourceOwnerId()); + } + + public function testAuthotDomainUrls() + { + $domain = 'https://app.xn--autht-9ta.com/'; + + $response = m::mock(\Psr\Http\Message\ResponseInterface::class); + $response->shouldReceive('getBody')->times(1)->andReturn('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&otherKey={1234}'); + $response->shouldReceive('getHeader')->andReturn(['content-type' => 'application/x-www-form-urlencoded']); + $response->shouldReceive('getStatusCode')->andReturn(200); + + $client = m::mock(\GuzzleHttp\ClientInterface::class); + $client->shouldReceive('send')->times(1)->andReturn($response); + $this->provider->setHttpClient($client); + + $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); + + self::assertEquals($domain . 'oauth/authorize', $this->provider->getBaseAuthorizationUrl()); + self::assertEquals($domain . 'oauth/token', $this->provider->getBaseAccessTokenUrl([])); + self::assertEquals($domain . 'oauth/resource', $this->provider->getResourceOwnerDetailsUrl($token)); + } + + /** + * @expectedException InvalidArgumentException + **/ + public function testExceptionThrownWhenErrorObjectReceived() + { + $status = rand(400, 600); + $postResponse = m::mock(\Psr\Http\Message\ResponseInterface::class); + $postResponse->shouldReceive('getBody')->andReturn('{"message": "Validation Failed","errors": [{"resource": "Issue","field": "title","code": "missing_field"}]}'); + $postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); + $postResponse->shouldReceive('getStatusCode')->andReturn($status); + + $client = m::mock(\GuzzleHttp\ClientInterface::class); + $client->shouldReceive('send') + ->times(1) + ->andReturn($postResponse); + + $this->provider->setHttpClient($client); + + $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); + } + + /** + * @expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException + **/ + public function testExceptionThrownWhenOAuthErrorReceived() + { + $status = 200; + $postResponse = m::mock(\Psr\Http\Message\ResponseInterface::class); + $postResponse->shouldReceive('getBody')->andReturn('{"error": "bad_verification_code","error_description": "The code passed is incorrect or expired.","error_uri": "https://developer.github.com/v3/oauth/#bad-verification-code"}'); + $postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); + $postResponse->shouldReceive('getStatusCode')->andReturn($status); + + $client = m::mock(\GuzzleHttp\ClientInterface::class); + $client->shouldReceive('send') + ->times(1) + ->andReturn($postResponse); + $this->provider->setHttpClient($client); + $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); + } +} \ No newline at end of file