mirror of
https://github.com/elyby/oauth2-server.git
synced 2024-12-15 17:58:56 +05:30
Updated tests
This commit is contained in:
parent
36760a07cc
commit
add1aa5949
26
phpunit.xml
26
phpunit.xml
@ -1,27 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit colors="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" stopOnError="false" stopOnFailure="false" stopOnIncomplete="false" stopOnSkipped="false" bootstrap="tests/Bootstrap.php">
|
||||
<testsuites>
|
||||
<testsuite name="Authorization Server">
|
||||
<directory suffix="Test.php">tests/authorization</directory>
|
||||
</testsuite>
|
||||
<testsuite name="Resource Server">
|
||||
<directory suffix="Test.php">tests/resource</directory>
|
||||
</testsuite>
|
||||
<testsuite name="Utility Methods">
|
||||
<directory suffix="Test.php">tests/util</directory>
|
||||
</testsuite>
|
||||
<testsuite name="Tests">
|
||||
<directory>./tests/</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<filter>
|
||||
<blacklist>
|
||||
<directory suffix=".php">PEAR_INSTALL_DIR</directory>
|
||||
<directory suffix=".php">PHP_LIBDIR</directory>
|
||||
<directory suffix=".php">vendor</directory>
|
||||
<directory suffix=".php">tests</directory>
|
||||
<directory suffix=".php">testing</directory>
|
||||
</blacklist>
|
||||
<whitelist addUncoveredFilesFromWhitelist="true">
|
||||
<directory suffix=".php">src</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
<logging>
|
||||
<log type="coverage-text" target="php://stdout" title="lncd/OAuth" charset="UTF-8" yui="true" highlight="true" lowUpperBound="60" highLowerBound="99"/>
|
||||
<log type="coverage-html" target="tests/coverage" title="lncd/OAuth" charset="UTF-8" yui="true" highlight="true" lowUpperBound="60" highLowerBound="99"/>
|
||||
<log type="coverage-text" target="php://stdout" title="lncd/OAuth" charset="UTF-8" yui="true" highlight="true" lowUpperBound="60" highLowerBound="90"/>
|
||||
<log type="coverage-html" target="tests/coverage" title="lncd/OAuth" charset="UTF-8" yui="true" highlight="true" lowUpperBound="60" highLowerBound="90"/>
|
||||
</logging>
|
||||
</phpunit>
|
||||
|
26
tests/AbstractServerTest.php
Normal file
26
tests/AbstractServerTest.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace LeagueTests;
|
||||
|
||||
use LeagueTests\Stubs\StubAbstractServer;
|
||||
use \Mockery as M;
|
||||
|
||||
class AbstractTokenTests extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
function testSetGet()
|
||||
{
|
||||
$server = new StubAbstractServer();
|
||||
$this->assertTrue($server->getRequest() instanceof \Symfony\Component\HttpFoundation\Request);
|
||||
|
||||
$server2 = new StubAbstractServer();
|
||||
$server2->setRequest((new \Symfony\Component\HttpFoundation\Request));
|
||||
$this->assertTrue($server2->getRequest() instanceof \Symfony\Component\HttpFoundation\Request);
|
||||
}
|
||||
|
||||
function testGetStorageException()
|
||||
{
|
||||
$this->setExpectedException('League\OAuth2\Server\Exception\ServerException');
|
||||
$server = new StubAbstractServer();
|
||||
$server->getStorage('foobar');
|
||||
}
|
||||
}
|
107
tests/AuthorizationTest.php
Normal file
107
tests/AuthorizationTest.php
Normal file
@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace LeagueTests;
|
||||
|
||||
use League\OAuth2\Server\Authorization;
|
||||
use League\OAuth2\Server\Grant\GrantTypeInterface;
|
||||
use League\OAuth2\Server\Storage\ScopeInterface;
|
||||
use \Mockery as M;
|
||||
|
||||
class AuthorizationTests extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testGetExceptionMessage()
|
||||
{
|
||||
$m = Authorization::getExceptionMessage('access_denied');
|
||||
|
||||
$reflector = new \ReflectionClass('League\OAuth2\Server\Authorization');
|
||||
$exceptionMessages = $reflector->getProperty('exceptionMessages');
|
||||
$exceptionMessages->setAccessible(true);
|
||||
$v = $exceptionMessages->getValue();
|
||||
|
||||
$this->assertEquals($v['access_denied'], $m);
|
||||
}
|
||||
|
||||
public function testGetExceptionCode()
|
||||
{
|
||||
$this->assertEquals('access_denied', Authorization::getExceptionType(2));
|
||||
}
|
||||
|
||||
public function testGetExceptionHttpHeaders()
|
||||
{
|
||||
$this->assertEquals(array('HTTP/1.1 401 Unauthorized'), Authorization::getExceptionHttpHeaders('access_denied'));
|
||||
$this->assertEquals(array('HTTP/1.1 500 Internal Server Error'), Authorization::getExceptionHttpHeaders('server_error'));
|
||||
$this->assertEquals(array('HTTP/1.1 501 Not Implemented'), Authorization::getExceptionHttpHeaders('unsupported_grant_type'));
|
||||
$this->assertEquals(array('HTTP/1.1 400 Bad Request'), Authorization::getExceptionHttpHeaders('invalid_refresh'));
|
||||
}
|
||||
|
||||
public function testSetGet()
|
||||
{
|
||||
$server = new Authorization;
|
||||
$server->requireScopeParam(true);
|
||||
$server->requireStateParam(true);
|
||||
$server->setDefaultScope('foobar');
|
||||
$server->setScopeDelimeter(',');
|
||||
$server->setAccessTokenTTL(1);
|
||||
|
||||
$grant = M::mock('League\OAuth2\Server\Grant\GrantTypeInterface');
|
||||
$grant->shouldReceive('getIdentifier')->andReturn('foobar');
|
||||
$grant->shouldReceive('getResponseType')->andReturn('foobar');
|
||||
$grant->shouldReceive('setAuthorizationServer');
|
||||
|
||||
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
|
||||
$scopeStorage->shouldReceive('setServer');
|
||||
|
||||
$server->addGrantType($grant);
|
||||
$server->setScopeStorage($scopeStorage);
|
||||
|
||||
$this->assertTrue($server->hasGrantType('foobar'));
|
||||
$this->assertTrue($server->getGrantType('foobar') instanceof GrantTypeInterface);
|
||||
$this->assertSame($server->getResponseTypes(), ['foobar']);
|
||||
$this->assertTrue($server->scopeParamRequired());
|
||||
$this->assertTrue($server->stateParamRequired());
|
||||
$this->assertTrue($server->getStorage('scope') instanceof ScopeInterface);
|
||||
$this->assertEquals('foobar', $server->getDefaultScope());
|
||||
$this->assertEquals(',', $server->getScopeDelimeter());
|
||||
$this->assertEquals(1, $server->getAccessTokenTTL());
|
||||
}
|
||||
|
||||
public function testInvalidGrantType()
|
||||
{
|
||||
$this->setExpectedException('League\OAuth2\Server\Exception\InvalidGrantTypeException');
|
||||
$server = new Authorization;
|
||||
$server->getGrantType('foobar');
|
||||
}
|
||||
|
||||
public function testIssueAccessToken()
|
||||
{
|
||||
$grant = M::mock('League\OAuth2\Server\Grant\GrantTypeInterface');
|
||||
$grant->shouldReceive('getIdentifier')->andReturn('foobar');
|
||||
$grant->shouldReceive('getResponseType')->andReturn('foobar');
|
||||
$grant->shouldReceive('setAuthorizationServer');
|
||||
$grant->shouldReceive('completeFlow')->andReturn(true);
|
||||
|
||||
$_POST['grant_type'] = 'foobar';
|
||||
|
||||
$server = new Authorization;
|
||||
$server->addGrantType($grant);
|
||||
|
||||
$this->assertTrue($server->issueAccessToken());
|
||||
}
|
||||
|
||||
public function testIssueAccessTokenEmptyGrantType()
|
||||
{
|
||||
$this->setExpectedException('League\OAuth2\Server\Exception\ClientException');
|
||||
$server = new Authorization;
|
||||
$this->assertTrue($server->issueAccessToken());
|
||||
}
|
||||
|
||||
public function testIssueAccessTokenInvalidGrantType()
|
||||
{
|
||||
$this->setExpectedException('League\OAuth2\Server\Exception\ClientException');
|
||||
|
||||
$_POST['grant_type'] = 'foobar';
|
||||
|
||||
$server = new Authorization;
|
||||
$this->assertTrue($server->issueAccessToken());
|
||||
}
|
||||
}
|
102
tests/Entities/AbstractTokenTest.php
Normal file
102
tests/Entities/AbstractTokenTest.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace LeagueTests\Entities;
|
||||
|
||||
use LeagueTests\Stubs\StubAbstractToken;
|
||||
use League\OAuth2\Server\Entity\Session;
|
||||
use League\OAuth2\Server\Entity\Scope;
|
||||
use League\OAuth2\Server\Authorization;
|
||||
use \Mockery as M;
|
||||
|
||||
class AbstractTokenTests extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testSetGet()
|
||||
{
|
||||
$server = M::mock('League\OAuth2\Server\AbstractServer');
|
||||
$time = time();
|
||||
|
||||
$entity = new StubAbstractToken($server);
|
||||
$entity->setToken('foobar');
|
||||
$entity->setExpireTime($time);
|
||||
$entity->setSession((new Session($server)));
|
||||
$entity->associateScope((new Scope($server))->setId('foo'));
|
||||
|
||||
$this->assertEquals('foobar', $entity->getToken());
|
||||
$this->assertEquals($time, $entity->getExpireTime());
|
||||
$this->assertTrue($entity->getSession() instanceof Session);
|
||||
$this->assertTrue($entity->hasScope('foo'));
|
||||
|
||||
$result = $entity->getScopes();
|
||||
$this->assertTrue(isset($result['foo']));
|
||||
}
|
||||
|
||||
public function testGetSession()
|
||||
{
|
||||
$server = new Authorization();
|
||||
|
||||
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
|
||||
$sessionStorage->shouldReceive('getByAccessToken')->andReturn(
|
||||
(new Session($server))
|
||||
);
|
||||
$sessionStorage->shouldReceive('setServer');
|
||||
|
||||
$server->setSessionStorage($sessionStorage);
|
||||
|
||||
$entity = new StubAbstractToken($server);
|
||||
$this->assertTrue($entity->getSession() instanceof Session);
|
||||
}
|
||||
|
||||
public function testGetScopes()
|
||||
{
|
||||
$server = new Authorization();
|
||||
|
||||
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
|
||||
$accessTokenStorage->shouldReceive('getScopes')->andReturn(
|
||||
[]
|
||||
);
|
||||
$accessTokenStorage->shouldReceive('setServer');
|
||||
|
||||
$server->setAccessTokenStorage($accessTokenStorage);
|
||||
|
||||
$entity = new StubAbstractToken($server);
|
||||
$this->assertEquals($entity->getScopes(), []);
|
||||
}
|
||||
|
||||
public function testHasScopes()
|
||||
{
|
||||
$server = new Authorization();
|
||||
|
||||
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
|
||||
$accessTokenStorage->shouldReceive('getScopes')->andReturn(
|
||||
[]
|
||||
);
|
||||
$accessTokenStorage->shouldReceive('setServer');
|
||||
|
||||
$server->setAccessTokenStorage($accessTokenStorage);
|
||||
|
||||
$entity = new StubAbstractToken($server);
|
||||
$this->assertFalse($entity->hasScope('foo'));
|
||||
}
|
||||
|
||||
public function testFormatScopes()
|
||||
{
|
||||
$server = M::mock('League\OAuth2\Server\AbstractServer');
|
||||
|
||||
$entity = new StubAbstractToken($server);
|
||||
$reflectedEntity = new \ReflectionClass('LeagueTests\Stubs\StubAbstractToken');
|
||||
$method = $reflectedEntity->getMethod('formatScopes');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$scopes = [
|
||||
(new Scope($server))->setId('scope1')->setDescription('foo'),
|
||||
(new Scope($server))->setId('scope2')->setDescription('bar')
|
||||
];
|
||||
|
||||
$result = $method->invokeArgs($entity, [$scopes]);
|
||||
|
||||
$this->assertTrue(isset($result['scope1']));
|
||||
$this->assertTrue(isset($result['scope2']));
|
||||
$this->assertTrue($result['scope1'] instanceof Scope);
|
||||
$this->assertTrue($result['scope2'] instanceof Scope);
|
||||
}
|
||||
}
|
51
tests/Entities/AccessTokenTest.php
Normal file
51
tests/Entities/AccessTokenTest.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace LeagueTests\Entities;
|
||||
|
||||
use League\OAuth2\Server\Entity\Scope;
|
||||
use League\OAuth2\Server\Entity\Session;
|
||||
use League\OAuth2\Server\Entity\AccessToken;
|
||||
use League\OAuth2\Server\Authorization;
|
||||
use \Mockery as M;
|
||||
|
||||
class AccessTokenTests extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
function testSave()
|
||||
{
|
||||
$server = new Authorization();
|
||||
|
||||
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
|
||||
$accessTokenStorage->shouldReceive('create');
|
||||
$accessTokenStorage->shouldReceive('associateScope');
|
||||
$accessTokenStorage->shouldReceive('setServer');
|
||||
$accessTokenStorage->shouldReceive('getScopes')->andReturn([
|
||||
(new Scope($server))->setId('foo')
|
||||
]);
|
||||
|
||||
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
|
||||
$sessionStorage->shouldReceive('getByAccessToken')->andReturn(
|
||||
(new Session($server))
|
||||
);
|
||||
$sessionStorage->shouldReceive('setServer');
|
||||
|
||||
$server->setAccessTokenStorage($accessTokenStorage);
|
||||
$server->setSessionStorage($sessionStorage);
|
||||
|
||||
$entity = new AccessToken($server);
|
||||
$this->assertTrue($entity->save() instanceof AccessToken);
|
||||
}
|
||||
|
||||
function testExpire()
|
||||
{
|
||||
$server = new Authorization();
|
||||
|
||||
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
|
||||
$accessTokenStorage->shouldReceive('delete');
|
||||
$accessTokenStorage->shouldReceive('setServer');
|
||||
|
||||
$server->setAccessTokenStorage($accessTokenStorage);
|
||||
|
||||
$entity = new AccessToken($server);
|
||||
$this->assertSame($entity->expire(), null);
|
||||
}
|
||||
}
|
24
tests/Entities/ClientTest.php
Normal file
24
tests/Entities/ClientTest.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace LeagueTests\Entities;
|
||||
|
||||
use League\OAuth2\Server\Entity\Client;
|
||||
use \Mockery as M;
|
||||
|
||||
class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testSetGet()
|
||||
{
|
||||
$server = M::mock('League\OAuth2\Server\AbstractServer');
|
||||
$client = new Client($server);
|
||||
$client->setId('foobar');
|
||||
$client->setSecret('barfoo');
|
||||
$client->setName('Test Client');
|
||||
$client->setRedirectUri('http://foo/bar');
|
||||
|
||||
$this->assertEquals('foobar', $client->getId());
|
||||
$this->assertEquals('barfoo', $client->getSecret());
|
||||
$this->assertEquals('Test Client', $client->getName());
|
||||
$this->assertEquals('http://foo/bar', $client->getRedirectUri());
|
||||
}
|
||||
}
|
75
tests/Entities/RefreshTokenTest.php
Normal file
75
tests/Entities/RefreshTokenTest.php
Normal file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace LeagueTests\Entities;
|
||||
|
||||
use League\OAuth2\Server\Entity\Scope;
|
||||
use League\OAuth2\Server\Entity\Session;
|
||||
use League\OAuth2\Server\Entity\AccessToken;
|
||||
use League\OAuth2\Server\Entity\RefreshToken;
|
||||
use League\OAuth2\Server\Authorization;
|
||||
use \Mockery as M;
|
||||
|
||||
class RefreshTokenTests extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
function testSetAccessToken()
|
||||
{
|
||||
$reader = function & ($object, $property) {
|
||||
$value = & \Closure::bind(function & () use ($property) {
|
||||
return $this->$property;
|
||||
}, $object, $object)->__invoke();
|
||||
|
||||
return $value;
|
||||
};
|
||||
|
||||
$server = M::mock('League\OAuth2\Server\AbstractServer');
|
||||
$entity = new RefreshToken($server);
|
||||
$entity->setAccessToken((new AccessToken($server)));
|
||||
|
||||
$this->assertTrue($reader($entity, 'accessToken') instanceof AccessToken);
|
||||
}
|
||||
|
||||
function testSave()
|
||||
{
|
||||
$server = new Authorization();
|
||||
|
||||
$refreshTokenStorage = M::mock('League\OAuth2\Server\Storage\RefreshTokenInterface');
|
||||
$refreshTokenStorage->shouldReceive('create');
|
||||
$refreshTokenStorage->shouldReceive('setServer');
|
||||
$refreshTokenStorage->shouldReceive('associateScope');
|
||||
|
||||
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
|
||||
$accessTokenStorage->shouldReceive('setServer');
|
||||
$accessTokenStorage->shouldReceive('getByRefreshToken')->andReturn(
|
||||
(new AccessToken($server))->setToken('foobar')
|
||||
);
|
||||
$accessTokenStorage->shouldReceive('getScopes')->andReturn([
|
||||
(new Scope($server))->setId('foo')
|
||||
]);
|
||||
|
||||
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
|
||||
$sessionStorage->shouldReceive('getByAccessToken')->andReturn(
|
||||
(new Session($server))
|
||||
);
|
||||
$sessionStorage->shouldReceive('setServer');
|
||||
|
||||
$server->setAccessTokenStorage($accessTokenStorage);
|
||||
$server->setRefreshTokenStorage($refreshTokenStorage);
|
||||
|
||||
$entity = new RefreshToken($server);
|
||||
$this->assertSame(null, $entity->save());
|
||||
}
|
||||
|
||||
function testExpire()
|
||||
{
|
||||
$server = new Authorization();
|
||||
|
||||
$refreshTokenStorage = M::mock('League\OAuth2\Server\Storage\RefreshTokenInterface');
|
||||
$refreshTokenStorage->shouldReceive('delete');
|
||||
$refreshTokenStorage->shouldReceive('setServer');
|
||||
|
||||
$server->setRefreshTokenStorage($refreshTokenStorage);
|
||||
|
||||
$entity = new RefreshToken($server);
|
||||
$this->assertSame($entity->expire(), null);
|
||||
}
|
||||
}
|
20
tests/Entities/ScopeTest.php
Normal file
20
tests/Entities/ScopeTest.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace LeagueTests\Entities;
|
||||
|
||||
use League\OAuth2\Server\Entity\Scope;
|
||||
use \Mockery as M;
|
||||
|
||||
class ScopeTests extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testSetGet()
|
||||
{
|
||||
$server = M::mock('League\OAuth2\Server\AbstractServer');
|
||||
$scope = new Scope($server);
|
||||
$scope->setId('foobar');
|
||||
$scope->setDescription('barfoo');
|
||||
|
||||
$this->assertEquals('foobar', $scope->getId());
|
||||
$this->assertEquals('barfoo', $scope->getDescription());
|
||||
}
|
||||
}
|
130
tests/Entities/SessionTest.php
Normal file
130
tests/Entities/SessionTest.php
Normal file
@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace LeagueTests\Entities;
|
||||
|
||||
use League\OAuth2\Server\Entity\AccessToken;
|
||||
use League\OAuth2\Server\Entity\AuthCode;
|
||||
use League\OAuth2\Server\Entity\Client;
|
||||
use League\OAuth2\Server\Entity\RefreshToken;
|
||||
use League\OAuth2\Server\Entity\Session;
|
||||
use League\OAuth2\Server\Entity\Scope;
|
||||
use League\OAuth2\Server\Authorization;
|
||||
use \Mockery as M;
|
||||
|
||||
class SessionTests extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testSetGet()
|
||||
{
|
||||
$server = M::mock('League\OAuth2\Server\AbstractServer');
|
||||
$entity = new Session($server);
|
||||
$entity->setId('foobar');
|
||||
$entity->setOwner('user', 123);
|
||||
$entity->associateAccessToken((new AccessToken($server)));
|
||||
$entity->associateRefreshToken((new RefreshToken($server)));
|
||||
$entity->associateClient((new Client($server)));
|
||||
$entity->associateScope((new Scope($server))->setId('foo'));
|
||||
// $entity->associateAuthCode((new AuthCode($server)));
|
||||
|
||||
$reader = function & ($object, $property) {
|
||||
$value = & \Closure::bind(function & () use ($property) {
|
||||
return $this->$property;
|
||||
}, $object, $object)->__invoke();
|
||||
|
||||
return $value;
|
||||
};
|
||||
|
||||
$this->assertEquals('foobar', $entity->getId());
|
||||
$this->assertEquals('user', $entity->getOwnerType());
|
||||
$this->assertEquals(123, $entity->getOwnerId());
|
||||
$this->assertTrue($reader($entity, 'accessToken') instanceof AccessToken);
|
||||
$this->assertTrue($reader($entity, 'refreshToken') instanceof RefreshToken);
|
||||
$this->assertTrue($entity->getClient() instanceof Client);
|
||||
$this->assertTrue($entity->hasScope('foo'));
|
||||
// $this->assertTrue($reader($entity, 'authCode') instanceof AuthCode);
|
||||
}
|
||||
|
||||
public function testFormatScopes()
|
||||
{
|
||||
$server = M::mock('League\OAuth2\Server\AbstractServer');
|
||||
|
||||
$entity = new Session($server);
|
||||
$reflectedEntity = new \ReflectionClass('League\OAuth2\Server\Entity\Session');
|
||||
$method = $reflectedEntity->getMethod('formatScopes');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$scopes = [
|
||||
(new Scope($server))->setId('scope1')->setDescription('foo'),
|
||||
(new Scope($server))->setId('scope2')->setDescription('bar')
|
||||
];
|
||||
|
||||
$result = $method->invokeArgs($entity, [$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 testGetScopes()
|
||||
{
|
||||
$server = new Authorization();
|
||||
|
||||
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
|
||||
$accessTokenStorage->shouldReceive('setServer');
|
||||
$server->setAccessTokenStorage($accessTokenStorage);
|
||||
|
||||
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
|
||||
$sessionStorage->shouldReceive('getScopes')->andReturn(
|
||||
[]
|
||||
);
|
||||
$sessionStorage->shouldReceive('setServer');
|
||||
$server->setSessionStorage($sessionStorage);
|
||||
|
||||
$entity = new Session($server);
|
||||
$this->assertEquals($entity->getScopes(), []);
|
||||
}
|
||||
|
||||
public function testHasScopes()
|
||||
{
|
||||
$server = new Authorization();
|
||||
|
||||
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
|
||||
$accessTokenStorage->shouldReceive('setServer');
|
||||
$server->setAccessTokenStorage($accessTokenStorage);
|
||||
|
||||
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
|
||||
$sessionStorage->shouldReceive('getScopes')->andReturn(
|
||||
[]
|
||||
);
|
||||
$sessionStorage->shouldReceive('setServer');
|
||||
$server->setSessionStorage($sessionStorage);
|
||||
|
||||
$entity = new Session($server);
|
||||
$this->assertFalse($entity->hasScope('foo'));
|
||||
}
|
||||
|
||||
function testSave()
|
||||
{
|
||||
$server = new Authorization();
|
||||
|
||||
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
|
||||
$sessionStorage->shouldReceive('create');
|
||||
$sessionStorage->shouldReceive('associateScope');
|
||||
$sessionStorage->shouldReceive('setServer');
|
||||
$sessionStorage->shouldReceive('getScopes')->andReturn([
|
||||
(new Scope($server))->setId('foo')
|
||||
]);
|
||||
|
||||
$clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface');
|
||||
$clientStorage->shouldReceive('getBySession')->andReturn(
|
||||
(new Client($server))->setId('foo')
|
||||
);
|
||||
$clientStorage->shouldReceive('setServer');
|
||||
|
||||
$server->setSessionStorage($sessionStorage);
|
||||
$server->setClientStorage($clientStorage);
|
||||
|
||||
$entity = new Session($server);
|
||||
$this->assertEquals(null, $entity->save());
|
||||
}
|
||||
}
|
214
tests/ResourceTest.php
Normal file
214
tests/ResourceTest.php
Normal file
@ -0,0 +1,214 @@
|
||||
<?php
|
||||
|
||||
namespace LeagueTests;
|
||||
|
||||
use League\OAuth2\Server\Resource;
|
||||
use League\OAuth2\Server\Grant\GrantTypeInterface;
|
||||
use League\OAuth2\Server\Entity\AccessToken;
|
||||
use League\OAuth2\Server\Entity\Session;
|
||||
use League\OAuth2\Server\Entity\Client;
|
||||
use League\OAuth2\Server\Entity\Scope;
|
||||
use \Mockery as M;
|
||||
|
||||
class ResourceTests extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
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');
|
||||
|
||||
$server = new Resource(
|
||||
$sessionStorage,
|
||||
$accessTokenStorage,
|
||||
$clientStorage,
|
||||
$scopeStorage
|
||||
);
|
||||
|
||||
return $server;
|
||||
}
|
||||
|
||||
function testGetSet()
|
||||
{
|
||||
$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 Resource(
|
||||
$sessionStorage,
|
||||
$accessTokenStorage,
|
||||
$clientStorage,
|
||||
$scopeStorage
|
||||
);
|
||||
}
|
||||
|
||||
public function testDetermineAccessTokenMissingToken()
|
||||
{
|
||||
$this->setExpectedException('League\OAuth2\Server\Exception\InvalidAccessTokenException');
|
||||
|
||||
$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');
|
||||
|
||||
$server = new Resource(
|
||||
$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 testDetermineAccessTokenBrokenCurlRequest()
|
||||
{
|
||||
$this->setExpectedException('League\OAuth2\Server\Exception\InvalidAccessTokenException');
|
||||
|
||||
$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');
|
||||
|
||||
$server = new Resource(
|
||||
$sessionStorage,
|
||||
$accessTokenStorage,
|
||||
$clientStorage,
|
||||
$scopeStorage
|
||||
);
|
||||
|
||||
$request = new \Symfony\Component\HttpFoundation\Request();
|
||||
$request->headers = new \Symfony\Component\HttpFoundation\ParameterBag([
|
||||
'Authorization' => 'Bearer, Bearer abcdef'
|
||||
]);
|
||||
$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');
|
||||
|
||||
$server = new Resource(
|
||||
$sessionStorage,
|
||||
$accessTokenStorage,
|
||||
$clientStorage,
|
||||
$scopeStorage
|
||||
);
|
||||
|
||||
$this->assertFalse($server->isValid());
|
||||
}
|
||||
|
||||
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');
|
||||
|
||||
$server = new Resource(
|
||||
$sessionStorage,
|
||||
$accessTokenStorage,
|
||||
$clientStorage,
|
||||
$scopeStorage
|
||||
);
|
||||
|
||||
$server->setTokenKey('at');
|
||||
|
||||
$accessTokenStorage->shouldReceive('get')->andReturn(
|
||||
(new AccessToken($server))->setToken('abcdef')
|
||||
);
|
||||
|
||||
$accessTokenStorage->shouldReceive('getScopes')->andReturn([
|
||||
(new Scope($server))->setId('foo'),
|
||||
(new Scope($server))->setId('bar')
|
||||
]);
|
||||
|
||||
$sessionStorage->shouldReceive('getByAccessToken')->andReturn(
|
||||
(new Session($server))->setId('foobar')->setOwner('user', 123)
|
||||
);
|
||||
|
||||
$clientStorage->shouldReceive('getBySession')->andReturn(
|
||||
(new Client($server))->setId('testapp')
|
||||
);
|
||||
|
||||
$request = new \Symfony\Component\HttpFoundation\Request();
|
||||
$request->headers = new \Symfony\Component\HttpFoundation\ParameterBag([
|
||||
'Authorization' => 'Bearer abcdef'
|
||||
]);
|
||||
$server->setRequest($request);
|
||||
|
||||
$this->assertTrue($server->isValid());
|
||||
$this->assertEquals('at', $server->getTokenKey());
|
||||
$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'));
|
||||
}
|
||||
}
|
24
tests/Storage/AdapterTest.php
Normal file
24
tests/Storage/AdapterTest.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace LeagueTests\Storage;
|
||||
|
||||
use League\OAuth2\Server\Storage\Adapter;
|
||||
use LeagueTests\Stubs\StubAbstractServer;
|
||||
use \Mockery as M;
|
||||
|
||||
class AdapterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
function testSetGet()
|
||||
{
|
||||
$adapter = new Adapter;
|
||||
|
||||
$reflector = new \ReflectionClass($adapter);
|
||||
$setMethod = $reflector->getMethod('setServer');
|
||||
$setMethod->setAccessible(true);
|
||||
$setMethod->invokeArgs($adapter, [new StubAbstractServer]);
|
||||
$getMethod = $reflector->getMethod('getServer');
|
||||
$getMethod->setAccessible(true);
|
||||
|
||||
$this->assertTrue($getMethod->invoke($adapter) instanceof StubAbstractServer);
|
||||
}
|
||||
}
|
8
tests/Stubs/StubAbstractServer.php
Normal file
8
tests/Stubs/StubAbstractServer.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace LeagueTests\Stubs;
|
||||
|
||||
class StubAbstractServer extends \League\OAuth2\Server\AbstractServer
|
||||
{
|
||||
|
||||
}
|
16
tests/Stubs/StubAbstractToken.php
Normal file
16
tests/Stubs/StubAbstractToken.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace LeagueTests\Stubs;
|
||||
|
||||
class StubAbstractToken extends \League\OAuth2\Server\Entity\AbstractToken
|
||||
{
|
||||
public function expire()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user