HooksTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 85
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 27 1
A testRegister() 0 7 1
A testFailedLoginCallback() 0 7 1
A testPostLoginCallback() 0 7 1
A testPreLoginCallback() 0 7 1
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 OC\User\Manager;
27
use OCA\Security\Hooks;
28
use OCA\Security\PasswordValidator;
29
use OCA\Security\Throttle;
30
use OCP\IRequest;
31
use Symfony\Component\EventDispatcher\EventDispatcher;
32
use Test\TestCase;
33
34
class HooksTest extends TestCase {
35
36
    /** @var  Hooks */
37
    private $hooks;
38
    /**
39
     * @var \PHPUnit_Framework_MockObject_MockObject | Manager
40
     */
41
    private $userManagerMock;
42
    /**
43
     * @var \PHPUnit_Framework_MockObject_MockObject | Throttle
44
     */
45
    private $throttleMock;
46
    /**
47
     * @var \PHPUnit_Framework_MockObject_MockObject | IRequest
48
     */
49
    private $requestMock;
50
    /**
51
     * @var \PHPUnit_Framework_MockObject_MockObject | PasswordValidator
52
     */
53
    private $passwordValidatorMock;
54
    /**
55
     * @var \PHPUnit_Framework_MockObject_MockObject | EventDispatcher
56
     */
57
    private $dispatcherMock;
58
59
    public function setUp() {
60
        parent::setUp();
61
62
        $this->userManagerMock = $this->getMockBuilder('\OC\User\Manager')
63
            ->disableOriginalConstructor()
64
            ->getMock();
65
66
        $this->throttleMock = $this->getMockBuilder('OCA\Security\Throttle')
67
            ->disableOriginalConstructor()
68
            ->getMock();
69
        $this->requestMock = $this->getMockBuilder('OCP\IRequest')
70
            ->disableOriginalConstructor()
71
            ->getMock();
72
        $this->passwordValidatorMock = $this->getMockBuilder('OCA\Security\PasswordValidator')
73
            ->disableOriginalConstructor()
74
            ->getMock();
75
        $this->dispatcherMock = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
76
            ->disableOriginalConstructor()
77
            ->getMock();
78
79
        $this->hooks = new Hooks(
80
            $this->userManagerMock,
81
            $this->throttleMock,
82
            $this->requestMock,
83
            $this->passwordValidatorMock,
84
            $this->dispatcherMock);
85
    }
86
87
    public function testRegister() {
88
        $this->userManagerMock->expects($this->exactly(3))
89
            ->method('listen');
90
        $this->dispatcherMock->expects($this->once())
91
            ->method('addListener');
92
        $this->hooks->register();
93
    }
94
95
    public function testFailedLoginCallback() {
96
        $this->throttleMock->expects($this->once())
97
            ->method('addFailedLoginAttempt');
98
99
        $this->hooks->failedLoginCallback("test");
100
        $this->assertTrue(true);
101
    }
102
103
    public function testPostLoginCallback() {
104
        $this->throttleMock->expects($this->once())
105
            ->method('clearSuspiciousAttemptsForUidIpCombination');
106
107
        $this->hooks->postLoginCallback("test");
108
        $this->assertTrue(true);
109
    }
110
111
    public function testPreLoginCallback() {
112
        $this->throttleMock->expects($this->once())
113
            ->method('applyBruteForcePolicy');
114
115
        $this->hooks->preLoginCallback('test');
116
        $this->assertTrue(true);
117
    }
118
}
119