Completed
Push — stable8.2 ( b54faa...3a8391 )
by
unknown
37:04
created

UserHooksTest::testSetPassphrase()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 77
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 56
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 77
ccs 56
cts 56
cp 1
rs 8.9342
cc 1
eloc 54
nc 1
nop 0
crap 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Björn Schießle <[email protected]>
4
 * @author Clark Tomlinson <[email protected]>
5
 *
6
 * @copyright Copyright (c) 2015, ownCloud, Inc.
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
24
25
namespace OCA\Encryption\Tests\Hooks;
26
27
28
use OCA\Encryption\Crypto\Crypt;
29
use OCA\Encryption\Hooks\UserHooks;
30
use Test\TestCase;
31
32
/**
33
 * Class UserHooksTest
34
 *
35
 * @group DB
36
 * @package OCA\Encryption\Tests\Hooks
37
 */
38
class UserHooksTest extends TestCase {
39
	/**
40
	 * @var \PHPUnit_Framework_MockObject_MockObject
41
	 */
42
	private $utilMock;
43
	/**
44
	 * @var \PHPUnit_Framework_MockObject_MockObject
45
	 */
46
	private $recoveryMock;
47
	/**
48
	 * @var \PHPUnit_Framework_MockObject_MockObject
49
	 */
50
	private $sessionMock;
51
	/**
52
	 * @var \PHPUnit_Framework_MockObject_MockObject
53
	 */
54
	private $keyManagerMock;
55
	/**
56
	 * @var \PHPUnit_Framework_MockObject_MockObject
57
	 */
58
	private $userManagerMock;
59
60
	/**
61
	 * @var \PHPUnit_Framework_MockObject_MockObject
62
	 */
63
	private $userSetupMock;
64
	/**
65
	 * @var \PHPUnit_Framework_MockObject_MockObject
66
	 */
67
	private $userSessionMock;
68
	/**
69
	 * @var \PHPUnit_Framework_MockObject_MockObject
70
	 */
71
	private $cryptMock;
72
	/**
73 1
	 * @var UserHooks
74 1
	 */
75 1
	private $instance;
76 1
77
	private $params = ['uid' => 'testUser', 'password' => 'password'];
78 1
79 1
	public function testLogin() {
80 1
		$this->userSetupMock->expects($this->exactly(2))
81
			->method('setupUser')
82 1
			->willReturnOnConsecutiveCalls(true, false);
83 1
84 1
		$this->keyManagerMock->expects($this->once())
85
			->method('init')
86 1
			->with('testUser', 'password');
87 1
88 1
		$this->assertNull($this->instance->login($this->params));
89
		$this->assertFalse($this->instance->login($this->params));
90 1
	}
91 1
92
	public function testLogout() {
93 1
		$this->sessionMock->expects($this->once())
94 1
			->method('clear');
95 1
96
		$this->assertNull($this->instance->logout());
97 1
	}
98 1
99
	public function testPostCreateUser() {
100 1
		$this->userSetupMock->expects($this->once())
101 1
			->method('setupUser');
102 1
103 1
		$this->assertNull($this->instance->postCreateUser($this->params));
104
	}
105 1
106 1
	public function testPostDeleteUser() {
107
		$this->keyManagerMock->expects($this->once())
108
			->method('deletePublicKey')
109
			->with('testUser');
110
111 2
		$this->assertNull($this->instance->postDeleteUser($this->params));
112
	}
113
114 2
	/**
115 2
	 * @dataProvider dataTestPreSetPassphrase
116
	 */
117 2
	public function testPreSetPassphrase($canChange) {
118 2
119 2
		/** @var UserHooks | \PHPUnit_Framework_MockObject_MockObject  $instance */
120 2
		$instance = $this->getMockBuilder('OCA\Encryption\Hooks\UserHooks')
121 2
			->setConstructorArgs(
122 2
				[
123 2
					$this->keyManagerMock,
124 2
					$this->userManagerMock,
125 2
					$this->loggerMock,
126 2
					$this->userSetupMock,
127 2
					$this->userSessionMock,
128 2
					$this->utilMock,
129 2
					$this->sessionMock,
130
					$this->cryptMock,
131 2
					$this->recoveryMock
132
				]
133 2
			)
134 2
			->setMethods(['setPassphrase'])
135 2
			->getMock();
136 2
137 2
		$userMock = $this->getMock('OCP\IUser');
138 2
139 2
		$this->userManagerMock->expects($this->once())
140
			->method('get')
141 2
			->with($this->params['uid'])
142
			->willReturn($userMock);
143 1
		$userMock->expects($this->once())
144 1
			->method('canChangePassword')
145
			->willReturn($canChange);
146
147 1
		if ($canChange) {
148 1
			// in this case the password will be changed in the post hook
149 1
			$instance->expects($this->never())->method('setPassphrase');
150
		} else {
151
			// if user can't change the password we update the encryption
152 2
			// key password already in the pre hook
153 2
			$instance->expects($this->once())
154
				->method('setPassphrase')
155
				->with($this->params);
156
		}
157
158
		$instance->preSetPassphrase($this->params);
159
	}
160
161
	public function dataTestPreSetPassphrase() {
162 1
		return [
163 1
			[true],
164 1
			[false]
165 1
		];
166
	}
167 1
168 1
	public function testSetPassphrase() {
169 1
		$this->sessionMock->expects($this->exactly(4))
170
			->method('getPrivateKey')
171 1
			->willReturnOnConsecutiveCalls(true, false);
172 1
173 1
		$this->cryptMock->expects($this->exactly(4))
174
			->method('encryptPrivateKey')
175 1
			->willReturn(true);
176 1
177 1
		$this->cryptMock->expects($this->any())
178 1
			->method('generateHeader')
179 1
			->willReturn(Crypt::HEADER_START . ':Cipher:test:' . Crypt::HEADER_END);
180 1
181 1
		$this->keyManagerMock->expects($this->exactly(4))
182 1
			->method('setPrivateKey')
183
			->willReturnCallback(function ($user, $key) {
184 1
				$header = substr($key, 0, strlen(Crypt::HEADER_START));
185 1
				$this->assertSame(
186
					Crypt::HEADER_START,
187 1
					$header, 'every encrypted file should start with a header');
188 1
			});
189 1
190 1
		$this->assertNull($this->instance->setPassphrase($this->params));
191
		$this->params['recoveryPassword'] = 'password';
192
193
		$this->recoveryMock->expects($this->exactly(3))
194 1
			->method('isRecoveryEnabledForUser')
195
			->with('testUser')
196
			->willReturnOnConsecutiveCalls(true, false);
197 1
198 1
199 1
		$this->instance = $this->getMockBuilder('OCA\Encryption\Hooks\UserHooks')
200 1
			->setConstructorArgs(
201
				[
202 1
					$this->keyManagerMock,
203
					$this->userManagerMock,
204
					$this->loggerMock,
205 1
					$this->userSetupMock,
206 1
					$this->userSessionMock,
207 1
					$this->utilMock,
208 1
					$this->sessionMock,
209
					$this->cryptMock,
210 1
					$this->recoveryMock
211 1
				]
212
			)->setMethods(['initMountPoints'])->getMock();
213 1
214 1
		$this->instance->expects($this->exactly(3))->method('initMountPoints');
215
216 1
		// Test first if statement
217 1
		$this->assertNull($this->instance->setPassphrase($this->params));
218 1
219
		// Test Second if conditional
220 1
		$this->keyManagerMock->expects($this->exactly(2))
221 1
			->method('userHasKeys')
222
			->with('testUser')
223 1
			->willReturn(true);
224 1
225 1
		$this->assertNull($this->instance->setPassphrase($this->params));
226 1
227
		// Test third and final if condition
228 1
		$this->utilMock->expects($this->once())
229 1
			->method('userHasFiles')
230 1
			->with('testUser')
231
			->willReturn(false);
232 1
233
		$this->cryptMock->expects($this->once())
234 1
			->method('createKeyPair');
235 1
236 1
		$this->keyManagerMock->expects($this->once())
237 1
			->method('setPrivateKey');
238
239 1
		$this->recoveryMock->expects($this->once())
240 1
			->method('recoverUsersFiles')
241 1
			->with('password', 'testUser');
242 1
243 1
		$this->assertNull($this->instance->setPassphrase($this->params));
244 1
	}
245 1
246 1
	public function testSetPasswordNoUser() {
247 1
		$this->sessionMock->expects($this->once())
248 1
			->method('getPrivateKey')
249
			->willReturn(true);
250 1
251 1
		$userSessionMock = $this->getMockBuilder('OCP\IUserSession')
252
			->disableOriginalConstructor()
253 1
			->getMock();
254 1
255 1
		$userSessionMock->expects($this->any())->method('getUser')->will($this->returnValue(null));
256 1
257
		$this->recoveryMock->expects($this->once())
258 1
			->method('isRecoveryEnabledForUser')
259 1
			->with('testUser')
260 1
			->willReturn(false);
261
262 1
		$userHooks = $this->getMockBuilder('OCA\Encryption\Hooks\UserHooks')
263 1
			->setConstructorArgs(
264
				[
265 9
					$this->keyManagerMock,
266 9
					$this->userManagerMock,
267 9
					$this->loggerMock,
268 9
					$this->userSetupMock,
269 9
					$userSessionMock,
270 9
					$this->utilMock,
271 9
					$this->sessionMock,
272 9
					$this->cryptMock,
273 9
					$this->recoveryMock
274 9
				]
275 9
			)->setMethods(['initMountPoints'])->getMock();
276 9
277
		$this->assertNull($userHooks->setPassphrase($this->params));
278 9
	}
279 9
280 9
	public function testPostPasswordReset() {
281 9
		$this->keyManagerMock->expects($this->once())
282 9
			->method('replaceUserKeys')
283 9
			->with('testUser');
284 9
285 9
		$this->userSetupMock->expects($this->once())
286 9
			->method('setupServerSide')
287
			->with('testUser', 'password');
288 9
289 9
		$this->assertNull($this->instance->postPasswordReset($this->params));
290
	}
291 9
292
	protected function setUp() {
293 9
		parent::setUp();
294 9
		$this->loggerMock = $this->getMock('OCP\ILogger');
295 9
		$this->keyManagerMock = $this->getMockBuilder('OCA\Encryption\KeyManager')
296
			->disableOriginalConstructor()
297 9
			->getMock();
298 9
		$this->userManagerMock = $this->getMockBuilder('OCP\IUserManager')
299 9
			->disableOriginalConstructor()
300
			->getMock();
301 9
		$this->userSetupMock = $this->getMockBuilder('OCA\Encryption\Users\Setup')
302 9
			->disableOriginalConstructor()
303 9
			->getMock();
304
305 9
		$this->userSessionMock = $this->getMockBuilder('OCP\IUserSession')
306 9
			->disableOriginalConstructor()
307 9
			->setMethods([
308 9
				'isLoggedIn',
309 9
				'getUID',
310 9
				'login',
311
				'logout',
312 9
				'setUser',
313 9
				'getUser',
314 9
				'canChangePassword'
315 9
			])
316 9
			->getMock();
317 9
318 9
		$this->userSessionMock->expects($this->any())->method('getUID')->will($this->returnValue('testUser'));
319 9
320 9
		$this->userSessionMock->expects($this->any())
321 9
			->method($this->anything())
322 9
			->will($this->returnSelf());
323 9
324 9
		$utilMock = $this->getMockBuilder('OCA\Encryption\Util')
325
			->disableOriginalConstructor()
326 9
			->getMock();
327
328
		$sessionMock = $this->getMockBuilder('OCA\Encryption\Session')
329
			->disableOriginalConstructor()
330
			->getMock();
331
332
		$this->cryptMock = $this->getMockBuilder('OCA\Encryption\Crypto\Crypt')
333
			->disableOriginalConstructor()
334
			->getMock();
335
		$recoveryMock = $this->getMockBuilder('OCA\Encryption\Recovery')
336
			->disableOriginalConstructor()
337
			->getMock();
338
339
		$this->sessionMock = $sessionMock;
340
		$this->recoveryMock = $recoveryMock;
341
		$this->utilMock = $utilMock;
342
		$this->instance = new UserHooks($this->keyManagerMock,
343
			$this->userManagerMock,
344
			$this->loggerMock,
345
			$this->userSetupMock,
346
			$this->userSessionMock,
347
			$this->utilMock,
348
			$this->sessionMock,
349
			$this->cryptMock,
350
			$this->recoveryMock
351
		);
352
353
	}
354
355
}
356