Skip to content

Commit

Permalink
Import namespaces in test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Jan 21, 2019
1 parent d11c0cc commit 3f65ac4
Show file tree
Hide file tree
Showing 16 changed files with 190 additions and 156 deletions.
36 changes: 18 additions & 18 deletions tests/AccessTokenControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
use PHPUnit\Framework\TestCase;
use Illuminate\Container\Container;
use Laravel\Passport\TokenRepository;
use Psr\Http\Message\ResponseInterface;
use Illuminate\Contracts\Config\Repository;
use Psr\Http\Message\ServerRequestInterface;
use League\OAuth2\Server\AuthorizationServer;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Laravel\Passport\Http\Controllers\AccessTokenController;

Expand All @@ -22,27 +25,22 @@ public function tearDown()

public function test_a_token_can_be_issued()
{
$server = m::mock('League\OAuth2\Server\AuthorizationServer');
$request = m::mock(ServerRequestInterface::class);
$response = m::type(ResponseInterface::class);
$tokens = m::mock(TokenRepository::class);
$jwt = m::mock(Parser::class);

$psrResponse = new Response();
$psrResponse->getBody()->write(json_encode(['access_token' => 'access-token']));

$server->shouldReceive('respondToAccessTokenRequest')->with(
m::type('Psr\Http\Message\ServerRequestInterface'), m::type('Psr\Http\Message\ResponseInterface')
)->andReturn($psrResponse);

$jwt = m::mock(Parser::class);
// $jwt->shouldReceive('parse->getClaim')->andReturn('token-id');

// $tokens->shouldReceive('find')->once()->with('token-id')->andReturn(new AccessTokenControllerTestStubToken);
// $tokens->shouldReceive('revokeOtherAccessTokens')->once()->with(1, 2, 'token-id', false);
$server = m::mock(AuthorizationServer::class);
$server->shouldReceive('respondToAccessTokenRequest')
->with($request, $response)
->andReturn($psrResponse);

$controller = new AccessTokenController($server, $tokens, $jwt);

$this->assertEquals('{"access_token":"access-token"}', $controller->issueToken(
m::mock('Psr\Http\Message\ServerRequestInterface')
)->getContent());
$this->assertEquals('{"access_token":"access-token"}', $controller->issueToken($request)->getContent());
}

public function test_exceptions_are_handled()
Expand All @@ -52,17 +50,19 @@ public function test_exceptions_are_handled()
$exceptions->shouldReceive('report')->once();
$config->shouldReceive('get')->once()->andReturn(true);

$request = m::mock(ServerRequestInterface::class);
$response = m::type(ResponseInterface::class);
$tokens = m::mock(TokenRepository::class);
$jwt = m::mock(Parser::class);

$server = m::mock('League\OAuth2\Server\AuthorizationServer');
$server->shouldReceive('respondToAccessTokenRequest')->with(
m::type('Psr\Http\Message\ServerRequestInterface'), m::type('Psr\Http\Message\ResponseInterface')
)->andThrow(new Exception('whoops'));
$server = m::mock(AuthorizationServer::class);
$server->shouldReceive('respondToAccessTokenRequest')
->with($request, $response)
->andThrow(new Exception('whoops'));

$controller = new AccessTokenController($server, $tokens, $jwt);

$this->assertEquals('whoops', $controller->issueToken(m::mock('Psr\Http\Message\ServerRequestInterface'))->getOriginalContent());
$this->assertEquals('whoops', $controller->issueToken($request)->getOriginalContent());
}
}

Expand Down
6 changes: 4 additions & 2 deletions tests/ApiTokenCookieFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Illuminate\Encryption\Encrypter;
use Illuminate\Contracts\Config\Repository;
use Laravel\Passport\ApiTokenCookieFactory;
use Symfony\Component\HttpFoundation\Cookie;

class ApiTokenCookieFactoryTest extends TestCase
{
Expand All @@ -16,7 +18,7 @@ public function tearDown()

public function test_cookie_can_be_successfully_created()
{
$config = m::mock('Illuminate\Contracts\Config\Repository');
$config = m::mock(Repository::class);
$config->shouldReceive('get')->with('session')->andReturn([
'lifetime' => 120,
'path' => '/',
Expand All @@ -29,6 +31,6 @@ public function test_cookie_can_be_successfully_created()

$cookie = $factory->make(1, 'token');

$this->assertInstanceOf('Symfony\Component\HttpFoundation\Cookie', $cookie);
$this->assertInstanceOf(Cookie::class, $cookie);
}
}
9 changes: 6 additions & 3 deletions tests/ApproveAuthorizationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Laravel\Passport\Tests;

use Mockery as m;
use Illuminate\Http\Request;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Response;
use PHPUnit\Framework\TestCase;
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
use Laravel\Passport\Http\Controllers\ApproveAuthorizationController;

class ApproveAuthorizationControllerTest extends TestCase
Expand All @@ -21,12 +24,12 @@ public function test_complete_authorization_request()

$controller = new ApproveAuthorizationController($server);

$request = m::mock('Illuminate\Http\Request');
$request = m::mock(Request::class);
$request->shouldReceive('session')->andReturn($session = m::mock());
$session->shouldReceive('get')
->once()
->with('authRequest')
->andReturn($authRequest = m::mock('League\OAuth2\Server\RequestTypes\AuthorizationRequest'));
->andReturn($authRequest = m::mock(AuthorizationRequest::class));
$request->shouldReceive('user')->andReturn(new ApproveAuthorizationControllerFakeUser);
$authRequest->shouldReceive('getClient->getIdentifier')->andReturn(1);
$authRequest->shouldReceive('getUser->getIdentifier')->andReturn(2);
Expand All @@ -37,7 +40,7 @@ public function test_complete_authorization_request()
$psrResponse->getBody()->write('response');

$server->shouldReceive('completeAuthorizationRequest')
->with($authRequest, m::type('Psr\Http\Message\ResponseInterface'))
->with($authRequest, m::type(ResponseInterface::class))
->andReturn($psrResponse);

$this->assertEquals('response', $controller->approve($request)->getContent());
Expand Down
41 changes: 22 additions & 19 deletions tests/AuthorizationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
namespace Laravel\Passport\Tests;

use Exception;
use Illuminate\Http\Request;
use Laravel\Passport\ClientRepository;
use Laravel\Passport\Token;
use Laravel\Passport\TokenRepository;
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
use Mockery as m;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response;
use Laravel\Passport\Passport;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -34,7 +41,7 @@ public function test_authorization_view_is_presented()

$server->shouldReceive('validateAuthorizationRequest')->andReturn($authRequest = m::mock());

$request = m::mock('Illuminate\Http\Request');
$request = m::mock(Request::class);
$request->shouldReceive('session')->andReturn($session = m::mock());
$session->shouldReceive('put')->with('authRequest', $authRequest);
$request->shouldReceive('user')->andReturn('user');
Expand All @@ -51,14 +58,14 @@ public function test_authorization_view_is_presented()
return 'view';
});

$clients = m::mock('Laravel\Passport\ClientRepository');
$clients = m::mock(ClientRepository::class);
$clients->shouldReceive('find')->with(1)->andReturn('client');

$tokens = m::mock('Laravel\Passport\TokenRepository');
$tokens = m::mock(TokenRepository::class);
$tokens->shouldReceive('findValidToken')->with('user', 'client')->andReturnNull();

$this->assertEquals('view', $controller->authorize(
m::mock('Psr\Http\Message\ServerRequestInterface'), $request, $clients, $tokens
m::mock(ServerRequestInterface::class), $request, $clients, $tokens
));
}

Expand All @@ -74,21 +81,17 @@ public function test_authorization_exceptions_are_handled()

$server->shouldReceive('validateAuthorizationRequest')->andThrow(new Exception('whoops'));

$request = m::mock('Illuminate\Http\Request');
$request = m::mock(Request::class);
$request->shouldReceive('session')->andReturn($session = m::mock());

$clients = m::mock('Laravel\Passport\ClientRepository');

$tokens = m::mock('Laravel\Passport\TokenRepository');
$clients = m::mock(ClientRepository::class);
$tokens = m::mock(TokenRepository::class);

$this->assertEquals('whoops', $controller->authorize(
m::mock('Psr\Http\Message\ServerRequestInterface'), $request, $clients, $tokens
m::mock(ServerRequestInterface::class), $request, $clients, $tokens
)->getContent());
}

/**
* @group shithead
*/
public function test_request_is_approved_if_valid_token_exists()
{
Passport::tokensCan([
Expand All @@ -102,12 +105,12 @@ public function test_request_is_approved_if_valid_token_exists()
$psrResponse = new Response();
$psrResponse->getBody()->write('approved');
$server->shouldReceive('validateAuthorizationRequest')
->andReturn($authRequest = m::mock('League\OAuth2\Server\RequestTypes\AuthorizationRequest'));
->andReturn($authRequest = m::mock(AuthorizationRequest::class));
$server->shouldReceive('completeAuthorizationRequest')
->with($authRequest, m::type('Psr\Http\Message\ResponseInterface'))
->with($authRequest, m::type(ResponseInterface::class))
->andReturn($psrResponse);

$request = m::mock('Illuminate\Http\Request');
$request = m::mock(Request::class);
$request->shouldReceive('user')->once()->andReturn($user = m::mock());
$user->shouldReceive('getKey')->andReturn(1);
$request->shouldNotReceive('session');
Expand All @@ -117,17 +120,17 @@ public function test_request_is_approved_if_valid_token_exists()
$authRequest->shouldReceive('setUser')->once()->andReturnNull();
$authRequest->shouldReceive('setAuthorizationApproved')->once()->with(true);

$clients = m::mock('Laravel\Passport\ClientRepository');
$clients = m::mock(ClientRepository::class);
$clients->shouldReceive('find')->with(1)->andReturn('client');

$tokens = m::mock('Laravel\Passport\TokenRepository');
$tokens = m::mock(TokenRepository::class);
$tokens->shouldReceive('findValidToken')
->with($user, 'client')
->andReturn($token = m::mock('Laravel\Passport\Token'));
->andReturn($token = m::mock(Token::class));
$token->shouldReceive('getAttribute')->with('scopes')->andReturn(['scope-1']);

$this->assertEquals('approved', $controller->authorize(
m::mock('Psr\Http\Message\ServerRequestInterface'), $request, $clients, $tokens
m::mock(ServerRequestInterface::class), $request, $clients, $tokens
)->getContent());
}
}
14 changes: 7 additions & 7 deletions tests/AuthorizedAccessTokenControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ class AuthorizedAccessTokenControllerTest extends TestCase
*/
protected $controller;

public function tearDown()
public function setUp()
{
m::close();
$this->tokenRepository = m::mock(TokenRepository::class);
$this->controller = new AuthorizedAccessTokenController($this->tokenRepository);
}

public function setUp()
public function tearDown()
{
$this->tokenRepository = m::mock(TokenRepository::class);
$this->controller = new AuthorizedAccessTokenController(
$this->tokenRepository
);
m::close();

unset($this->tokenRepository, $this->controller);
}

public function test_tokens_can_be_retrieved_for_users()
Expand Down
7 changes: 4 additions & 3 deletions tests/BridgeAccessTokenRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use PHPUnit\Framework\TestCase;
use Laravel\Passport\Bridge\Scope;
use Laravel\Passport\Bridge\Client;
use Laravel\Passport\TokenRepository;
use Laravel\Passport\Bridge\AccessToken;
use Illuminate\Contracts\Events\Dispatcher;
use Laravel\Passport\Bridge\AccessTokenRepository;

class BridgeAccessTokenRepositoryTest extends TestCase
Expand All @@ -21,9 +23,8 @@ public function test_access_tokens_can_be_persisted()
{
$expiration = Carbon::now();

$tokenRepository = m::mock('Laravel\Passport\TokenRepository');

$events = m::mock('Illuminate\Contracts\Events\Dispatcher');
$tokenRepository = m::mock(TokenRepository::class);
$events = m::mock(Dispatcher::class);

$tokenRepository->shouldReceive('create')->once()->andReturnUsing(function ($array) use ($expiration) {
$this->assertEquals(1, $array['id']);
Expand Down
20 changes: 13 additions & 7 deletions tests/BridgeClientRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Laravel\Passport\Bridge\Client;
use Laravel\Passport\ClientRepository;
use Laravel\Passport\Bridge\ClientRepository as BridgeClientRepository;

Expand Down Expand Up @@ -33,13 +34,15 @@ public function setUp()
public function tearDown()
{
m::close();

unset($this->clientModelRepository, $this->repository);
}

public function test_can_get_client_for_auth_code_grant()
{
$client = $this->repository->getClientEntity(1, 'authorization_code', 'secret', true);

$this->assertInstanceOf('Laravel\Passport\Bridge\Client', $client);
$this->assertInstanceOf(Client::class, $client);
$this->assertNull($this->repository->getClientEntity(1, 'authorization_code', 'wrong-secret', true));
$this->assertNull($this->repository->getClientEntity(1, 'client_credentials', 'wrong-secret', true));
}
Expand All @@ -49,7 +52,10 @@ public function test_can_get_client_for_client_credentials_grant()
$client = $this->clientModelRepository->findActive(1);
$client->personal_access_client = true;

$this->assertInstanceOf('Laravel\Passport\Bridge\Client', $this->repository->getClientEntity(1, 'client_credentials', 'secret', true));
$this->assertInstanceOf(
Client::class,
$this->repository->getClientEntity(1, 'client_credentials', 'secret', true)
);
$this->assertNull($this->repository->getClientEntity(1, 'authorization_code', 'secret', true));
}

Expand All @@ -58,7 +64,7 @@ public function test_password_grant_is_permitted()
$client = $this->clientModelRepository->findActive(1);
$client->password_client = true;

$this->assertInstanceOf('Laravel\Passport\Bridge\Client', $this->repository->getClientEntity(1, 'password', 'secret'));
$this->assertInstanceOf(Client::class, $this->repository->getClientEntity(1, 'password', 'secret'));
}

public function test_password_grant_is_prevented()
Expand All @@ -68,7 +74,7 @@ public function test_password_grant_is_prevented()

public function test_authorization_code_grant_is_permitted()
{
$this->assertInstanceOf('Laravel\Passport\Bridge\Client', $this->repository->getClientEntity(1, 'authorization_code', 'secret'));
$this->assertInstanceOf(Client::class, $this->repository->getClientEntity(1, 'authorization_code', 'secret'));
}

public function test_authorization_code_grant_is_prevented()
Expand All @@ -84,7 +90,7 @@ public function test_personal_access_grant_is_permitted()
$client = $this->clientModelRepository->findActive(1);
$client->personal_access_client = true;

$this->assertInstanceOf('Laravel\Passport\Bridge\Client', $this->repository->getClientEntity(1, 'personal_access', 'secret'));
$this->assertInstanceOf(Client::class, $this->repository->getClientEntity(1, 'personal_access', 'secret'));
}

public function test_personal_access_grant_is_prevented()
Expand All @@ -94,7 +100,7 @@ public function test_personal_access_grant_is_prevented()

public function test_client_credentials_grant_is_permitted()
{
$this->assertInstanceOf('Laravel\Passport\Bridge\Client', $this->repository->getClientEntity(1, 'client_credentials', 'secret'));
$this->assertInstanceOf(Client::class, $this->repository->getClientEntity(1, 'client_credentials', 'secret'));
}

public function test_client_credentials_grant_is_prevented()
Expand All @@ -110,7 +116,7 @@ public function test_grant_types_allows_request()
$client = $this->clientModelRepository->findActive(1);
$client->grant_types = ['client_credentials'];

$this->assertInstanceOf('Laravel\Passport\Bridge\Client', $this->repository->getClientEntity(1, 'client_credentials', 'secret'));
$this->assertInstanceOf(Client::class, $this->repository->getClientEntity(1, 'client_credentials', 'secret'));
}

public function test_grant_types_disallows_request()
Expand Down
Loading

0 comments on commit 3f65ac4

Please sign in to comment.