You are here

public function GALoginHotpSetupPluginTest::testPluginSetup in Google Authenticator login 8

Test setting up the tfa_test_plugins_validation plugin as a generic user.

File

tests/src/Functional/GALoginHotpSetupPluginTest.php, line 77

Class

GALoginHotpSetupPluginTest
Class GALoginHotpSetupPluginTest.

Namespace

Drupal\Tests\ga_login\Functional

Code

public function testPluginSetup() {
  $this
    ->drupalGet('user/' . $this->userAccount
    ->id() . '/security/tfa/' . $this->validationPluginId);
  $assert = $this
    ->assertSession();
  $assert
    ->statusCodeEquals(200);
  $assert
    ->pageTextContains('Enter your current password');

  // Provide the user's password to continue.
  $edit = [
    'current_pass' => $this->userAccount->passRaw,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Confirm');
  $assert
    ->statusCodeEquals(200);
  $assert
    ->pageTextContains('Application verification code');

  // Fetch seed.
  $result = $this
    ->xpath('//input[@name="seed"]');
  if (empty($result)) {
    $this
      ->fail('Unable to extract seed from page. Aborting test.');
    return;
  }
  $seed = $result[0]
    ->getValue();
  $this->setupPlugin
    ->setSeed($seed);

  // Try invalid code.
  $edit = [
    'code' => substr(str_shuffle('1234567890'), 0, 6),
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Verify and save');
  $assert
    ->statusCodeEquals(200);
  $assert
    ->pageTextContains('Invalid application code. Please try again.');

  // Submit valid code.
  $edit = [
    'code' => $this->setupPlugin->auth->otp
      ->hotp(Encoding::base32DecodeUpper($seed), 1),
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Verify and save');
  $assert
    ->statusCodeEquals(200);
  $assert
    ->linkExists('Disable TFA');
}