-
-
Notifications
You must be signed in to change notification settings - Fork 641
Expand file tree
/
Copy pathLoginTest.php
More file actions
277 lines (228 loc) · 7.56 KB
/
Copy pathLoginTest.php
File metadata and controls
277 lines (228 loc) · 7.56 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
namespace Tests\Auth;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Event;
use Orchestra\Testbench\Attributes\DefineEnvironment;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Auth\TwoFactor\RecoveryCode;
use Statamic\Contracts\Auth\TwoFactor\TwoFactorAuthenticationProvider;
use Statamic\Events\TwoFactorAuthenticationChallenged;
use Statamic\Facades\User;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;
#[Group('2fa')]
class LoginTest extends TestCase
{
use PreventSavingStacheItemsToDisk;
#[Test]
public function it_shows_the_login_page()
{
$this
->get(cp_route('login'))
->assertOk()
->assertInertia(fn ($page) => $page->component('auth/Login'));
}
#[Test]
public function it_doesnt_show_the_login_page_when_authenticated()
{
$this
->actingAs($this->user())
->get(cp_route('login'))
->assertRedirect(cp_route('index'));
}
#[Test]
public function it_allows_logging_in()
{
$user = $this->user();
$this
->assertGuest()
->post(cp_route('login'), [
'email' => $user->email(),
'password' => 'secret',
'remember' => true,
])
->assertRedirect(cp_route('index'));
$this->assertAuthenticatedAs($user);
}
#[Test]
public function it_doesnt_allow_logging_in_with_invalid_credentials()
{
$user = $this->user();
$this
->assertGuest()
->post(cp_route('login'), [
'email' => $user->email(),
'password' => 'invalid-password',
'remember' => true,
])
->assertSessionHasErrors(['email']);
$this->assertGuest();
}
#[Test]
public function it_redirects_to_the_two_factor_challenge_page()
{
Event::fake();
$user = $this->userWithTwoFactorEnabled();
$this
->assertGuest()
->post(cp_route('login'), [
'email' => $user->email(),
'password' => 'secret',
'remember' => true,
])
->assertRedirect(cp_route('two-factor-challenge'))
->assertSessionHas('login.id', $user->id())
->assertSessionHas('login.remember', true);
$this->assertGuest();
Event::assertDispatched(TwoFactorAuthenticationChallenged::class, fn ($event) => $event->user->id === $user->id);
}
#[Test]
#[DefineEnvironment('disableTwoFactor')]
public function it_skips_two_factor_challenge_when_two_factor_is_disabled()
{
Event::fake();
$user = $this->userWithTwoFactorEnabled();
$this->withoutExceptionHandling();
$this
->assertGuest()
->post(cp_route('login'), [
'email' => $user->email(),
'password' => 'secret',
])
->assertRedirect(cp_route('index'));
$this->assertAuthenticatedAs($user);
Event::assertNotDispatched(TwoFactorAuthenticationChallenged::class);
}
#[Test]
public function it_redirects_to_intended_url()
{
$user = $this->user();
$this
->assertGuest()
->session(['url.intended' => 'http://localhost/cp/cp/collections'])
->post(cp_route('login'), [
'email' => $user->email(),
'password' => 'secret',
])
->assertRedirect('http://localhost/cp/cp/collections');
$this->assertAuthenticatedAs($user);
}
#[Test]
public function inertia_login_returns_a_full_page_redirect()
{
// Inertia would otherwise auto-follow a 302 with X-Inertia headers and swap to
// the dashboard component without the protected props it needs to render.
// Returning 409 + X-Inertia-Location forces a full browser navigation instead.
$user = $this->user();
$this
->assertGuest()
->post(cp_route('login'), [
'email' => $user->email(),
'password' => 'secret',
], ['X-Inertia' => 'true'])
->assertStatus(409)
->assertHeader('X-Inertia-Location', cp_route('index'));
$this->assertAuthenticatedAs($user);
}
#[Test]
public function inertia_login_redirects_to_intended_url_via_full_page_redirect()
{
$user = $this->user();
$this
->assertGuest()
->session(['url.intended' => 'http://localhost/cp/cp/collections'])
->post(cp_route('login'), [
'email' => $user->email(),
'password' => 'secret',
], ['X-Inertia' => 'true'])
->assertStatus(409)
->assertHeader('X-Inertia-Location', 'http://localhost/cp/cp/collections');
$this->assertAuthenticatedAs($user);
}
#[Test]
public function it_stores_the_intended_url_when_redirected_to_login()
{
$this
->get(cp_route('collections.index'))
->assertSessionHas('url.intended', 'http://localhost/cp/collections');
}
#[Test]
public function it_can_logout()
{
$this
->actingAs($this->user())
->get(cp_route('logout'))
->assertRedirect('/');
$this->assertGuest();
}
#[Test]
public function it_can_logout_with_redirect()
{
$this
->actingAs($this->user())
->get(cp_route('logout').'?redirect=/cp')
->assertRedirect('/cp');
$this->assertGuest();
}
#[Test]
public function it_does_not_redirect_to_external_url_on_logout()
{
$this
->actingAs($this->user())
->get(cp_route('logout').'?redirect=https://evil.com')
->assertRedirect('/');
$this->assertGuest();
}
#[Test]
public function it_cant_logout_when_unauthenticated()
{
$this
->get(cp_route('logout'))
->assertRedirect();
$this->assertGuest();
}
#[Test]
#[DefineEnvironment('cpOnTopLevel')]
#[DefineEnvironment('addOauthProvider')]
public function it_shows_oauth_providers_even_if_cp_is_on_top_level()
{
$this
->get(cp_route('login'))
->assertInertia(fn ($page) => $page
->component('auth/Login')
->has('providers', 1)
->where('oauthEnabled', true)
);
}
protected function cpOnTopLevel($app)
{
$app['config']->set('statamic.cp.route', '');
}
protected function addOauthProvider($app)
{
$app['config']->set('statamic.oauth.enabled', true);
$app['config']->set('statamic.oauth.providers', ['github']);
}
protected function disableTwoFactor($app)
{
$app['config']->set('statamic.users.two_factor_enabled', false);
}
private function user()
{
return tap(User::make()->makeSuper()->email('david@hasselhoff.com')->password('secret'))->save();
}
private function userWithTwoFactorEnabled()
{
$user = $this->user();
$user->merge([
'two_factor_confirmed_at' => now()->timestamp,
'two_factor_secret' => encrypt(app(TwoFactorAuthenticationProvider::class)->generateSecretKey()),
'two_factor_recovery_codes' => encrypt(json_encode(Collection::times(8, function () {
return RecoveryCode::generate();
})->all())),
]);
$user->save();
return $user;
}
}