PublicShareControllerTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\PublicShareController;
  31. use OCP\Files\NotFoundException;
  32. use OCP\IConfig;
  33. use OCP\IRequest;
  34. use OCP\ISession;
  35. use OCP\IURLGenerator;
  36. class PublicShareControllerTest extends \Test\TestCase {
  37. /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
  38. private $request;
  39. /** @var ISession|\PHPUnit_Framework_MockObject_MockObject */
  40. private $session;
  41. /** @var PublicShareController|\PHPUnit_Framework_MockObject_MockObject */
  42. private $controller;
  43. protected function setUp() {
  44. parent::setUp();
  45. $this->request = $this->createMock(IRequest::class);
  46. $this->session = $this->createMock(ISession::class);
  47. $this->controller = $this->getMockBuilder(PublicShareController::class)
  48. ->setConstructorArgs([
  49. 'app',
  50. $this->request,
  51. $this->session
  52. ])->getMock();
  53. }
  54. public function testGetToken() {
  55. $this->controller->setToken('test');
  56. $this->assertEquals('test', $this->controller->getToken());
  57. }
  58. public function dataIsAuthenticated() {
  59. return [
  60. [false, 'token1', 'token1', 'hash1', 'hash1', true],
  61. [false, 'token1', 'token1', 'hash1', 'hash2', true],
  62. [false, 'token1', 'token2', 'hash1', 'hash1', true],
  63. [false, 'token1', 'token2', 'hash1', 'hash2', true],
  64. [ true, 'token1', 'token1', 'hash1', 'hash1', true],
  65. [ true, 'token1', 'token1', 'hash1', 'hash2', false],
  66. [ true, 'token1', 'token2', 'hash1', 'hash1', false],
  67. [ true, 'token1', 'token2', 'hash1', 'hash2', false],
  68. ];
  69. }
  70. /**
  71. * @dataProvider dataIsAuthenticated
  72. */
  73. public function testIsAuthenticatedNotPasswordProtected(bool $protected, string $token1, string $token2, string $hash1, string $hash2, bool $expected) {
  74. $this->controller->method('isPasswordProtected')
  75. ->willReturn($protected);
  76. $this->session->method('get')
  77. ->willReturnMap([
  78. ['public_link_authenticated_token', $token1],
  79. ['public_link_authenticated_password_hash', $hash1],
  80. ]);
  81. $this->controller->setToken($token2);
  82. $this->controller->method('getPasswordHash')
  83. ->willReturn($hash2);
  84. $this->assertEquals($expected, $this->controller->isAuthenticated());
  85. }
  86. }