2019-08-07 00:15:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2022-09-26 15:22:26 +02:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2019-08-07 00:15:54 +02:00
|
|
|
*
|
2022-09-26 15:22:26 +02:00
|
|
|
* https://www.d3data.de
|
2019-08-07 00:15:54 +02:00
|
|
|
*
|
|
|
|
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
2022-09-26 15:22:26 +02:00
|
|
|
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
|
|
|
|
* @link https://www.oxidmodule.com
|
2019-08-07 00:15:54 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace D3\Totp\tests\unit\Modules\Application\Component;
|
|
|
|
|
|
|
|
use D3\Totp\Application\Model\d3totp;
|
|
|
|
use D3\Totp\Application\Model\Exceptions\d3totp_wrongOtpException;
|
|
|
|
use D3\Totp\Modules\Application\Component\d3_totp_UserComponent;
|
|
|
|
use D3\Totp\tests\unit\d3TotpUnitTestCase;
|
2022-11-09 12:03:16 +01:00
|
|
|
use InvalidArgumentException;
|
2019-08-07 00:15:54 +02:00
|
|
|
use OxidEsales\Eshop\Application\Component\UserComponent;
|
|
|
|
use OxidEsales\Eshop\Application\Model\User;
|
|
|
|
use OxidEsales\Eshop\Core\Controller\BaseController;
|
|
|
|
use OxidEsales\Eshop\Core\Registry;
|
|
|
|
use OxidEsales\Eshop\Core\Session;
|
2022-11-09 10:18:31 +01:00
|
|
|
use OxidEsales\Eshop\Core\Utils;
|
2019-08-07 00:15:54 +02:00
|
|
|
use OxidEsales\Eshop\Core\UtilsView;
|
2022-09-28 00:08:36 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2019-08-07 00:15:54 +02:00
|
|
|
use ReflectionException;
|
|
|
|
|
|
|
|
class d3_totp_UserComponentTest extends d3TotpUnitTestCase
|
|
|
|
{
|
|
|
|
/** @var d3_totp_UserComponent */
|
|
|
|
protected $_oController;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* setup basic requirements
|
|
|
|
*/
|
2022-09-28 00:08:36 +02:00
|
|
|
public function setUp(): void
|
2019-08-07 00:15:54 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->_oController = oxNew(UserComponent::class);
|
|
|
|
|
|
|
|
Registry::getSession()->setVariable(d3totp::TOTP_SESSION_VARNAME, false);
|
|
|
|
}
|
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
public function tearDown(): void
|
2019-08-07 00:15:54 +02:00
|
|
|
{
|
|
|
|
parent::tearDown();
|
|
|
|
|
|
|
|
unset($this->_oController);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-11-09 12:03:16 +01:00
|
|
|
* @covers \D3\Totp\Modules\Application\Component\d3_totp_UserComponent::_afterLogin
|
2019-08-07 00:15:54 +02:00
|
|
|
*/
|
2022-11-09 12:03:16 +01:00
|
|
|
public function afterLoginFailsIfNoUserLoggedIn()
|
2019-08-07 00:15:54 +02:00
|
|
|
{
|
|
|
|
$oUser = false;
|
|
|
|
|
2022-11-09 10:18:31 +01:00
|
|
|
/** @var Utils|MockObject $oUtilsMock */
|
|
|
|
$oUtilsMock = $this->getMockBuilder(Utils::class)
|
|
|
|
->onlyMethods(['redirect'])
|
|
|
|
->getMock();
|
|
|
|
$oUtilsMock->expects($this->never())->method('redirect')->willReturn(true);
|
|
|
|
|
|
|
|
/** @var Session|MockObject $oSessionMock */
|
|
|
|
$oSessionMock = $this->getMockBuilder(Session::class)
|
|
|
|
->onlyMethods(['setVariable'])
|
2022-09-28 00:08:36 +02:00
|
|
|
->getMock();
|
2022-11-09 10:18:31 +01:00
|
|
|
$oSessionMock->expects($this->never())->method('setVariable');
|
2019-08-07 00:15:54 +02:00
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var d3totp|MockObject $oTotpMock */
|
|
|
|
$oTotpMock = $this->getMockBuilder(d3totp::class)
|
|
|
|
->onlyMethods(['isActive'])
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2019-08-07 00:15:54 +02:00
|
|
|
$oTotpMock->expects($this->never())->method('isActive')->willReturn(false);
|
2022-09-30 21:06:30 +02:00
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var UserComponent|MockObject $oControllerMock */
|
|
|
|
$oControllerMock = $this->getMockBuilder(UserComponent::class)
|
|
|
|
->onlyMethods([
|
|
|
|
'd3GetTotpObject',
|
2022-11-09 12:03:16 +01:00
|
|
|
'd3GetSession',
|
|
|
|
'd3GetUtils',
|
2022-09-28 00:08:36 +02:00
|
|
|
])
|
|
|
|
->getMock();
|
2019-08-07 00:15:54 +02:00
|
|
|
$oControllerMock->method('d3GetTotpObject')->willReturn($oTotpMock);
|
2022-11-09 12:03:16 +01:00
|
|
|
$oControllerMock->method('d3GetSession')->willReturn($oSessionMock);
|
|
|
|
$oControllerMock->method('d3GetUtils')->willReturn($oUtilsMock);
|
2022-09-28 00:08:36 +02:00
|
|
|
|
2019-08-07 00:15:54 +02:00
|
|
|
$this->_oController = $oControllerMock;
|
|
|
|
|
2022-11-09 12:03:16 +01:00
|
|
|
$this->expectException( InvalidArgumentException::class);
|
|
|
|
|
|
|
|
$this->callMethod($this->_oController, '_afterLogin', [$oUser]);
|
2019-08-07 00:15:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-11-09 12:03:16 +01:00
|
|
|
* @covers \D3\Totp\Modules\Application\Component\d3_totp_UserComponent::_afterLogin
|
|
|
|
* @dataProvider afterLoginFailTotpNotActiveOrAlreadyCheckedDataProvider
|
2019-08-07 00:15:54 +02:00
|
|
|
*/
|
2022-11-09 12:03:16 +01:00
|
|
|
public function afterLoginFailTotpNotActiveOrAlreadyChecked($isActive, $checked)
|
2019-08-07 00:15:54 +02:00
|
|
|
{
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var User|MockObject $oUserMock */
|
|
|
|
$oUserMock = $this->getMockBuilder(User::class)
|
|
|
|
->onlyMethods([
|
|
|
|
'logout',
|
2022-09-30 21:06:30 +02:00
|
|
|
'getId',
|
2022-09-28 00:08:36 +02:00
|
|
|
])
|
|
|
|
->getMock();
|
2019-08-07 00:15:54 +02:00
|
|
|
$oUserMock->expects($this->never())->method('logout')->willReturn(false);
|
|
|
|
$oUserMock->method('getId')->willReturn('foo');
|
|
|
|
|
2022-11-09 10:18:31 +01:00
|
|
|
/** @var Utils|MockObject $oUtilsMock */
|
|
|
|
$oUtilsMock = $this->getMockBuilder(Utils::class)
|
|
|
|
->onlyMethods(['redirect'])
|
|
|
|
->getMock();
|
|
|
|
$oUtilsMock->expects($this->never())->method('redirect')->willReturn(true);
|
|
|
|
|
|
|
|
/** @var Session|MockObject $oSessionMock */
|
|
|
|
$oSessionMock = $this->getMockBuilder(Session::class)
|
|
|
|
->onlyMethods(['setVariable', 'getVariable'])
|
2022-09-28 00:08:36 +02:00
|
|
|
->getMock();
|
2022-11-09 10:18:31 +01:00
|
|
|
$oSessionMock->expects($this->never())->method('setVariable');
|
|
|
|
$oSessionMock->method('getVariable')->willReturn($checked);
|
2019-08-07 00:15:54 +02:00
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var d3totp|MockObject $oTotpMock */
|
|
|
|
$oTotpMock = $this->getMockBuilder(d3totp::class)
|
|
|
|
->onlyMethods([
|
|
|
|
'isActive',
|
2022-09-30 21:06:30 +02:00
|
|
|
'loadByUserId',
|
2022-09-28 00:08:36 +02:00
|
|
|
])
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2022-11-09 10:18:31 +01:00
|
|
|
$oTotpMock->expects($this->once())->method('isActive')->willReturn($isActive);
|
2019-08-11 00:33:59 +02:00
|
|
|
$oTotpMock->method('loadByUserId')->willReturn(true);
|
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var UserComponent|MockObject $oControllerMock */
|
|
|
|
$oControllerMock = $this->getMockBuilder(UserComponent::class)
|
|
|
|
->onlyMethods([
|
|
|
|
'd3GetTotpObject',
|
2022-11-09 12:03:16 +01:00
|
|
|
'd3GetSession',
|
|
|
|
'd3GetUtils',
|
2022-09-28 00:08:36 +02:00
|
|
|
])
|
|
|
|
->getMock();
|
2019-08-07 00:15:54 +02:00
|
|
|
$oControllerMock->method('d3GetTotpObject')->willReturn($oTotpMock);
|
2022-11-09 12:03:16 +01:00
|
|
|
$oControllerMock->method('d3GetSession')->willReturn($oSessionMock);
|
|
|
|
$oControllerMock->method('d3GetUtils')->willReturn($oUtilsMock);
|
2022-09-28 00:08:36 +02:00
|
|
|
|
2019-08-07 00:15:54 +02:00
|
|
|
$this->_oController = $oControllerMock;
|
|
|
|
|
2022-11-09 12:03:16 +01:00
|
|
|
$this->callMethod($this->_oController, '_afterLogin', [$oUserMock]);
|
2022-11-09 10:18:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2022-11-09 12:03:16 +01:00
|
|
|
public function afterLoginFailTotpNotActiveOrAlreadyCheckedDataProvider(): array
|
2022-11-09 10:18:31 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'TOTP not active, not checked' => [false, null],
|
|
|
|
'TOTP not active, checked' => [false, true],
|
|
|
|
'TOTP active, checked' => [true, true],
|
|
|
|
];
|
2019-08-07 00:15:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-11-09 12:03:16 +01:00
|
|
|
* @covers \D3\Totp\Modules\Application\Component\d3_totp_UserComponent::_afterLogin
|
2019-08-07 00:15:54 +02:00
|
|
|
*/
|
2022-11-09 12:03:16 +01:00
|
|
|
public function afterLoginPass()
|
2019-08-07 00:15:54 +02:00
|
|
|
{
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var User|MockObject $oUserMock */
|
|
|
|
$oUserMock = $this->getMockBuilder(User::class)
|
|
|
|
->onlyMethods([
|
|
|
|
'logout',
|
|
|
|
'getId',
|
|
|
|
])
|
|
|
|
->getMock();
|
2019-08-07 00:15:54 +02:00
|
|
|
$oUserMock->expects($this->once())->method('logout')->willReturn(false);
|
|
|
|
$oUserMock->method('getId')->willReturn('foo');
|
|
|
|
|
2022-11-09 10:18:31 +01:00
|
|
|
/** @var Utils|MockObject $oUtilsMock */
|
|
|
|
$oUtilsMock = $this->getMockBuilder(Utils::class)
|
|
|
|
->onlyMethods(['redirect'])
|
|
|
|
->getMock();
|
|
|
|
$oUtilsMock->expects($this->once())->method('redirect')->willReturn(true);
|
|
|
|
|
|
|
|
/** @var Session|MockObject $oSessionMock */
|
|
|
|
$oSessionMock = $this->getMockBuilder(Session::class)
|
|
|
|
->onlyMethods(['setVariable', 'getVariable'])
|
|
|
|
->getMock();
|
|
|
|
$oSessionMock->expects($this->atLeast(3))->method('setVariable');
|
|
|
|
$oSessionMock->method('getVariable')->willReturn(null);
|
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var BaseController|MockObject $oParentMock */
|
|
|
|
$oParentMock = $this->getMockBuilder(BaseController::class)
|
2022-11-09 10:18:31 +01:00
|
|
|
->onlyMethods(['getClassKey'])
|
2022-09-28 00:08:36 +02:00
|
|
|
->getMock();
|
2022-11-09 10:18:31 +01:00
|
|
|
$oParentMock->method('getClassKey')->willReturn('foo');
|
2019-08-07 00:15:54 +02:00
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var d3totp|MockObject $oTotpMock */
|
|
|
|
$oTotpMock = $this->getMockBuilder(d3totp::class)
|
|
|
|
->onlyMethods([
|
|
|
|
'isActive',
|
2022-09-30 21:06:30 +02:00
|
|
|
'loadByUserId',
|
2022-09-28 00:08:36 +02:00
|
|
|
])
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2019-08-07 00:15:54 +02:00
|
|
|
$oTotpMock->expects($this->once())->method('isActive')->willReturn(true);
|
2019-08-11 00:33:59 +02:00
|
|
|
$oTotpMock->method('loadByUserId')->willReturn(true);
|
2019-08-07 00:15:54 +02:00
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var UserComponent|MockObject $oControllerMock */
|
|
|
|
$oControllerMock = $this->getMockBuilder(UserComponent::class)
|
|
|
|
->onlyMethods([
|
|
|
|
'd3GetTotpObject',
|
2022-11-09 12:03:16 +01:00
|
|
|
'd3GetSession',
|
|
|
|
'd3GetUtils',
|
2022-11-09 10:18:31 +01:00
|
|
|
'getParent'
|
2022-09-28 00:08:36 +02:00
|
|
|
])
|
|
|
|
->getMock();
|
2019-08-07 00:15:54 +02:00
|
|
|
$oControllerMock->method('d3GetTotpObject')->willReturn($oTotpMock);
|
|
|
|
$oControllerMock->method('getParent')->willReturn($oParentMock);
|
2022-11-09 12:03:16 +01:00
|
|
|
$oControllerMock->method('d3GetSession')->willReturn($oSessionMock);
|
|
|
|
$oControllerMock->method('d3GetUtils')->willReturn($oUtilsMock);
|
2022-09-28 00:08:36 +02:00
|
|
|
|
2019-08-07 00:15:54 +02:00
|
|
|
$this->_oController = $oControllerMock;
|
|
|
|
|
2022-11-09 12:03:16 +01:00
|
|
|
$this->callMethod($this->_oController, '_afterLogin', [$oUserMock]);
|
2019-08-07 00:15:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-09-28 22:25:51 +02:00
|
|
|
* @covers \D3\Totp\Modules\Application\Component\d3_totp_UserComponent::d3GetTotpObject
|
2019-08-07 00:15:54 +02:00
|
|
|
*/
|
|
|
|
public function d3GetTotpObjectReturnsRightInstance()
|
|
|
|
{
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
d3totp::class,
|
|
|
|
$this->callMethod($this->_oController, 'd3GetTotpObject')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-11-09 12:03:16 +01:00
|
|
|
* @covers \D3\Totp\Modules\Application\Component\d3_totp_UserComponent::checkTotplogin
|
2019-08-07 00:15:54 +02:00
|
|
|
*/
|
2022-09-30 21:06:30 +02:00
|
|
|
public function checkTotploginNoTotpLogin()
|
2019-08-07 00:15:54 +02:00
|
|
|
{
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var d3totp|MockObject $oTotpMock */
|
|
|
|
$oTotpMock = $this->getMockBuilder(d3totp::class)
|
|
|
|
->onlyMethods(['loadByUserId'])
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2019-08-11 00:33:59 +02:00
|
|
|
$oTotpMock->method('loadByUserId')->willReturn(true);
|
|
|
|
|
2022-11-09 10:18:31 +01:00
|
|
|
/** @var Session|MockObject $oSessionMock */
|
|
|
|
$oSessionMock = $this->getMockBuilder(Session::class)
|
|
|
|
->onlyMethods(['setVariable'])
|
|
|
|
->getMock();
|
|
|
|
$oSessionMock->expects($this->never())->method('setVariable');
|
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var UserComponent|MockObject $oControllerMock */
|
|
|
|
$oControllerMock = $this->getMockBuilder(UserComponent::class)
|
|
|
|
->onlyMethods([
|
2022-11-09 12:03:16 +01:00
|
|
|
'isNoTotpOrNoLogin',
|
|
|
|
'hasValidTotp',
|
2022-09-30 21:06:30 +02:00
|
|
|
'd3GetTotpObject',
|
2022-11-09 12:03:16 +01:00
|
|
|
'd3GetSession',
|
2022-09-28 00:08:36 +02:00
|
|
|
])
|
|
|
|
->getMock();
|
2022-11-09 12:03:16 +01:00
|
|
|
$oControllerMock->method('isNoTotpOrNoLogin')->willReturn(true);
|
|
|
|
$oControllerMock->expects($this->never())->method('hasValidTotp')->willReturn(false);
|
2019-08-11 00:33:59 +02:00
|
|
|
$oControllerMock->method('d3GetTotpObject')->willReturn($oTotpMock);
|
2022-11-09 12:03:16 +01:00
|
|
|
$oControllerMock->method('d3GetSession')->willReturn($oSessionMock);
|
2019-08-07 00:15:54 +02:00
|
|
|
|
|
|
|
$this->_oController = $oControllerMock;
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
'd3totplogin',
|
2022-11-09 12:03:16 +01:00
|
|
|
$this->callMethod($this->_oController, 'checkTotplogin')
|
2019-08-07 00:15:54 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-11-09 12:03:16 +01:00
|
|
|
* @covers \D3\Totp\Modules\Application\Component\d3_totp_UserComponent::checkTotplogin
|
2019-08-07 00:15:54 +02:00
|
|
|
*/
|
2022-09-30 21:06:30 +02:00
|
|
|
public function checkTotploginUnvalidTotp()
|
2019-08-07 00:15:54 +02:00
|
|
|
{
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var d3totp|MockObject $oTotpMock */
|
|
|
|
$oTotpMock = $this->getMockBuilder(d3totp::class)
|
|
|
|
->onlyMethods(['loadByUserId'])
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2019-08-11 00:33:59 +02:00
|
|
|
$oTotpMock->method('loadByUserId')->willReturn(true);
|
|
|
|
|
2022-11-09 10:18:31 +01:00
|
|
|
/** @var Session|MockObject $oSessionMock */
|
|
|
|
$oSessionMock = $this->getMockBuilder(Session::class)
|
|
|
|
->onlyMethods(['setVariable'])
|
|
|
|
->getMock();
|
|
|
|
$oSessionMock->expects($this->never())->method('setVariable');
|
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var d3totp_wrongOtpException|MockObject $oUtilsViewMock */
|
|
|
|
$oTotpExceptionMock = $this->getMockBuilder(d3totp_wrongOtpException::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2019-08-11 00:33:59 +02:00
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var UtilsView|MockObject $oUtilsViewMock */
|
|
|
|
$oUtilsViewMock = $this->getMockBuilder(UtilsView::class)
|
|
|
|
->onlyMethods(['addErrorToDisplay'])
|
|
|
|
->getMock();
|
2019-08-07 00:15:54 +02:00
|
|
|
$oUtilsViewMock->expects($this->atLeast(1))->method('addErrorToDisplay')->willReturn(true);
|
2022-09-30 21:06:30 +02:00
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var UserComponent|MockObject $oControllerMock */
|
|
|
|
$oControllerMock = $this->getMockBuilder(UserComponent::class)
|
|
|
|
->onlyMethods([
|
2022-11-09 12:03:16 +01:00
|
|
|
'isNoTotpOrNoLogin',
|
|
|
|
'hasValidTotp',
|
|
|
|
'd3GetUtilsView',
|
2022-09-30 21:06:30 +02:00
|
|
|
'd3GetTotpObject',
|
2022-11-09 12:03:16 +01:00
|
|
|
'd3GetSession',
|
2022-09-28 00:08:36 +02:00
|
|
|
])
|
|
|
|
->getMock();
|
2022-11-09 12:03:16 +01:00
|
|
|
$oControllerMock->method('isNoTotpOrNoLogin')->willReturn(false);
|
|
|
|
$oControllerMock->expects($this->once())->method('hasValidTotp')->willThrowException($oTotpExceptionMock);
|
|
|
|
$oControllerMock->method('d3GetUtilsView')->willReturn($oUtilsViewMock);
|
2019-08-11 00:33:59 +02:00
|
|
|
$oControllerMock->method('d3GetTotpObject')->willReturn($oTotpMock);
|
2022-11-09 12:03:16 +01:00
|
|
|
$oControllerMock->method('d3GetSession')->willReturn($oSessionMock);
|
2019-08-07 00:15:54 +02:00
|
|
|
|
|
|
|
$this->_oController = $oControllerMock;
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
'd3totplogin',
|
2022-11-09 12:03:16 +01:00
|
|
|
$this->callMethod($this->_oController, 'checkTotplogin')
|
2019-08-07 00:15:54 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-11-09 12:03:16 +01:00
|
|
|
* @covers \D3\Totp\Modules\Application\Component\d3_totp_UserComponent::checkTotplogin
|
2019-08-07 00:15:54 +02:00
|
|
|
*/
|
2022-09-30 21:06:30 +02:00
|
|
|
public function checkTotploginValidTotp()
|
2019-08-07 00:15:54 +02:00
|
|
|
{
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var d3totp|MockObject $oTotpMock */
|
|
|
|
$oTotpMock = $this->getMockBuilder(d3totp::class)
|
|
|
|
->onlyMethods(['loadByUserId'])
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2019-08-11 00:33:59 +02:00
|
|
|
$oTotpMock->method('loadByUserId')->willReturn(true);
|
|
|
|
|
2022-11-09 10:18:31 +01:00
|
|
|
/** @var Session|MockObject $oSessionMock */
|
|
|
|
$oSessionMock = $this->getMockBuilder(Session::class)
|
|
|
|
->onlyMethods(['setVariable'])
|
|
|
|
->getMock();
|
|
|
|
$oSessionMock->expects($this->atLeast(2))->method('setVariable');
|
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var UtilsView|MockObject $oUtilsViewMock */
|
|
|
|
$oUtilsViewMock = $this->getMockBuilder(UtilsView::class)
|
|
|
|
->onlyMethods(['addErrorToDisplay'])
|
|
|
|
->getMock();
|
2019-08-07 00:15:54 +02:00
|
|
|
$oUtilsViewMock->expects($this->never())->method('addErrorToDisplay')->willReturn(true);
|
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var UserComponent|MockObject $oControllerMock */
|
|
|
|
$oControllerMock = $this->getMockBuilder(UserComponent::class)
|
|
|
|
->onlyMethods([
|
2022-11-09 12:03:16 +01:00
|
|
|
'isNoTotpOrNoLogin',
|
|
|
|
'hasValidTotp',
|
|
|
|
'd3GetUtilsView',
|
2022-09-30 21:06:30 +02:00
|
|
|
'd3GetTotpObject',
|
2022-11-09 12:03:16 +01:00
|
|
|
'd3GetSession',
|
2022-11-09 10:18:31 +01:00
|
|
|
'setLoginStatus'
|
2022-09-28 00:08:36 +02:00
|
|
|
])
|
|
|
|
->getMock();
|
2022-11-09 12:03:16 +01:00
|
|
|
$oControllerMock->method('isNoTotpOrNoLogin')->willReturn(false);
|
|
|
|
$oControllerMock->expects($this->once())->method('hasValidTotp')->willReturn(true);
|
|
|
|
$oControllerMock->method('d3GetUtilsView')->willReturn($oUtilsViewMock);
|
2019-08-11 00:33:59 +02:00
|
|
|
$oControllerMock->method('d3GetTotpObject')->willReturn($oTotpMock);
|
2022-11-09 12:03:16 +01:00
|
|
|
$oControllerMock->method('d3GetSession')->willReturn($oSessionMock);
|
2022-11-09 10:18:31 +01:00
|
|
|
$oControllerMock->expects($this->once())->method('setLoginStatus')->with(
|
|
|
|
$this->identicalTo(USER_LOGIN_SUCCESS)
|
|
|
|
);
|
2019-08-07 00:15:54 +02:00
|
|
|
|
|
|
|
$this->_oController = $oControllerMock;
|
|
|
|
|
|
|
|
$this->assertFalse(
|
2022-11-09 12:03:16 +01:00
|
|
|
$this->callMethod($this->_oController, 'checkTotplogin')
|
2019-08-07 00:15:54 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-11-09 12:03:16 +01:00
|
|
|
* @covers \D3\Totp\Modules\Application\Component\d3_totp_UserComponent::d3GetUtilsView
|
2019-08-07 00:15:54 +02:00
|
|
|
*/
|
|
|
|
public function d3GetUtilsViewReturnsRightInstance()
|
|
|
|
{
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
UtilsView::class,
|
2022-11-09 12:03:16 +01:00
|
|
|
$this->callMethod($this->_oController, 'd3GetUtilsView')
|
2019-08-07 00:15:54 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-11-09 12:03:16 +01:00
|
|
|
* @covers \D3\Totp\Modules\Application\Component\d3_totp_UserComponent::cancelTotpLogin
|
2019-08-07 00:15:54 +02:00
|
|
|
*/
|
|
|
|
public function canCancelTotpLogin()
|
|
|
|
{
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var UserComponent|MockObject $oControllerMock */
|
|
|
|
$oControllerMock = $this->getMockBuilder(UserComponent::class)
|
|
|
|
->onlyMethods(['d3TotpClearSessionVariables'])
|
|
|
|
->getMock();
|
2019-08-07 00:15:54 +02:00
|
|
|
$oControllerMock->expects($this->once())->method('d3TotpClearSessionVariables')->willReturn(false);
|
|
|
|
|
|
|
|
$this->_oController = $oControllerMock;
|
|
|
|
|
2022-11-09 12:03:16 +01:00
|
|
|
$this->callMethod($this->_oController, 'cancelTotpLogin');
|
2019-08-07 00:15:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-11-09 12:03:16 +01:00
|
|
|
* @covers \D3\Totp\Modules\Application\Component\d3_totp_UserComponent::isNoTotpOrNoLogin
|
2019-08-07 00:15:54 +02:00
|
|
|
*/
|
|
|
|
public function isNoTotpOrNoLoginTrueNoSessionVariable()
|
|
|
|
{
|
|
|
|
Registry::getSession()->setVariable(d3totp::TOTP_SESSION_CURRENTUSER, false);
|
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var d3totp|MockObject $oTotpMock */
|
|
|
|
$oTotpMock = $this->getMockBuilder(d3totp::class)
|
|
|
|
->onlyMethods(['isActive'])
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2019-08-07 00:15:54 +02:00
|
|
|
$oTotpMock->method('isActive')->willReturn(true);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
2022-11-09 12:03:16 +01:00
|
|
|
$this->callMethod($this->_oController, 'isNoTotpOrNoLogin', [$oTotpMock])
|
2019-08-07 00:15:54 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-11-09 12:03:16 +01:00
|
|
|
* @covers \D3\Totp\Modules\Application\Component\d3_totp_UserComponent::isNoTotpOrNoLogin
|
2019-08-07 00:15:54 +02:00
|
|
|
*/
|
|
|
|
public function isNoTotpOrNoLoginTrueTotpNotActive()
|
|
|
|
{
|
|
|
|
Registry::getSession()->setVariable(d3totp::TOTP_SESSION_CURRENTUSER, true);
|
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var d3totp|MockObject $oTotpMock */
|
|
|
|
$oTotpMock = $this->getMockBuilder(d3totp::class)
|
|
|
|
->onlyMethods(['isActive'])
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2019-08-07 00:15:54 +02:00
|
|
|
$oTotpMock->method('isActive')->willReturn(false);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
2022-11-09 12:03:16 +01:00
|
|
|
$this->callMethod($this->_oController, 'isNoTotpOrNoLogin', [$oTotpMock])
|
2019-08-07 00:15:54 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-11-09 12:03:16 +01:00
|
|
|
* @covers \D3\Totp\Modules\Application\Component\d3_totp_UserComponent::isNoTotpOrNoLogin
|
2019-08-07 00:15:54 +02:00
|
|
|
*/
|
|
|
|
public function isNoTotpOrNoLoginFalse()
|
|
|
|
{
|
|
|
|
Registry::getSession()->setVariable(d3totp::TOTP_SESSION_CURRENTUSER, true);
|
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var d3totp|MockObject $oTotpMock */
|
|
|
|
$oTotpMock = $this->getMockBuilder(d3totp::class)
|
|
|
|
->onlyMethods(['isActive'])
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2019-08-07 00:15:54 +02:00
|
|
|
$oTotpMock->method('isActive')->willReturn(true);
|
|
|
|
|
|
|
|
$this->assertFalse(
|
2022-11-09 12:03:16 +01:00
|
|
|
$this->callMethod($this->_oController, 'isNoTotpOrNoLogin', [$oTotpMock])
|
2019-08-07 00:15:54 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-11-09 12:03:16 +01:00
|
|
|
* @covers \D3\Totp\Modules\Application\Component\d3_totp_UserComponent::hasValidTotp
|
2019-08-07 00:15:54 +02:00
|
|
|
*/
|
|
|
|
public function hasValidTotpTrueSessionVarname()
|
|
|
|
{
|
|
|
|
Registry::getSession()->setVariable(d3totp::TOTP_SESSION_VARNAME, true);
|
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var d3totp|MockObject $oTotpMock */
|
|
|
|
$oTotpMock = $this->getMockBuilder(d3totp::class)
|
|
|
|
->onlyMethods(['verify'])
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2019-08-07 00:15:54 +02:00
|
|
|
$oTotpMock->method('verify')->willReturn(false);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
2022-11-09 12:03:16 +01:00
|
|
|
$this->callMethod($this->_oController, 'hasValidTotp', ['123456', $oTotpMock])
|
2019-08-07 00:15:54 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-11-09 12:03:16 +01:00
|
|
|
* @covers \D3\Totp\Modules\Application\Component\d3_totp_UserComponent::hasValidTotp
|
2019-08-07 00:15:54 +02:00
|
|
|
*/
|
|
|
|
public function hasValidTotpTrueValidTotp()
|
|
|
|
{
|
|
|
|
Registry::getSession()->setVariable(d3totp::TOTP_SESSION_VARNAME, false);
|
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var d3totp|MockObject $oTotpMock */
|
|
|
|
$oTotpMock = $this->getMockBuilder(d3totp::class)
|
|
|
|
->onlyMethods(['verify'])
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2019-08-07 00:15:54 +02:00
|
|
|
$oTotpMock->method('verify')->willReturn(true);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
2022-11-09 12:03:16 +01:00
|
|
|
$this->callMethod($this->_oController, 'hasValidTotp', ['123456', $oTotpMock])
|
2019-08-07 00:15:54 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-11-09 12:03:16 +01:00
|
|
|
* @covers \D3\Totp\Modules\Application\Component\d3_totp_UserComponent::hasValidTotp
|
2019-08-07 00:15:54 +02:00
|
|
|
*/
|
|
|
|
public function hasValidTotpFalseMissingTotp()
|
|
|
|
{
|
|
|
|
Registry::getSession()->setVariable(d3totp::TOTP_SESSION_VARNAME, false);
|
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var d3totp|MockObject $oTotpMock */
|
|
|
|
$oTotpMock = $this->getMockBuilder(d3totp::class)
|
|
|
|
->onlyMethods(['verify'])
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2019-08-07 00:15:54 +02:00
|
|
|
$oTotpMock->method('verify')->willReturn(true);
|
|
|
|
|
|
|
|
$this->assertFalse(
|
2022-11-09 12:03:16 +01:00
|
|
|
$this->callMethod($this->_oController, 'hasValidTotp', [null, $oTotpMock])
|
2019-08-07 00:15:54 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-11-09 12:03:16 +01:00
|
|
|
* @covers \D3\Totp\Modules\Application\Component\d3_totp_UserComponent::hasValidTotp
|
2019-08-07 00:15:54 +02:00
|
|
|
*/
|
|
|
|
public function hasValidTotpFalseUnverifiedTotp()
|
|
|
|
{
|
|
|
|
Registry::getSession()->setVariable(d3totp::TOTP_SESSION_VARNAME, false);
|
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var d3totp|MockObject $oTotpMock */
|
|
|
|
$oTotpMock = $this->getMockBuilder(d3totp::class)
|
|
|
|
->onlyMethods(['verify'])
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2019-08-07 00:15:54 +02:00
|
|
|
$oTotpMock->method('verify')->willReturn(false);
|
|
|
|
|
|
|
|
$this->assertFalse(
|
2022-11-09 12:03:16 +01:00
|
|
|
$this->callMethod($this->_oController, 'hasValidTotp', ['123456', $oTotpMock])
|
2019-08-07 00:15:54 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-09-28 22:25:51 +02:00
|
|
|
* @covers \D3\Totp\Modules\Application\Component\d3_totp_UserComponent::d3TotpClearSessionVariables
|
2019-08-07 00:15:54 +02:00
|
|
|
*/
|
|
|
|
public function d3TotpClearSessionVariablesPass()
|
|
|
|
{
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var Session|MockObject $oSessionMock */
|
|
|
|
$oSessionMock = $this->getMockBuilder(Session::class)
|
|
|
|
->onlyMethods(['deleteVariable'])
|
|
|
|
->getMock();
|
2019-08-07 00:15:54 +02:00
|
|
|
$oSessionMock->expects($this->atLeast(3))->method('deleteVariable')->willReturn(false);
|
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var UserComponent|MockObject $oControllerMock */
|
|
|
|
$oControllerMock = $this->getMockBuilder(UserComponent::class)
|
2022-11-09 12:03:16 +01:00
|
|
|
->onlyMethods(['d3GetSession'])
|
2022-09-28 00:08:36 +02:00
|
|
|
->getMock();
|
2022-11-09 12:03:16 +01:00
|
|
|
$oControllerMock->method('d3GetSession')->willReturn($oSessionMock);
|
2019-08-07 00:15:54 +02:00
|
|
|
|
|
|
|
$this->_oController = $oControllerMock;
|
|
|
|
|
|
|
|
$this->callMethod($this->_oController, 'd3TotpClearSessionVariables');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-11-09 12:03:16 +01:00
|
|
|
* @covers \D3\Totp\Modules\Application\Component\d3_totp_UserComponent::d3GetSession
|
2019-08-07 00:15:54 +02:00
|
|
|
*/
|
|
|
|
public function d3GetSessionReturnsRightInstance()
|
|
|
|
{
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
Session::class,
|
2022-11-09 12:03:16 +01:00
|
|
|
$this->callMethod($this->_oController, 'd3GetSession')
|
2019-08-07 00:15:54 +02:00
|
|
|
);
|
|
|
|
}
|
2022-09-30 21:06:30 +02:00
|
|
|
}
|