unitTestsAreRunning = true; $this->resetGetAndPost(); parent::setUp(); $this->reloadDbTables(['mod_users']); // Create new class $this->pubUserManager = $this->initBaseClassObjectBySiteMapId(siteMap::PUB_USER_SECURITY_ENFORCEMENT); } protected function tearDown(): void { $this->reloadDbTables(['mod_users', 'mod_users_permissions']); $this->logOutUnitTestUser(); parent::tearDown(); } public function testSecurityEnforcement2faTrapActionBaseCase(): void { $this->logOutUnitTestUser(); // Clear 2fa $loginData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed); $this->cDb->_queryUpdate('mod_users', ['user_2fa_secret_enabled' => ''], ['id' => $loginData['id']]); // Log in & catch redirect exception $this->loginUnitTestUserForCoverage(siteUsers::loginAppSelfEmployed, false, false); ob_start(); $this->pubUserManager->securityEnforcement2faTrapAction(); $pageHTML = ob_get_contents(); ob_end_clean(); $this->assertExpected( [ 'Zutenis | Lapas piekļuves kļūda ...', 'Lai piekļūtu grāmatvedības sistēmai, ir jāiespējo
2FA - Divu faktoru autentifikācijas drošība!
', 'Divu faktoru autentifikācijas uzstādīšana

', 'Uzinstalējiet "Google Authenticator" lietojumprogrammu savā ierīcē', 'Noskenējiet svītrkodu ar "Google Authenticator"', '

Ievadi Google Authenticator lietotnes piešķirto
Divu faktoru autentifikācijas kodu

', '', ], $pageHTML ); } public function testSecurityEnforcement2faTrapActionBaseCaseWhenUserHasLoggedIn(): void { try { $this->pubUserManager->securityEnforcement2faTrapAction(); $this->fail("Exception expected!"); } catch (Exception $error) { $this->assertExpected( ['Redirected to Location: /gramatvedibas-sistema/sistemas-informacijas-panelis'], $error->getMessage() ); } } public function testSecurityEnforcement2faTrapActionSet2FAQRCode(): void { $this->logOutUnitTestUser(); // Clear 2fa $loginData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed); $this->cDb->_queryUpdate('mod_users', ['user_2fa_secret_enabled' => ''], ['id' => $loginData['id']]); // Log in & catch redirect exception $this->loginUnitTestUserForCoverage(siteUsers::loginAppSelfEmployed, false, false); $oGoogle2FA = new cGoogle2FA(); $secret = $oGoogle2FA->getNewGoogleAuthSecret(); $this->setPost([ 'action' => cCore::C_ACTION_ACTIVATE_2FA, 'secret' => $secret, 'code' => $oGoogle2FA->getGoogleAuthCode($secret) ]); try { $this->pubUserManager->securityEnforcement2faTrapAction(); $this->fail("Exception expected!"); } catch (Exception $error) { $this->assertExpected( ['{"jsonok":true,"url":"\/gramatvedibas-sistema\/sistemas-informacijas-panelis"}'], $error->getMessage() ); } } public function testSecurityEnforcement2faTrapActionSet2FAQRCodeErrorCase(): void { $this->logOutUnitTestUser(); // Clear 2fa $loginData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed); $this->cDb->_queryUpdate('mod_users', ['user_2fa_secret_enabled' => ''], ['id' => $loginData['id']]); // Log in & catch redirect exception $this->loginUnitTestUserForCoverage(siteUsers::loginAppSelfEmployed, false, false); $this->setPost([ 'action' => cCore::C_ACTION_ACTIVATE_2FA, 'secret' => '', 'code' => '' ]); try { $this->pubUserManager->securityEnforcement2faTrapAction(); $this->fail("Exception expected!"); } catch (Exception $error) { $this->assertExpected( [ '{"error":true,"data":{"code":"Ievad\u012bts k\u013c\u016bdains ' . '2FA iestat\u012b\u0161anas dro\u0161\u012bbas kods!"}}' ], $error->getMessage() ); } } public function testSecurityEnforcement2faTrapActionDeactivate2FA(): void { $oGoogle2FA = new cGoogle2FA(); $loginData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed); $this->setPost([ 'action' => 'deactivate-2FA', 'code' => $oGoogle2FA->getGoogleAuthCode($loginData['2fa']) ]); try { $this->pubUserManager->securityEnforcement2faTrapAction(); $this->fail("Exception expected!"); } catch (Exception $error) { $this->assertExpected( ['{"jsonok":true,"data":true}'], $error->getMessage() ); } } public function testSecurityEnforcement2faTrapActionDeactivate2FAErrorCase1(): void { $this->setPost([ 'action' => 'deactivate-2FA', 'code' => '123456' ]); try { $this->pubUserManager->securityEnforcement2faTrapAction(); $this->fail("Exception expected!"); } catch (Exception $error) { $this->assertExpected( ['{"error":true,"data":{"code":"Login 2FA Security removal code input error!"}}'], $error->getMessage() ); } } public function testSecurityEnforcement2faTrapActionDeactivate2FAErrorCase2(): void { $this->setPost([ 'action' => 'deactivate-2FA', 'code' => '' ]); try { $this->pubUserManager->securityEnforcement2faTrapAction(); $this->fail("Exception expected!"); } catch (Exception $error) { $this->assertExpected( ['{"error":true,"data":{"code":"Login 2FA Security removal code input error!"}}'], $error->getMessage() ); } } public function testSecurityEnforcement2faTrapActionGet2FAQRCodeCase1(): void { $this->setPost(['action' => 'get2FA-QRCode']); try { $this->pubUserManager->securityEnforcement2faTrapAction(); $this->fail("Exception expected!"); } catch (Exception $error) { // get JSON response $response = json_decode($error->getMessage(), true); $this->assertTrue(isset($response['jsonok'])); $this->assertTrue(isset($response['html'])); $this->assertTrue(isset($response['msgEnableAuth'])); $this->assertTrue(isset($response['buttonYes'])); $this->assertTrue(isset($response['buttonCancel'])); $this->assertTrue(isset($response['buttonActivate'])); $this->assertExpected( [ '

Pierakstiet savu slepeno atslēgu un glabājiet to droši.
', 'Tā būs nepieciešama, lai atgūtu 2FA, nozaudēšanas gadījumā.

', '', ], $response['html'] ); } } public function testSecurityEnforcement2faTrapActionGet2FAQRCodeCase2(): void { $this->setPost([ 'action' => 'get2FA-QRCode', 'reload' => true ]); try { $this->pubUserManager->securityEnforcement2faTrapAction(); $this->fail("Exception expected!"); } catch (Exception $error) { // get JSON response $response = json_decode($error->getMessage(), true); $this->assertTrue(isset($response['jsonok'])); $this->assertTrue(isset($response['sUrl'])); $this->assertTrue(isset($response['sSecret'])); $this->assertTrue(strlen($response['sSecret']) > 0); } } }