|
1
|
|
|
<?php
|
|
2
|
|
|
/**
|
|
3
|
|
|
* @author Semih Serhat Karakaya <[email protected]>
|
|
4
|
|
|
*
|
|
5
|
|
|
* @copyright Copyright (c) 2017, ownCloud GmbH
|
|
6
|
|
|
* @license AGPL-3.0
|
|
7
|
|
|
*
|
|
8
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
10
|
|
|
* as published by the Free Software Foundation.
|
|
11
|
|
|
*
|
|
12
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15
|
|
|
* GNU Affero General Public License for more details.
|
|
16
|
|
|
*
|
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
19
|
|
|
*
|
|
20
|
|
|
*/
|
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
namespace OCA\Security\Tests;
|
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
use OCA\Security\Db\DbService;
|
|
27
|
|
|
use OCA\Security\Throttle;
|
|
28
|
|
|
use Test\TestCase;
|
|
29
|
|
|
|
|
30
|
|
|
class ThrottleTest extends TestCase {
|
|
31
|
|
|
|
|
32
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | Throttle */
|
|
33
|
|
|
private $throttle;
|
|
34
|
|
|
/**
|
|
35
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject | DbService
|
|
36
|
|
|
*/
|
|
37
|
|
|
private $dbServiceMock;
|
|
38
|
|
|
|
|
39
|
|
|
public function setUp() {
|
|
40
|
|
|
parent::setUp();
|
|
41
|
|
|
|
|
42
|
|
|
$this->dbServiceMock = $this->getMockBuilder('OCA\Security\Db\DbService')
|
|
43
|
|
|
->disableOriginalConstructor()
|
|
44
|
|
|
->getMock();
|
|
45
|
|
|
|
|
46
|
|
|
$this->throttle = $this->getMockBuilder('OCA\Security\Throttle')
|
|
47
|
|
|
->setConstructorArgs(
|
|
48
|
|
|
[
|
|
49
|
|
|
$this->dbServiceMock
|
|
50
|
|
|
]
|
|
51
|
|
|
)->setMethods()->getMock();
|
|
52
|
|
|
}
|
|
53
|
|
|
|
|
54
|
|
|
public function testAddFailedLoginAttempt() {
|
|
55
|
|
|
$this->dbServiceMock->expects($this->once())->method('addFailedLoginAttempt')
|
|
56
|
|
|
->with('test', '192.168.1.1');
|
|
57
|
|
|
|
|
58
|
|
|
$this->throttle->addFailedLoginAttempt('test', '192.168.1.1');
|
|
59
|
|
|
}
|
|
60
|
|
|
|
|
61
|
|
|
public function testPutDelay() {
|
|
62
|
|
|
|
|
63
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | Throttle $throttle */
|
|
64
|
|
|
$throttle = $this->getMockBuilder('OCA\Security\Throttle')
|
|
65
|
|
|
->setConstructorArgs(
|
|
66
|
|
|
[
|
|
67
|
|
|
$this->dbServiceMock
|
|
68
|
|
|
]
|
|
69
|
|
|
)->setMethods(['calculateDelayForIp'])->getMock();
|
|
70
|
|
|
|
|
71
|
|
|
$throttle->expects($this->once())->method('calculateDelayForIp')
|
|
72
|
|
|
->with('192.168.1.1');
|
|
73
|
|
|
|
|
74
|
|
|
$throttle->putDelay('test', '192.168.1.1');
|
|
75
|
|
|
}
|
|
76
|
|
|
|
|
77
|
|
View Code Duplication |
public function testCalculateDelayForUid() {
|
|
|
|
|
|
|
78
|
|
|
$this->dbServiceMock->expects($this->once())->method('getSuspiciousActivityCountForUid')
|
|
79
|
|
|
->with('test')
|
|
80
|
|
|
->willReturn(2);
|
|
81
|
|
|
|
|
82
|
|
|
$this->assertEquals(2, $this->throttle->calculateDelayForUid('test'));
|
|
83
|
|
|
}
|
|
84
|
|
|
|
|
85
|
|
View Code Duplication |
public function testCalculateDelayForIp() {
|
|
|
|
|
|
|
86
|
|
|
$this->dbServiceMock->expects($this->once())->method('getSuspiciousActivityCountForIp')
|
|
87
|
|
|
->with('192.168.1.1')
|
|
88
|
|
|
->willReturn(5);
|
|
89
|
|
|
|
|
90
|
|
|
$this->assertEquals(5, $this->throttle->calculateDelayForIp('192.168.1.1'));
|
|
91
|
|
|
}
|
|
92
|
|
|
|
|
93
|
|
|
public function testClearSuspiciousAttemptsForIp() {
|
|
94
|
|
|
$this->dbServiceMock->expects($this->once())->method('deleteSuspiciousAttemptsForIp')
|
|
95
|
|
|
->with('192.168.1.1');
|
|
96
|
|
|
|
|
97
|
|
|
$this->throttle->clearSuspiciousAttemptsForIp('192.168.1.1');
|
|
98
|
|
|
}
|
|
99
|
|
|
}
|
|
100
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.