Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config/schema/helfi_platform_config.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ helfi_platform_config.redirect_cleaner:
enable:
type: boolean

helfi_platform_config.e2e_test_users:
type: config_object
mapping:
users:
type: sequence
sequence:
type: mapping
mapping:
username:
type: string

# Default value.
field.value.location:
type: mapping
Expand Down
46 changes: 46 additions & 0 deletions modules/helfi_tfa/src/Hook/UserHooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Drupal\helfi_tfa\Hook;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\tfa\TfaUserDataTrait;
use Drupal\user\UserDataInterface;
use Drupal\user\UserInterface;

/**
* User hook implementations for TFA.
*/
class UserHooks {

use TfaUserDataTrait;

public function __construct(
private readonly ConfigFactoryInterface $configFactory,
private readonly UserDataInterface $userDataService,
) {
}

/**
* Implements hook_user_login().
*/
#[Hook('user_login')]
public function userLogin(UserInterface $user): void {
$e2eTestUsers = $this->configFactory->get('helfi_platform_config.e2e_test_users')->get('users');
if (empty($e2eTestUsers)) {
return;
}

$e2eTestUsers = array_column($e2eTestUsers, 'username');
$username = $user->getAccountName();

// Clear TFA data for E2E test users to allow repeated login without
// having to activate TFA.
if ($username && in_array($username, $e2eTestUsers)) {
$this->deleteUserData('tfa', NULL, $user->id(), $this->userDataService);
}
}

}
92 changes: 92 additions & 0 deletions modules/helfi_tfa/tests/src/Unit/Hook/UserHooksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\helfi_tfa\Unit\Hook;

use Drupal\helfi_tfa\Hook\UserHooks;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\user\UserDataInterface;
use Drupal\user\UserInterface;
use Drupal\Tests\UnitTestCase;

/**
* Tests the user hooks.
*
* @group helfi_tfa
*/
final class UserHooksTest extends UnitTestCase {

/**
* Tests userLogin() basic behavior with different inputs.
*
* @dataProvider providerUserLogin
*/
public function testUserLogin(
$testUserConfig,
$currentUserName,
$expectedDeleteCount,
): void {
$configFactory = $this->createMock(ConfigFactoryInterface::class);
$config = $this->createMock(ImmutableConfig::class);
$userDataService = $this->createMock(UserDataInterface::class);
$user = $this->createMock(UserInterface::class);
$userId = 123;

$configFactory
->method('get')
->with('helfi_platform_config.e2e_test_users')
->willReturn($config);
$config
->method('get')
->with('users')
->willReturn($testUserConfig);

$user
->method('getAccountName')
->willReturn($currentUserName);
$user
->method('id')
->willReturn($userId);

$userDataService
->expects($this->exactly($expectedDeleteCount))
->method('delete')
->with('tfa', $userId, NULL);

$sut = new UserHooks(
$configFactory,
$userDataService
);

$sut->userLogin($user);
}

/**
* Data provider for testUserLogin().
*
* @return array[]
* The test cases.
*/
public static function providerUserLogin(): array {
return [
'no test user config' => [
'testUserConfig' => [],
'currentUserName' => 'testuser',
'expectedDeleteCount' => 0,
],
'current user is not a test user' => [
'testUserConfig' => [['username' => 'testuser']],
'currentUserName' => 'nottestuser',
'expectedDeleteCount' => 0,
],
'current user is a test user' => [
'testUserConfig' => [['username' => 'testuser']],
'currentUserName' => 'testuser',
'expectedDeleteCount' => 1,
],
];
}

}