2018-06-14 05:31:32 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2025-09-29 06:34:49 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2018-06-14 05:31:32 -04:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2018-06-14 05:31:32 -04:00
|
|
|
*/
|
2019-11-22 14:52:10 -05:00
|
|
|
|
2018-06-14 05:31:32 -04:00
|
|
|
namespace Test\AppFramework\Controller;
|
|
|
|
|
|
|
|
|
|
use OCP\AppFramework\PublicShareController;
|
|
|
|
|
use OCP\IRequest;
|
|
|
|
|
use OCP\ISession;
|
2025-09-29 06:34:49 -04:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-06-14 05:31:32 -04:00
|
|
|
|
2021-03-08 09:05:28 -05:00
|
|
|
class TestController extends PublicShareController {
|
2025-06-12 12:31:58 -04:00
|
|
|
public function __construct(
|
|
|
|
|
string $appName,
|
|
|
|
|
IRequest $request,
|
|
|
|
|
ISession $session,
|
|
|
|
|
private string $hash,
|
|
|
|
|
private bool $isProtected,
|
|
|
|
|
) {
|
2021-03-08 09:05:28 -05:00
|
|
|
parent::__construct($appName, $request, $session);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getPasswordHash(): string {
|
|
|
|
|
return $this->hash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isValidToken(): bool {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function isPasswordProtected(): bool {
|
|
|
|
|
return $this->isProtected;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-14 05:31:32 -04:00
|
|
|
class PublicShareControllerTest extends \Test\TestCase {
|
2025-09-29 06:34:49 -04:00
|
|
|
private IRequest&MockObject $request;
|
|
|
|
|
private ISession&MockObject $session;
|
2018-06-14 05:31:32 -04:00
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2018-06-14 05:31:32 -04:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->request = $this->createMock(IRequest::class);
|
|
|
|
|
$this->session = $this->createMock(ISession::class);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testGetToken(): void {
|
2021-03-08 09:05:28 -05:00
|
|
|
$controller = new TestController('app', $this->request, $this->session, 'hash', false);
|
|
|
|
|
|
|
|
|
|
$controller->setToken('test');
|
|
|
|
|
$this->assertEquals('test', $controller->getToken());
|
2018-06-14 05:31:32 -04:00
|
|
|
}
|
|
|
|
|
|
2025-05-12 11:18:04 -04:00
|
|
|
public static function dataIsAuthenticated(): array {
|
2018-06-14 05:31:32 -04:00
|
|
|
return [
|
|
|
|
|
[false, 'token1', 'token1', 'hash1', 'hash1', true],
|
|
|
|
|
[false, 'token1', 'token1', 'hash1', 'hash2', true],
|
|
|
|
|
[false, 'token1', 'token2', 'hash1', 'hash1', true],
|
|
|
|
|
[false, 'token1', 'token2', 'hash1', 'hash2', true],
|
|
|
|
|
[ true, 'token1', 'token1', 'hash1', 'hash1', true],
|
|
|
|
|
[ true, 'token1', 'token1', 'hash1', 'hash2', false],
|
|
|
|
|
[ true, 'token1', 'token2', 'hash1', 'hash1', false],
|
|
|
|
|
[ true, 'token1', 'token2', 'hash1', 'hash2', false],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('dataIsAuthenticated')]
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testIsAuthenticatedNotPasswordProtected(bool $protected, string $token1, string $token2, string $hash1, string $hash2, bool $expected): void {
|
2021-03-08 09:05:28 -05:00
|
|
|
$controller = new TestController('app', $this->request, $this->session, $hash2, $protected);
|
2018-06-14 05:31:32 -04:00
|
|
|
|
|
|
|
|
$this->session->method('get')
|
2025-10-23 05:57:03 -04:00
|
|
|
->with(PublicShareController::DAV_AUTHENTICATED_FRONTEND)
|
|
|
|
|
->willReturn("{\"$token1\":\"$hash1\"}");
|
2018-06-14 05:31:32 -04:00
|
|
|
|
2021-03-08 09:05:28 -05:00
|
|
|
$controller->setToken($token2);
|
2018-06-14 05:31:32 -04:00
|
|
|
|
2021-03-08 09:05:28 -05:00
|
|
|
$this->assertEquals($expected, $controller->isAuthenticated());
|
2018-06-14 05:31:32 -04:00
|
|
|
}
|
|
|
|
|
}
|