Completed
Push — master ( 7a29a1...33d37f )
by Thomas
01:49
created

ThrottleTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 14
loc 70
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 14 1
A testAddFailedLoginAttempt() 0 6 1
A testPutDelay() 0 15 1
A testCalculateDelayForUid() 7 7 1
A testCalculateDelayForIp() 7 7 1
A testClearSuspiciousAttemptsForIp() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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