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

SecurityConfigTest::testGetAllSecurityConfigs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Semih Serhat Karakaya <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
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
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
namespace OCA\Security\Tests;
22
23
use OCA\Security\SecurityConfig;
24
use OCP\IConfig;
25
use Test\TestCase;
26
class SecurityConfigTest extends TestCase {
27
	/** @var  IConfig|\PHPUnit_Framework_MockObject_MockObject */
28
	private $config;
29
	/** @var  SecurityConfig */
30
	private $securityConfig;
31
	public function setUp() {
32
		parent::setUp();
33
		$this->config = $this->createMock(IConfig::class);
34
		$this->securityConfig = new SecurityConfig($this->config);
35
	}
36
37
	/**
38
	 * @param array $mockedMethods
39
	 * @return SecurityConfig | \PHPUnit_Framework_MockObject_MockObject
40
	 */
41
	private function getMockInstance($mockedMethods = []) {
42
		$passwordValidator = $this->getMockBuilder('OCA\Security\SecurityConfig')
43
			->setConstructorArgs([$this->config])
44
			->setMethods($mockedMethods)->getMock();
45
		return $passwordValidator;
46
	}
47
48
	public function testGetAllSecurityConfigs() {
49
		$instance = $this->getMockInstance(
50
			[
51
				'getIsBruteForceProtectionEnabled',
52
				'getMinPasswordLength',
53
				'getIsUpperLowerCaseEnforced',
54
				'getIsNumericCharactersEnforced',
55
				'getIsSpecialCharactersEnforced'
56
			]
57
		);
58
		$instance->expects($this->once())->method('getIsBruteForceProtectionEnabled');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OCA\Security\SecurityConfig>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
		$instance->expects($this->once())->method('getMinPasswordLength');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OCA\Security\SecurityConfig>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
		$instance->expects($this->once())->method('getIsUpperLowerCaseEnforced');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OCA\Security\SecurityConfig>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
		$instance->expects($this->once())->method('getIsNumericCharactersEnforced');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OCA\Security\SecurityConfig>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
		$instance->expects($this->once())->method('getIsNumericCharactersEnforced');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OCA\Security\SecurityConfig>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
		$instance->getAllSecurityConfigs();
64
	}
65
	/**
66
	 * @dataProvider configTestData
67
	 * @param string $appConfigValue
68
	 * @param bool $expected
69
	 */
70 View Code Duplication
	public function testGetIsBruteForceProtectionEnabled($appConfigValue, $expected) {
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...
71
		$this->config->expects($this->once())->method('getAppValue')
72
			->with('security', 'enable_brute_force_protection', '0')
73
			->willReturn($appConfigValue);
74
		$this->assertSame($expected,
75
			$this->securityConfig->getIsBruteForceProtectionEnabled()
76
		);
77
	}
78
	/**
79
	 * @dataProvider minPassTestData
80
	 * @param string $appConfigValue
81
	 * @param bool $expected
82
	 */
83 View Code Duplication
	public function testGetMinPasswordLength($appConfigValue, $expected) {
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...
84
		$this->config->expects($this->once())->method('getAppValue')
85
			->with('security', 'min_password_length', '8')
86
			->willReturn($appConfigValue);
87
		$this->assertSame($expected,
88
			$this->securityConfig->getMinPasswordLength()
89
		);
90
	}
91
	/**
92
	 * @dataProvider configTestData
93
	 * @param string $appConfigValue
94
	 * @param bool $expected
95
	 */
96 View Code Duplication
	public function testGetIsUpperLowerCaseEnforced($appConfigValue, $expected) {
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...
97
		$this->config->expects($this->once())->method('getAppValue')
98
			->with('security', 'enforce_upper_lower_case', '0')
99
			->willReturn($appConfigValue);
100
		$this->assertSame($expected,
101
			$this->securityConfig->getIsUpperLowerCaseEnforced()
102
		);
103
	}
104
	/**
105
	 * @dataProvider configTestData
106
	 * @param string $appConfigValue
107
	 * @param bool $expected
108
	 */
109 View Code Duplication
	public function testGetIsNumericCharactersEnforced($appConfigValue, $expected) {
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...
110
		$this->config->expects($this->once())->method('getAppValue')
111
			->with('security', 'enforce_numeric_characters', '0')
112
			->willReturn($appConfigValue);
113
		$this->assertSame($expected,
114
			$this->securityConfig->getIsNumericCharactersEnforced()
115
		);
116
	}
117
	/**
118
	 * @dataProvider configTestData
119
	 * @param string $appConfigValue
120
	 * @param bool $expected
121
	 */
122 View Code Duplication
	public function testGetIsSpecialCharactersEnforced($appConfigValue, $expected) {
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...
123
		$this->config->expects($this->once())->method('getAppValue')
124
			->with('security', 'enforce_special_characters', '0')
125
			->willReturn($appConfigValue);
126
		$this->assertSame($expected,
127
			$this->securityConfig->getIsSpecialCharactersEnforced()
128
		);
129
	}
130
	/**
131
	 * @dataProvider configTestData
132
	 * @param string $expected
133
	 * @param bool $setValue
134
	 */
135
	public function testSetIsBruteForceProtectionEnabled($expected, $setValue) {
136
		$this->config->expects($this->once())->method('setAppValue')
137
			->with('security', 'enable_brute_force_protection', $expected);
138
		$this->securityConfig->setIsBruteForceProtectionEnabled($setValue);
139
	}
140
	/**
141
	 * @dataProvider minPassTestData
142
	 * @param string $expected
143
	 * @param bool $setValue
144
	 */
145
	public function testSetMinPasswordLength($expected, $setValue) {
146
		$this->config->expects($this->once())->method('setAppValue')
147
			->with('security', 'min_password_length', $expected);
148
		$this->securityConfig->setMinPasswordLength($setValue);
0 ignored issues
show
Documentation introduced by
$setValue is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
149
	}
150
	/**
151
	 * @dataProvider configTestData
152
	 * @param string $expected
153
	 * @param bool $setValue
154
	 */
155
	public function testSetIsUpperLowerCaseEnforced($expected, $setValue) {
156
		$this->config->expects($this->once())->method('setAppValue')
157
			->with('security', 'enforce_upper_lower_case', $expected);
158
		$this->securityConfig->setIsUpperLowerCaseEnforced($setValue);
159
	}
160
	/**
161
	 * @dataProvider configTestData
162
	 * @param string $expected
163
	 * @param bool $setValue
164
	 */
165
	public function testSetIsNumericCharactersEnforced($expected, $setValue) {
166
		$this->config->expects($this->once())->method('setAppValue')
167
			->with('security', 'enforce_numeric_characters', $expected);
168
		$this->securityConfig->setIsNumericCharactersEnforced($setValue);
169
	}
170
	/**
171
	 * @dataProvider configTestData
172
	 * @param string $expected
173
	 * @param bool $setValue
174
	 */
175
	public function testSetIsSpecialCharactersEnforced($expected, $setValue) {
176
		$this->config->expects($this->once())->method('setAppValue')
177
			->with('security', 'enforce_special_characters', $expected);
178
		$this->securityConfig->setIsSpecialCharactersEnforced($setValue);
179
	}
180
	public function configTestData() {
181
		return [
182
			['1', true],
183
			['0', false],
184
		];
185
	}
186
	public function minPassTestData() {
187
		return [
188
			['8', 8],
189
			['16', 16],
190
			['255', 255]
191
		];
192
	}
193
}