1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
<?php
/**
* @author Clark Tomlinson <clark@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*/
namespace OCA\Encryption\Tests\Controller;
use OCA\Encryption\Controller\RecoveryController;
use OCP\AppFramework\Http;
use Test\TestCase;
class RecoveryControllerTest extends TestCase {
/**
* @var RecoveryController
*/
private $controller;
private $appName;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $requestMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $configMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $l10nMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $recoveryMock;
public function adminRecoveryProvider() {
return [
['test', 'test', '1', 'Recovery key successfully enabled', HTTP::STATUS_OK],
['', 'test', '1', 'Missing recovery key password', HTTP::STATUS_INTERNAL_SERVER_ERROR],
['test', '', '1', 'Please repeat the recovery key password', HTTP::STATUS_INTERNAL_SERVER_ERROR],
['test', 'soimething that doesn\'t match', '1', 'Repeated recovery key password does not match the provided recovery key password', HTTP::STATUS_INTERNAL_SERVER_ERROR],
['test', 'test', '0', 'Recovery key successfully disabled', HTTP::STATUS_OK],
];
}
/**
* @dataProvider adminRecoveryProvider
* @param $recoveryPassword
* @param $passconfirm
* @param $enableRecovery
* @param $expectedMessage
* @param $expectedStatus
*/
public function testAdminRecovery($recoveryPassword, $passconfirm, $enableRecovery, $expectedMessage, $expectedStatus) {
$this->recoveryMock->expects($this->any())
->method('enableAdminRecovery')
->willReturn(true);
$this->recoveryMock->expects($this->any())
->method('disableAdminRecovery')
->willReturn(true);
$response = $this->controller->adminRecovery($recoveryPassword,
$passconfirm,
$enableRecovery);
$this->assertEquals($expectedMessage, $response->getData()['data']['message']);
$this->assertEquals($expectedStatus, $response->getStatus());
}
public function changeRecoveryPasswordProvider() {
return [
['test', 'test', 'oldtestFail', 'Could not change the password. Maybe the old password was not correct.', HTTP::STATUS_INTERNAL_SERVER_ERROR],
['test', 'test', 'oldtest', 'Password successfully changed.', HTTP::STATUS_OK],
['test', 'notmatch', 'oldtest', 'Repeated recovery key password does not match the provided recovery key password', HTTP::STATUS_INTERNAL_SERVER_ERROR],
['', 'test', 'oldtest', 'Please provide a new recovery password', HTTP::STATUS_INTERNAL_SERVER_ERROR],
['test', 'test', '', 'Please provide the old recovery password', HTTP::STATUS_INTERNAL_SERVER_ERROR]
];
}
/**
* @dataProvider changeRecoveryPasswordProvider
* @param $password
* @param $confirmPassword
* @param $oldPassword
* @param $expectedMessage
* @param $expectedStatus
*/
public function testChangeRecoveryPassword($password, $confirmPassword, $oldPassword, $expectedMessage, $expectedStatus) {
$this->recoveryMock->expects($this->any())
->method('changeRecoveryKeyPassword')
->with($password, $oldPassword)
->will($this->returnValueMap([
['test', 'oldTestFail', false],
['test', 'oldtest', true]
]));
$response = $this->controller->changeRecoveryPassword($password,
$oldPassword,
$confirmPassword);
$this->assertEquals($expectedMessage, $response->getData()['data']['message']);
$this->assertEquals($expectedStatus, $response->getStatus());
}
public function userSetRecoveryProvider() {
return [
['1', 'Recovery Key enabled', Http::STATUS_OK],
['0', 'Could not enable the recovery key, please try again or contact your administrator', Http::STATUS_INTERNAL_SERVER_ERROR]
];
}
/**
* @dataProvider userSetRecoveryProvider
* @param $enableRecovery
* @param $expectedMessage
* @param $expectedStatus
*/
public function testUserSetRecovery($enableRecovery, $expectedMessage, $expectedStatus) {
$this->recoveryMock->expects($this->any())
->method('setRecoveryForUser')
->with($enableRecovery)
->will($this->returnValueMap([
['1', true],
['0', false]
]));
$response = $this->controller->userSetRecovery($enableRecovery);
$this->assertEquals($expectedMessage, $response->getData()['data']['message']);
$this->assertEquals($expectedStatus, $response->getStatus());
}
protected function setUp() {
parent::setUp();
$this->appName = 'encryption';
$this->requestMock = $this->getMockBuilder('\OCP\IRequest')
->disableOriginalConstructor()
->getMock();
$this->configMock = $this->getMockBuilder('OCP\IConfig')
->disableOriginalConstructor()
->getMock();
$this->l10nMock = $this->getMockBuilder('OCP\IL10N')
->disableOriginalConstructor()
->getMock();
// Make l10n work in our tests
$this->l10nMock->expects($this->any())
->method('t')
->willReturnArgument(0);
$this->recoveryMock = $this->getMockBuilder('OCA\Encryption\Recovery')
->disableOriginalConstructor()
->getMock();
$this->controller = new RecoveryController($this->appName,
$this->requestMock,
$this->configMock,
$this->l10nMock,
$this->recoveryMock);
}
}
|