-
Notifications
You must be signed in to change notification settings - Fork 5
/
MiddlewareTest.php
124 lines (96 loc) · 4.32 KB
/
MiddlewareTest.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
use Lcobucci\JWT\Validation\ConstraintViolation;
use Orchestra\Testbench\TestCase;
use STS\JWT\Exceptions\InvalidID;
use STS\JWT\Facades\JWT;
class MiddlewareTest extends TestCase
{
protected function getPackageProviders($app): array
{
return [\STS\JWT\JWTServiceProvider::class];
}
protected function getPackageAliases($app): array
{
return [
'JWT' => \STS\JWT\Facades\JWT::class
];
}
protected function getEnvironmentSetUp($app): void
{
$app['config']->set([
'jwt.key' => 'thisissigningkeythisissigningkey',
'jwt.audience' => 'myappaud',
'jwt.issuer' => 'myappiss'
]);
}
public function testMissingToken()
{
$middleware = new \STS\JWT\JwtValidateMiddleware();
$request = new \Illuminate\Http\Request();
$this->expectException(\Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException::class);
$middleware->findJWT($request);
}
public function testTokenInRequest()
{
$middleware = new \STS\JWT\JwtValidateMiddleware();
$request = new \Illuminate\Http\Request();
$request->offsetSet("jwt", "foobar");
$this->assertEquals("foobar", $middleware->findJWT($request));
$request = new \Illuminate\Http\Request();
$request->offsetSet("token", "baz");
$this->assertEquals("baz", $middleware->findJWT($request));
}
public function testTokenInRoute()
{
$middleware = new \STS\JWT\JwtValidateMiddleware();
$request = new \Illuminate\Http\Request();
$route = new \Illuminate\Routing\Route([], '', []);
$route->parameters = ['jwt' => 'foobar'];
$request->setRouteResolver(function() use($route) { return $route; });
$this->assertEquals("foobar", $middleware->findJWT($request));
$route->parameters = ['token' => 'baz'];
$this->assertEquals("baz", $middleware->findJWT($request));
}
public function testTokenInAuthorizationHeader()
{
$middleware = new \STS\JWT\JwtValidateMiddleware();
$request = new \Illuminate\Http\Request();
$request->headers->set('Authorization', 'Basic' . base64_encode('username:foobar'));
$this->assertEquals("foobar", $middleware->findJWT($request));
$request = new \Illuminate\Http\Request();
$request->headers->set('Authorization', 'Bearer baz');
$this->assertEquals("baz", $middleware->findJWT($request));
$request = new \Illuminate\Http\Request();
$request->headers->set('Authorization', 'Token baz');
$this->assertEquals("baz", $middleware->findJWT($request));
}
public function testIdFromRouteName()
{
$middleware = new \STS\JWT\JwtValidateMiddleware();
$request = new \Illuminate\Http\Request();
$route = new \Illuminate\Routing\Route([], '', []);
// Set the jwt id to match our route name
$route->parameters = ['jwt' => JWT::get('my.route')];
$route->action = ['as' => 'my.route'];
$request->setRouteResolver(fn() => $route);
$this->assertEquals("success", $middleware->handle($request, function() { return "success"; }));
// Change the route name and the JWT won't pass
$route->action = ['as' => 'new.name'];
$this->expectException(InvalidID::class);
$this->expectExceptionMessage('The token is not identified with the expected ID');
$this->assertEquals("success", $middleware->handle($request, function() { return "success"; }));
}
public function testSpecifiedId()
{
$middleware = new \STS\JWT\JwtValidateMiddleware();
$request = new \Illuminate\Http\Request();
$route = new \Illuminate\Routing\Route([], '', []);
// Set the jwt id to match our route name
$route->parameters = ['jwt' => JWT::get('test-id')];
$request->setRouteResolver(function() use($route) { return $route; });
$this->assertEquals("success", $middleware->handle($request, function() { return "success"; }, 'test-id'));
$this->expectException(InvalidID::class);
$this->expectExceptionMessage('The token is not identified with the expected ID');
$this->assertEquals("success", $middleware->handle($request, function() { return "success"; }, 'different-id'));
}
}