2014-01-16 22:21:06 +05:30
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace LeagueTests;
|
|
|
|
|
2014-02-24 20:13:26 +05:30
|
|
|
use League\OAuth2\Server\ResourceServer;
|
2014-05-02 19:44:12 +05:30
|
|
|
use League\OAuth2\Server\Entity\AccessTokenEntity;
|
|
|
|
use League\OAuth2\Server\Entity\SessionEntity;
|
|
|
|
use League\OAuth2\Server\Entity\ClientEntity;
|
|
|
|
use League\OAuth2\Server\Entity\ScopeEntity;
|
2014-01-16 22:21:06 +05:30
|
|
|
use \Mockery as M;
|
|
|
|
|
2014-05-01 19:03:11 +05:30
|
|
|
class ResourceServerTest extends \PHPUnit_Framework_TestCase
|
2014-01-16 22:21:06 +05:30
|
|
|
{
|
|
|
|
private function returnDefault()
|
|
|
|
{
|
|
|
|
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
|
|
|
|
$sessionStorage->shouldReceive('setServer');
|
|
|
|
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
|
|
|
|
$accessTokenStorage->shouldReceive('setServer');
|
|
|
|
$clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface');
|
|
|
|
$clientStorage->shouldReceive('setServer');
|
|
|
|
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
|
|
|
|
$scopeStorage->shouldReceive('setServer');
|
|
|
|
|
2014-02-24 20:13:26 +05:30
|
|
|
$server = new ResourceServer(
|
2014-01-16 22:21:06 +05:30
|
|
|
$sessionStorage,
|
|
|
|
$accessTokenStorage,
|
|
|
|
$clientStorage,
|
|
|
|
$scopeStorage
|
|
|
|
);
|
|
|
|
|
|
|
|
return $server;
|
|
|
|
}
|
|
|
|
|
2014-05-03 15:25:25 +05:30
|
|
|
public function testGetSet()
|
2014-01-16 22:21:06 +05:30
|
|
|
{
|
|
|
|
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
|
|
|
|
$sessionStorage->shouldReceive('setServer');
|
|
|
|
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
|
|
|
|
$accessTokenStorage->shouldReceive('setServer');
|
|
|
|
$clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface');
|
|
|
|
$clientStorage->shouldReceive('setServer');
|
|
|
|
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
|
|
|
|
$scopeStorage->shouldReceive('setServer');
|
|
|
|
|
2014-02-24 20:13:26 +05:30
|
|
|
$server = new ResourceServer(
|
2014-01-16 22:21:06 +05:30
|
|
|
$sessionStorage,
|
|
|
|
$accessTokenStorage,
|
|
|
|
$clientStorage,
|
|
|
|
$scopeStorage
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDetermineAccessTokenMissingToken()
|
|
|
|
{
|
2014-05-01 19:03:11 +05:30
|
|
|
$this->setExpectedException('League\OAuth2\Server\Exception\InvalidRequestException');
|
2014-01-16 22:21:06 +05:30
|
|
|
|
|
|
|
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
|
|
|
|
$sessionStorage->shouldReceive('setServer');
|
|
|
|
|
|
|
|
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
|
|
|
|
$accessTokenStorage->shouldReceive('setServer');
|
|
|
|
$accessTokenStorage->shouldReceive('get')->andReturn(false);
|
|
|
|
|
|
|
|
$clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface');
|
|
|
|
$clientStorage->shouldReceive('setServer');
|
|
|
|
|
|
|
|
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
|
|
|
|
$scopeStorage->shouldReceive('setServer');
|
|
|
|
|
2014-02-24 20:13:26 +05:30
|
|
|
$server = new ResourceServer(
|
2014-01-16 22:21:06 +05:30
|
|
|
$sessionStorage,
|
|
|
|
$accessTokenStorage,
|
|
|
|
$clientStorage,
|
|
|
|
$scopeStorage
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new \Symfony\Component\HttpFoundation\Request();
|
|
|
|
$request->headers = new \Symfony\Component\HttpFoundation\ParameterBag([
|
|
|
|
'HTTP_AUTHORIZATION' => 'Bearer'
|
|
|
|
]);
|
|
|
|
$server->setRequest($request);
|
|
|
|
|
|
|
|
$reflector = new \ReflectionClass($server);
|
|
|
|
$method = $reflector->getMethod('determineAccessToken');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
$method->invoke($server);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsValidNotValid()
|
|
|
|
{
|
|
|
|
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
|
|
|
|
$sessionStorage->shouldReceive('setServer');
|
|
|
|
|
|
|
|
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
|
|
|
|
$accessTokenStorage->shouldReceive('setServer');
|
|
|
|
$accessTokenStorage->shouldReceive('get')->andReturn(false);
|
|
|
|
|
|
|
|
$clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface');
|
|
|
|
$clientStorage->shouldReceive('setServer');
|
|
|
|
|
|
|
|
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
|
|
|
|
$scopeStorage->shouldReceive('setServer');
|
|
|
|
|
2014-02-24 20:13:26 +05:30
|
|
|
$server = new ResourceServer(
|
2014-01-16 22:21:06 +05:30
|
|
|
$sessionStorage,
|
|
|
|
$accessTokenStorage,
|
|
|
|
$clientStorage,
|
|
|
|
$scopeStorage
|
|
|
|
);
|
|
|
|
|
2014-05-08 14:59:40 +05:30
|
|
|
$this->setExpectedException('League\OAuth2\Server\Exception\AccessDeniedException');
|
|
|
|
$server->isValidRequest(false, 'foobar');
|
2014-01-16 22:21:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsValid()
|
|
|
|
{
|
|
|
|
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
|
|
|
|
$sessionStorage->shouldReceive('setServer');
|
|
|
|
|
|
|
|
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
|
|
|
|
$accessTokenStorage->shouldReceive('setServer');
|
|
|
|
|
|
|
|
$clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface');
|
|
|
|
$clientStorage->shouldReceive('setServer');
|
|
|
|
|
|
|
|
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
|
|
|
|
$scopeStorage->shouldReceive('setServer');
|
|
|
|
|
2014-02-24 20:13:26 +05:30
|
|
|
$server = new ResourceServer(
|
2014-01-16 22:21:06 +05:30
|
|
|
$sessionStorage,
|
|
|
|
$accessTokenStorage,
|
|
|
|
$clientStorage,
|
|
|
|
$scopeStorage
|
|
|
|
);
|
|
|
|
|
2014-07-11 22:49:10 +05:30
|
|
|
$server->setIdKey('at');
|
2014-01-16 22:21:06 +05:30
|
|
|
|
2014-07-22 16:15:19 +05:30
|
|
|
$server->addEventListener('session.owner', function($event) {
|
|
|
|
$this->assertTrue($event->getSession() instanceof \League\OAuth2\Server\Entity\SessionEntity);
|
|
|
|
});
|
|
|
|
|
2014-01-16 22:21:06 +05:30
|
|
|
$accessTokenStorage->shouldReceive('get')->andReturn(
|
2014-07-11 22:49:10 +05:30
|
|
|
(new AccessTokenEntity($server))->setId('abcdef')
|
2014-01-16 22:21:06 +05:30
|
|
|
);
|
|
|
|
|
2014-07-12 13:28:18 +05:30
|
|
|
$accessTokenStorage->shouldReceive('getScopes')->andReturn([
|
|
|
|
(new ScopeEntity($server))->hydrate(['id' => 'foo']),
|
|
|
|
(new ScopeEntity($server))->hydrate(['id' => 'bar'])
|
|
|
|
]);
|
2014-01-16 22:21:06 +05:30
|
|
|
|
|
|
|
$sessionStorage->shouldReceive('getByAccessToken')->andReturn(
|
2014-05-02 21:51:53 +05:30
|
|
|
(new SessionEntity($server))->setId('foobar')->setOwner('user', 123)
|
2014-01-16 22:21:06 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
$clientStorage->shouldReceive('getBySession')->andReturn(
|
2014-07-11 22:49:10 +05:30
|
|
|
(new ClientEntity($server))->hydrate(['id' => 'testapp'])
|
2014-01-16 22:21:06 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
$request = new \Symfony\Component\HttpFoundation\Request();
|
|
|
|
$request->headers = new \Symfony\Component\HttpFoundation\ParameterBag([
|
|
|
|
'Authorization' => 'Bearer abcdef'
|
|
|
|
]);
|
|
|
|
$server->setRequest($request);
|
|
|
|
|
2014-05-01 19:03:11 +05:30
|
|
|
$this->assertTrue($server->isValidRequest());
|
2014-07-11 22:49:10 +05:30
|
|
|
$this->assertEquals('at', $server->getIdKey());
|
2014-01-16 22:21:06 +05:30
|
|
|
$this->assertEquals(123, $server->getOwnerId());
|
|
|
|
$this->assertEquals('user', $server->getOwnerType());
|
|
|
|
$this->assertEquals('abcdef', $server->getAccessToken());
|
|
|
|
$this->assertEquals('testapp', $server->getClientId());
|
|
|
|
$this->assertTrue($server->hasScope('foo'));
|
|
|
|
$this->assertTrue($server->hasScope('bar'));
|
|
|
|
$this->assertTrue($server->hasScope(['foo', 'bar']));
|
|
|
|
$this->assertTrue(isset($server->getScopes()['foo']));
|
|
|
|
$this->assertTrue(isset($server->getScopes()['bar']));
|
|
|
|
$this->assertFalse($server->hasScope(['foobar']));
|
|
|
|
$this->assertFalse($server->hasScope('foobar'));
|
|
|
|
}
|
|
|
|
}
|