2019-05-03 09:09:19 -04:00
|
|
|
<?php
|
|
|
|
|
|
2026-05-21 10:24:48 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2019-05-03 09:09:19 -04:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2019-05-03 09:09:19 -04:00
|
|
|
*/
|
|
|
|
|
|
2021-04-16 03:41:54 -04:00
|
|
|
namespace Test\Authentication\Login;
|
2019-05-03 09:09:19 -04:00
|
|
|
|
|
|
|
|
use OC\Authentication\Login\CreateSessionTokenCommand;
|
|
|
|
|
use OC\Authentication\Token\IToken;
|
|
|
|
|
use OC\User\Session;
|
2026-05-21 10:24:48 -04:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2019-05-03 09:09:19 -04:00
|
|
|
use OCP\IConfig;
|
2026-05-21 10:24:48 -04:00
|
|
|
use OCP\IURLGenerator;
|
2019-05-03 09:09:19 -04:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
|
|
|
|
|
|
class CreateSessionTokenCommandTest extends ALoginCommandTest {
|
2026-05-21 10:24:48 -04:00
|
|
|
private IConfig&MockObject $config;
|
|
|
|
|
private Session&MockObject $userSession;
|
|
|
|
|
private IURLGenerator&MockObject $urlGenerator;
|
|
|
|
|
private ITimeFactory&MockObject $timeFactory;
|
2019-05-03 09:09:19 -04:00
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2019-05-03 09:09:19 -04:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->config = $this->createMock(IConfig::class);
|
|
|
|
|
$this->userSession = $this->createMock(Session::class);
|
2026-05-21 10:24:48 -04:00
|
|
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
|
|
|
|
$this->timeFactory = $this->createMock(ITimeFactory::class);
|
2019-05-03 09:09:19 -04:00
|
|
|
|
|
|
|
|
$this->cmd = new CreateSessionTokenCommand(
|
|
|
|
|
$this->config,
|
2026-05-21 10:24:48 -04:00
|
|
|
$this->userSession,
|
|
|
|
|
$this->urlGenerator,
|
|
|
|
|
$this->timeFactory,
|
2019-05-03 09:09:19 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testProcess() {
|
2026-05-21 10:24:48 -04:00
|
|
|
// Just return the route name as path to not return an empty string
|
|
|
|
|
$this->urlGenerator->expects(self::once())
|
|
|
|
|
->method('linkToRoute')
|
|
|
|
|
->willReturnArgument(0);
|
2019-05-03 09:09:19 -04:00
|
|
|
$data = $this->getLoggedInLoginData();
|
|
|
|
|
$this->config->expects($this->once())
|
2021-04-16 03:41:54 -04:00
|
|
|
->method('getSystemValueInt')
|
2019-05-03 09:09:19 -04:00
|
|
|
->with(
|
|
|
|
|
'remember_login_cookie_lifetime',
|
|
|
|
|
60 * 60 * 24 * 15
|
|
|
|
|
)
|
|
|
|
|
->willReturn(100);
|
|
|
|
|
$this->user->expects($this->any())
|
|
|
|
|
->method('getUID')
|
|
|
|
|
->willReturn($this->username);
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
|
->method('createSessionToken')
|
|
|
|
|
->with(
|
|
|
|
|
$this->request,
|
|
|
|
|
$this->username,
|
|
|
|
|
$this->username,
|
|
|
|
|
$this->password,
|
2026-05-21 10:24:48 -04:00
|
|
|
IToken::REMEMBER,
|
|
|
|
|
null
|
2019-05-03 09:09:19 -04:00
|
|
|
);
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
|
->method('updateTokens')
|
|
|
|
|
->with(
|
|
|
|
|
$this->username,
|
2019-09-18 13:46:21 -04:00
|
|
|
$this->password
|
2019-05-03 09:09:19 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = $this->cmd->process($data);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($result->isSuccess());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testProcessDoNotRemember() {
|
2026-05-21 10:24:48 -04:00
|
|
|
// Just return the route name as path to not return an empty string
|
|
|
|
|
$this->urlGenerator->expects(self::once())
|
|
|
|
|
->method('linkToRoute')
|
|
|
|
|
->willReturnArgument(0);
|
2019-05-03 09:09:19 -04:00
|
|
|
$data = $this->getLoggedInLoginData();
|
|
|
|
|
$this->config->expects($this->once())
|
2021-04-16 03:41:54 -04:00
|
|
|
->method('getSystemValueInt')
|
2019-05-03 09:09:19 -04:00
|
|
|
->with(
|
|
|
|
|
'remember_login_cookie_lifetime',
|
|
|
|
|
60 * 60 * 24 * 15
|
|
|
|
|
)
|
|
|
|
|
->willReturn(0);
|
|
|
|
|
$this->user->expects($this->any())
|
|
|
|
|
->method('getUID')
|
|
|
|
|
->willReturn($this->username);
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
|
->method('createSessionToken')
|
|
|
|
|
->with(
|
|
|
|
|
$this->request,
|
|
|
|
|
$this->username,
|
|
|
|
|
$this->username,
|
|
|
|
|
$this->password,
|
2026-05-21 10:24:48 -04:00
|
|
|
IToken::DO_NOT_REMEMBER,
|
|
|
|
|
null
|
2019-05-03 09:09:19 -04:00
|
|
|
);
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
|
->method('updateTokens')
|
|
|
|
|
->with(
|
|
|
|
|
$this->username,
|
2019-09-18 13:46:21 -04:00
|
|
|
$this->password
|
2019-05-03 09:09:19 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = $this->cmd->process($data);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($result->isSuccess());
|
|
|
|
|
$this->assertFalse($data->isRememberLogin());
|
|
|
|
|
}
|
2026-05-21 10:24:48 -04:00
|
|
|
|
|
|
|
|
public function testLoginFlowEphemeral(): void {
|
|
|
|
|
$this->redirectUrl = 'EPHEMERAL_ROUTE';
|
|
|
|
|
$this->urlGenerator->expects(self::once())
|
|
|
|
|
->method('linkToRoute')
|
|
|
|
|
->willReturn($this->redirectUrl);
|
|
|
|
|
$this->timeFactory->expects(self::once())
|
|
|
|
|
->method('getTime')
|
|
|
|
|
->willReturn(1000);
|
|
|
|
|
|
|
|
|
|
$data = $this->getLoggedInLoginDataWithRedirectUrl();
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
|
->method('getSystemValueInt')
|
|
|
|
|
->with(
|
|
|
|
|
'remember_login_cookie_lifetime',
|
|
|
|
|
60 * 60 * 24 * 15
|
|
|
|
|
)
|
|
|
|
|
->willReturn(100);
|
|
|
|
|
$this->user->expects($this->any())
|
|
|
|
|
->method('getUID')
|
|
|
|
|
->willReturn($this->username);
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
|
->method('createSessionToken')
|
|
|
|
|
->with(
|
|
|
|
|
$this->request,
|
|
|
|
|
$this->username,
|
|
|
|
|
$this->username,
|
|
|
|
|
$this->password,
|
|
|
|
|
IToken::REMEMBER,
|
|
|
|
|
1000 + 5 * 60
|
|
|
|
|
);
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
|
->method('updateTokens')
|
|
|
|
|
->with(
|
|
|
|
|
$this->username,
|
|
|
|
|
$this->password
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = $this->cmd->process($data);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($result->isSuccess());
|
|
|
|
|
}
|
2019-05-03 09:09:19 -04:00
|
|
|
}
|