2019-08-11 23:49:49 +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-11 23:49:49 +02:00
|
|
|
*
|
2022-09-26 15:22:26 +02:00
|
|
|
* https://www.d3data.de
|
2019-08-11 23:49:49 +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-11 23:49:49 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace D3\Totp\tests\unit\Application\Model;
|
|
|
|
|
|
|
|
use D3\Totp\Application\Model\d3backupcode;
|
|
|
|
use D3\Totp\Application\Model\d3totp;
|
2022-11-10 00:00:50 +01:00
|
|
|
use D3\Totp\Application\Model\d3totp_conf;
|
2019-08-11 23:49:49 +02:00
|
|
|
use D3\Totp\tests\unit\d3TotpUnitTestCase;
|
|
|
|
use OxidEsales\Eshop\Application\Model\User;
|
|
|
|
use OxidEsales\Eshop\Core\Registry;
|
2022-09-28 00:08:36 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2019-08-11 23:49:49 +02:00
|
|
|
use ReflectionException;
|
|
|
|
|
|
|
|
class d3backupcodeTest extends d3TotpUnitTestCase
|
|
|
|
{
|
|
|
|
/** @var d3backupcode */
|
|
|
|
protected $_oModel;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* setup basic requirements
|
|
|
|
*/
|
2022-09-28 00:08:36 +02:00
|
|
|
public function setUp(): void
|
2019-08-11 23:49:49 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->_oModel = oxNew(d3backupcode::class);
|
|
|
|
}
|
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
public function tearDown(): void
|
2019-08-11 23:49:49 +02:00
|
|
|
{
|
|
|
|
parent::tearDown();
|
|
|
|
|
|
|
|
unset($this->_oModel);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-09-28 22:25:51 +02:00
|
|
|
* @covers \D3\Totp\Application\Model\d3backupcode::generateCode
|
2019-08-11 23:49:49 +02:00
|
|
|
*/
|
|
|
|
public function generateCodePass()
|
|
|
|
{
|
|
|
|
$sTestUserId = 'testUserId';
|
|
|
|
$sBackupCode = '123456';
|
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var d3backupcode|MockObject $oModelMock */
|
|
|
|
$oModelMock = $this->getMockBuilder(d3backupcode::class)
|
|
|
|
->onlyMethods([
|
|
|
|
'getRandomTotpBackupCode',
|
2022-09-30 21:06:30 +02:00
|
|
|
'd3EncodeBC',
|
2022-09-28 00:08:36 +02:00
|
|
|
])
|
|
|
|
->getMock();
|
2019-08-11 23:49:49 +02:00
|
|
|
$oModelMock->method('getRandomTotpBackupCode')->willReturn($sBackupCode);
|
|
|
|
$oModelMock->method('d3EncodeBC')->will(
|
|
|
|
$this->returnCallback(function ($arg) {
|
|
|
|
return $arg;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->_oModel = $oModelMock;
|
|
|
|
|
2022-09-30 21:06:30 +02:00
|
|
|
$this->callMethod($this->_oModel, 'generateCode', [$sTestUserId]);
|
2019-08-11 23:49:49 +02:00
|
|
|
|
|
|
|
$this->assertSame($sTestUserId, $this->_oModel->getFieldData('oxuserid'));
|
|
|
|
$this->assertSame($sBackupCode, $this->_oModel->getFieldData('backupcode'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-09-28 22:25:51 +02:00
|
|
|
* @covers \D3\Totp\Application\Model\d3backupcode::getRandomTotpBackupCode
|
2019-08-11 23:49:49 +02:00
|
|
|
*/
|
|
|
|
public function getRandomTotpBackupCodePass()
|
|
|
|
{
|
|
|
|
$this->assertRegExp(
|
|
|
|
'@[0-9]{6}@',
|
|
|
|
$this->callMethod($this->_oModel, 'getRandomTotpBackupCode')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-09-28 22:25:51 +02:00
|
|
|
* @covers \D3\Totp\Application\Model\d3backupcode::d3EncodeBC
|
2019-08-11 23:49:49 +02:00
|
|
|
*/
|
|
|
|
public function d3EncodeBCPass()
|
|
|
|
{
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var User|MockObject $oUserMock */
|
|
|
|
$oUserMock = $this->getMockBuilder(User::class)
|
|
|
|
->onlyMethods(['load'])
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2019-08-16 23:17:48 +02:00
|
|
|
$oUserMock->method('load')->willReturn(true);
|
2019-08-11 23:49:49 +02:00
|
|
|
$oUserMock->assign(
|
2022-09-28 00:08:36 +02:00
|
|
|
[
|
2022-09-30 21:06:30 +02:00
|
|
|
'oxpasssalt' => '6162636465666768696A6B',
|
2022-09-28 00:08:36 +02:00
|
|
|
]
|
2019-08-11 23:49:49 +02:00
|
|
|
);
|
2022-09-30 21:06:30 +02:00
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var d3backupcode|MockObject $oModelMock */
|
|
|
|
$oModelMock = $this->getMockBuilder(d3backupcode::class)
|
2022-11-09 12:03:16 +01:00
|
|
|
->onlyMethods(['d3TotpGetUserObject'])
|
2022-09-28 00:08:36 +02:00
|
|
|
->getMock();
|
2022-11-09 12:03:16 +01:00
|
|
|
$oModelMock->method('d3TotpGetUserObject')->willReturn($oUserMock);
|
2019-08-11 23:49:49 +02:00
|
|
|
|
|
|
|
$this->_oModel = $oModelMock;
|
|
|
|
|
|
|
|
$this->assertSame(
|
2022-09-28 00:08:36 +02:00
|
|
|
'9f7f502a8148f90732a4aa4d880b8cf5',
|
|
|
|
$this->callMethod($this->_oModel, 'd3EncodeBC', ['123456', 'userId'])
|
2019-08-11 23:49:49 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-09-28 22:25:51 +02:00
|
|
|
* @covers \D3\Totp\Application\Model\d3backupcode::d3GetUser
|
2019-08-11 23:49:49 +02:00
|
|
|
*/
|
|
|
|
public function d3GetUserReturnCachedUser()
|
|
|
|
{
|
2022-09-28 00:08:36 +02:00
|
|
|
/** @var User|MockObject $oUserMock */
|
|
|
|
$oUserMock = $this->getMockBuilder(User::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2019-08-11 23:49:49 +02:00
|
|
|
$oUserMock->assign(
|
2022-09-28 00:08:36 +02:00
|
|
|
[
|
2022-09-30 21:06:30 +02:00
|
|
|
'oxid' => 'foobar',
|
2022-09-28 00:08:36 +02:00
|
|
|
]
|
2019-08-11 23:49:49 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->_oModel->setUser($oUserMock);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$oUserMock,
|
|
|
|
$this->callMethod($this->_oModel, 'd3GetUser')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-09-28 22:25:51 +02:00
|
|
|
* @covers \D3\Totp\Application\Model\d3backupcode::d3GetUser
|
2019-08-11 23:49:49 +02:00
|
|
|
*/
|
|
|
|
public function d3GetUserReturnCurrentUser()
|
|
|
|
{
|
2022-11-10 00:00:50 +01:00
|
|
|
Registry::getSession()->setVariable(d3totp_conf::SESSION_CURRENTUSER, 'foobar');
|
2019-08-11 23:49:49 +02:00
|
|
|
|
|
|
|
$oUser = $this->callMethod($this->_oModel, 'd3GetUser');
|
|
|
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
User::class,
|
|
|
|
$oUser
|
|
|
|
);
|
|
|
|
$this->assertNull(
|
|
|
|
$oUser->getId()
|
|
|
|
);
|
|
|
|
}
|
2019-08-16 23:17:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-11-09 12:03:16 +01:00
|
|
|
* @covers \D3\Totp\Application\Model\d3backupcode::d3TotpGetUserObject
|
2019-08-16 23:17:48 +02:00
|
|
|
*/
|
|
|
|
public function d3getUserObjectReturnsRightInstance()
|
|
|
|
{
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
User::class,
|
2022-11-09 12:03:16 +01:00
|
|
|
$this->callMethod($this->_oModel, 'd3TotpGetUserObject')
|
2019-08-16 23:17:48 +02:00
|
|
|
);
|
|
|
|
}
|
2022-09-30 21:06:30 +02:00
|
|
|
}
|