forked from laravel/passport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccessTokenControllerTest.php
67 lines (52 loc) · 2.1 KB
/
AccessTokenControllerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace Laravel\Passport\Tests;
use Laminas\Diactoros\Response;
use Laravel\Passport\Exceptions\OAuthServerException;
use Laravel\Passport\Http\Controllers\AccessTokenController;
use Laravel\Passport\TokenRepository;
use Lcobucci\JWT\Parser;
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\Exception\OAuthServerException as LeagueException;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class AccessTokenControllerTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
public function test_a_token_can_be_issued()
{
$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 = 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($request)->getContent());
}
public function test_exceptions_are_handled()
{
$tokens = m::mock(TokenRepository::class);
$jwt = m::mock(Parser::class);
$server = m::mock(AuthorizationServer::class);
$server->shouldReceive('respondToAccessTokenRequest')->with(
m::type(ServerRequestInterface::class), m::type(ResponseInterface::class)
)->andThrow(LeagueException::invalidCredentials());
$controller = new AccessTokenController($server, $tokens, $jwt);
$this->expectException(OAuthServerException::class);
$controller->issueToken(m::mock(ServerRequestInterface::class));
}
}
class AccessTokenControllerTestStubToken
{
public $client_id = 1;
public $user_id = 2;
}