mirror of
https://github.com/elyby/oauth2-server.git
synced 2024-12-22 13:09:44 +05:30
Some initial grant testing
This commit is contained in:
parent
40ea409aed
commit
20df1f50a6
@ -13,6 +13,7 @@ namespace League\OAuth2\Server\Grant;
|
||||
|
||||
use League\OAuth2\Server\Authorization;
|
||||
use League\OAuth2\Server\Entity\Scope;
|
||||
use League\OAuth2\Server\Exception\ClientException;
|
||||
|
||||
/**
|
||||
* Abstract grant class
|
||||
|
150
tests/Grant/AbstractGrantTest.php
Normal file
150
tests/Grant/AbstractGrantTest.php
Normal file
@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
namespace LeagueTests\Grant;
|
||||
|
||||
use League\OAuth2\Server\Grant;
|
||||
use League\OAuth2\Server\Entity\Scope;
|
||||
use League\OAuth2\Server\Authorization;
|
||||
use League\OAuth2\Server\Grant\ClientException;
|
||||
use LeagueTests\Stubs\StubAbstractGrant;
|
||||
use Mockery as M;
|
||||
|
||||
class AbstractGrantTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
function testSetGet()
|
||||
{
|
||||
$server = new Authorization;
|
||||
|
||||
$grant = new StubAbstractGrant;
|
||||
$grant->setIdentifier('foobar');
|
||||
$grant->setAccessTokenTTL(300);
|
||||
$grant->setAuthorizationServer($server);
|
||||
|
||||
$this->assertEquals('foobar', $grant->getIdentifier());
|
||||
$this->assertEquals('foobar', $grant->getResponseType());
|
||||
$this->assertEquals(300, $grant->getAccessTokenTTL());
|
||||
$this->assertTrue($grant->getAuthorizationServer() instanceof Authorization);
|
||||
}
|
||||
|
||||
public function testFormatScopes()
|
||||
{
|
||||
$server = M::mock('League\OAuth2\Server\AbstractServer');
|
||||
|
||||
$grant = new StubAbstractGrant;
|
||||
$reflectedGrant = new \ReflectionClass('LeagueTests\Stubs\StubAbstractGrant');
|
||||
$method = $reflectedGrant->getMethod('formatScopes');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$scopes = [
|
||||
(new Scope($server))->setId('scope1')->setDescription('foo'),
|
||||
(new Scope($server))->setId('scope2')->setDescription('bar')
|
||||
];
|
||||
|
||||
$result = $method->invokeArgs($grant, [$scopes]);
|
||||
|
||||
$this->assertTrue(isset($result['scope1']));
|
||||
$this->assertTrue(isset($result['scope2']));
|
||||
$this->assertTrue($result['scope1'] instanceof Scope);
|
||||
$this->assertTrue($result['scope2'] instanceof Scope);
|
||||
}
|
||||
|
||||
public function testValidateScopes()
|
||||
{
|
||||
$server = new Authorization;
|
||||
|
||||
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
|
||||
$scopeStorage->shouldReceive('setServer');
|
||||
$scopeStorage->shouldReceive('get')->andReturn(
|
||||
(new Scope($server))->setId('foo')
|
||||
);
|
||||
|
||||
$server->setScopeStorage($scopeStorage);
|
||||
|
||||
$grant = new StubAbstractGrant;
|
||||
$grant->setAuthorizationServer($server);
|
||||
|
||||
$this->assertEquals(
|
||||
[
|
||||
'foo' => (new Scope($server))->setId('foo')
|
||||
],
|
||||
|
||||
$grant->validateScopes('foo')
|
||||
);
|
||||
}
|
||||
|
||||
public function testValidateScopesMissingScope()
|
||||
{
|
||||
$this->setExpectedException('League\OAuth2\Server\Exception\ClientException');
|
||||
|
||||
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
|
||||
$scopeStorage->shouldReceive('setServer');
|
||||
|
||||
$server = new Authorization;
|
||||
$server->requireScopeParam(true);
|
||||
$server->setScopeStorage($scopeStorage);
|
||||
|
||||
$grant = new StubAbstractGrant;
|
||||
$grant->setAuthorizationServer($server);
|
||||
|
||||
$grant->validateScopes();
|
||||
}
|
||||
|
||||
public function testValidateScopesInvalidScope()
|
||||
{
|
||||
$this->setExpectedException('League\OAuth2\Server\Exception\ClientException');
|
||||
|
||||
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
|
||||
$scopeStorage->shouldReceive('setServer');
|
||||
$scopeStorage->shouldReceive('get')->andReturn(null);
|
||||
|
||||
$server = new Authorization;
|
||||
$server->setScopeStorage($scopeStorage);
|
||||
|
||||
$grant = new StubAbstractGrant;
|
||||
$grant->setAuthorizationServer($server);
|
||||
|
||||
$grant->validateScopes('blah');
|
||||
}
|
||||
|
||||
public function testValidateScopesDefaultScope()
|
||||
{
|
||||
$server = new Authorization;
|
||||
|
||||
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
|
||||
$scopeStorage->shouldReceive('setServer');
|
||||
$scopeStorage->shouldReceive('get')->andReturn(
|
||||
(new Scope($server))->setId('foo')
|
||||
);
|
||||
$server->setScopeStorage($scopeStorage);
|
||||
|
||||
$server->requireScopeParam(true);
|
||||
$server->setScopeStorage($scopeStorage);
|
||||
$server->setDefaultScope('foo');
|
||||
|
||||
$grant = new StubAbstractGrant;
|
||||
$grant->setAuthorizationServer($server);
|
||||
|
||||
$grant->validateScopes();
|
||||
}
|
||||
|
||||
public function testValidateScopesDefaultScopeArray()
|
||||
{
|
||||
$server = new Authorization;
|
||||
|
||||
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
|
||||
$scopeStorage->shouldReceive('setServer');
|
||||
$scopeStorage->shouldReceive('get')->andReturn(
|
||||
(new Scope($server))->setId('foo')
|
||||
);
|
||||
$server->setScopeStorage($scopeStorage);
|
||||
|
||||
$server->requireScopeParam(true);
|
||||
$server->setScopeStorage($scopeStorage);
|
||||
$server->setDefaultScope(['foo', 'bar']);
|
||||
|
||||
$grant = new StubAbstractGrant;
|
||||
$grant->setAuthorizationServer($server);
|
||||
|
||||
$grant->validateScopes();
|
||||
}
|
||||
}
|
202
tests/Grant/ClientCredentialsTest.php
Normal file
202
tests/Grant/ClientCredentialsTest.php
Normal file
@ -0,0 +1,202 @@
|
||||
<?php
|
||||
|
||||
namespace LeagueTests\Grant;
|
||||
|
||||
use League\OAuth2\Server\Grant\ClientCredentials;
|
||||
use League\OAuth2\Server\Entity\Scope;
|
||||
use League\OAuth2\Server\Entity\Client;
|
||||
use League\OAuth2\Server\Authorization;
|
||||
use League\OAuth2\Server\Grant\ClientException;
|
||||
use Mockery as M;
|
||||
|
||||
class ClientCredentialsTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
function testCompleteFlowMissingClientId()
|
||||
{
|
||||
$this->setExpectedException('League\OAuth2\Server\Exception\ClientException');
|
||||
|
||||
$_POST['grant_type'] = 'client_credentials';
|
||||
|
||||
$server = new Authorization;
|
||||
$grant = new ClientCredentials;
|
||||
|
||||
$server->addGrantType($grant);
|
||||
$server->issueAccessToken();
|
||||
|
||||
}
|
||||
|
||||
function testCompleteFlowMissingClientSecret()
|
||||
{
|
||||
$this->setExpectedException('League\OAuth2\Server\Exception\ClientException');
|
||||
|
||||
$_POST = [
|
||||
'grant_type' => 'client_credentials',
|
||||
'client_id' => 'testapp'
|
||||
];
|
||||
|
||||
$server = new Authorization;
|
||||
$grant = new ClientCredentials;
|
||||
|
||||
$server->addGrantType($grant);
|
||||
$server->issueAccessToken();
|
||||
}
|
||||
|
||||
function testCompleteFlowInvalidClient()
|
||||
{
|
||||
$this->setExpectedException('League\OAuth2\Server\Exception\ClientException');
|
||||
|
||||
$_POST = [
|
||||
'grant_type' => 'client_credentials',
|
||||
'client_id' => 'testapp',
|
||||
'client_secret' => 'foobar'
|
||||
];
|
||||
|
||||
$server = new Authorization;
|
||||
$grant = new ClientCredentials;
|
||||
|
||||
$clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface');
|
||||
$clientStorage->shouldReceive('setServer');
|
||||
$clientStorage->shouldReceive('get')->andReturn(null);
|
||||
|
||||
$server->setClientStorage($clientStorage);
|
||||
|
||||
$server->addGrantType($grant);
|
||||
$server->issueAccessToken();
|
||||
}
|
||||
|
||||
function testCompleteFlowInvalidScope()
|
||||
{
|
||||
$this->setExpectedException('League\OAuth2\Server\Exception\ClientException');
|
||||
|
||||
$_POST = [
|
||||
'grant_type' => 'client_credentials',
|
||||
'client_id' => 'testapp',
|
||||
'client_secret' => 'foobar',
|
||||
'scope' => 'foo'
|
||||
];
|
||||
|
||||
$server = new Authorization;
|
||||
$grant = new ClientCredentials;
|
||||
|
||||
$clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface');
|
||||
$clientStorage->shouldReceive('setServer');
|
||||
$clientStorage->shouldReceive('get')->andReturn(
|
||||
(new Client($server))->setId('testapp')
|
||||
);
|
||||
|
||||
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
|
||||
$sessionStorage->shouldReceive('setServer');
|
||||
$sessionStorage->shouldReceive('create');
|
||||
$sessionStorage->shouldReceive('getScopes')->andReturn([]);
|
||||
|
||||
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
|
||||
$accessTokenStorage->shouldReceive('setServer');
|
||||
$accessTokenStorage->shouldReceive('create');
|
||||
$accessTokenStorage->shouldReceive('getScopes')->andReturn([]);
|
||||
|
||||
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
|
||||
$scopeStorage->shouldReceive('setServer');
|
||||
$scopeStorage->shouldReceive('get')->andReturn(null);
|
||||
|
||||
$server->setClientStorage($clientStorage);
|
||||
$server->setScopeStorage($scopeStorage);
|
||||
$server->setSessionStorage($sessionStorage);
|
||||
$server->setAccessTokenStorage($accessTokenStorage);
|
||||
|
||||
$server->addGrantType($grant);
|
||||
$server->issueAccessToken();
|
||||
}
|
||||
|
||||
function testCompleteFlowNoScopes()
|
||||
{
|
||||
$_POST = [
|
||||
'grant_type' => 'client_credentials',
|
||||
'client_id' => 'testapp',
|
||||
'client_secret' => 'foobar'
|
||||
];
|
||||
|
||||
$server = new Authorization;
|
||||
$grant = new ClientCredentials;
|
||||
|
||||
$clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface');
|
||||
$clientStorage->shouldReceive('setServer');
|
||||
$clientStorage->shouldReceive('get')->andReturn(
|
||||
(new Client($server))->setId('testapp')
|
||||
);
|
||||
|
||||
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
|
||||
$sessionStorage->shouldReceive('setServer');
|
||||
$sessionStorage->shouldReceive('create')->andreturn(123);
|
||||
$sessionStorage->shouldReceive('getScopes')->shouldReceive('getScopes')->andReturn([]);
|
||||
$sessionStorage->shouldReceive('associateScope');
|
||||
|
||||
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
|
||||
$accessTokenStorage->shouldReceive('setServer');
|
||||
$accessTokenStorage->shouldReceive('create');
|
||||
$accessTokenStorage->shouldReceive('getScopes')->andReturn([]);
|
||||
$accessTokenStorage->shouldReceive('associateScope');
|
||||
|
||||
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
|
||||
$scopeStorage->shouldReceive('setServer');
|
||||
// $scopeStorage->shouldReceive('get')->andReturn(
|
||||
// // (new Scope($server))->setId('foo')
|
||||
// );
|
||||
|
||||
$server->setClientStorage($clientStorage);
|
||||
$server->setScopeStorage($scopeStorage);
|
||||
$server->setSessionStorage($sessionStorage);
|
||||
$server->setAccessTokenStorage($accessTokenStorage);
|
||||
|
||||
$server->addGrantType($grant);
|
||||
$server->issueAccessToken();
|
||||
}
|
||||
|
||||
function testCompleteFlow()
|
||||
{
|
||||
$_POST = [
|
||||
'grant_type' => 'client_credentials',
|
||||
'client_id' => 'testapp',
|
||||
'client_secret' => 'foobar',
|
||||
'scope' => 'foo'
|
||||
];
|
||||
|
||||
$server = new Authorization;
|
||||
$grant = new ClientCredentials;
|
||||
|
||||
$clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface');
|
||||
$clientStorage->shouldReceive('setServer');
|
||||
$clientStorage->shouldReceive('get')->andReturn(
|
||||
(new Client($server))->setId('testapp')
|
||||
);
|
||||
|
||||
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
|
||||
$sessionStorage->shouldReceive('setServer');
|
||||
$sessionStorage->shouldReceive('create')->andreturn(123);
|
||||
$sessionStorage->shouldReceive('getScopes')->shouldReceive('getScopes')->andReturn([
|
||||
(new Scope($server))->setId('foo')
|
||||
]);
|
||||
$sessionStorage->shouldReceive('associateScope');
|
||||
|
||||
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
|
||||
$accessTokenStorage->shouldReceive('setServer');
|
||||
$accessTokenStorage->shouldReceive('create');
|
||||
$accessTokenStorage->shouldReceive('getScopes')->andReturn([
|
||||
(new Scope($server))->setId('foo')
|
||||
]);
|
||||
$accessTokenStorage->shouldReceive('associateScope');
|
||||
|
||||
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
|
||||
$scopeStorage->shouldReceive('setServer');
|
||||
$scopeStorage->shouldReceive('get')->andReturn(
|
||||
(new Scope($server))->setId('foo')
|
||||
);
|
||||
|
||||
$server->setClientStorage($clientStorage);
|
||||
$server->setScopeStorage($scopeStorage);
|
||||
$server->setSessionStorage($sessionStorage);
|
||||
$server->setAccessTokenStorage($accessTokenStorage);
|
||||
|
||||
$server->addGrantType($grant);
|
||||
$server->issueAccessToken();
|
||||
}
|
||||
}
|
23
tests/Stubs/StubAbstractGrant.php
Normal file
23
tests/Stubs/StubAbstractGrant.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace LeagueTests\Stubs;
|
||||
|
||||
class StubAbstractGrant extends \League\OAuth2\Server\Grant\AbstractGrant
|
||||
{
|
||||
protected $responseType = 'foobar';
|
||||
|
||||
public function completeFlow()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getAccessTokenTTL()
|
||||
{
|
||||
return $this->accessTokenTTL;
|
||||
}
|
||||
|
||||
public function getAuthorizationServer()
|
||||
{
|
||||
return $this->server;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user