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
|
<?php
namespace Psecio\Gatekeeper;
class ThrottleModelTest extends \Psecio\Gatekeeper\Base
{
/**
* Test the update of the login attempt properties on a throttle record
*/
public function testUpdateLoginAttempts()
{
$ds = $this->buildMock(true, 'save');
$throttle = new ThrottleModel($ds, array('attempts' => 1));
$throttle->updateAttempts();
$this->assertEquals(2, $throttle->attempts);
$this->assertNotNull($throttle->lastAttempt);
}
/**
* Test the changes made when a user is set back to allowed
*/
public function testSetUserToAllow()
{
$ds = $this->buildMock(true, 'save');
$throttle = new ThrottleModel(
$ds, array('attempts' => 1, 'status' => ThrottleModel::STATUS_BLOCKED)
);
$throttle->allow();
$this->assertEquals($throttle->status, ThrottleModel::STATUS_ALLOWED);
$this->assertNotNull($throttle->statusChange);
}
/**
* Test that a user is allowed after the default timeout (1 minute) has passed
*/
public function testCheckDefaultTimeoutAllowUser()
{
$ds = $this->buildMock(true, 'save');
$throttle = new ThrottleModel(
$ds,
array(
'status' => ThrottleModel::STATUS_BLOCKED,
'statusChange' => date('Y/m/d H:i:s', strtotime('-5 minutes'))
)
);
$throttle->checkTimeout();
$this->assertEquals($throttle->status, ThrottleModel::STATUS_ALLOWED);
}
/**
* Test that, when the status change time hasn't reached the timeout, no
* status change is made.
*/
public function testCheckDefaultTimeoutNoChange()
{
$ds = $this->buildMock(true, 'save');
$throttle = new ThrottleModel(
$ds,
array(
'status' => ThrottleModel::STATUS_BLOCKED,
'statusChange' => date('Y/m/d H:i:s', strtotime('-10 seconds'))
)
);
$throttle->checkTimeout();
$this->assertEquals($throttle->status, ThrottleModel::STATUS_BLOCKED);
}
/**
* Test that a user is allowed after the given timeout (-10 minutes) has passed
*/
public function testCheckInputTimeoutAllowUser()
{
$ds = $this->buildMock(true, 'save');
$throttle = new ThrottleModel(
$ds,
array(
'status' => ThrottleModel::STATUS_BLOCKED,
'statusChange' => date('Y/m/d H:i:s', strtotime('-12 minutes'))
)
);
$throttle->checkTimeout('-10 minutes');
$this->assertEquals($throttle->status, ThrottleModel::STATUS_ALLOWED);
}
/**
* Check that when the user has reached or gone over the number of attempts
* (default is 5) they're set to blocked
*/
public function testCheckAttemptsBlockUser()
{
$ds = $this->buildMock(true, 'save');
$throttle = new ThrottleModel(
$ds, array('attempts' => 6)
);
$throttle->checkAttempts();
$this->assertEquals($throttle->status, ThrottleModel::STATUS_BLOCKED);
}
/**
* Check that when the user hasn't reached or gone over the number of attempts
* (default is 5) they're not blocked
*/
public function testCheckAttemptsNotBlockUser()
{
$ds = $this->buildMock(true, 'save');
$throttle = new ThrottleModel(
$ds, array('attempts' => 2, 'status' => ThrottleModel::STATUS_ALLOWED)
);
$throttle->checkAttempts();
$this->assertEquals($throttle->status, ThrottleModel::STATUS_ALLOWED);
}
/**
* Test that the find by user ID works correctly and populates the model
*/
public function testFindByUserId()
{
$userId = 10;
$data = array(
array('userId' => $userId, 'attempts' => 1, 'status' => ThrottleModel::STATUS_ALLOWED)
);
$ds = $this->getMockBuilder('\Psecio\Gatekeeper\DataSource\Mysql')
->disableOriginalConstructor()
->setMethods(array('fetch'))
->getMock();
$ds->method('fetch')
->willReturn($data);
$throttle = new ThrottleModel($ds);
$throttle->findByUserId($userId);
$this->assertEquals($throttle->attempts, 1);
$this->assertEquals($throttle->userId, 10);
$this->assertEquals($throttle->status, ThrottleModel::STATUS_ALLOWED);
}
}
|