2014-01-17 16:06:57 +05:30
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace LeagueTests\Grant;
|
|
|
|
|
|
|
|
use League\OAuth2\Server\Grant;
|
|
|
|
use League\OAuth2\Server\Entity\Scope;
|
2014-02-24 20:12:22 +05:30
|
|
|
use League\OAuth2\Server\AuthorizationServer;
|
2014-01-17 16:06:57 +05:30
|
|
|
use League\OAuth2\Server\Grant\ClientException;
|
|
|
|
use LeagueTests\Stubs\StubAbstractGrant;
|
|
|
|
use Mockery as M;
|
|
|
|
|
|
|
|
class AbstractGrantTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
function testSetGet()
|
|
|
|
{
|
2014-02-24 20:12:22 +05:30
|
|
|
$server = new AuthorizationServer;
|
2014-01-17 16:06:57 +05:30
|
|
|
|
|
|
|
$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());
|
2014-02-24 20:12:22 +05:30
|
|
|
$this->assertTrue($grant->getAuthorizationServer() instanceof AuthorizationServer);
|
2014-01-17 16:06:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2014-02-24 20:12:22 +05:30
|
|
|
$server = new AuthorizationServer;
|
2014-01-17 16:06:57 +05:30
|
|
|
|
|
|
|
$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');
|
|
|
|
|
2014-02-24 20:12:22 +05:30
|
|
|
$server = new AuthorizationServer;
|
2014-01-17 16:06:57 +05:30
|
|
|
$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);
|
|
|
|
|
2014-02-24 20:12:22 +05:30
|
|
|
$server = new AuthorizationServer;
|
2014-01-17 16:06:57 +05:30
|
|
|
$server->setScopeStorage($scopeStorage);
|
|
|
|
|
|
|
|
$grant = new StubAbstractGrant;
|
|
|
|
$grant->setAuthorizationServer($server);
|
|
|
|
|
|
|
|
$grant->validateScopes('blah');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateScopesDefaultScope()
|
|
|
|
{
|
2014-02-24 20:12:22 +05:30
|
|
|
$server = new AuthorizationServer;
|
2014-01-17 16:06:57 +05:30
|
|
|
|
|
|
|
$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()
|
|
|
|
{
|
2014-02-24 20:12:22 +05:30
|
|
|
$server = new AuthorizationServer;
|
2014-01-17 16:06:57 +05:30
|
|
|
|
|
|
|
$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();
|
|
|
|
}
|
|
|
|
}
|