2018-06-14 15:15:16 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2024-05-10 15:09:14 +02:00
|
|
|
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2018-06-14 15:15:16 +02:00
|
|
|
*/
|
2019-11-22 20:52:10 +01:00
|
|
|
|
2018-06-14 15:15:16 +02:00
|
|
|
namespace Test\AppFramework\Controller;
|
|
|
|
|
|
|
|
use OCP\AppFramework\AuthPublicShareController;
|
|
|
|
use OCP\AppFramework\Http\RedirectResponse;
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\ISession;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
|
|
|
|
class AuthPublicShareControllerTest extends \Test\TestCase {
|
2020-08-11 21:32:18 +02:00
|
|
|
/** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
|
2018-06-14 15:15:16 +02:00
|
|
|
private $request;
|
2020-08-11 21:32:18 +02:00
|
|
|
/** @var ISession|\PHPUnit\Framework\MockObject\MockObject */
|
2018-06-14 15:15:16 +02:00
|
|
|
private $session;
|
2020-08-11 21:32:18 +02:00
|
|
|
/** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
|
2018-06-14 15:15:16 +02:00
|
|
|
private $urlGenerator;
|
|
|
|
|
2020-08-11 21:32:18 +02:00
|
|
|
/** @var AuthPublicShareController|\PHPUnit\Framework\MockObject\MockObject */
|
2018-06-14 15:15:16 +02:00
|
|
|
private $controller;
|
|
|
|
|
|
|
|
|
2019-11-21 16:40:38 +01:00
|
|
|
protected function setUp(): void {
|
2018-06-14 15:15:16 +02:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->request = $this->createMock(IRequest::class);
|
|
|
|
$this->session = $this->createMock(ISession::class);
|
|
|
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
|
|
|
|
|
|
|
$this->controller = $this->getMockBuilder(AuthPublicShareController::class)
|
|
|
|
->setConstructorArgs([
|
|
|
|
'app',
|
|
|
|
$this->request,
|
|
|
|
$this->session,
|
|
|
|
$this->urlGenerator
|
|
|
|
])->setMethods([
|
|
|
|
'authFailed',
|
|
|
|
'getPasswordHash',
|
|
|
|
'isAuthenticated',
|
|
|
|
'isPasswordProtected',
|
|
|
|
'isValidToken',
|
|
|
|
'showShare',
|
2022-02-05 20:49:17 +01:00
|
|
|
'verifyPassword',
|
|
|
|
'validateIdentity',
|
|
|
|
'generatePassword'
|
2018-06-14 15:15:16 +02:00
|
|
|
])->getMock();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShowAuthenticate() {
|
|
|
|
$expects = new TemplateResponse('core', 'publicshareauth', [], 'guest');
|
|
|
|
|
|
|
|
$this->assertEquals($expects, $this->controller->showAuthenticate());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAuthenticateAuthenticated() {
|
|
|
|
$this->controller->method('isAuthenticated')
|
|
|
|
->willReturn(true);
|
|
|
|
|
2018-06-19 21:40:45 +02:00
|
|
|
$this->controller->setToken('myToken');
|
|
|
|
|
2018-06-14 15:15:16 +02:00
|
|
|
$this->session->method('get')
|
|
|
|
->willReturnMap(['public_link_authenticate_redirect', ['foo' => 'bar']]);
|
|
|
|
|
|
|
|
$this->urlGenerator->method('linkToRoute')
|
|
|
|
->willReturn('myLink!');
|
|
|
|
|
|
|
|
$result = $this->controller->authenticate('password');
|
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $result);
|
|
|
|
$this->assertSame('myLink!', $result->getRedirectURL());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAuthenticateInvalidPassword() {
|
|
|
|
$this->controller->setToken('token');
|
|
|
|
$this->controller->method('isPasswordProtected')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->controller->method('verifyPassword')
|
|
|
|
->with('password')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->controller->expects($this->once())
|
|
|
|
->method('authFailed');
|
|
|
|
|
|
|
|
$expects = new TemplateResponse('core', 'publicshareauth', ['wrongpw' => true], 'guest');
|
|
|
|
$expects->throttle();
|
|
|
|
|
|
|
|
$result = $this->controller->authenticate('password');
|
|
|
|
|
|
|
|
$this->assertEquals($expects, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAuthenticateValidPassword() {
|
|
|
|
$this->controller->setToken('token');
|
|
|
|
$this->controller->method('isPasswordProtected')
|
|
|
|
->willReturn(true);
|
|
|
|
$this->controller->method('verifyPassword')
|
|
|
|
->with('password')
|
|
|
|
->willReturn(true);
|
|
|
|
$this->controller->method('getPasswordHash')
|
|
|
|
->willReturn('hash');
|
|
|
|
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('regenerateId');
|
|
|
|
$this->session->method('get')
|
|
|
|
->willReturnMap(['public_link_authenticate_redirect', ['foo' => 'bar']]);
|
|
|
|
|
|
|
|
$tokenSet = false;
|
|
|
|
$hashSet = false;
|
|
|
|
$this->session
|
|
|
|
->method('set')
|
2020-04-09 13:53:40 +02:00
|
|
|
->willReturnCallback(function ($key, $value) use (&$tokenSet, &$hashSet) {
|
2018-06-14 15:15:16 +02:00
|
|
|
if ($key === 'public_link_authenticated_token' && $value === 'token') {
|
|
|
|
$tokenSet = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ($key === 'public_link_authenticated_password_hash' && $value === 'hash') {
|
|
|
|
$hashSet = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-03-25 22:21:27 +01:00
|
|
|
});
|
2018-06-14 15:15:16 +02:00
|
|
|
|
|
|
|
$this->urlGenerator->method('linkToRoute')
|
|
|
|
->willReturn('myLink!');
|
|
|
|
|
|
|
|
$result = $this->controller->authenticate('password');
|
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $result);
|
|
|
|
$this->assertSame('myLink!', $result->getRedirectURL());
|
|
|
|
$this->assertTrue($tokenSet);
|
|
|
|
$this->assertTrue($hashSet);
|
|
|
|
}
|
|
|
|
}
|