oauth2-server/tests/unit/Grant/AbstractGrantTest.php

161 lines
5.3 KiB
PHP
Raw Normal View History

2014-01-17 16:06:57 +05:30
<?php
namespace LeagueTests\Grant;
use League\OAuth2\Server\AuthorizationServer;
2014-11-08 23:56:12 +05:30
use League\OAuth2\Server\Entity\ClientEntity;
use League\OAuth2\Server\Entity\ScopeEntity;
use League\OAuth2\Server\Exception\InvalidRequestException;
2014-11-08 23:56:12 +05:30
use League\OAuth2\Server\Grant;
2014-12-10 18:40:35 +05:30
use LeagueTests\Stubs\StubAbstractGrant;
2014-01-17 16:06:57 +05:30
use Mockery as M;
class AbstractGrantTest extends \PHPUnit_Framework_TestCase
{
2014-05-03 15:25:25 +05:30
public function testSetGet()
2014-01-17 16:06:57 +05:30
{
2014-11-08 23:56:12 +05:30
$server = new AuthorizationServer();
2014-01-17 16:06:57 +05:30
2014-11-08 23:56:12 +05:30
$grant = new StubAbstractGrant();
2014-01-17 16:06:57 +05:30
$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 AuthorizationServer);
2014-01-17 16:06:57 +05:30
}
public function testFormatScopes()
{
$server = M::mock('League\OAuth2\Server\AbstractServer');
2014-11-08 23:56:12 +05:30
$grant = new StubAbstractGrant();
2014-01-17 16:06:57 +05:30
$reflectedGrant = new \ReflectionClass('LeagueTests\Stubs\StubAbstractGrant');
$method = $reflectedGrant->getMethod('formatScopes');
$method->setAccessible(true);
$scopes = [
2014-07-11 22:49:10 +05:30
(new ScopeEntity($server))->hydrate(['id' => 'scope1', 'description' => 'foo']),
2014-11-08 23:56:12 +05:30
(new ScopeEntity($server))->hydrate(['id' => 'scope2', 'description' => 'bar']),
2014-01-17 16:06:57 +05:30
];
$result = $method->invokeArgs($grant, [$scopes]);
$this->assertTrue(isset($result['scope1']));
$this->assertTrue(isset($result['scope2']));
2014-05-02 21:51:53 +05:30
$this->assertTrue($result['scope1'] instanceof ScopeEntity);
$this->assertTrue($result['scope2'] instanceof ScopeEntity);
2014-01-17 16:06:57 +05:30
}
public function testValidateScopes()
{
2014-11-08 23:56:12 +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(
2014-07-11 22:49:10 +05:30
(new ScopeEntity($server))->hydrate(['id' => 'foo'])
2014-01-17 16:06:57 +05:30
);
$server->setScopeStorage($scopeStorage);
2014-11-08 23:56:12 +05:30
$grant = new StubAbstractGrant();
2014-01-17 16:06:57 +05:30
$grant->setAuthorizationServer($server);
2014-10-01 04:25:21 +05:30
$client = (new ClientEntity($server))->hydrate(['id' => 'testapp']);
2014-01-17 16:06:57 +05:30
$this->assertEquals(
[
2014-11-08 23:56:12 +05:30
'foo' => (new ScopeEntity($server))->hydrate(['id' => 'foo']),
2014-01-17 16:06:57 +05:30
],
2014-10-01 04:25:21 +05:30
$grant->validateScopes('foo', $client)
2014-01-17 16:06:57 +05:30
);
}
public function testValidateScopesMissingScope()
{
$this->setExpectedException('League\OAuth2\Server\Exception\InvalidRequestException');
2014-01-17 16:06:57 +05:30
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
$scopeStorage->shouldReceive('setServer');
2014-11-08 23:56:12 +05:30
$server = new AuthorizationServer();
2014-01-17 16:06:57 +05:30
$server->requireScopeParam(true);
$server->setScopeStorage($scopeStorage);
2014-11-08 23:56:12 +05:30
$grant = new StubAbstractGrant();
2014-01-17 16:06:57 +05:30
$grant->setAuthorizationServer($server);
2014-10-01 04:25:21 +05:30
$client = (new ClientEntity($server))->hydrate(['id' => 'testapp']);
$grant->validateScopes(null, $client);
2014-01-17 16:06:57 +05:30
}
public function testValidateScopesInvalidScope()
{
$this->setExpectedException('League\OAuth2\Server\Exception\InvalidScopeException');
2014-01-17 16:06:57 +05:30
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
$scopeStorage->shouldReceive('setServer');
$scopeStorage->shouldReceive('get')->andReturn(null);
2014-11-08 23:56:12 +05:30
$server = new AuthorizationServer();
2014-01-17 16:06:57 +05:30
$server->setScopeStorage($scopeStorage);
2014-11-08 23:56:12 +05:30
$grant = new StubAbstractGrant();
2014-01-17 16:06:57 +05:30
$grant->setAuthorizationServer($server);
2014-10-01 04:25:21 +05:30
$client = (new ClientEntity($server))->hydrate(['id' => 'testapp']);
$grant->validateScopes('blah', $client);
2014-01-17 16:06:57 +05:30
}
public function testValidateScopesDefaultScope()
{
2014-11-08 23:56:12 +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(
2014-07-11 22:49:10 +05:30
(new ScopeEntity($server))->hydrate(['id' => 'foo'])
2014-01-17 16:06:57 +05:30
);
$server->setScopeStorage($scopeStorage);
$server->requireScopeParam(true);
$server->setScopeStorage($scopeStorage);
$server->setDefaultScope('foo');
2014-11-08 23:56:12 +05:30
$grant = new StubAbstractGrant();
2014-01-17 16:06:57 +05:30
$grant->setAuthorizationServer($server);
2014-10-01 04:25:21 +05:30
$client = (new ClientEntity($server))->hydrate(['id' => 'testapp']);
$grant->validateScopes(null, $client);
2014-01-17 16:06:57 +05:30
}
public function testValidateScopesDefaultScopeArray()
{
2014-11-08 23:56:12 +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(
2014-07-11 22:49:10 +05:30
(new ScopeEntity($server))->hydrate(['id' => 'foo'])
2014-01-17 16:06:57 +05:30
);
$server->setScopeStorage($scopeStorage);
$server->requireScopeParam(true);
$server->setScopeStorage($scopeStorage);
$server->setDefaultScope(['foo', 'bar']);
2014-11-08 23:56:12 +05:30
$grant = new StubAbstractGrant();
2014-01-17 16:06:57 +05:30
$grant->setAuthorizationServer($server);
2014-10-01 04:25:21 +05:30
$client = (new ClientEntity($server))->hydrate(['id' => 'testapp']);
$grant->validateScopes(null, $client);
2014-01-17 16:06:57 +05:30
}
}