mirror of
https://github.com/elyby/oauth2-server.git
synced 2024-12-23 05:29:52 +05:30
Added middleware tests
This commit is contained in:
parent
a716a08be6
commit
c490cd4ef2
85
tests/Middleware/AuthenticationServerMiddlewareTest.php
Normal file
85
tests/Middleware/AuthenticationServerMiddlewareTest.php
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace LeagueTests\Middleware;
|
||||||
|
|
||||||
|
use League\OAuth2\Server\Entities\ClientEntity;
|
||||||
|
use League\OAuth2\Server\Grant\ClientCredentialsGrant;
|
||||||
|
use League\OAuth2\Server\Middleware\AuthenticationServerMiddleware;
|
||||||
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
|
||||||
|
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
|
||||||
|
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
|
||||||
|
use League\OAuth2\Server\Server;
|
||||||
|
use LeagueTests\Stubs\StubResponseType;
|
||||||
|
use Zend\Diactoros\Response;
|
||||||
|
use Zend\Diactoros\ServerRequestFactory;
|
||||||
|
|
||||||
|
class AuthenticationServerMiddlewareTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
public function testValidResponse()
|
||||||
|
{
|
||||||
|
$clientRepository = $this->getMock(ClientRepositoryInterface::class);
|
||||||
|
$clientRepository->method('getClientEntity')->willReturn(new ClientEntity());
|
||||||
|
|
||||||
|
$server = new Server(
|
||||||
|
$clientRepository,
|
||||||
|
$this->getMock(AccessTokenRepositoryInterface::class),
|
||||||
|
$this->getMock(ScopeRepositoryInterface::class),
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
new StubResponseType()
|
||||||
|
);
|
||||||
|
|
||||||
|
$server->enableGrantType(new ClientCredentialsGrant(), new \DateInterval('PT1M'));
|
||||||
|
|
||||||
|
$_POST['grant_type'] = 'client_credentials';
|
||||||
|
$_POST['client_id'] = 'foo';
|
||||||
|
$_POST['client_secret'] = 'bar';
|
||||||
|
|
||||||
|
$request = ServerRequestFactory::fromGlobals();
|
||||||
|
|
||||||
|
$middleware = new AuthenticationServerMiddleware($server);
|
||||||
|
$response = $middleware->__invoke(
|
||||||
|
$request,
|
||||||
|
new Response(),
|
||||||
|
function () {
|
||||||
|
return func_get_args()[1];
|
||||||
|
}
|
||||||
|
);
|
||||||
|
$this->assertEquals(200, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testOAuthErrorResponse()
|
||||||
|
{
|
||||||
|
$clientRepository = $this->getMock(ClientRepositoryInterface::class);
|
||||||
|
$clientRepository->method('getClientEntity')->willReturn(null);
|
||||||
|
|
||||||
|
$server = new Server(
|
||||||
|
$clientRepository,
|
||||||
|
$this->getMock(AccessTokenRepositoryInterface::class),
|
||||||
|
$this->getMock(ScopeRepositoryInterface::class),
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
new StubResponseType()
|
||||||
|
);
|
||||||
|
|
||||||
|
$server->enableGrantType(new ClientCredentialsGrant(), new \DateInterval('PT1M'));
|
||||||
|
|
||||||
|
$_POST['grant_type'] = 'client_credentials';
|
||||||
|
$_POST['client_id'] = 'foo';
|
||||||
|
$_POST['client_secret'] = 'bar';
|
||||||
|
|
||||||
|
$request = ServerRequestFactory::fromGlobals();
|
||||||
|
|
||||||
|
$middleware = new AuthenticationServerMiddleware($server);
|
||||||
|
|
||||||
|
$response = $middleware->__invoke(
|
||||||
|
$request,
|
||||||
|
new Response(),
|
||||||
|
function () {
|
||||||
|
return func_get_args()[1];
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(401, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
}
|
73
tests/Middleware/ResourceServerMiddlewareTest.php
Normal file
73
tests/Middleware/ResourceServerMiddlewareTest.php
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace LeagueTests\Middleware;
|
||||||
|
|
||||||
|
use League\OAuth2\Server\Middleware\ResourceServerMiddleware;
|
||||||
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
|
||||||
|
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
|
||||||
|
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
|
||||||
|
use League\OAuth2\Server\Server;
|
||||||
|
use LeagueTests\Stubs\StubResponseType;
|
||||||
|
use Zend\Diactoros\Response;
|
||||||
|
use Zend\Diactoros\ServerRequest;
|
||||||
|
|
||||||
|
class ResourceServerMiddlewareTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
public function testValidResponse()
|
||||||
|
{
|
||||||
|
$clientRepository = $this->getMock(ClientRepositoryInterface::class);
|
||||||
|
|
||||||
|
$server = new Server(
|
||||||
|
$clientRepository,
|
||||||
|
$this->getMock(AccessTokenRepositoryInterface::class),
|
||||||
|
$this->getMock(ScopeRepositoryInterface::class),
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
new StubResponseType()
|
||||||
|
);
|
||||||
|
|
||||||
|
$request = new ServerRequest();
|
||||||
|
$request = $request->withHeader('authorization', 'Basic test');
|
||||||
|
|
||||||
|
$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(200, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testErrorResponse()
|
||||||
|
{
|
||||||
|
$clientRepository = $this->getMock(ClientRepositoryInterface::class);
|
||||||
|
|
||||||
|
$server = new Server(
|
||||||
|
$clientRepository,
|
||||||
|
$this->getMock(AccessTokenRepositoryInterface::class),
|
||||||
|
$this->getMock(ScopeRepositoryInterface::class),
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
new StubResponseType()
|
||||||
|
);
|
||||||
|
|
||||||
|
$request = new ServerRequest();
|
||||||
|
$request = $request->withHeader('authorization', '');
|
||||||
|
|
||||||
|
$middleware = new ResourceServerMiddleware($server);
|
||||||
|
$response = $middleware->__invoke(
|
||||||
|
$request,
|
||||||
|
new Response(),
|
||||||
|
function () {
|
||||||
|
return func_get_args()[1];
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(401, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user