RecoveryControllerTest.php 5.7 KB

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