forked from tymondesigns/jwt-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRefreshTokenTest.php
More file actions
87 lines (69 loc) 路 2.7 KB
/
Copy pathRefreshTokenTest.php
File metadata and controls
87 lines (69 loc) 路 2.7 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Test\Middleware;
use Mockery;
use Illuminate\Http\Response;
use Tymon\JWTAuth\Http\Parser\Parser;
use Tymon\JWTAuth\Http\Middleware\RefreshToken;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
class RefreshTokenTest extends AbstractMiddlewareTest
{
/**
* @var \Tymon\JWTAuth\Http\Middleware\RefreshToken
*/
protected $middleware;
public function setUp()
{
parent::setUp();
$this->middleware = new RefreshToken($this->auth);
}
/** @test */
public function it_should_refresh_a_token()
{
$parser = Mockery::mock(Parser::class);
$parser->shouldReceive('hasToken')->once()->andReturn(true);
$this->auth->shouldReceive('parser')->andReturn($parser);
$this->auth->parser()->shouldReceive('setRequest')->once()->with($this->request)->andReturn($this->auth->parser());
$this->auth->shouldReceive('parseToken->refresh')->once()->andReturn('foo.bar.baz');
$response = $this->middleware->handle($this->request, function () {
return new Response;
});
$this->assertSame($response->headers->get('authorization'), 'Bearer foo.bar.baz');
}
/**
* @test
* @expectedException \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
*/
public function it_should_throw_an_unauthorized_exception_if_token_not_provided()
{
$parser = Mockery::mock(Parser::class);
$parser->shouldReceive('hasToken')->once()->andReturn(false);
$this->auth->shouldReceive('parser')->andReturn($parser);
$this->auth->parser()->shouldReceive('setRequest')->once()->with($this->request)->andReturn($this->auth->parser());
$this->middleware->handle($this->request, function () {
//
});
}
/**
* @test
* @expectedException \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
*/
public function it_should_throw_an_unauthorized_exception_if_token_invalid()
{
$parser = Mockery::mock(Parser::class);
$parser->shouldReceive('hasToken')->once()->andReturn(true);
$this->auth->shouldReceive('parser')->andReturn($parser);
$this->auth->parser()->shouldReceive('setRequest')->once()->with($this->request)->andReturn($this->auth->parser());
$this->auth->shouldReceive('parseToken->refresh')->once()->andThrow(new TokenInvalidException);
$this->middleware->handle($this->request, function () {
//
});
}
}