1
0

RecoveryControllerTest.php 5.8 KB

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