Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overwriting isAuthenticated #26004

Merged
merged 1 commit into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/public/AppFramework/PublicShareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ abstract protected function isPasswordProtected(): bool;
*
* @since 14.0.0
*/
final public function isAuthenticated(): bool {
public function isAuthenticated(): bool {
// Always authenticated against non password protected shares
if (!$this->isPasswordProtected()) {
return true;
Expand Down
54 changes: 35 additions & 19 deletions tests/lib/AppFramework/Controller/PublicShareControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,53 @@
use OCP\IRequest;
use OCP\ISession;

class TestController extends PublicShareController {

/** @var string */
private $hash;

/** @var bool */
private $isProtected;

public function __construct(string $appName, IRequest $request, ISession $session, string $hash, bool $isProtected) {
parent::__construct($appName, $request, $session);

$this->hash = $hash;
$this->isProtected = $isProtected;
}

protected function getPasswordHash(): string {
return $this->hash;
}

public function isValidToken(): bool {
return false;
}

protected function isPasswordProtected(): bool {
return $this->isProtected;
}
}

class PublicShareControllerTest extends \Test\TestCase {

/** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
private $request;
/** @var ISession|\PHPUnit\Framework\MockObject\MockObject */
private $session;

/** @var PublicShareController|\PHPUnit\Framework\MockObject\MockObject */
private $controller;


protected function setUp(): void {
parent::setUp();

$this->request = $this->createMock(IRequest::class);
$this->session = $this->createMock(ISession::class);

$this->controller = $this->getMockBuilder(PublicShareController::class)
->setConstructorArgs([
'app',
$this->request,
$this->session
])->getMock();
}

public function testGetToken() {
$this->controller->setToken('test');
$this->assertEquals('test', $this->controller->getToken());
$controller = new TestController('app', $this->request, $this->session, 'hash', false);

$controller->setToken('test');
$this->assertEquals('test', $controller->getToken());
}

public function dataIsAuthenticated() {
Expand All @@ -74,19 +93,16 @@ public function dataIsAuthenticated() {
* @dataProvider dataIsAuthenticated
*/
public function testIsAuthenticatedNotPasswordProtected(bool $protected, string $token1, string $token2, string $hash1, string $hash2, bool $expected) {
$this->controller->method('isPasswordProtected')
->willReturn($protected);
$controller = new TestController('app', $this->request, $this->session, $hash2, $protected);

$this->session->method('get')
->willReturnMap([
['public_link_authenticated_token', $token1],
['public_link_authenticated_password_hash', $hash1],
]);

$this->controller->setToken($token2);
$this->controller->method('getPasswordHash')
->willReturn($hash2);
$controller->setToken($token2);

$this->assertEquals($expected, $this->controller->isAuthenticated());
$this->assertEquals($expected, $controller->isAuthenticated());
}
}