mirror of
https://github.com/elyby/oauth2-server.git
synced 2025-05-31 14:12:07 +05:30
Updated tests
This commit is contained in:
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user