AuthPublicShareControllerTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 OC\AppFramework\Middleware\PublicShare\Exceptions\NeedAuthenticationException;
  25. use OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware;
  26. use OCP\AppFramework\AuthPublicShareController;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http\NotFoundResponse;
  29. use OCP\AppFramework\Http\RedirectResponse;
  30. use OCP\AppFramework\Http\TemplateResponse;
  31. use OCP\AppFramework\PublicShareController;
  32. use OCP\Files\NotFoundException;
  33. use OCP\IConfig;
  34. use OCP\IRequest;
  35. use OCP\ISession;
  36. use OCP\IURLGenerator;
  37. class AuthPublicShareControllerTest extends \Test\TestCase {
  38. /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
  39. private $request;
  40. /** @var ISession|\PHPUnit_Framework_MockObject_MockObject */
  41. private $session;
  42. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  43. private $urlGenerator;
  44. /** @var AuthPublicShareController|\PHPUnit_Framework_MockObject_MockObject */
  45. private $controller;
  46. protected function setUp() {
  47. parent::setUp();
  48. $this->request = $this->createMock(IRequest::class);
  49. $this->session = $this->createMock(ISession::class);
  50. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  51. $this->controller = $this->getMockBuilder(AuthPublicShareController::class)
  52. ->setConstructorArgs([
  53. 'app',
  54. $this->request,
  55. $this->session,
  56. $this->urlGenerator
  57. ])->setMethods([
  58. 'authFailed',
  59. 'getPasswordHash',
  60. 'isAuthenticated',
  61. 'isPasswordProtected',
  62. 'isValidToken',
  63. 'showShare',
  64. 'verifyPassword'
  65. ])->getMock();
  66. }
  67. public function testShowAuthenticate() {
  68. $expects = new TemplateResponse('core', 'publicshareauth', [], 'guest');
  69. $this->assertEquals($expects, $this->controller->showAuthenticate());
  70. }
  71. public function testAuthenticateAuthenticated() {
  72. $this->controller->method('isAuthenticated')
  73. ->willReturn(true);
  74. $this->controller->setToken('myToken');
  75. $this->session->method('get')
  76. ->willReturnMap(['public_link_authenticate_redirect', ['foo' => 'bar']]);
  77. $this->urlGenerator->method('linkToRoute')
  78. ->willReturn('myLink!');
  79. $result = $this->controller->authenticate('password');
  80. $this->assertInstanceOf(RedirectResponse::class, $result);
  81. $this->assertSame('myLink!', $result->getRedirectURL());
  82. }
  83. public function testAuthenticateInvalidPassword() {
  84. $this->controller->setToken('token');
  85. $this->controller->method('isPasswordProtected')
  86. ->willReturn(true);
  87. $this->controller->method('verifyPassword')
  88. ->with('password')
  89. ->willReturn(false);
  90. $this->controller->expects($this->once())
  91. ->method('authFailed');
  92. $expects = new TemplateResponse('core', 'publicshareauth', ['wrongpw' => true], 'guest');
  93. $expects->throttle();
  94. $result = $this->controller->authenticate('password');
  95. $this->assertEquals($expects, $result);
  96. }
  97. public function testAuthenticateValidPassword() {
  98. $this->controller->setToken('token');
  99. $this->controller->method('isPasswordProtected')
  100. ->willReturn(true);
  101. $this->controller->method('verifyPassword')
  102. ->with('password')
  103. ->willReturn(true);
  104. $this->controller->method('getPasswordHash')
  105. ->willReturn('hash');
  106. $this->session->expects($this->once())
  107. ->method('regenerateId');
  108. $this->session->method('get')
  109. ->willReturnMap(['public_link_authenticate_redirect', ['foo' => 'bar']]);
  110. $tokenSet = false;
  111. $hashSet = false;
  112. $this->session
  113. ->method('set')
  114. ->will($this->returnCallback(function($key, $value) use (&$tokenSet, &$hashSet) {
  115. if ($key === 'public_link_authenticated_token' && $value === 'token') {
  116. $tokenSet = true;
  117. return true;
  118. }
  119. if ($key === 'public_link_authenticated_password_hash' && $value === 'hash') {
  120. $hashSet = true;
  121. return true;
  122. }
  123. return false;
  124. }));
  125. $this->urlGenerator->method('linkToRoute')
  126. ->willReturn('myLink!');
  127. $result = $this->controller->authenticate('password');
  128. $this->assertInstanceOf(RedirectResponse::class, $result);
  129. $this->assertSame('myLink!', $result->getRedirectURL());
  130. $this->assertTrue($tokenSet);
  131. $this->assertTrue($hashSet);
  132. }
  133. }