2016-05-11 12:23:25 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Christoph Wurst <christoph@owncloud.com>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-05-25 13:02:05 +03:00
|
|
|
namespace Test\Core\Controller;
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2017-01-03 11:57:52 +03:00
|
|
|
use OC\Authentication\TwoFactorAuth\Manager;
|
2018-05-22 09:52:16 +03:00
|
|
|
use OC\Authentication\TwoFactorAuth\ProviderSet;
|
2016-05-25 13:02:05 +03:00
|
|
|
use OC\Core\Controller\TwoFactorChallengeController;
|
2017-01-03 11:57:52 +03:00
|
|
|
use OC_Util;
|
|
|
|
use OCP\AppFramework\Http\RedirectResponse;
|
2019-02-04 10:54:56 +03:00
|
|
|
use OCP\AppFramework\Http\StandaloneTemplateResponse;
|
2019-04-05 19:21:08 +03:00
|
|
|
use OCP\Authentication\TwoFactorAuth\IActivatableAtLogin;
|
|
|
|
use OCP\Authentication\TwoFactorAuth\ILoginSetupProvider;
|
2017-01-03 11:57:52 +03:00
|
|
|
use OCP\Authentication\TwoFactorAuth\IProvider;
|
|
|
|
use OCP\Authentication\TwoFactorAuth\TwoFactorException;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\ISession;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
use OCP\Template;
|
|
|
|
use PHPUnit_Framework_MockObject_MockObject;
|
2016-05-11 12:23:25 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class TwoFactorChallengeControllerTest extends TestCase {
|
|
|
|
|
2017-01-03 11:57:52 +03:00
|
|
|
/** @var IRequest|PHPUnit_Framework_MockObject_MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $request;
|
2017-01-03 11:57:52 +03:00
|
|
|
|
|
|
|
/** @var Manager|PHPUnit_Framework_MockObject_MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $twoFactorManager;
|
2017-01-03 11:57:52 +03:00
|
|
|
|
|
|
|
/** @var IUserSession|PHPUnit_Framework_MockObject_MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $userSession;
|
2017-01-03 11:57:52 +03:00
|
|
|
|
|
|
|
/** @var ISession|PHPUnit_Framework_MockObject_MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $session;
|
2017-01-03 11:57:52 +03:00
|
|
|
|
|
|
|
/** @var IURLGenerator|PHPUnit_Framework_MockObject_MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $urlGenerator;
|
|
|
|
|
2017-01-03 11:57:52 +03:00
|
|
|
/** @var TwoFactorChallengeController|PHPUnit_Framework_MockObject_MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $controller;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2016-05-11 12:23:25 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2017-01-03 11:57:52 +03:00
|
|
|
$this->request = $this->createMock(IRequest::class);
|
|
|
|
$this->twoFactorManager = $this->createMock(Manager::class);
|
|
|
|
$this->userSession = $this->createMock(IUserSession::class);
|
|
|
|
$this->session = $this->createMock(ISession::class);
|
|
|
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2017-01-03 11:57:52 +03:00
|
|
|
$this->controller = $this->getMockBuilder(TwoFactorChallengeController::class)
|
2016-06-07 18:53:00 +03:00
|
|
|
->setConstructorArgs([
|
|
|
|
'core',
|
|
|
|
$this->request,
|
|
|
|
$this->twoFactorManager,
|
|
|
|
$this->userSession,
|
|
|
|
$this->session,
|
|
|
|
$this->urlGenerator,
|
|
|
|
])
|
2017-08-18 13:16:43 +03:00
|
|
|
->setMethods(['getLogoutUrl'])
|
2016-06-07 18:53:00 +03:00
|
|
|
->getMock();
|
|
|
|
$this->controller->expects($this->any())
|
2017-08-18 13:16:43 +03:00
|
|
|
->method('getLogoutUrl')
|
2016-06-07 18:53:00 +03:00
|
|
|
->willReturn('logoutAttribute');
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSelectChallenge() {
|
2017-01-03 11:57:52 +03:00
|
|
|
$user = $this->getMockBuilder(IUser::class)->getMock();
|
2019-04-05 19:21:08 +03:00
|
|
|
$p1 = $this->createMock(IActivatableAtLogin::class);
|
2018-05-22 09:52:16 +03:00
|
|
|
$p1->method('getId')->willReturn('p1');
|
|
|
|
$backupProvider = $this->createMock(IProvider::class);
|
|
|
|
$backupProvider->method('getId')->willReturn('backup_codes');
|
|
|
|
$providerSet = new ProviderSet([$p1, $backupProvider], true);
|
2019-04-05 19:21:08 +03:00
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('getLoginSetupProviders')
|
|
|
|
->with($user)
|
|
|
|
->willReturn([$p1]);
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($user);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->twoFactorManager->expects($this->once())
|
2018-05-22 09:52:16 +03:00
|
|
|
->method('getProviderSet')
|
2016-08-29 20:19:44 +03:00
|
|
|
->with($user)
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($providerSet);
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2019-02-04 10:54:56 +03:00
|
|
|
$expected = new StandaloneTemplateResponse('core', 'twofactorselectchallenge', [
|
2018-05-22 09:52:16 +03:00
|
|
|
'providers' => [
|
|
|
|
$p1,
|
|
|
|
],
|
|
|
|
'providerMissing' => true,
|
|
|
|
'backupProvider' => $backupProvider,
|
2016-06-01 14:54:08 +03:00
|
|
|
'redirect_url' => '/some/url',
|
2017-08-18 13:16:43 +03:00
|
|
|
'logout_url' => 'logoutAttribute',
|
2019-04-05 19:21:08 +03:00
|
|
|
'hasSetupProviders' => true,
|
|
|
|
], 'guest');
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2016-06-01 14:54:08 +03:00
|
|
|
$this->assertEquals($expected, $this->controller->selectChallenge('/some/url'));
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testShowChallenge() {
|
2017-01-03 11:57:52 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$provider = $this->createMock(IProvider::class);
|
2018-05-22 09:52:16 +03:00
|
|
|
$provider->method('getId')->willReturn('myprovider');
|
2017-01-03 11:57:52 +03:00
|
|
|
$backupProvider = $this->createMock(IProvider::class);
|
2018-05-22 09:52:16 +03:00
|
|
|
$backupProvider->method('getId')->willReturn('backup_codes');
|
2017-01-03 11:57:52 +03:00
|
|
|
$tmpl = $this->createMock(Template::class);
|
2018-05-22 09:52:16 +03:00
|
|
|
$providerSet = new ProviderSet([$provider, $backupProvider], true);
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($user);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->twoFactorManager->expects($this->once())
|
2018-05-22 09:52:16 +03:00
|
|
|
->method('getProviderSet')
|
2016-08-29 20:19:44 +03:00
|
|
|
->with($user)
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($providerSet);
|
2016-08-29 20:19:44 +03:00
|
|
|
$provider->expects($this->once())
|
|
|
|
->method('getId')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('u2f');
|
2016-08-29 20:19:44 +03:00
|
|
|
$backupProvider->expects($this->once())
|
|
|
|
->method('getId')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('backup_codes');
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('exists')
|
|
|
|
->with('two_factor_auth_error')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2016-11-15 10:51:07 +03:00
|
|
|
$this->session->expects($this->exactly(2))
|
2016-05-11 12:23:25 +03:00
|
|
|
->method('remove')
|
2017-01-03 11:57:52 +03:00
|
|
|
->with($this->logicalOr($this->equalTo('two_factor_auth_error'), $this->equalTo('two_factor_auth_error_message')));
|
2016-05-11 12:23:25 +03:00
|
|
|
$provider->expects($this->once())
|
|
|
|
->method('getTemplate')
|
|
|
|
->with($user)
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($tmpl);
|
2016-05-11 12:23:25 +03:00
|
|
|
$tmpl->expects($this->once())
|
|
|
|
->method('fetchPage')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('<html/>');
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2019-02-04 10:54:56 +03:00
|
|
|
$expected = new StandaloneTemplateResponse('core', 'twofactorshowchallenge', [
|
2016-05-11 12:23:25 +03:00
|
|
|
'error' => true,
|
|
|
|
'provider' => $provider,
|
2016-08-29 20:19:44 +03:00
|
|
|
'backupProvider' => $backupProvider,
|
2017-08-18 13:16:43 +03:00
|
|
|
'logout_url' => 'logoutAttribute',
|
2016-05-11 12:23:25 +03:00
|
|
|
'template' => '<html/>',
|
2016-12-23 22:53:26 +03:00
|
|
|
'redirect_url' => '/re/dir/ect/url',
|
2016-11-15 10:51:07 +03:00
|
|
|
'error_message' => null,
|
2019-04-05 19:21:08 +03:00
|
|
|
], 'guest');
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2016-06-01 14:54:08 +03:00
|
|
|
$this->assertEquals($expected, $this->controller->showChallenge('myprovider', '/re/dir/ect/url'));
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testShowInvalidChallenge() {
|
2017-01-03 11:57:52 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
2018-05-22 09:52:16 +03:00
|
|
|
$providerSet = new ProviderSet([], false);
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($user);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->twoFactorManager->expects($this->once())
|
2018-05-22 09:52:16 +03:00
|
|
|
->method('getProviderSet')
|
|
|
|
->with($user)
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($providerSet);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with('core.TwoFactorChallenge.selectChallenge')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('select/challenge/url');
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2017-01-03 11:57:52 +03:00
|
|
|
$expected = new RedirectResponse('select/challenge/url');
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2016-06-01 14:54:08 +03:00
|
|
|
$this->assertEquals($expected, $this->controller->showChallenge('myprovider', 'redirect/url'));
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSolveChallenge() {
|
2017-01-03 11:57:52 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$provider = $this->createMock(IProvider::class);
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($user);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('getProvider')
|
|
|
|
->with($user, 'myprovider')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($provider);
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('verifyChallenge')
|
|
|
|
->with('myprovider', $user, 'token')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2017-01-03 11:57:52 +03:00
|
|
|
$expected = new RedirectResponse(OC_Util::getDefaultPageUrl());
|
2016-08-30 14:28:04 +03:00
|
|
|
$this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token'));
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
2017-01-03 11:57:52 +03:00
|
|
|
public function testSolveValidChallengeAndRedirect() {
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$provider = $this->createMock(IProvider::class);
|
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($user);
|
2017-01-03 11:57:52 +03:00
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('getProvider')
|
|
|
|
->with($user, 'myprovider')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($provider);
|
2017-01-03 11:57:52 +03:00
|
|
|
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('verifyChallenge')
|
|
|
|
->with('myprovider', $user, 'token')
|
|
|
|
->willReturn(true);
|
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('getAbsoluteURL')
|
|
|
|
->with('redirect url')
|
|
|
|
->willReturn('redirect/url');
|
|
|
|
|
|
|
|
$expected = new RedirectResponse('redirect/url');
|
|
|
|
$this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token', 'redirect%20url'));
|
|
|
|
}
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
public function testSolveChallengeInvalidProvider() {
|
2017-01-03 11:57:52 +03:00
|
|
|
$user = $this->getMockBuilder(IUser::class)->getMock();
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($user);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('getProvider')
|
|
|
|
->with($user, 'myprovider')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(null);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with('core.TwoFactorChallenge.selectChallenge')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('select/challenge/url');
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2017-01-03 11:57:52 +03:00
|
|
|
$expected = new RedirectResponse('select/challenge/url');
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
$this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSolveInvalidChallenge() {
|
2017-01-03 11:57:52 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$provider = $this->createMock(IProvider::class);
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($user);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('getProvider')
|
|
|
|
->with($user, 'myprovider')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($provider);
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('verifyChallenge')
|
|
|
|
->with('myprovider', $user, 'token')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(false);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('set')
|
|
|
|
->with('two_factor_auth_error', true);
|
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with('core.TwoFactorChallenge.showChallenge', [
|
|
|
|
'challengeProviderId' => 'myprovider',
|
2016-06-01 14:54:08 +03:00
|
|
|
'redirect_url' => '/url',
|
2016-05-11 12:23:25 +03:00
|
|
|
])
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('files/index/url');
|
2016-05-11 12:23:25 +03:00
|
|
|
$provider->expects($this->once())
|
|
|
|
->method('getId')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('myprovider');
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2017-01-03 11:57:52 +03:00
|
|
|
$expected = new RedirectResponse('files/index/url');
|
|
|
|
$this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token', '/url'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSolveChallengeTwoFactorException() {
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$provider = $this->createMock(IProvider::class);
|
|
|
|
$exception = new TwoFactorException("2FA failed");
|
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($user);
|
2017-01-03 11:57:52 +03:00
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('getProvider')
|
|
|
|
->with($user, 'myprovider')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($provider);
|
2017-01-03 11:57:52 +03:00
|
|
|
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('verifyChallenge')
|
|
|
|
->with('myprovider', $user, 'token')
|
|
|
|
->will($this->throwException($exception));
|
|
|
|
$this->session->expects($this->at(0))
|
|
|
|
->method('set')
|
|
|
|
->with('two_factor_auth_error_message', "2FA failed");
|
|
|
|
$this->session->expects($this->at(1))
|
|
|
|
->method('set')
|
|
|
|
->with('two_factor_auth_error', true);
|
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with('core.TwoFactorChallenge.showChallenge', [
|
|
|
|
'challengeProviderId' => 'myprovider',
|
|
|
|
'redirect_url' => '/url',
|
|
|
|
])
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('files/index/url');
|
2017-01-03 11:57:52 +03:00
|
|
|
$provider->expects($this->once())
|
|
|
|
->method('getId')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('myprovider');
|
2017-01-03 11:57:52 +03:00
|
|
|
|
|
|
|
$expected = new RedirectResponse('files/index/url');
|
2016-06-01 14:54:08 +03:00
|
|
|
$this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token', '/url'));
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
2019-04-05 19:21:08 +03:00
|
|
|
public function testSetUpProviders() {
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($user);
|
2019-04-05 19:21:08 +03:00
|
|
|
$provider = $this->createMock(IActivatableAtLogin::class);
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('getLoginSetupProviders')
|
|
|
|
->with($user)
|
|
|
|
->willReturn([
|
|
|
|
$provider,
|
|
|
|
]);
|
|
|
|
$expected = new StandaloneTemplateResponse(
|
|
|
|
'core',
|
|
|
|
'twofactorsetupselection',
|
|
|
|
[
|
|
|
|
'providers' => [
|
|
|
|
$provider,
|
|
|
|
],
|
|
|
|
'logout_url' => 'logoutAttribute',
|
|
|
|
],
|
|
|
|
'guest'
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->controller->setupProviders();
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetUpInvalidProvider() {
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($user);
|
2019-04-05 19:21:08 +03:00
|
|
|
$provider = $this->createMock(IActivatableAtLogin::class);
|
|
|
|
$provider->expects($this->any())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn('prov1');
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('getLoginSetupProviders')
|
|
|
|
->with($user)
|
|
|
|
->willReturn([
|
|
|
|
$provider,
|
|
|
|
]);
|
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with('core.TwoFactorChallenge.selectChallenge')
|
|
|
|
->willReturn('2fa/select/page');
|
|
|
|
$expected = new RedirectResponse('2fa/select/page');
|
|
|
|
|
|
|
|
$response = $this->controller->setupProvider('prov2');
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetUpProvider() {
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($user);
|
2019-04-05 19:21:08 +03:00
|
|
|
$provider = $this->createMock(IActivatableAtLogin::class);
|
|
|
|
$provider->expects($this->any())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn('prov1');
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('getLoginSetupProviders')
|
|
|
|
->with($user)
|
|
|
|
->willReturn([
|
|
|
|
$provider,
|
|
|
|
]);
|
|
|
|
$loginSetup = $this->createMock(ILoginSetupProvider::class);
|
|
|
|
$provider->expects($this->any())
|
|
|
|
->method('getLoginSetup')
|
|
|
|
->with($user)
|
|
|
|
->willReturn($loginSetup);
|
|
|
|
$tmpl = $this->createMock(Template::class);
|
|
|
|
$loginSetup->expects($this->once())
|
|
|
|
->method('getBody')
|
|
|
|
->willReturn($tmpl);
|
|
|
|
$tmpl->expects($this->once())
|
|
|
|
->method('fetchPage')
|
|
|
|
->willReturn('tmpl');
|
|
|
|
$expected = new StandaloneTemplateResponse(
|
|
|
|
'core',
|
|
|
|
'twofactorsetupchallenge',
|
|
|
|
[
|
|
|
|
'provider' => $provider,
|
|
|
|
'logout_url' => 'logoutAttribute',
|
|
|
|
'template' => 'tmpl',
|
|
|
|
],
|
|
|
|
'guest'
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->controller->setupProvider('prov1');
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConfirmProviderSetup() {
|
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with(
|
|
|
|
'core.TwoFactorChallenge.showChallenge',
|
|
|
|
[
|
|
|
|
'challengeProviderId' => 'totp',
|
|
|
|
])
|
|
|
|
->willReturn('2fa/select/page');
|
|
|
|
$expected = new RedirectResponse('2fa/select/page');
|
|
|
|
|
|
|
|
$response = $this->controller->confirmProviderSetup('totp');
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $response);
|
|
|
|
}
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|