-
Notifications
You must be signed in to change notification settings - Fork 11.3k
/
Copy pathAuthenticateMiddlewareTest.php
224 lines (171 loc) · 6.27 KB
/
AuthenticateMiddlewareTest.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
namespace Illuminate\Tests\Auth;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Auth\AuthManager;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Auth\Middleware\Authenticate;
use Illuminate\Auth\Middleware\AuthenticateWithBasicAuth;
use Illuminate\Auth\RequestGuard;
use Illuminate\Config\Repository as Config;
use Illuminate\Container\Container;
use Illuminate\Http\Request;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use stdClass;
class AuthenticateMiddlewareTest extends TestCase
{
protected $auth;
protected function setUp(): void
{
$container = Container::setInstance(new Container);
$this->auth = new AuthManager($container);
$container->singleton('config', function () {
return $this->createConfig();
});
}
protected function tearDown(): void
{
m::close();
Container::setInstance(null);
}
public function testItCanGenerateDefinitionViaStaticMethod()
{
$signature = (string) Authenticate::using('foo');
$this->assertSame('Illuminate\Auth\Middleware\Authenticate:foo', $signature);
$signature = (string) Authenticate::using('foo', 'bar');
$this->assertSame('Illuminate\Auth\Middleware\Authenticate:foo,bar', $signature);
$signature = (string) Authenticate::using('foo', 'bar', 'baz');
$this->assertSame('Illuminate\Auth\Middleware\Authenticate:foo,bar,baz', $signature);
}
public function testItCanGenerateDefinitionViaStaticMethodForBasic()
{
$signature = (string) AuthenticateWithBasicAuth::using('guard');
$this->assertSame('Illuminate\Auth\Middleware\AuthenticateWithBasicAuth:guard', $signature);
$signature = (string) AuthenticateWithBasicAuth::using('guard', 'field');
$this->assertSame('Illuminate\Auth\Middleware\AuthenticateWithBasicAuth:guard,field', $signature);
$signature = (string) AuthenticateWithBasicAuth::using(field: 'field');
$this->assertSame('Illuminate\Auth\Middleware\AuthenticateWithBasicAuth:,field', $signature);
}
public function testDefaultUnauthenticatedThrows()
{
$this->expectException(AuthenticationException::class);
$this->expectExceptionMessage('Unauthenticated.');
$this->registerAuthDriver('default', false);
$this->authenticate();
}
public function testDefaultUnauthenticatedThrowsWithGuards()
{
try {
$this->registerAuthDriver('default', false);
$this->authenticate('default');
} catch (AuthenticationException $e) {
$this->assertContains('default', $e->guards());
return;
}
$this->fail();
}
public function testDefaultAuthenticatedKeepsDefaultDriver()
{
$driver = $this->registerAuthDriver('default', true);
$this->authenticate();
$this->assertSame($driver, $this->auth->guard());
}
public function testSecondaryAuthenticatedUpdatesDefaultDriver()
{
$this->registerAuthDriver('default', false);
$secondary = $this->registerAuthDriver('secondary', true);
$this->authenticate('secondary');
$this->assertSame($secondary, $this->auth->guard());
}
public function testMultipleDriversUnauthenticatedThrows()
{
$this->expectException(AuthenticationException::class);
$this->expectExceptionMessage('Unauthenticated.');
$this->registerAuthDriver('default', false);
$this->registerAuthDriver('secondary', false);
$this->authenticate('default', 'secondary');
}
public function testMultipleDriversUnauthenticatedThrowsWithGuards()
{
$expectedGuards = ['default', 'secondary'];
try {
$this->registerAuthDriver('default', false);
$this->registerAuthDriver('secondary', false);
$this->authenticate(...$expectedGuards);
} catch (AuthenticationException $e) {
$this->assertEquals($expectedGuards, $e->guards());
return;
}
$this->fail();
}
public function testMultipleDriversAuthenticatedUpdatesDefault()
{
$this->registerAuthDriver('default', false);
$secondary = $this->registerAuthDriver('secondary', true);
$this->authenticate('default', 'secondary');
$this->assertSame($secondary, $this->auth->guard());
}
/**
* Create a new config repository instance.
*
* @return \Illuminate\Config\Repository
*/
protected function createConfig()
{
return new Config([
'auth' => [
'defaults' => ['guard' => 'default'],
'guards' => [
'default' => ['driver' => 'default'],
'secondary' => ['driver' => 'secondary'],
],
],
]);
}
/**
* Create and register a new auth driver with the auth manager.
*
* @param string $name
* @param bool $authenticated
* @return \Illuminate\Auth\RequestGuard
*/
protected function registerAuthDriver($name, $authenticated)
{
$driver = $this->createAuthDriver($authenticated);
$this->auth->extend($name, function () use ($driver) {
return $driver;
});
return $driver;
}
/**
* Create a new auth driver.
*
* @param bool $authenticated
* @return \Illuminate\Auth\RequestGuard
*/
protected function createAuthDriver($authenticated)
{
return new RequestGuard(function () use ($authenticated) {
return $authenticated ? new stdClass : null;
}, m::mock(Request::class), m::mock(EloquentUserProvider::class));
}
/**
* Call the authenticate middleware with the given guards.
*
* @param string ...$guards
* @return void
*
* @throws \Illuminate\Auth\AuthenticationException
*/
protected function authenticate(...$guards)
{
$request = m::mock(Request::class);
$request->shouldReceive('expectsJson')->andReturn(false);
$nextParam = null;
$next = function ($param) use (&$nextParam) {
$nextParam = $param;
};
(new Authenticate($this->auth))->handle($request, $next, ...$guards);
$this->assertSame($request, $nextParam);
}
}