AuthPublicShareControllerTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\AppFramework\Controller;
  7. use OCP\AppFramework\AuthPublicShareController;
  8. use OCP\AppFramework\Http\RedirectResponse;
  9. use OCP\AppFramework\Http\TemplateResponse;
  10. use OCP\IRequest;
  11. use OCP\ISession;
  12. use OCP\IURLGenerator;
  13. class AuthPublicShareControllerTest extends \Test\TestCase {
  14. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  15. private $request;
  16. /** @var ISession|\PHPUnit\Framework\MockObject\MockObject */
  17. private $session;
  18. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  19. private $urlGenerator;
  20. /** @var AuthPublicShareController|\PHPUnit\Framework\MockObject\MockObject */
  21. private $controller;
  22. protected function setUp(): void {
  23. parent::setUp();
  24. $this->request = $this->createMock(IRequest::class);
  25. $this->session = $this->createMock(ISession::class);
  26. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  27. $this->controller = $this->getMockBuilder(AuthPublicShareController::class)
  28. ->setConstructorArgs([
  29. 'app',
  30. $this->request,
  31. $this->session,
  32. $this->urlGenerator
  33. ])->setMethods([
  34. 'authFailed',
  35. 'getPasswordHash',
  36. 'isAuthenticated',
  37. 'isPasswordProtected',
  38. 'isValidToken',
  39. 'showShare',
  40. 'verifyPassword',
  41. 'validateIdentity',
  42. 'generatePassword'
  43. ])->getMock();
  44. }
  45. public function testShowAuthenticate(): void {
  46. $expects = new TemplateResponse('core', 'publicshareauth', [], 'guest');
  47. $this->assertEquals($expects, $this->controller->showAuthenticate());
  48. }
  49. public function testAuthenticateAuthenticated(): void {
  50. $this->controller->method('isAuthenticated')
  51. ->willReturn(true);
  52. $this->controller->setToken('myToken');
  53. $this->session->method('get')
  54. ->willReturnMap(['public_link_authenticate_redirect', ['foo' => 'bar']]);
  55. $this->urlGenerator->method('linkToRoute')
  56. ->willReturn('myLink!');
  57. $result = $this->controller->authenticate('password');
  58. $this->assertInstanceOf(RedirectResponse::class, $result);
  59. $this->assertSame('myLink!', $result->getRedirectURL());
  60. }
  61. public function testAuthenticateInvalidPassword(): void {
  62. $this->controller->setToken('token');
  63. $this->controller->method('isPasswordProtected')
  64. ->willReturn(true);
  65. $this->controller->method('verifyPassword')
  66. ->with('password')
  67. ->willReturn(false);
  68. $this->controller->expects($this->once())
  69. ->method('authFailed');
  70. $expects = new TemplateResponse('core', 'publicshareauth', ['wrongpw' => true], 'guest');
  71. $expects->throttle();
  72. $result = $this->controller->authenticate('password');
  73. $this->assertEquals($expects, $result);
  74. }
  75. public function testAuthenticateValidPassword(): void {
  76. $this->controller->setToken('token');
  77. $this->controller->method('isPasswordProtected')
  78. ->willReturn(true);
  79. $this->controller->method('verifyPassword')
  80. ->with('password')
  81. ->willReturn(true);
  82. $this->controller->method('getPasswordHash')
  83. ->willReturn('hash');
  84. $this->session->expects($this->once())
  85. ->method('regenerateId');
  86. $this->session->method('get')
  87. ->willReturnMap(['public_link_authenticate_redirect', ['foo' => 'bar']]);
  88. $tokenSet = false;
  89. $hashSet = false;
  90. $this->session
  91. ->method('set')
  92. ->willReturnCallback(function ($key, $value) use (&$tokenSet, &$hashSet) {
  93. if ($key === 'public_link_authenticated_token' && $value === 'token') {
  94. $tokenSet = true;
  95. return true;
  96. }
  97. if ($key === 'public_link_authenticated_password_hash' && $value === 'hash') {
  98. $hashSet = true;
  99. return true;
  100. }
  101. return false;
  102. });
  103. $this->urlGenerator->method('linkToRoute')
  104. ->willReturn('myLink!');
  105. $result = $this->controller->authenticate('password');
  106. $this->assertInstanceOf(RedirectResponse::class, $result);
  107. $this->assertSame('myLink!', $result->getRedirectURL());
  108. $this->assertTrue($tokenSet);
  109. $this->assertTrue($hashSet);
  110. }
  111. }