oauth2-server/tests/unit/ResourceServerTest.php

227 lines
8.2 KiB
PHP
Raw Normal View History

2014-01-16 22:21:06 +05:30
<?php
namespace LeagueTests;
2014-05-02 19:44:12 +05:30
use League\OAuth2\Server\Entity\AccessTokenEntity;
use League\OAuth2\Server\Entity\ClientEntity;
use League\OAuth2\Server\Entity\ScopeEntity;
2014-11-08 23:56:12 +05:30
use League\OAuth2\Server\Entity\SessionEntity;
use League\OAuth2\Server\ResourceServer;
use Mockery as M;
2014-01-16 22:21:06 +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()
{
$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([
2014-11-08 23:56:12 +05:30
'HTTP_AUTHORIZATION' => 'Bearer',
2014-01-16 22:21:06 +05:30
]);
$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
);
$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-11-08 23:56:12 +05:30
$server->addEventListener('session.owner', function ($event) {
2014-07-22 16:15:19 +05:30
$this->assertTrue($event->getSession() instanceof \League\OAuth2\Server\Entity\SessionEntity);
});
2014-01-16 22:21:06 +05:30
$accessTokenStorage->shouldReceive('get')->andReturn(
2014-11-08 22:14:39 +05:30
(new AccessTokenEntity($server))->setId('abcdef')->setExpireTime(time() + 300)
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']),
2014-11-08 23:56:12 +05:30
(new ScopeEntity($server))->hydrate(['id' => 'bar']),
2014-07-12 13:28:18 +05:30
]);
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([
2014-11-08 23:56:12 +05:30
'Authorization' => 'Bearer abcdef',
2014-01-16 22:21:06 +05:30
]);
$server->setRequest($request);
$this->assertTrue($server->isValidRequest());
2014-01-16 22:21:06 +05:30
$this->assertEquals('abcdef', $server->getAccessToken());
}
2014-11-08 22:14:39 +05:30
/**
* @expectedException League\OAuth2\Server\Exception\AccessDeniedException
*/
public function testIsValidExpiredToken()
{
$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');
$server = new ResourceServer(
$sessionStorage,
$accessTokenStorage,
$clientStorage,
$scopeStorage
);
$server->setIdKey('at');
2014-11-08 23:56:12 +05:30
$server->addEventListener('session.owner', function ($event) {
2014-11-08 22:14:39 +05:30
$this->assertTrue($event->getSession() instanceof \League\OAuth2\Server\Entity\SessionEntity);
});
$accessTokenStorage->shouldReceive('get')->andReturn(
(new AccessTokenEntity($server))->setId('abcdef')->setExpireTime(time() - 300)
);
$accessTokenStorage->shouldReceive('getScopes')->andReturn([
(new ScopeEntity($server))->hydrate(['id' => 'foo']),
2014-11-08 23:56:12 +05:30
(new ScopeEntity($server))->hydrate(['id' => 'bar']),
2014-11-08 22:14:39 +05:30
]);
$sessionStorage->shouldReceive('getByAccessToken')->andReturn(
(new SessionEntity($server))->setId('foobar')->setOwner('user', 123)
);
$clientStorage->shouldReceive('getBySession')->andReturn(
(new ClientEntity($server))->hydrate(['id' => 'testapp'])
);
$request = new \Symfony\Component\HttpFoundation\Request();
$request->headers = new \Symfony\Component\HttpFoundation\ParameterBag([
2014-11-08 23:56:12 +05:30
'Authorization' => 'Bearer abcdef',
2014-11-08 22:14:39 +05:30
]);
$server->setRequest($request);
$server->isValidRequest();
}
2014-01-16 22:21:06 +05:30
}