PublicShareControllerTest.php 3.2 KB

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