1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Created by PhpStorm. |
6
|
|
|
* User: benedikt |
7
|
|
|
* Date: 9/17/17 |
8
|
|
|
* Time: 12:33 AM |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Tfboe\FmLib\Tests\Unit\Http\Middleware; |
12
|
|
|
|
13
|
|
|
use Illuminate\Contracts\Auth\Factory; |
14
|
|
|
use Illuminate\Http\Request; |
15
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
16
|
|
|
use ReflectionException; |
17
|
|
|
use Tfboe\FmLib\Entity\UserInterface; |
18
|
|
|
use Tfboe\FmLib\Exceptions\AuthenticationException; |
19
|
|
|
use Tfboe\FmLib\Http\Middleware\Authenticate; |
20
|
|
|
use Tfboe\FmLib\Tests\Helpers\UnitTestCase; |
21
|
|
|
use Tymon\JWTAuth\Contracts\JWTSubject; |
22
|
|
|
use Tymon\JWTAuth\JWTGuard; |
23
|
|
|
use Tymon\JWTAuth\Payload; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class BaseControllerTest |
27
|
|
|
* @package Tests\Unit\App\Http\Controllers |
28
|
|
|
*/ |
29
|
|
|
class AuthenticateTest extends UnitTestCase |
30
|
|
|
{ |
31
|
|
|
//tests also private method disable this tests as soon as all are used in public interfaces |
32
|
|
|
//<editor-fold desc="Public Methods"> |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @covers \Tfboe\FmLib\Http\Middleware\Authenticate::__construct |
36
|
|
|
* @throws ReflectionException |
37
|
|
|
*/ |
38
|
|
|
public function testConstruct() |
39
|
|
|
{ |
40
|
|
|
$authenticate = $this->authenticate(); |
41
|
|
|
self::assertInstanceOf(Authenticate::class, $authenticate); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @covers \Tfboe\FmLib\Http\Middleware\Authenticate::handle |
46
|
|
|
* @throws AuthenticationException |
47
|
|
|
* @throws ReflectionException |
48
|
|
|
* @uses \Tfboe\FmLib\Exceptions\AuthenticationException::__construct |
49
|
|
|
* @uses \Tfboe\FmLib\Http\Middleware\Authenticate::__construct |
50
|
|
|
*/ |
51
|
|
|
public function testHandleGuest() |
52
|
|
|
{ |
53
|
|
|
$this->expectException(AuthenticationException::class); |
54
|
|
|
$this->expectExceptionMessage('Not logged in!'); |
55
|
|
|
|
56
|
|
|
$auth = $this->getAuth(true, true, UserInterface::class, 6); |
57
|
|
|
$authenticate = $this->authenticate($auth); |
|
|
|
|
58
|
|
|
$authenticate->handle(null, function () { |
|
|
|
|
59
|
|
|
}, "guardName"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @covers \Tfboe\FmLib\Http\Middleware\Authenticate::handle |
64
|
|
|
* @throws AuthenticationException |
65
|
|
|
* @throws ReflectionException |
66
|
|
|
* @uses \Tfboe\FmLib\Exceptions\AuthenticationException::__construct |
67
|
|
|
* @uses \Tfboe\FmLib\Http\Middleware\Authenticate::__construct |
68
|
|
|
*/ |
69
|
|
|
public function testHandleNoVer() |
70
|
|
|
{ |
71
|
|
|
$this->expectException(AuthenticationException::class); |
72
|
|
|
$this->expectExceptionMessage('Payload version expired!'); |
73
|
|
|
|
74
|
|
|
$auth = $this->getAuth(false, false, UserInterface::class, 6); |
75
|
|
|
$authenticate = $this->authenticate($auth); |
|
|
|
|
76
|
|
|
$authenticate->handle(null, function () { |
|
|
|
|
77
|
|
|
}, "guardName"); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @covers \Tfboe\FmLib\Http\Middleware\Authenticate::handle |
82
|
|
|
* @throws AuthenticationException |
83
|
|
|
* @throws ReflectionException |
84
|
|
|
* @uses \Tfboe\FmLib\Exceptions\AuthenticationException::__construct |
85
|
|
|
* @uses \Tfboe\FmLib\Http\Middleware\Authenticate::__construct |
86
|
|
|
*/ |
87
|
|
|
public function testHandleOldVersion() |
88
|
|
|
{ |
89
|
|
|
$this->expectException(AuthenticationException::class); |
90
|
|
|
$this->expectExceptionMessage('Payload version expired!'); |
91
|
|
|
|
92
|
|
|
$auth = $this->getAuth(false, true, UserInterface::class, 8); |
93
|
|
|
$authenticate = $this->authenticate($auth); |
|
|
|
|
94
|
|
|
$authenticate->handle(null, function () { |
|
|
|
|
95
|
|
|
}, "guardName"); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @covers \Tfboe\FmLib\Http\Middleware\Authenticate::handle |
100
|
|
|
* @throws AuthenticationException |
101
|
|
|
* @throws ReflectionException |
102
|
|
|
* @uses \Tfboe\FmLib\Http\Middleware\Authenticate::__construct |
103
|
|
|
*/ |
104
|
|
|
public function testHandleSuccess() |
105
|
|
|
{ |
106
|
|
|
$auth = $this->getAuth(false, true, UserInterface::class, 6); |
107
|
|
|
/** @var Request $request */ |
108
|
|
|
$request = $this->createMock(Request::class); |
109
|
|
|
|
110
|
|
|
$called = false; |
111
|
|
|
$next = function ($req) use (&$called, $request) { |
112
|
|
|
self::assertTrue($request === $req); |
113
|
|
|
self::assertFalse($called); |
114
|
|
|
$called = true; |
115
|
|
|
}; |
116
|
|
|
|
117
|
|
|
$authenticate = $this->authenticate($auth); |
|
|
|
|
118
|
|
|
self::assertFalse($called); |
119
|
|
|
$authenticate->handle($request, $next, "guardName"); |
|
|
|
|
120
|
|
|
self::assertTrue($called); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @covers \Tfboe\FmLib\Http\Middleware\Authenticate::handle |
125
|
|
|
* @throws AuthenticationException |
126
|
|
|
* @throws ReflectionException |
127
|
|
|
* @uses \Tfboe\FmLib\Exceptions\AuthenticationException::__construct |
128
|
|
|
* @uses \Tfboe\FmLib\Http\Middleware\Authenticate::__construct |
129
|
|
|
*/ |
130
|
|
|
public function testHandleWrongUser() |
131
|
|
|
{ |
132
|
|
|
$this->expectException(AuthenticationException::class); |
133
|
|
|
$this->expectExceptionMessage('Payload version expired!'); |
134
|
|
|
|
135
|
|
|
$auth = $this->getAuth(false, true, JWTSubject::class, 6); |
136
|
|
|
$authenticate = $this->authenticate($auth); |
|
|
|
|
137
|
|
|
$authenticate->handle(null, function () { |
|
|
|
|
138
|
|
|
}, "guardName"); |
139
|
|
|
} |
140
|
|
|
//</editor-fold desc="Public Methods"> |
141
|
|
|
|
142
|
|
|
//<editor-fold desc="Private Methods"> |
143
|
|
|
/** |
144
|
|
|
* @param Factory|null $auth |
145
|
|
|
* @param array $stubbedMethods |
146
|
|
|
* @return Authenticate|MockObject |
147
|
|
|
* @throws ReflectionException |
148
|
|
|
*/ |
149
|
|
|
private function authenticate(?Factory $auth = null, |
150
|
|
|
array $stubbedMethods = []): MockObject |
151
|
|
|
{ |
152
|
|
|
if ($auth === null) { |
153
|
|
|
$auth = $this->createMock(Factory::class); |
154
|
|
|
} |
155
|
|
|
return $this->getMockForAbstractClass(Authenticate::class, [$auth], '', true, |
156
|
|
|
true, true, $stubbedMethods); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param bool $guest |
161
|
|
|
* @param bool $hasVer |
162
|
|
|
* @param string $userClass |
163
|
|
|
* @param int $userConfirmedVersion |
164
|
|
|
* @return MockObject|Factory |
165
|
|
|
*/ |
166
|
|
|
private function getAuth(bool $guest, bool $hasVer, string $userClass, int $userConfirmedVersion): MockObject |
167
|
|
|
{ |
168
|
|
|
$guard = $this->createMock(JWTGuard::class); |
169
|
|
|
$auth = $this->createMock(Factory::class); |
170
|
|
|
$payload = $this->createMock(Payload::class); |
171
|
|
|
$user = $this->createMock($userClass); |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
$auth->expects(self::once())->method("guard")->with("guardName")->willReturn($guard); |
175
|
|
|
|
176
|
|
|
$guard->method('guest')->willReturn($guest); |
177
|
|
|
$guard->method('getPayload')->willReturn($payload); |
178
|
|
|
$guard->method('getUser')->willReturn($user); |
179
|
|
|
|
180
|
|
|
$payload->method('hasKey')->with('ver')->willReturn($hasVer); |
181
|
|
|
$payload->method('get')->with(['ver'])->willReturn([7]); |
182
|
|
|
|
183
|
|
|
if ($userClass === UserInterface::class) { |
184
|
|
|
$user->method('getJwtVersion')->willReturn($userConfirmedVersion); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $auth; |
188
|
|
|
} |
189
|
|
|
//</editor-fold desc="Private Methods"> |
190
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.