forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLockoutHandlerTest.php
More file actions
138 lines (125 loc) · 4.57 KB
/
Copy pathLockoutHandlerTest.php
File metadata and controls
138 lines (125 loc) · 4.57 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
<?php
declare(strict_types=1);
namespace CakeDC\Users\Test\TestCase\Identifier\PasswordLockout;
use Cake\I18n\DateTime;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use CakeDC\Users\Identifier\PasswordLockout\LockoutHandler;
class LockoutHandlerTest extends TestCase
{
/**
* @inheritDoc
*/
protected array $fixtures = [
'plugin.CakeDC/Users.Users',
'plugin.CakeDC/Users.FailedPasswordAttempts',
];
/**
* @return void
*/
public function testNewFail()
{
$AttemptsTable = TableRegistry::getTableLocator()->get('CakeDC/Users.FailedPasswordAttempts');
$id = '00000000-0000-0000-0000-000000000002';
$handler = new LockoutHandler();
$currentCount = 5;
$attemptsBefore = $AttemptsTable->find()
->where(['user_id' => $id])
->orderByAsc('created')
->all()
->toArray();
//First time will remove old records and still add a new one
$handler->newFail($id);
$this->assertSame($currentCount, count($attemptsBefore));
$this->assertFalse($AttemptsTable->exists(['id' => $attemptsBefore[0]->id]));
//Now only add a new one because there is nothing to remove
$handler = new LockoutHandler();
$handler->newFail($id);
$attemptsAfterSecond = $AttemptsTable->find()->where(['user_id' => $id])->count();
$this->assertSame($currentCount + 1, $attemptsAfterSecond);
}
/**
* @return void
*/
public function testIsUnlockedYes()
{
$handler = new LockoutHandler();
$UsersTable = TableRegistry::getTableLocator()->get('Users');
$actual = $handler->isUnlocked($UsersTable->get('00000000-0000-0000-0000-000000000002'));
$this->assertTrue($actual);
}
/**
* @return void
*/
public function testIsUnlockedNotSavedLockoutAndLastFailureMax()
{
$userId = '00000000-0000-0000-0000-000000000004';
$UsersTable = TableRegistry::getTableLocator()->get('Users');
$userBefore = $UsersTable->get($userId);
$this->assertNull($userBefore->lockout_time);
$handler = new LockoutHandler();
$actual = $handler->isUnlocked($UsersTable->get($userId));
$this->assertFalse($actual);
$userAfter = $UsersTable->get($userId);
$this->assertInstanceOf(DateTime::class, $userAfter->lockout_time);
}
/**
* @return void
*/
public function testIsUnlockedSaveLockoutAndCompleted()
{
$handler = new LockoutHandler([
'numberOfAttemptsFail' => 7,
]);
$expiredTimeout = $handler->getConfig('lockoutTimeInSeconds') + 10;
$this->assertGreaterThan(10, $expiredTimeout);
$UsersTable = TableRegistry::getTableLocator()->get('Users');
$userId = '00000000-0000-0000-0000-000000000004';
$UsersTable->updateAll(['lockout_time' => new DateTime('-' . $expiredTimeout . 'minutes')], ['id' => $userId]);
$userBefore = $UsersTable->get($userId);
$this->assertInstanceOf(DateTime::class, $userBefore->lockout_time);
$actual = $handler->isUnlocked($UsersTable->get($userId));
$this->assertTrue($actual);
}
/**
* @return void
*/
public function testIsUnlockedSaveLockoutAndNotCompleted()
{
$handler = new LockoutHandler([
'numberOfAttemptsFail' => 7,
]);
$userId = '00000000-0000-0000-0000-000000000004';
$UsersTable = TableRegistry::getTableLocator()->get('Users');
$UsersTable->updateAll(['lockout_time' => new DateTime('-4 minutes')], ['id' => $userId]);
$userBefore = $UsersTable->get($userId);
$this->assertInstanceOf(DateTime::class, $userBefore->lockout_time);
$actual = $handler->isUnlocked($UsersTable->get($userId));
$this->assertFalse($actual);
$userAfter = $UsersTable->get($userId);
$this->assertInstanceOf(DateTime::class, $userAfter->lockout_time);
$this->assertEquals($userBefore->lockout_time, $userAfter->lockout_time);
}
/**
* @return void
*/
public function testIsUnlockedWithoutIdButNotEmpty()
{
$handler = new LockoutHandler();
$user = [
'username' => 'user-2',
'email' => 'user-2@test.com',
];
$actual = $handler->isUnlocked($user);
$this->assertFalse($actual);
}
/**
* @return void
*/
public function testIsUnlockedWithoutIdAndEmpty()
{
$handler = new LockoutHandler();
$actual = $handler->isUnlocked([]);
$this->assertFalse($actual);
}
}