mirror of
https://github.com/elyby/oauth2-server.git
synced 2024-12-23 05:29:52 +05:30
Updated phpunit tests
This commit is contained in:
parent
7f2fd69d0a
commit
08ad67e401
@ -1,8 +1,8 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<phpunit colors="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" stopOnError="true" stopOnFailure="true" stopOnIncomplete="false" stopOnSkipped="false" bootstrap="tests/unit/Bootstrap.php">
|
<phpunit colors="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" stopOnError="true" stopOnFailure="true" stopOnIncomplete="false" stopOnSkipped="false" bootstrap="tests/Bootstrap.php">
|
||||||
<testsuites>
|
<testsuites>
|
||||||
<testsuite name="Tests">
|
<testsuite name="Tests">
|
||||||
<directory>./tests/unit/</directory>
|
<directory>./tests/</directory>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
</testsuites>
|
</testsuites>
|
||||||
<filter>
|
<filter>
|
||||||
@ -11,7 +11,7 @@
|
|||||||
</whitelist>
|
</whitelist>
|
||||||
</filter>
|
</filter>
|
||||||
<logging>
|
<logging>
|
||||||
<!-- <log type="coverage-text" target="php://stdout" title="thephpleague/oauth2-server" charset="UTF-8" yui="true" highlight="true" lowUpperBound="60" highLowerBound="90"/> -->
|
<log type="coverage-text" target="php://stdout" title="thephpleague/oauth2-server" charset="UTF-8" yui="true" highlight="true" lowUpperBound="60" highLowerBound="90"/>
|
||||||
<log type="coverage-html" target="build/coverage" title="thephpleague/oauth2-server" charset="UTF-8" yui="true" highlight="true" lowUpperBound="60" highLowerBound="90"/>
|
<log type="coverage-html" target="build/coverage" title="thephpleague/oauth2-server" charset="UTF-8" yui="true" highlight="true" lowUpperBound="60" highLowerBound="90"/>
|
||||||
</logging>
|
</logging>
|
||||||
</phpunit>
|
</phpunit>
|
||||||
|
5
tests/Bootstrap.php
Normal file
5
tests/Bootstrap.php
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
if (! @include_once __DIR__ . '/../vendor/autoload.php') {
|
||||||
|
exit("You must set up the project dependencies, run the following commands:\n> wget http://getcomposer.org/composer.phar\n> php composer.phar install\n");
|
||||||
|
}
|
289
tests/Grant/AbstractGrantTest.php
Normal file
289
tests/Grant/AbstractGrantTest.php
Normal file
@ -0,0 +1,289 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace LeagueTests\Grant;
|
||||||
|
|
||||||
|
use League\Event\Emitter;
|
||||||
|
use League\OAuth2\Server\Entities\AccessTokenEntity;
|
||||||
|
use League\OAuth2\Server\Entities\ClientEntity;
|
||||||
|
use League\OAuth2\Server\Entities\Interfaces\AccessTokenEntityInterface;
|
||||||
|
use League\OAuth2\Server\Entities\Interfaces\AuthCodeEntityInterface;
|
||||||
|
use League\OAuth2\Server\Entities\Interfaces\RefreshTokenEntityInterface;
|
||||||
|
use League\OAuth2\Server\Entities\ScopeEntity;
|
||||||
|
use League\OAuth2\Server\Grant\AbstractGrant;
|
||||||
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
|
||||||
|
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
|
||||||
|
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
|
||||||
|
use Zend\Diactoros\ServerRequest;
|
||||||
|
|
||||||
|
class AbstractGrantTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
public function testGetSet()
|
||||||
|
{
|
||||||
|
$clientRepositoryMock = $this->getMock(ClientRepositoryInterface::class);
|
||||||
|
$accessTokenRepositoryMock = $this->getMock(AccessTokenRepositoryInterface::class);
|
||||||
|
$scopeRepositoryMock = $this->getMock(ScopeRepositoryInterface::class);
|
||||||
|
|
||||||
|
/** @var AbstractGrant $grantMock */
|
||||||
|
$grantMock = $this->getMockForAbstractClass(AbstractGrant::class);
|
||||||
|
$grantMock->setClientRepository($clientRepositoryMock);
|
||||||
|
$grantMock->setAccessTokenRepository($accessTokenRepositoryMock);
|
||||||
|
$grantMock->setScopeRepository($scopeRepositoryMock);
|
||||||
|
$grantMock->setPathToPrivateKey('./private.key');
|
||||||
|
$grantMock->setPathToPublicKey('./public.key');
|
||||||
|
$grantMock->setEmitter(new Emitter());
|
||||||
|
$grantMock->setRefreshTokenTTL(new \DateInterval('PT1H'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidateClient()
|
||||||
|
{
|
||||||
|
$client = new ClientEntity();
|
||||||
|
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
|
||||||
|
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
|
||||||
|
|
||||||
|
/** @var AbstractGrant $grantMock */
|
||||||
|
$grantMock = $this->getMockForAbstractClass(AbstractGrant::class);
|
||||||
|
$grantMock->setClientRepository($clientRepositoryMock);
|
||||||
|
|
||||||
|
$abstractGrantReflection = new \ReflectionClass($grantMock);
|
||||||
|
|
||||||
|
$serverRequest = new ServerRequest();
|
||||||
|
$serverRequest = $serverRequest->withParsedBody(
|
||||||
|
[
|
||||||
|
'client_id' => 'foo',
|
||||||
|
'client_secret' => 'bar',
|
||||||
|
'redirect_uri' => 'http://foo/bar',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
$validateClientMethod = $abstractGrantReflection->getMethod('validateClient');
|
||||||
|
$validateClientMethod->setAccessible(true);
|
||||||
|
|
||||||
|
$result = $validateClientMethod->invoke($grantMock, $serverRequest, true, true);
|
||||||
|
$this->assertEquals($client, $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
|
||||||
|
*/
|
||||||
|
public function testValidateClientMissingClientId()
|
||||||
|
{
|
||||||
|
$client = new ClientEntity();
|
||||||
|
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
|
||||||
|
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
|
||||||
|
|
||||||
|
/** @var AbstractGrant $grantMock */
|
||||||
|
$grantMock = $this->getMockForAbstractClass(AbstractGrant::class);
|
||||||
|
$grantMock->setClientRepository($clientRepositoryMock);
|
||||||
|
|
||||||
|
$abstractGrantReflection = new \ReflectionClass($grantMock);
|
||||||
|
|
||||||
|
$serverRequest = new ServerRequest();
|
||||||
|
$validateClientMethod = $abstractGrantReflection->getMethod('validateClient');
|
||||||
|
$validateClientMethod->setAccessible(true);
|
||||||
|
|
||||||
|
$validateClientMethod->invoke($grantMock, $serverRequest, true, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
|
||||||
|
*/
|
||||||
|
public function testValidateClientMissingClientSecret()
|
||||||
|
{
|
||||||
|
$client = new ClientEntity();
|
||||||
|
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
|
||||||
|
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
|
||||||
|
|
||||||
|
/** @var AbstractGrant $grantMock */
|
||||||
|
$grantMock = $this->getMockForAbstractClass(AbstractGrant::class);
|
||||||
|
$grantMock->setClientRepository($clientRepositoryMock);
|
||||||
|
|
||||||
|
$abstractGrantReflection = new \ReflectionClass($grantMock);
|
||||||
|
|
||||||
|
$serverRequest = new ServerRequest();
|
||||||
|
$serverRequest = $serverRequest->withParsedBody([
|
||||||
|
'client_id' => 'foo',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$validateClientMethod = $abstractGrantReflection->getMethod('validateClient');
|
||||||
|
$validateClientMethod->setAccessible(true);
|
||||||
|
|
||||||
|
$validateClientMethod->invoke($grantMock, $serverRequest, true, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
|
||||||
|
*/
|
||||||
|
public function testValidateClientMissingRedirectUri()
|
||||||
|
{
|
||||||
|
$client = new ClientEntity();
|
||||||
|
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
|
||||||
|
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
|
||||||
|
|
||||||
|
/** @var AbstractGrant $grantMock */
|
||||||
|
$grantMock = $this->getMockForAbstractClass(AbstractGrant::class);
|
||||||
|
$grantMock->setClientRepository($clientRepositoryMock);
|
||||||
|
|
||||||
|
$abstractGrantReflection = new \ReflectionClass($grantMock);
|
||||||
|
|
||||||
|
$serverRequest = new ServerRequest();
|
||||||
|
$serverRequest = $serverRequest->withParsedBody([
|
||||||
|
'client_id' => 'foo',
|
||||||
|
'client_secret' => 'bar',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$validateClientMethod = $abstractGrantReflection->getMethod('validateClient');
|
||||||
|
$validateClientMethod->setAccessible(true);
|
||||||
|
|
||||||
|
$validateClientMethod->invoke($grantMock, $serverRequest, true, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCanRespondToRequest()
|
||||||
|
{
|
||||||
|
$grantMock = $this->getMockForAbstractClass(AbstractGrant::class);
|
||||||
|
$grantMock->method('getIdentifier')->willReturn('foobar');
|
||||||
|
|
||||||
|
$serverRequest = new ServerRequest();
|
||||||
|
$serverRequest = $serverRequest->withParsedBody([
|
||||||
|
'grant_type' => 'foobar',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertTrue($grantMock->canRespondToRequest($serverRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIssueRefreshToken()
|
||||||
|
{
|
||||||
|
/** @var AbstractGrant $grantMock */
|
||||||
|
$grantMock = $this->getMockForAbstractClass(AbstractGrant::class);
|
||||||
|
$grantMock->setRefreshTokenTTL(new \DateInterval('PT1M'));
|
||||||
|
|
||||||
|
$abstractGrantReflection = new \ReflectionClass($grantMock);
|
||||||
|
$issueRefreshTokenMethod = $abstractGrantReflection->getMethod('issueRefreshToken');
|
||||||
|
$issueRefreshTokenMethod->setAccessible(true);
|
||||||
|
|
||||||
|
$accessToken = new AccessTokenEntity();
|
||||||
|
/** @var RefreshTokenEntityInterface $refreshToken */
|
||||||
|
$refreshToken = $issueRefreshTokenMethod->invoke($grantMock, $accessToken);
|
||||||
|
$this->assertTrue($refreshToken instanceof RefreshTokenEntityInterface);
|
||||||
|
$this->assertFalse($refreshToken->isExpired());
|
||||||
|
$this->assertEquals($accessToken, $refreshToken->getAccessToken());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIssueAccessToken()
|
||||||
|
{
|
||||||
|
/** @var AbstractGrant $grantMock */
|
||||||
|
$grantMock = $this->getMockForAbstractClass(AbstractGrant::class);
|
||||||
|
|
||||||
|
$abstractGrantReflection = new \ReflectionClass($grantMock);
|
||||||
|
$issueAccessTokenMethod = $abstractGrantReflection->getMethod('issueAccessToken');
|
||||||
|
$issueAccessTokenMethod->setAccessible(true);
|
||||||
|
|
||||||
|
/** @var AccessTokenEntityInterface $accessToken */
|
||||||
|
$accessToken = $issueAccessTokenMethod->invoke(
|
||||||
|
$grantMock,
|
||||||
|
new \DateInterval('PT1H'),
|
||||||
|
new ClientEntity(),
|
||||||
|
123,
|
||||||
|
[new ScopeEntity()]
|
||||||
|
);
|
||||||
|
$this->assertTrue($accessToken instanceof AccessTokenEntityInterface);
|
||||||
|
$this->assertFalse($accessToken->isExpired());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIssueAuthCode()
|
||||||
|
{
|
||||||
|
/** @var AbstractGrant $grantMock */
|
||||||
|
$grantMock = $this->getMockForAbstractClass(AbstractGrant::class);
|
||||||
|
|
||||||
|
$abstractGrantReflection = new \ReflectionClass($grantMock);
|
||||||
|
$issueAuthCodeMethod = $abstractGrantReflection->getMethod('issueAuthCode');
|
||||||
|
$issueAuthCodeMethod->setAccessible(true);
|
||||||
|
|
||||||
|
$this->assertTrue(
|
||||||
|
$issueAuthCodeMethod->invoke(
|
||||||
|
$grantMock,
|
||||||
|
new \DateInterval('PT1H'),
|
||||||
|
new ClientEntity(),
|
||||||
|
123,
|
||||||
|
'http://foo/bar',
|
||||||
|
[new ScopeEntity()]
|
||||||
|
) instanceof AuthCodeEntityInterface
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetCookieParameter()
|
||||||
|
{
|
||||||
|
$grantMock = $this->getMockForAbstractClass(AbstractGrant::class);
|
||||||
|
$grantMock->method('getIdentifier')->willReturn('foobar');
|
||||||
|
|
||||||
|
$abstractGrantReflection = new \ReflectionClass($grantMock);
|
||||||
|
$method = $abstractGrantReflection->getMethod('getCookieParameter');
|
||||||
|
$method->setAccessible(true);
|
||||||
|
|
||||||
|
$serverRequest = new ServerRequest();
|
||||||
|
$serverRequest = $serverRequest->withCookieParams([
|
||||||
|
'foo' => 'bar',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals('bar', $method->invoke($grantMock, 'foo', $serverRequest));
|
||||||
|
$this->assertEquals('foo', $method->invoke($grantMock, 'bar', $serverRequest, 'foo'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetQueryStringParameter()
|
||||||
|
{
|
||||||
|
$grantMock = $this->getMockForAbstractClass(AbstractGrant::class);
|
||||||
|
$grantMock->method('getIdentifier')->willReturn('foobar');
|
||||||
|
|
||||||
|
$abstractGrantReflection = new \ReflectionClass($grantMock);
|
||||||
|
$method = $abstractGrantReflection->getMethod('getQueryStringParameter');
|
||||||
|
$method->setAccessible(true);
|
||||||
|
|
||||||
|
$serverRequest = new ServerRequest();
|
||||||
|
$serverRequest = $serverRequest->withQueryParams([
|
||||||
|
'foo' => 'bar',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals('bar', $method->invoke($grantMock, 'foo', $serverRequest));
|
||||||
|
$this->assertEquals('foo', $method->invoke($grantMock, 'bar', $serverRequest, 'foo'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidateScopes()
|
||||||
|
{
|
||||||
|
$scope = new ScopeEntity();
|
||||||
|
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
|
||||||
|
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scope);
|
||||||
|
|
||||||
|
/** @var AbstractGrant $grantMock */
|
||||||
|
$grantMock = $this->getMockForAbstractClass(AbstractGrant::class);
|
||||||
|
$grantMock->setScopeRepository($scopeRepositoryMock);
|
||||||
|
|
||||||
|
$serverRequest = new ServerRequest();
|
||||||
|
$serverRequest = $serverRequest->withParsedBody(
|
||||||
|
[
|
||||||
|
'scope' => 'basic ',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals([$scope], $grantMock->validateScopes($serverRequest, new ClientEntity()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException
|
||||||
|
*/
|
||||||
|
public function testValidateScopesBadScope()
|
||||||
|
{
|
||||||
|
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
|
||||||
|
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn(null);
|
||||||
|
|
||||||
|
/** @var AbstractGrant $grantMock */
|
||||||
|
$grantMock = $this->getMockForAbstractClass(AbstractGrant::class);
|
||||||
|
$grantMock->setScopeRepository($scopeRepositoryMock);
|
||||||
|
|
||||||
|
$serverRequest = new ServerRequest();
|
||||||
|
$serverRequest = $serverRequest->withParsedBody(
|
||||||
|
[
|
||||||
|
'scope' => 'basic ',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
$grantMock->validateScopes($serverRequest, new ClientEntity());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user