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

HooksTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 28 1
A testRegister() 0 7 1
A testFailedLoginCallback() 0 9 1
A testPostLoginCallback() 0 12 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
80
        $this->hooks = new Hooks(
81
        	$this->userManagerMock,
82
			$this->throttleMock,
83
			$this->requestMock,
84
			$this->passwordValidatorMock,
85
			$this->dispatcherMock);
86
    }
87
88
    public function testRegister() {
89
    	$this->userManagerMock->expects($this->exactly(2))
90
			->method('listen');
91
    	$this->dispatcherMock->expects($this->once())
92
			->method('addListener');
93
    	$this->hooks->register();
94
	}
95
96
    public function testFailedLoginCallback() {
97
        $this->throttleMock->expects($this->once())
98
            ->method('addFailedLoginAttempt');
99
        $this->throttleMock->expects($this->once())
100
            ->method('putDelay');
101
102
        $this->hooks->failedLoginCallback("test");
103
        $this->assertTrue(true);
104
    }
105
106
    public function testPostLoginCallback() {
107
        $this->throttleMock->expects($this->once())
108
            ->method('clearSuspiciousAttemptsForIp');
109
110
		/** @var \OCP\IUser $user*/
111
        $user = $this->getMockBuilder('OCP\IUser')
112
            ->disableOriginalConstructor()
113
            ->getMock();
114
115
        $this->hooks->postLoginCallback($user);
116
        $this->assertTrue(true);
117
    }
118
}
119