You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.9 KiB

10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Clark Tomlinson <fallen013@gmail.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Encryption\Tests\Controller;
  27. use OCA\Encryption\Controller\RecoveryController;
  28. use OCA\Encryption\Recovery;
  29. use OCP\AppFramework\Http;
  30. use OCP\IConfig;
  31. use OCP\IL10N;
  32. use OCP\IRequest;
  33. use Test\TestCase;
  34. class RecoveryControllerTest extends TestCase {
  35. /** @var RecoveryController */
  36. private $controller;
  37. /** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */
  38. private $requestMock;
  39. /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
  40. private $configMock;
  41. /** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject */
  42. private $l10nMock;
  43. /** @var \OCA\Encryption\Recovery|\PHPUnit_Framework_MockObject_MockObject */
  44. private $recoveryMock;
  45. public function adminRecoveryProvider() {
  46. return [
  47. ['test', 'test', '1', 'Recovery key successfully enabled', Http::STATUS_OK],
  48. ['', 'test', '1', 'Missing recovery key password', Http::STATUS_BAD_REQUEST],
  49. ['test', '', '1', 'Please repeat the recovery key password', Http::STATUS_BAD_REQUEST],
  50. ['test', 'soimething that doesn\'t match', '1', 'Repeated recovery key password does not match the provided recovery key password', Http::STATUS_BAD_REQUEST],
  51. ['test', 'test', '0', 'Recovery key successfully disabled', Http::STATUS_OK],
  52. ];
  53. }
  54. /**
  55. * @dataProvider adminRecoveryProvider
  56. * @param $recoveryPassword
  57. * @param $passConfirm
  58. * @param $enableRecovery
  59. * @param $expectedMessage
  60. * @param $expectedStatus
  61. */
  62. public function testAdminRecovery($recoveryPassword, $passConfirm, $enableRecovery, $expectedMessage, $expectedStatus) {
  63. $this->recoveryMock->expects($this->any())
  64. ->method('enableAdminRecovery')
  65. ->willReturn(true);
  66. $this->recoveryMock->expects($this->any())
  67. ->method('disableAdminRecovery')
  68. ->willReturn(true);
  69. $response = $this->controller->adminRecovery($recoveryPassword,
  70. $passConfirm,
  71. $enableRecovery);
  72. $this->assertEquals($expectedMessage, $response->getData()['data']['message']);
  73. $this->assertEquals($expectedStatus, $response->getStatus());
  74. }
  75. public function changeRecoveryPasswordProvider() {
  76. return [
  77. ['test', 'test', 'oldtestFail', 'Could not change the password. Maybe the old password was not correct.', Http::STATUS_BAD_REQUEST],
  78. ['test', 'test', 'oldtest', 'Password successfully changed.', Http::STATUS_OK],
  79. ['test', 'notmatch', 'oldtest', 'Repeated recovery key password does not match the provided recovery key password', Http::STATUS_BAD_REQUEST],
  80. ['', 'test', 'oldtest', 'Please provide a new recovery password', Http::STATUS_BAD_REQUEST],
  81. ['test', 'test', '', 'Please provide the old recovery password', Http::STATUS_BAD_REQUEST]
  82. ];
  83. }
  84. /**
  85. * @dataProvider changeRecoveryPasswordProvider
  86. * @param $password
  87. * @param $confirmPassword
  88. * @param $oldPassword
  89. * @param $expectedMessage
  90. * @param $expectedStatus
  91. */
  92. public function testChangeRecoveryPassword($password, $confirmPassword, $oldPassword, $expectedMessage, $expectedStatus) {
  93. $this->recoveryMock->expects($this->any())
  94. ->method('changeRecoveryKeyPassword')
  95. ->with($password, $oldPassword)
  96. ->willReturnMap([
  97. ['test', 'oldTestFail', false],
  98. ['test', 'oldtest', true]
  99. ]);
  100. $response = $this->controller->changeRecoveryPassword($password,
  101. $oldPassword,
  102. $confirmPassword);
  103. $this->assertEquals($expectedMessage, $response->getData()['data']['message']);
  104. $this->assertEquals($expectedStatus, $response->getStatus());
  105. }
  106. public function userSetRecoveryProvider() {
  107. return [
  108. ['1', 'Recovery Key enabled', Http::STATUS_OK],
  109. ['0', 'Could not enable the recovery key, please try again or contact your administrator', Http::STATUS_BAD_REQUEST]
  110. ];
  111. }
  112. /**
  113. * @dataProvider userSetRecoveryProvider
  114. * @param $enableRecovery
  115. * @param $expectedMessage
  116. * @param $expectedStatus
  117. */
  118. public function testUserSetRecovery($enableRecovery, $expectedMessage, $expectedStatus) {
  119. $this->recoveryMock->expects($this->any())
  120. ->method('setRecoveryForUser')
  121. ->with($enableRecovery)
  122. ->willReturnMap([
  123. ['1', true],
  124. ['0', false]
  125. ]);
  126. $response = $this->controller->userSetRecovery($enableRecovery);
  127. $this->assertEquals($expectedMessage, $response->getData()['data']['message']);
  128. $this->assertEquals($expectedStatus, $response->getStatus());
  129. }
  130. protected function setUp(): void {
  131. parent::setUp();
  132. $this->requestMock = $this->getMockBuilder(IRequest::class)
  133. ->disableOriginalConstructor()
  134. ->getMock();
  135. $this->configMock = $this->getMockBuilder(IConfig::class)
  136. ->disableOriginalConstructor()
  137. ->getMock();
  138. $this->l10nMock = $this->getMockBuilder(IL10N::class)
  139. ->disableOriginalConstructor()
  140. ->getMock();
  141. // Make l10n work in our tests
  142. $this->l10nMock->expects($this->any())
  143. ->method('t')
  144. ->willReturnArgument(0);
  145. $this->recoveryMock = $this->getMockBuilder(Recovery::class)
  146. ->disableOriginalConstructor()
  147. ->getMock();
  148. $this->controller = new RecoveryController('encryption',
  149. $this->requestMock,
  150. $this->configMock,
  151. $this->l10nMock,
  152. $this->recoveryMock);
  153. }
  154. }