league-oauth2-ely/tests/ProviderTest.php

129 lines
4.8 KiB
PHP
Raw Permalink Normal View History

2016-11-25 16:56:29 +05:30
<?php
/** @noinspection PhpUnhandledExceptionInspection */
2016-11-25 16:56:29 +05:30
namespace Ely\OAuth2\Client\Test;
use Ely\OAuth2\Client\Exception\IdentityProviderException;
2016-11-25 16:56:29 +05:30
use Ely\OAuth2\Client\Provider;
use Ely\OAuth2\Client\ResourceOwner;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
2016-11-25 16:56:29 +05:30
use League\OAuth2\Client\Token\AccessToken;
use PHPUnit\Framework\TestCase;
2016-11-25 16:56:29 +05:30
class ProviderTest extends TestCase {
2018-08-20 18:09:57 +05:30
2016-11-25 16:56:29 +05:30
/**
* @var Provider
*/
private $provider;
/**
* @var MockHandler
*/
private $mockHandler;
2016-11-25 16:56:29 +05:30
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',
]);
$this->mockHandler = new MockHandler();
$client = new Client(['handler' => HandlerStack::create($this->mockHandler)]);
$this->provider->setHttpClient($client);
2016-11-25 16:56:29 +05:30
}
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']));
$this->assertSame('/api/account/v1/info', parse_url($url, PHP_URL_PATH));
2016-11-25 16:56:29 +05:30
}
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->assertSame('/oauth2/v1/mock_client_id', $uri['path']);
2016-11-25 16:56:29 +05:30
$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('scope=minecraft_server_session%2Caccount_info', $url);
2016-11-25 16:56:29 +05:30
}
2018-08-20 18:09:57 +05:30
public function testGetBaseAccessTokenUrl() {
$url = $this->provider->getBaseAccessTokenUrl([]);
$this->assertSame('/api/oauth2/v1/token', parse_url($url, PHP_URL_PATH));
2016-11-25 16:56:29 +05:30
}
2018-08-20 18:09:57 +05:30
public function testGetAccessToken() {
$this->mockHandler->append(new Response(200, ['content-type' => 'json'], $this->getAccessTokenResponse()));
2016-11-25 16:56:29 +05:30
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
$this->assertSame('mock_access_token', $token->getToken());
2016-11-25 16:56:29 +05:30
$this->assertNotNull($token->getExpires());
$this->assertNull($token->getRefreshToken());
}
2018-08-20 18:09:57 +05:30
public function testExceptionThrownWhenErrorObjectReceived() {
$this->mockHandler->append(new Response(418, ['content-type' => 'json'], json_encode([
'name' => 'Some error happened',
'message' => 'Some exception message',
'status' => 418,
2016-11-25 16:56:29 +05:30
'code' => 0,
])));
$this->setExpectedException(IdentityProviderException::class, 'Some exception message');
2016-11-25 16:56:29 +05:30
$this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
}
2018-08-20 18:09:57 +05:30
public function testExceptionThrownOnIncorrectContentType() {
$this->mockHandler->append(new Response(502, ['content-type' => 'text/html; charset=UTF-8'], 'html content'));
$this->setExpectedException(IdentityProviderException::class, 'Bad Gateway');
$this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
}
2018-08-20 18:09:57 +05:30
public function testGetResourceOwner() {
$this->mockHandler->append(new Response(200, ['content-type' => 'json'], $this->getAccessTokenResponse()));
$this->mockHandler->append(new Response(200, ['content-type' => 'json'], json_encode([
'id' => 1,
'uuid' => 'ffc8fdc9-5824-509e-8a57-c99b940fb996',
'username' => 'ErickSkrauch',
'registeredAt' => 1470566470,
'profileLink' => 'http://ely.by/u1',
'preferredLanguage' => 'be',
'email' => 'erickskrauch@ely.by',
])));
/** @var AccessToken $token */
2016-11-25 16:56:29 +05:30
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
$user = $this->provider->getResourceOwner($token);
$this->assertInstanceOf(ResourceOwner::class, $user);
$this->assertSame(0, $this->mockHandler->count());
2016-11-25 16:56:29 +05:30
}
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
}