RecoveryControllerTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Encryption\Tests\Controller;
  8. use OCA\Encryption\Controller\RecoveryController;
  9. use OCA\Encryption\Recovery;
  10. use OCP\AppFramework\Http;
  11. use OCP\IConfig;
  12. use OCP\IL10N;
  13. use OCP\IRequest;
  14. use Test\TestCase;
  15. class RecoveryControllerTest extends TestCase {
  16. /** @var RecoveryController */
  17. private $controller;
  18. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  19. private $requestMock;
  20. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  21. private $configMock;
  22. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  23. private $l10nMock;
  24. /** @var Recovery|\PHPUnit\Framework\MockObject\MockObject */
  25. private $recoveryMock;
  26. public function adminRecoveryProvider() {
  27. return [
  28. ['test', 'test', '1', 'Recovery key successfully enabled', Http::STATUS_OK],
  29. ['', 'test', '1', 'Missing recovery key password', Http::STATUS_BAD_REQUEST],
  30. ['test', '', '1', 'Please repeat the recovery key password', Http::STATUS_BAD_REQUEST],
  31. ['test', 'soimething that doesn\'t match', '1', 'Repeated recovery key password does not match the provided recovery key password', Http::STATUS_BAD_REQUEST],
  32. ['test', 'test', '0', 'Recovery key successfully disabled', Http::STATUS_OK],
  33. ];
  34. }
  35. /**
  36. * @dataProvider adminRecoveryProvider
  37. * @param $recoveryPassword
  38. * @param $passConfirm
  39. * @param $enableRecovery
  40. * @param $expectedMessage
  41. * @param $expectedStatus
  42. */
  43. public function testAdminRecovery($recoveryPassword, $passConfirm, $enableRecovery, $expectedMessage, $expectedStatus): void {
  44. $this->recoveryMock->expects($this->any())
  45. ->method('enableAdminRecovery')
  46. ->willReturn(true);
  47. $this->recoveryMock->expects($this->any())
  48. ->method('disableAdminRecovery')
  49. ->willReturn(true);
  50. $response = $this->controller->adminRecovery($recoveryPassword,
  51. $passConfirm,
  52. $enableRecovery);
  53. $this->assertEquals($expectedMessage, $response->getData()['data']['message']);
  54. $this->assertEquals($expectedStatus, $response->getStatus());
  55. }
  56. public function changeRecoveryPasswordProvider() {
  57. return [
  58. ['test', 'test', 'oldtestFail', 'Could not change the password. Maybe the old password was not correct.', Http::STATUS_BAD_REQUEST],
  59. ['test', 'test', 'oldtest', 'Password successfully changed.', Http::STATUS_OK],
  60. ['test', 'notmatch', 'oldtest', 'Repeated recovery key password does not match the provided recovery key password', Http::STATUS_BAD_REQUEST],
  61. ['', 'test', 'oldtest', 'Please provide a new recovery password', Http::STATUS_BAD_REQUEST],
  62. ['test', 'test', '', 'Please provide the old recovery password', Http::STATUS_BAD_REQUEST]
  63. ];
  64. }
  65. /**
  66. * @dataProvider changeRecoveryPasswordProvider
  67. * @param $password
  68. * @param $confirmPassword
  69. * @param $oldPassword
  70. * @param $expectedMessage
  71. * @param $expectedStatus
  72. */
  73. public function testChangeRecoveryPassword($password, $confirmPassword, $oldPassword, $expectedMessage, $expectedStatus): void {
  74. $this->recoveryMock->expects($this->any())
  75. ->method('changeRecoveryKeyPassword')
  76. ->with($password, $oldPassword)
  77. ->willReturnMap([
  78. ['test', 'oldTestFail', false],
  79. ['test', 'oldtest', true]
  80. ]);
  81. $response = $this->controller->changeRecoveryPassword($password,
  82. $oldPassword,
  83. $confirmPassword);
  84. $this->assertEquals($expectedMessage, $response->getData()['data']['message']);
  85. $this->assertEquals($expectedStatus, $response->getStatus());
  86. }
  87. public function userSetRecoveryProvider() {
  88. return [
  89. ['1', 'Recovery Key enabled', Http::STATUS_OK],
  90. ['0', 'Could not enable the recovery key, please try again or contact your administrator', Http::STATUS_BAD_REQUEST]
  91. ];
  92. }
  93. /**
  94. * @dataProvider userSetRecoveryProvider
  95. * @param $enableRecovery
  96. * @param $expectedMessage
  97. * @param $expectedStatus
  98. */
  99. public function testUserSetRecovery($enableRecovery, $expectedMessage, $expectedStatus): void {
  100. $this->recoveryMock->expects($this->any())
  101. ->method('setRecoveryForUser')
  102. ->with($enableRecovery)
  103. ->willReturnMap([
  104. ['1', true],
  105. ['0', false]
  106. ]);
  107. $response = $this->controller->userSetRecovery($enableRecovery);
  108. $this->assertEquals($expectedMessage, $response->getData()['data']['message']);
  109. $this->assertEquals($expectedStatus, $response->getStatus());
  110. }
  111. protected function setUp(): void {
  112. parent::setUp();
  113. $this->requestMock = $this->getMockBuilder(IRequest::class)
  114. ->disableOriginalConstructor()
  115. ->getMock();
  116. $this->configMock = $this->getMockBuilder(IConfig::class)
  117. ->disableOriginalConstructor()
  118. ->getMock();
  119. $this->l10nMock = $this->getMockBuilder(IL10N::class)
  120. ->disableOriginalConstructor()
  121. ->getMock();
  122. // Make l10n work in our tests
  123. $this->l10nMock->expects($this->any())
  124. ->method('t')
  125. ->willReturnArgument(0);
  126. $this->recoveryMock = $this->getMockBuilder(Recovery::class)
  127. ->disableOriginalConstructor()
  128. ->getMock();
  129. $this->controller = new RecoveryController('encryption',
  130. $this->requestMock,
  131. $this->configMock,
  132. $this->l10nMock,
  133. $this->recoveryMock);
  134. }
  135. }