Commit cbe1ae22 authored by Thomas Persohn's avatar Thomas Persohn

add unit tests

parent 47175d16
Pipeline #217 skipped
...@@ -52,7 +52,7 @@ if (!isset($_GET['code'])) { ...@@ -52,7 +52,7 @@ if (!isset($_GET['code'])) {
## Testing ## Testing
``` bash ``` bash
$ ./vendor/bin/phpunit $ ./vendor/bin/phpunit tests
``` ```
## License ## License
......
{ {
"name": "libcast/oauth2-authot", "name": "libcast/oauth2-authot",
"description": "Authot Provider for OAuth 2.0 Client", "description": "Authôt OAuth 2.0 Client Provider for The PHP League OAuth2-Client",
"homepage": "https://code.libcast.net/libcast/oauth2-authot",
"license": "MIT", "license": "MIT",
"authors": [ "authors": [
{ {
...@@ -17,9 +16,18 @@ ...@@ -17,9 +16,18 @@
"php": ">=5.5.0", "php": ">=5.5.0",
"league/oauth2-client": "^1.4" "league/oauth2-client": "^1.4"
}, },
"require-dev": {
"phpunit/phpunit": "~4.0",
"mockery/mockery": "~0.9"
},
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"Libcast\\OAuth2\\Client\\": "src/" "Libcast\\OAuth2\\Client\\": "src/"
} }
},
"autoload-dev": {
"psr-4": {
"Libcast\\OAuth2\\Client\\Test\\": "tests/src/"
}
} }
} }
\ No newline at end of file
<?php
use Mockery as m;
class AuthotTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \League\OAuth2\Client\Provider\AbstractProvider
*/
protected $provider;
protected function setUp()
{
$this->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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment