PublicShareControllerTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\PublicShareController;
  8. use OCP\IRequest;
  9. use OCP\ISession;
  10. class TestController extends PublicShareController {
  11. /** @var string */
  12. private $hash;
  13. /** @var bool */
  14. private $isProtected;
  15. public function __construct(string $appName, IRequest $request, ISession $session, string $hash, bool $isProtected) {
  16. parent::__construct($appName, $request, $session);
  17. $this->hash = $hash;
  18. $this->isProtected = $isProtected;
  19. }
  20. protected function getPasswordHash(): string {
  21. return $this->hash;
  22. }
  23. public function isValidToken(): bool {
  24. return false;
  25. }
  26. protected function isPasswordProtected(): bool {
  27. return $this->isProtected;
  28. }
  29. }
  30. class PublicShareControllerTest extends \Test\TestCase {
  31. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  32. private $request;
  33. /** @var ISession|\PHPUnit\Framework\MockObject\MockObject */
  34. private $session;
  35. protected function setUp(): void {
  36. parent::setUp();
  37. $this->request = $this->createMock(IRequest::class);
  38. $this->session = $this->createMock(ISession::class);
  39. }
  40. public function testGetToken(): void {
  41. $controller = new TestController('app', $this->request, $this->session, 'hash', false);
  42. $controller->setToken('test');
  43. $this->assertEquals('test', $controller->getToken());
  44. }
  45. public function dataIsAuthenticated() {
  46. return [
  47. [false, 'token1', 'token1', 'hash1', 'hash1', true],
  48. [false, 'token1', 'token1', 'hash1', 'hash2', true],
  49. [false, 'token1', 'token2', 'hash1', 'hash1', true],
  50. [false, 'token1', 'token2', 'hash1', 'hash2', true],
  51. [ true, 'token1', 'token1', 'hash1', 'hash1', true],
  52. [ true, 'token1', 'token1', 'hash1', 'hash2', false],
  53. [ true, 'token1', 'token2', 'hash1', 'hash1', false],
  54. [ true, 'token1', 'token2', 'hash1', 'hash2', false],
  55. ];
  56. }
  57. /**
  58. * @dataProvider dataIsAuthenticated
  59. */
  60. public function testIsAuthenticatedNotPasswordProtected(bool $protected, string $token1, string $token2, string $hash1, string $hash2, bool $expected): void {
  61. $controller = new TestController('app', $this->request, $this->session, $hash2, $protected);
  62. $this->session->method('get')
  63. ->willReturnMap([
  64. ['public_link_authenticated_token', $token1],
  65. ['public_link_authenticated_password_hash', $hash1],
  66. ]);
  67. $controller->setToken($token2);
  68. $this->assertEquals($expected, $controller->isAuthenticated());
  69. }
  70. }