league-oauth2-ely/tests/ProviderTest.php

174 lines
6.7 KiB
PHP
Raw Normal View History

2016-11-25 16:56:29 +05:30
<?php
namespace Ely\OAuth2\Client\Test;
use Ely\OAuth2\Client\Provider;
use Ely\OAuth2\Client\ResourceOwner;
use GuzzleHttp\ClientInterface;
use League\OAuth2\Client\Token\AccessToken;
use Mockery as m;
use Psr\Http\Message\ResponseInterface;
2018-08-20 18:09:57 +05:30
class ProviderTest extends \PHPUnit_Framework_TestCase {
2016-11-25 16:56:29 +05:30
/**
* @var Provider
*/
protected $provider;
2018-08-20 18:09:57 +05:30
protected function setUp() {
2016-11-25 16:56:29 +05:30
$this->provider = new Provider([
'clientId' => 'mock_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',
]);
}
2018-08-20 18:09:57 +05:30
public function tearDown() {
2016-11-25 16:56:29 +05:30
m::close();
parent::tearDown();
}
2018-08-20 18:09:57 +05:30
public function testGetResourceOwnerDetailsUrl() {
2016-11-25 16:56:29 +05:30
$url = $this->provider->getResourceOwnerDetailsUrl(new AccessToken(['access_token' => 'mock_token']));
$uri = parse_url($url);
$this->assertEquals('/api/account/v1/info', $uri['path']);
}
2018-08-20 18:09:57 +05:30
public function testGetAuthorizationUrl() {
2016-11-25 16:56:29 +05:30
$url = $this->provider->getAuthorizationUrl();
$uri = parse_url($url);
parse_str($uri['query'], $query);
$this->assertEquals('/oauth2/v1/mock_client_id', $uri['path']);
$this->assertArrayNotHasKey('client_id', $query);
$this->assertArrayHasKey('redirect_uri', $query);
$this->assertArrayHasKey('state', $query);
$this->assertArrayHasKey('scope', $query);
$this->assertArrayHasKey('response_type', $query);
$this->assertArrayNotHasKey('approval_prompt', $query);
$this->assertNotNull($this->provider->getState());
}
2018-08-20 18:09:57 +05:30
public function testScopes() {
2016-11-25 16:56:29 +05:30
$options = ['scope' => ['minecraft_server_session', 'account_info']];
$url = $this->provider->getAuthorizationUrl($options);
$this->assertContains(urlencode(implode(',', $options['scope'])), $url);
}
2018-08-20 18:09:57 +05:30
public function testGetBaseAccessTokenUrl() {
2016-11-25 16:56:29 +05:30
$params = [];
$url = $this->provider->getBaseAccessTokenUrl($params);
$uri = parse_url($url);
$this->assertEquals('/api/oauth2/v1/token', $uri['path']);
}
2018-08-20 18:09:57 +05:30
public function testGetAccessToken() {
2016-11-25 16:56:29 +05:30
/** @var m\Mock|ResponseInterface $response */
$response = m::mock(ResponseInterface::class);
$response->shouldReceive('getBody')->andReturn($this->getAccessTokenResponse());
$response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$response->shouldReceive('getStatusCode')->andReturn(200);
/** @var m\Mock|ClientInterface $client */
$client = m::mock(ClientInterface::class);
$client->shouldReceive('send')->times(1)->andReturn($response);
$this->provider->setHttpClient($client);
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
$this->assertEquals('mock_access_token', $token->getToken());
$this->assertNotNull($token->getExpires());
$this->assertNull($token->getRefreshToken());
}
/**
* @expectedException \Ely\OAuth2\Client\Exception\IdentityProviderException
* @expectedExceptionMessageRegExp /Exception message .+/
*/
2018-08-20 18:09:57 +05:30
public function testExceptionThrownWhenErrorObjectReceived() {
2016-11-25 16:56:29 +05:30
$name = 'Error ' . uniqid();
$message = 'Exception message ' . uniqid();
2018-08-20 18:09:57 +05:30
$status = mt_rand(400, 600);
2016-11-25 16:56:29 +05:30
/** @var m\Mock|ResponseInterface $postResponse */
$postResponse = m::mock(ResponseInterface::class);
$postResponse->shouldReceive('getBody')->andReturn(json_encode([
'name' => $name,
'message' => $message,
'status' => $status,
'code' => 0,
]));
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$postResponse->shouldReceive('getStatusCode')->andReturn($status);
/** @var m\Mock|ClientInterface $client */
$client = m::mock(ClientInterface::class);
$client->shouldReceive('send')
->times(1)
->andReturn($postResponse);
$this->provider->setHttpClient($client);
$this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
}
/**
* @expectedException \Ely\OAuth2\Client\Exception\IdentityProviderException
* @expectedExceptionMessage Bad Gateway
*/
2018-08-20 18:09:57 +05:30
public function testExceptionThrownOnIncorrectContentType() {
/** @var m\Mock|ResponseInterface $postResponse */
$postResponse = m::mock(ResponseInterface::class);
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'text/html; charset=UTF-8']);
$postResponse->shouldReceive('getBody')->andReturn('html content');
$postResponse->shouldReceive('getStatusCode')->andReturn(502);
$postResponse->shouldReceive('getReasonPhrase')->andReturn('Bad Gateway');
/** @var m\Mock|ClientInterface $client */
$client = m::mock(ClientInterface::class);
$client->shouldReceive('send')
->times(1)
->andReturn($postResponse);
$this->provider->setHttpClient($client);
$this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
}
2018-08-20 18:09:57 +05:30
public function testGetResourceOwner() {
2016-11-25 16:56:29 +05:30
/** @var m\Mock|ResponseInterface $postResponse */
$postResponse = m::mock(ResponseInterface::class);
$postResponse->shouldReceive('getBody')->andReturn($this->getAccessTokenResponse());
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$postResponse->shouldReceive('getStatusCode')->andReturn(200);
/** @var m\Mock|ResponseInterface $userResponse */
$userResponse = m::mock(ResponseInterface::class);
$userResponse->shouldReceive('getBody')->andReturn(
file_get_contents(__DIR__ . '/data/identity-info-response.json')
);
$userResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$userResponse->shouldReceive('getStatusCode')->andReturn(200);
/** @var m\Mock|ClientInterface $client */
$client = m::mock(ClientInterface::class);
$client->shouldReceive('send')
2018-08-20 18:09:57 +05:30
->times(2)
->andReturn($postResponse, $userResponse);
2016-11-25 16:56:29 +05:30
$this->provider->setHttpClient($client);
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
$user = $this->provider->getResourceOwner($token);
$this->assertInstanceOf(ResourceOwner::class, $user);
}
2018-08-20 18:09:57 +05:30
private function getAccessTokenResponse() {
2016-11-25 16:56:29 +05:30
return json_encode([
'access_token' => 'mock_access_token',
'token_type' => 'bearer',
'expires_in' => 3600,
]);
}
2018-08-20 18:09:57 +05:30
2016-11-25 16:56:29 +05:30
}