-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathActionsTest.php
310 lines (250 loc) · 9.77 KB
/
ActionsTest.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Shield.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Tests\Controllers;
use CodeIgniter\Config\Factories;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\Shield\Authentication\Actions\Email2FA;
use CodeIgniter\Shield\Authentication\Actions\EmailActivator;
use CodeIgniter\Shield\Authentication\Authenticators\Session;
use CodeIgniter\Shield\Models\UserIdentityModel;
use CodeIgniter\Shield\Test\AuthenticationTesting;
use CodeIgniter\Test\DatabaseTestTrait;
use CodeIgniter\Test\FeatureTestTrait;
use Config\Services;
use Tests\Support\DatabaseTestCase;
use Tests\Support\FakeUser;
/**
* @internal
*/
final class ActionsTest extends DatabaseTestCase
{
use DatabaseTestTrait;
use FeatureTestTrait;
use AuthenticationTesting;
use FakeUser;
protected $namespace;
protected function setUp(): void
{
parent::setUp();
// Ensure our actions are registered with the system
$config = config('Auth');
$config->forceMfa = true;
$config->actionsMfa['email'] = Email2FA::class;
$config->actions['register'] = EmailActivator::class;
Factories::injectMock('config', 'Auth', $config);
// Add auth routes
$routes = service('routes');
auth()->routes($routes);
Services::injectMock('routes', $routes);
$_SESSION = [];
$this->user->createEmailIdentity(['email' => '[email protected]', 'password' => 'secret123']);
}
public function testActionShowNoneAvailable(): void
{
$this->expectException(PageNotFoundException::class);
$result = $this->withSession([])->get('/auth/a/show');
// Nothing found, it should die gracefully.
$result->assertStatus(404);
}
public function testEmail2FAShow(): void
{
$this->insertIdentityEmal2FA();
$result = $this->actingAs($this->user, true)
->withSession($this->getSessionUserInfo())
->get('/auth/a/show');
$result->assertStatus(200);
// Should auto populate in the form
$result->assertSee($this->user->email);
}
private function getSessionUserInfo(string $class = Email2FA::class): array
{
return [
'user' => [
'id' => $this->user->id,
'auth_action' => $class,
],
];
}
public function testEmail2FAHandleInvalidEmail(): void
{
$this->insertIdentityEmal2FA();
$result = $this->actingAs($this->user, true)
->withSession($this->getSessionUserInfo())
->post('/auth/a/handle', [
'email' => '[email protected]',
]);
$result->assertRedirect();
$this->assertSame(site_url('/auth/a/show'), $result->getRedirectUrl());
$result->assertSessionHas('error', lang('Auth.invalidEmail', ['[email protected]']));
}
private function insertIdentityEmal2FA(): void
{
// An identity with 2FA info would have been stored previously
$identities = model(UserIdentityModel::class);
$identities->insert([
'user_id' => $this->user->id,
'type' => Session::ID_TYPE_EMAIL_2FA,
'secret' => '123456',
'name' => 'login',
'extra' => lang('Auth.need2FA'),
]);
}
public function testEmail2FAHandleSendsEmail(): void
{
$this->insertIdentityEmal2FA();
$result = $this->actingAs($this->user, true)
->withSession($this->getSessionUserInfo())
->post('/auth/a/handle', [
'email' => $this->user->email,
]);
$result->assertStatus(200);
$result->assertSee(lang('Auth.emailEnterCode'));
// Should have sent an email with the code....
$this->assertStringContainsString('Your authentication code is:', service('email')->archive['body']);
// Should have included the username in the email
$this->assertStringContainsString($this->user->username, service('email')->archive['body']);
}
public function testEmail2FAVerifyFails(): void
{
$this->insertIdentityEmal2FA();
$result = $this->actingAs($this->user, true)
->withSession($this->getSessionUserInfo())
->post('/auth/a/verify', [
'token' => '234567',
]);
$result->assertStatus(200);
$result->assertSee(lang('Auth.invalid2FAToken'));
}
public function testEmail2FAVerify(): void
{
$this->insertIdentityEmal2FA();
$result = $this->actingAs($this->user, true)
->withSession($this->getSessionUserInfo())
->post('/auth/a/verify', [
'token' => '123456',
]);
$result->assertRedirect();
$this->assertSame(site_url(), $result->getRedirectUrl());
// Identity should have been removed
$this->dontSeeInDatabase($this->tables['identities'], [
'user_id' => $this->user->id,
'type' => Session::ID_TYPE_EMAIL_2FA,
]);
// Session should have been cleared
$result->assertSessionMissing('auth_action');
}
public function testShowEmail2FACreatesIdentity(): void
{
$this->insertIdentityEmal2FA();
$result = $this->actingAs($this->user, true)
->withSession($this->getSessionUserInfo())
->get('/auth/a/show');
$result->assertOK();
$this->seeInDatabase($this->tables['identities'], [
'user_id' => $this->user->id,
'type' => Session::ID_TYPE_EMAIL_2FA,
'name' => 'login',
]);
}
public function testEmail2FACannotBeBypassed(): void
{
// Ensure filter is enabled for all routes
$config = config('Filters');
$config->globals['before'][] = 'session';
Factories::injectMock('config', 'Filters', $config);
// An identity with 2FA info would have been stored previously
$identities = model(UserIdentityModel::class);
$identities->insert([
'user_id' => $this->user->id,
'type' => Session::ID_TYPE_EMAIL_2FA,
'secret' => '123456',
'name' => 'login',
'extra' => lang('Auth.need2FA'),
]);
// Try to visit any other page, skipping the 2FA
$result = $this->actingAs($this->user, true)
->withSession($this->getSessionUserInfo())
->get('/');
$result->assertRedirect();
$this->assertSame(site_url('/auth/a/show'), $result->getRedirectUrl());
}
private function insertIdentityEmailActivate(): void
{
// An identity with Email activation info would have been stored previously
$identities = model(UserIdentityModel::class);
$identities->insert([
'user_id' => $this->user->id,
'type' => Session::ID_TYPE_EMAIL_ACTIVATE,
'secret' => '123456',
'name' => 'register',
'extra' => lang('Auth.needVerification'),
]);
}
public function testEmailActivateShow(): void
{
$this->insertIdentityEmailActivate();
$result = $this->actingAs($this->user, true)
->withSession($this->getSessionUserInfo(EmailActivator::class))
->get('/auth/a/show');
$result->assertStatus(200);
// Should have sent an email with the link....
$this->assertStringContainsString(
'Please use the code below to activate your account and start using the site',
service('email')->archive['body'],
);
$this->assertMatchesRegularExpression(
'!<h1>[0-9]{6}</h1>!',
service('email')->archive['body'],
);
// Should have included the username in the email
$this->assertStringContainsString($this->user->username, service('email')->archive['body']);
}
public function testEmailActivateVerify(): void
{
$this->insertIdentityEmailActivate();
$this->user->active = false;
$model = auth()->getProvider();
$model->save($this->user);
$result = $this->actingAs($this->user, true)
->withSession($this->getSessionUserInfo(EmailActivator::class))
->post('/auth/a/verify', [
'token' => '123456',
]);
$result->assertRedirect();
$this->assertSame(site_url(), $result->getRedirectUrl());
// Identity should have been removed
$this->dontSeeInDatabase($this->tables['identities'], [
'user_id' => $this->user->id,
'type' => Session::ID_TYPE_EMAIL_2FA,
]);
// Session should have been cleared
$result->assertSessionMissing('auth_action');
// User should have been set as active
$this->seeInDatabase($this->tables['users'], [
'id' => $this->user->id,
'active' => 1,
]);
}
public function testEmailActivateCannotBeBypassed(): void
{
// Ensure filter is enabled for all routes
$config = config('Filters');
$config->globals['before'][] = 'session';
Factories::injectMock('config', 'Filters', $config);
$this->insertIdentityEmailActivate();
// Try to visit any other page, skipping the 2FA
$result = $this->actingAs($this->user, true)
->withSession($this->getSessionUserInfo(EmailActivator::class))
->get('/');
$result->assertRedirect();
$this->assertSame(site_url('/auth/a/show'), $result->getRedirectUrl());
}
}