oauth2-server/tests/Middleware/ResourceServerMiddlewareTest.php

110 lines
3.6 KiB
PHP
Raw Permalink Normal View History

2016-03-10 22:52:48 +05:30
<?php
namespace LeagueTests\Middleware;
use DateInterval;
use DateTimeImmutable;
2016-03-28 20:12:34 +05:30
use League\OAuth2\Server\CryptKey;
2016-03-10 22:52:48 +05:30
use League\OAuth2\Server\Middleware\ResourceServerMiddleware;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
use League\OAuth2\Server\ResourceServer;
2016-04-09 20:16:40 +05:30
use LeagueTests\Stubs\AccessTokenEntity;
use LeagueTests\Stubs\ClientEntity;
use PHPUnit\Framework\TestCase;
2016-03-10 22:52:48 +05:30
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
class ResourceServerMiddlewareTest extends TestCase
2016-03-10 22:52:48 +05:30
{
public function testValidResponse()
{
$server = new ResourceServer(
2016-07-08 18:59:21 +05:30
$this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(),
'file://' . __DIR__ . '/../Stubs/public.key'
2016-03-10 22:52:48 +05:30
);
$client = new ClientEntity();
$client->setIdentifier('clientName');
$accessToken = new AccessTokenEntity();
$accessToken->setIdentifier('test');
$accessToken->setUserIdentifier(123);
$accessToken->setExpiryDateTime((new DateTimeImmutable())->add(new DateInterval('PT1H')));
$accessToken->setClient($client);
2018-05-23 21:04:39 +05:30
$accessToken->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
2018-05-23 21:04:39 +05:30
$token = (string) $accessToken;
$request = (new ServerRequest())->withHeader('authorization', sprintf('Bearer %s', $token));
2016-03-10 22:52:48 +05:30
$middleware = new ResourceServerMiddleware($server);
$response = $middleware->__invoke(
$request,
new Response(),
function () {
$this->assertEquals('test', func_get_args()[0]->getAttribute('oauth_access_token_id'));
2016-03-10 23:10:28 +05:30
2016-03-10 22:52:48 +05:30
return func_get_args()[1];
}
);
$this->assertEquals(200, $response->getStatusCode());
}
2016-04-10 20:28:01 +05:30
public function testValidResponseExpiredToken()
{
$server = new ResourceServer(
2016-07-08 18:59:21 +05:30
$this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(),
'file://' . __DIR__ . '/../Stubs/public.key'
2016-04-10 20:28:01 +05:30
);
$client = new ClientEntity();
$client->setIdentifier('clientName');
$accessToken = new AccessTokenEntity();
$accessToken->setIdentifier('test');
$accessToken->setUserIdentifier(123);
$accessToken->setExpiryDateTime((new DateTimeImmutable())->sub(new DateInterval('PT1H')));
2016-04-10 20:28:01 +05:30
$accessToken->setClient($client);
2018-05-23 21:04:39 +05:30
$accessToken->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
2016-04-10 20:28:01 +05:30
2018-05-23 21:04:39 +05:30
$token = (string) $accessToken;
2016-04-10 20:28:01 +05:30
$request = (new ServerRequest())->withHeader('authorization', sprintf('Bearer %s', $token));
2016-04-10 20:28:01 +05:30
$middleware = new ResourceServerMiddleware($server);
$response = $middleware->__invoke(
$request,
new Response(),
function () {
$this->assertEquals('test', func_get_args()[0]->getAttribute('oauth_access_token_id'));
return func_get_args()[1];
}
);
$this->assertEquals(401, $response->getStatusCode());
}
2016-03-10 22:52:48 +05:30
public function testErrorResponse()
{
$server = new ResourceServer(
2016-07-08 18:59:21 +05:30
$this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(),
'file://' . __DIR__ . '/../Stubs/public.key'
2016-03-10 22:52:48 +05:30
);
$request = (new ServerRequest())->withHeader('authorization', '');
2016-03-10 22:52:48 +05:30
$middleware = new ResourceServerMiddleware($server);
$response = $middleware->__invoke(
$request,
new Response(),
function () {
return func_get_args()[1];
}
);
$this->assertEquals(401, $response->getStatusCode());
}
}