-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserTwoFactorTest.php
More file actions
173 lines (145 loc) · 5.87 KB
/
UserTwoFactorTest.php
File metadata and controls
173 lines (145 loc) · 5.87 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
<?php namespace Tests\unit;
/**
* Copyright 2026 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use App\libs\Auth\Models\IGroupSlugs;
use Auth\Group;
use Auth\User;
use Illuminate\Support\Facades\Config;
use models\exceptions\ValidationException;
use Tests\TestCase;
/**
* Class UserTwoFactorTest
* Unit tests for the 2FA-related methods on the User entity.
*/
class UserTwoFactorTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
Config::set('two_factor.enforced_groups', [
IGroupSlugs::SuperAdminGroup,
IGroupSlugs::AdminGroup,
IGroupSlugs::OAuth2ServerAdminGroup,
IGroupSlugs::OpenIdServerAdminsGroup,
]);
}
private function buildGroup(string $slug): Group
{
$group = new Group();
$group->setName($slug);
$group->setSlug($slug);
return $group;
}
private function assignGroups(User $user, array $groups): void
{
$reflection = new \ReflectionClass(User::class);
$property = $reflection->getProperty('groups');
$property->setAccessible(true);
$collection = $property->getValue($user);
foreach ($groups as $group) {
$collection->add($group);
}
}
private function setEmailVerified(User $user, bool $verified): void
{
$reflection = new \ReflectionClass(User::class);
$property = $reflection->getProperty('email_verified');
$property->setAccessible(true);
$property->setValue($user, $verified);
}
public function testShouldRequire2FA_superAdminUser(): void
{
$user = new User();
$this->assignGroups($user, [$this->buildGroup(IGroupSlugs::SuperAdminGroup)]);
$this->assertFalse($user->isTwoFactorEnabled());
$this->assertTrue($user->shouldRequire2FA());
}
public function testShouldRequire2FA_adminUser(): void
{
$user = new User();
$this->assignGroups($user, [$this->buildGroup(IGroupSlugs::AdminGroup)]);
$this->assertFalse($user->isTwoFactorEnabled());
$this->assertTrue($user->shouldRequire2FA());
}
public function testShouldRequire2FA_oauth2AdminUser(): void
{
// Guard against the gotcha: shouldRequire2FA() must look at the full
// config('two_factor.enforced_groups') list, not just call isAdmin().
$user = new User();
$this->assignGroups($user, [$this->buildGroup(IGroupSlugs::OAuth2ServerAdminGroup)]);
$this->assertFalse($user->isAdmin(), 'oauth2-server-admins is NOT an admin group per isAdmin()');
$this->assertTrue($user->shouldRequire2FA());
}
public function testShouldRequire2FA_regularUser_enabled(): void
{
$user = new User();
$this->assignGroups($user, [$this->buildGroup(IGroupSlugs::RawUsersGroup)]);
$user->setTwoFactorEnabled(true);
$this->assertTrue($user->shouldRequire2FA());
}
public function testShouldRequire2FA_regularUser_disabled(): void
{
$user = new User();
$this->assignGroups($user, [$this->buildGroup(IGroupSlugs::RawUsersGroup)]);
$this->assertFalse($user->isTwoFactorEnabled());
$this->assertFalse($user->shouldRequire2FA());
}
public function testEnable2FA_validMethod(): void
{
$user = new User();
$before = new \DateTime('now', new \DateTimeZone('UTC'));
$user->enable2FA('email_otp');
$after = new \DateTime('now', new \DateTimeZone('UTC'));
$this->assertTrue($user->isTwoFactorEnabled());
$this->assertSame('email_otp', $user->getTwoFactorMethod());
$enforcedAt = $user->getTwoFactorEnforcedAt();
$this->assertInstanceOf(\DateTime::class, $enforcedAt);
$this->assertGreaterThanOrEqual($before->getTimestamp(), $enforcedAt->getTimestamp());
$this->assertLessThanOrEqual($after->getTimestamp(), $enforcedAt->getTimestamp());
}
public function testEnable2FA_invalidMethod_throws(): void
{
$user = new User();
$this->expectException(ValidationException::class);
$user->enable2FA('invalid_method');
}
public function testEnable2FA_phaseTwoMethod_throwsInPhaseOne(): void
{
// sms_otp/totp/passkey are Phase II/III — ValidMFAMethods should reject them in Phase I.
$user = new User();
$this->expectException(ValidationException::class);
$user->enable2FA('sms_otp');
}
public function testGetAvailableTwoFactorMethods_emailVerified(): void
{
$user = new User();
$this->setEmailVerified($user, true);
$this->assertSame(['email_otp'], $user->getAvailableTwoFactorMethods());
}
public function testGetAvailableTwoFactorMethods_emailNotVerified(): void
{
$user = new User();
$this->setEmailVerified($user, false);
$this->assertSame([], $user->getAvailableTwoFactorMethods());
}
public function testIsTwoFactorMethodEnable(): void
{
$user = new User();
$this->setEmailVerified($user, true);
$this->assertTrue($user->isTwoFactorMethodEnable('email_otp'));
$this->assertFalse($user->isTwoFactorMethodEnable('sms_otp'));
$this->assertFalse($user->isTwoFactorMethodEnable('totp'));
$this->assertFalse($user->isTwoFactorMethodEnable('passkey'));
$this->assertFalse($user->isTwoFactorMethodEnable('garbage'));
}
}