-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathUserIdentityModelTest.php
111 lines (90 loc) · 3.2 KB
/
UserIdentityModelTest.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
<?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\Unit;
use CodeIgniter\Shield\Authentication\Authenticators\Session;
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Models\DatabaseException;
use CodeIgniter\Shield\Models\UserIdentityModel;
use CodeIgniter\Shield\Models\UserModel;
use CodeIgniter\Test\DatabaseTestTrait;
use CodeIgniter\Test\Fabricator;
use Tests\Support\TestCase;
/**
* @internal
*/
final class UserIdentityModelTest extends TestCase
{
use DatabaseTestTrait;
protected $namespace;
protected $refresh = true;
private function createUserIdentityModel(): UserIdentityModel
{
return new UserIdentityModel();
}
public function testCreateDuplicateRecordThrowsException(): void
{
$this->expectException(DatabaseException::class);
$model = $this->createUserIdentityModel();
// "type and secret" are unique.
$model->create([
'user_id' => 1,
'type' => Session::ID_TYPE_EMAIL_ACTIVATE,
'secret' => '123456',
'name' => 'register',
'extra' => lang('Auth.needVerification'),
]);
$model->create([
'user_id' => 1,
'type' => Session::ID_TYPE_EMAIL_ACTIVATE,
'secret' => '123456',
'name' => 'register',
'extra' => lang('Auth.needVerification'),
]);
}
public function testCreateCodeIdentityThrowsExceptionIfUniqueCodeIsNotGot(): void
{
$this->expectException(DatabaseException::class);
/** @var User $user */
$user = fake(UserModel::class, ['id' => '1']);
fake(UserIdentityModel::class, ['user_id' => 2, 'type' => Session::ID_TYPE_EMAIL_ACTIVATE, 'secret' => '666666']);
$model = $this->createUserIdentityModel();
$generator = static fn (): string => '666666';
$model->createCodeIdentity(
$user,
[
'type' => Session::ID_TYPE_EMAIL_ACTIVATE,
'name' => 'register',
'extra' => lang('Auth.needVerification'),
],
$generator
);
}
public function testForceMultiplePasswordReset(): void
{
/** @var Fabricator $fabricator */
$fabricator = new Fabricator(UserIdentityModel::class);
$fabricator->create(10);
/** @var UserIdentityModel $identities */
$identities = model(UserIdentityModel::class);
$result = $identities->select('user_id')->findAll();
$userIds = [];
foreach ($result as $row) {
$userIds[] = $row->user_id;
}
$identities->forceMultiplePasswordReset($userIds);
/** @var UserModel $users */
$users = model(UserModel::class);
$first_user = $users->findById($userIds[0]);
$last_user = $users->findById($userIds[9]);
$this->assertTrue($first_user->requiresPasswordReset());
$this->assertTrue($last_user->requiresPasswordReset());
}
}