Skip to content

Commit b76cc70

Browse files
committed
OXDEV-9992 Add integration tests with fixtures for user model
1 parent 1acc83f commit b76cc70

5 files changed

Lines changed: 159 additions & 11 deletions

File tree

tests/Fixtures/testdata_ce.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
SET @@session.sql_mode = '';
2+
3+
REPLACE INTO `oxuser` (`OXID`, `OXACTIVE`, `OXRIGHTS`, `OXSHOPID`, `OXUSERNAME`, `OXPASSWORD`, `OXPASSSALT`, `OXCUSTNR`, `OXUSTID`, `OXCOMPANY`, `OXFNAME`, `OXLNAME`, `OXSTREET`, `OXSTREETNR`, `OXADDINFO`, `OXCITY`, `OXCOUNTRYID`, `OXZIP`, `OXFON`, `OXFAX`, `OXSAL`, `OXBONI`, `OXCREATE`, `OXREGISTER`, `OXPRIVFON`, `OXMOBFON`, `OXBIRTHDATE`)
4+
VALUES ('testuser', 1, 'user', 1, 'user@oxid-esales.com', '$2y$10$b186f117054b700a89de9uXDzfahkizUucitfPov3C2cwF5eit2M2', 'b186f117054b700a89de929ce90c6aef', 8, '', 'UserCompany šÄßüл', 'UserNamešÄßüл', 'UserSurnamešÄßüл', 'Musterstr.šÄßüл', '1', 'User additional info šÄßüл', 'Musterstadt šÄßüл', 'testcountry_de', '79098', '0800 111111', '0800 111112', 'Mr', 500, '2008-02-05 14:42:42', '2008-02-05 14:42:42', '0800 111113', '0800 111114', '1980-01-01');

tests/Fixtures/testdata_ee.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
SET @@session.sql_mode = '';
2+
3+
REPLACE INTO `oxuser` (`OXID`, `OXACTIVE`, `OXRIGHTS`, `OXSHOPID`, `OXUSERNAME`, `OXPASSWORD`, `OXPASSSALT`, `OXCUSTNR`, `OXUSTID`, `OXUSTIDSTATUS`, `OXCOMPANY`, `OXFNAME`, `OXLNAME`, `OXSTREET`, `OXSTREETNR`, `OXADDINFO`, `OXCITY`, `OXCOUNTRYID`, `OXSTATEID`, `OXZIP`, `OXFON`, `OXFAX`, `OXSAL`, `OXBONI`, `OXCREATE`, `OXREGISTER`, `OXPRIVFON`, `OXMOBFON`, `OXBIRTHDATE`, `OXURL`, `OXWRONGLOGINS`, `OXUPDATEKEY`, `OXUPDATEEXP`, `OXPOINTS`) VALUES
4+
('e7af1c3b786fd02906ccd75698f4e6b9', 1, 'user', 1, 'user@oxid-esales.com', '$2y$10$b186f117054b700a89de9uXDzfahkizUucitfPov3C2cwF5eit2M2', 'b186f117054b700a89de929ce90c6aef', 2, '', 1, '', 'Marc', 'Muster', 'Hauptstr.', '13', '', 'Freiburg', 'a7c40f631fc920687.20179984', '', '79098', '', '', 'MR', 1000, '2011-02-01 08:41:25', '2011-02-01 08:41:25', '', '', '1984-12-21', '', 0, '', 0, 0);

tests/Fixtures/testdata_pe.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
SET @@session.sql_mode = '';
2+
3+
REPLACE INTO `oxuser` (`OXID`, `OXACTIVE`, `OXRIGHTS`, `OXSHOPID`, `OXUSERNAME`, `OXPASSWORD`, `OXPASSSALT`, `OXCUSTNR`, `OXUSTID`, `OXUSTIDSTATUS`, `OXCOMPANY`, `OXFNAME`, `OXLNAME`, `OXSTREET`, `OXSTREETNR`, `OXADDINFO`, `OXCITY`, `OXCOUNTRYID`, `OXSTATEID`, `OXZIP`, `OXFON`, `OXFAX`, `OXSAL`, `OXBONI`, `OXCREATE`, `OXREGISTER`, `OXPRIVFON`, `OXMOBFON`, `OXBIRTHDATE`, `OXURL`, `OXWRONGLOGINS`, `OXUPDATEKEY`, `OXUPDATEEXP`, `OXPOINTS`) VALUES
4+
('e7af1c3b786fd02906ccd75698f4e6b9', 1, 'user', 1, 'user@oxid-esales.com', '$2y$10$b186f117054b700a89de9uXDzfahkizUucitfPov3C2cwF5eit2M2', 'b186f117054b700a89de929ce90c6aef', 2, '', 1, '', 'Marc', 'Muster', 'Hauptstr.', '13', '', 'Freiburg', 'a7c40f631fc920687.20179984', '', '79098', '', '', 'MR', 1000, '2011-02-01 08:41:25', '2011-02-01 08:41:25', '', '', '1984-12-21', '', 0, '', 0, 0);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
namespace OxidEsales\SecurityModule\Tests\Integration;
9+
10+
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
11+
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
12+
use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase as EshopIntegrationTestCase;
13+
use OxidEsales\Facts\Facts;
14+
15+
class IntegrationTestCase extends EshopIntegrationTestCase
16+
{
17+
public function setUp(): void
18+
{
19+
$facts = new Facts();
20+
21+
$container = ContainerFactory::getInstance()->getContainer();
22+
$connection = $container->get(QueryBuilderFactoryInterface::class)
23+
->create()
24+
->getConnection();
25+
26+
$connection->executeStatement(
27+
file_get_contents(
28+
__DIR__ . '/../Fixtures/testdata_'
29+
. strtolower($facts->getEdition()) . '.sql'
30+
)
31+
);
32+
33+
parent::setUp();
34+
}
35+
}

tests/Integration/Shared/Model/UserTest.php

Lines changed: 115 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,21 @@
1414
use OxidEsales\Eshop\Core\Exception\UserException;
1515
use OxidEsales\Eshop\Core\Registry;
1616
use OxidEsales\Eshop\Core\Request;
17-
use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase;
17+
use OxidEsales\Eshop\Core\Utils;
18+
use OxidEsales\EshopCommunity\Core\Di\ContainerFacade;
19+
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface;
20+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\AuthorizeService;
21+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\ModuleSettingsService;
22+
use OxidEsales\SecurityModule\Captcha\Service\ModuleSettingsServiceInterface as CaptchaSettingsServiceInterface;
23+
use OxidEsales\SecurityModule\Core\Module;
24+
use OxidEsales\SecurityModule\Tests\Integration\IntegrationTestCase;
1825

1926
class UserTest extends IntegrationTestCase
2027
{
28+
// Fixture users from tests/Fixtures/testdata_ce.sql (password: useruser)
29+
private const OTP_USER_NAME = 'user@oxid-esales.com';
30+
private const OTP_USER_PASSWORD = 'useruser';
31+
2132
protected Request $requestMock;
2233

2334
public function setUp(): void
@@ -34,6 +45,12 @@ public function setUp(): void
3445
Registry::getSession()->setVariable('captcha_expiration', time() + 60);
3546
}
3647

48+
public function tearDown(): void
49+
{
50+
$this->disableTwoFactorAuth();
51+
parent::tearDown();
52+
}
53+
3754
public function testCheckValuesWithInvalidCaptcha()
3855
{
3956
$this->requestMock
@@ -100,20 +117,107 @@ public function testLoginWithEmptyCaptcha()
100117
$subject->login('', '');
101118
}
102119

103-
public function testLoginWithOtpEnabledAndInvalidPasswordThrows(): void
120+
public function testLoginWithOTPEnabledAndValidCredentialsReturnsFalse(): void
104121
{
105-
$this->requestMock
106-
->method('getRequestParameter')
107-
->willReturnCallback(function ($param) {
108-
if ($param === 'captcha') {
109-
return 'valid_captcha';
110-
}
111-
return null;
112-
});
122+
$this->disableCaptcha();
123+
$this->enableTwoFactorAuth();
124+
125+
$utilsMock = $this->createMock(Utils::class);
126+
$utilsMock->expects($this->once())->method('redirect');
127+
Registry::set(Utils::class, $utilsMock);
128+
129+
$subject = oxNew(User::class);
130+
$result = $subject->login(self::OTP_USER_NAME, self::OTP_USER_PASSWORD);
131+
132+
$this->assertFalse($result);
133+
$this->assertEquals(
134+
self::OTP_USER_NAME,
135+
Registry::getSession()->getVariable(AuthorizeService::USER_SESSION_KEY)
136+
);
137+
}
138+
139+
public function testLoginWithOTPEnabledAndInvalidCredentialsThrowsException(): void
140+
{
141+
$this->disableCaptcha();
142+
$this->enableTwoFactorAuth();
143+
144+
$this->expectException(UserException::class);
145+
$this->expectExceptionMessage('ERROR_MESSAGE_USER_NOVALIDLOGIN');
146+
147+
$subject = oxNew(User::class);
148+
$subject->login(self::OTP_USER_NAME, uniqid());
149+
}
150+
151+
public function testLoginWithOTPEnabledAndNonExistentUserThrowsException(): void
152+
{
153+
$this->disableCaptcha();
154+
$this->enableTwoFactorAuth();
113155

114156
$this->expectException(UserException::class);
157+
$this->expectExceptionMessage('ERROR_MESSAGE_USER_NOVALIDLOGIN');
158+
159+
$subject = oxNew(User::class);
160+
$subject->login('nonexistent@test.com', 'anypassword');
161+
}
162+
163+
public function testLoginWithOTPDisabledCallsParentLogin(): void
164+
{
165+
$this->disableCaptcha();
166+
$this->disableTwoFactorAuth();
115167

116168
$subject = oxNew(User::class);
117-
$subject->login(uniqid(), uniqid());
169+
$result = $subject->login(self::OTP_USER_NAME, self::OTP_USER_PASSWORD);
170+
171+
$this->assertTrue($result);
172+
$this->assertNull(
173+
Registry::getSession()->getVariable(AuthorizeService::USER_SESSION_KEY)
174+
);
175+
}
176+
177+
public function testLoginWithOTPEnabledStoresUserInSession(): void
178+
{
179+
$this->disableCaptcha();
180+
$this->enableTwoFactorAuth();
181+
182+
$utilsMock = $this->createMock(Utils::class);
183+
$utilsMock->method('redirect');
184+
Registry::set(Utils::class, $utilsMock);
185+
186+
$subject = oxNew(User::class);
187+
$subject->login(self::OTP_USER_NAME, self::OTP_USER_PASSWORD);
188+
189+
$sessionUserName = Registry::getSession()->getVariable(AuthorizeService::USER_SESSION_KEY);
190+
$this->assertEquals(self::OTP_USER_NAME, $sessionUserName);
191+
}
192+
193+
private function enableTwoFactorAuth(): void
194+
{
195+
$moduleSettingService = ContainerFacade::get(ModuleSettingServiceInterface::class);
196+
$moduleSettingService->saveBoolean(
197+
ModuleSettingsService::ACTIVE,
198+
true,
199+
Module::MODULE_ID
200+
);
201+
$moduleSettingService->saveString(
202+
ModuleSettingsService::TWO_FACTOR_TYPE,
203+
'otp',
204+
Module::MODULE_ID
205+
);
206+
}
207+
208+
private function disableTwoFactorAuth(): void
209+
{
210+
$moduleSettingService = ContainerFacade::get(ModuleSettingServiceInterface::class);
211+
$moduleSettingService->saveBoolean(
212+
ModuleSettingsService::ACTIVE,
213+
false,
214+
Module::MODULE_ID
215+
);
216+
}
217+
218+
private function disableCaptcha(): void
219+
{
220+
$captchaSettings = ContainerFacade::get(CaptchaSettingsServiceInterface::class);
221+
$captchaSettings->saveIsCaptchaEnabled(false);
118222
}
119223
}

0 commit comments

Comments
 (0)