AuthPublicShareControllerTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  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
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\AppFramework\Controller;
  24. use OCP\AppFramework\AuthPublicShareController;
  25. use OCP\AppFramework\Http\RedirectResponse;
  26. use OCP\AppFramework\Http\TemplateResponse;
  27. use OCP\IRequest;
  28. use OCP\ISession;
  29. use OCP\IURLGenerator;
  30. class AuthPublicShareControllerTest extends \Test\TestCase {
  31. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  32. private $request;
  33. /** @var ISession|\PHPUnit\Framework\MockObject\MockObject */
  34. private $session;
  35. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  36. private $urlGenerator;
  37. /** @var AuthPublicShareController|\PHPUnit\Framework\MockObject\MockObject */
  38. private $controller;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->request = $this->createMock(IRequest::class);
  42. $this->session = $this->createMock(ISession::class);
  43. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  44. $this->controller = $this->getMockBuilder(AuthPublicShareController::class)
  45. ->setConstructorArgs([
  46. 'app',
  47. $this->request,
  48. $this->session,
  49. $this->urlGenerator
  50. ])->setMethods([
  51. 'authFailed',
  52. 'getPasswordHash',
  53. 'isAuthenticated',
  54. 'isPasswordProtected',
  55. 'isValidToken',
  56. 'showShare',
  57. 'verifyPassword',
  58. 'validateIdentity',
  59. 'generatePassword'
  60. ])->getMock();
  61. }
  62. public function testShowAuthenticate() {
  63. $expects = new TemplateResponse('core', 'publicshareauth', [], 'guest');
  64. $this->assertEquals($expects, $this->controller->showAuthenticate());
  65. }
  66. public function testAuthenticateAuthenticated() {
  67. $this->controller->method('isAuthenticated')
  68. ->willReturn(true);
  69. $this->controller->setToken('myToken');
  70. $this->session->method('get')
  71. ->willReturnMap(['public_link_authenticate_redirect', ['foo' => 'bar']]);
  72. $this->urlGenerator->method('linkToRoute')
  73. ->willReturn('myLink!');
  74. $result = $this->controller->authenticate('password');
  75. $this->assertInstanceOf(RedirectResponse::class, $result);
  76. $this->assertSame('myLink!', $result->getRedirectURL());
  77. }
  78. public function testAuthenticateInvalidPassword() {
  79. $this->controller->setToken('token');
  80. $this->controller->method('isPasswordProtected')
  81. ->willReturn(true);
  82. $this->controller->method('verifyPassword')
  83. ->with('password')
  84. ->willReturn(false);
  85. $this->controller->expects($this->once())
  86. ->method('authFailed');
  87. $expects = new TemplateResponse('core', 'publicshareauth', ['wrongpw' => true], 'guest');
  88. $expects->throttle();
  89. $result = $this->controller->authenticate('password');
  90. $this->assertEquals($expects, $result);
  91. }
  92. public function testAuthenticateValidPassword() {
  93. $this->controller->setToken('token');
  94. $this->controller->method('isPasswordProtected')
  95. ->willReturn(true);
  96. $this->controller->method('verifyPassword')
  97. ->with('password')
  98. ->willReturn(true);
  99. $this->controller->method('getPasswordHash')
  100. ->willReturn('hash');
  101. $this->session->expects($this->once())
  102. ->method('regenerateId');
  103. $this->session->method('get')
  104. ->willReturnMap(['public_link_authenticate_redirect', ['foo' => 'bar']]);
  105. $tokenSet = false;
  106. $hashSet = false;
  107. $this->session
  108. ->method('set')
  109. ->willReturnCallback(function ($key, $value) use (&$tokenSet, &$hashSet) {
  110. if ($key === 'public_link_authenticated_token' && $value === 'token') {
  111. $tokenSet = true;
  112. return true;
  113. }
  114. if ($key === 'public_link_authenticated_password_hash' && $value === 'hash') {
  115. $hashSet = true;
  116. return true;
  117. }
  118. return false;
  119. });
  120. $this->urlGenerator->method('linkToRoute')
  121. ->willReturn('myLink!');
  122. $result = $this->controller->authenticate('password');
  123. $this->assertInstanceOf(RedirectResponse::class, $result);
  124. $this->assertSame('myLink!', $result->getRedirectURL());
  125. $this->assertTrue($tokenSet);
  126. $this->assertTrue($hashSet);
  127. }
  128. }