You are here

public function GaLoginBasicPermissionsTestCase::testCheckboxOnAccountEditPage in Google Authenticator login 7

Test checkbox on account edit page and delete code operation.

File

./ga_login.test, line 434
Tests for ga_login.module.

Class

GaLoginBasicPermissionsTestCase
Test basic permissions.

Code

public function testCheckboxOnAccountEditPage() {
  $account = $this
    ->drupalCreateUser(array(
    'login without code',
    'create own login code',
    'delete own login code',
  ));

  // Login.
  $this
    ->drupalLogin($account);

  // Check the checkbox on the account edit page.
  $edit = array();
  $edit['ga_login_force_tfa'] = '1';
  $this
    ->drupalPost("user/{$account->uid}/edit", $edit, t('Save'));

  // Make sure we are redirected to the ga_login tab.
  $this
    ->assertUrl("user/{$account->uid}/ga_login");

  // Create code for account user.
  $edit = array();
  $this
    ->drupalPost("user/{$account->uid}/ga_login", $edit, t('Get started'));

  // Seconds step: select time based code.
  $edit = array();
  $edit['tokentype'] = 'TOTP';
  $this
    ->drupalPost(NULL, $edit, t('Create code'));

  // Get the secret key from the page.
  $code_on_page = $this
    ->xpath('//span[@class=:class]', array(
    ':class' => 'secret-key',
  ));
  $code_on_page = $code_on_page[0][0];
  $edit = array();
  $edit['verify_code'] = ga_login_test_generate_code($code_on_page);
  $this
    ->drupalPost(NULL, $edit, t('Use this code'));
  $this
    ->assertText(t("You can now log in with your new code."));
  $this
    ->assertUrl("user/{$account->uid}/ga_login");

  // Make sure we can stil access the ga_login tab.
  $edit = array();
  $this
    ->drupalGet("user/{$account->uid}/ga_login");
  $this
    ->assertText(t("Two step verification"));

  // Make sure tfa checkbox is checked.
  $this
    ->drupalGet("user/{$account->uid}/edit");
  $this
    ->assertFieldChecked('edit-ga-login-force-tfa');
  $this
    ->drupalLogout();

  // Make sure we need a code to log in.
  // Login without a code.
  $edit = array();
  $edit['name'] = $account->name;
  $edit['pass'] = $account->pass_raw;
  $this
    ->drupalPost('user', $edit, t('Log in'));
  $this
    ->assertText(t('Sorry, unrecognized username or password.'));

  // Login using a code.
  $edit = array();
  $edit['name'] = $account->name;
  $edit['pass'] = $account->pass_raw;

  // Make sure we have a new code.
  $edit['gacode'] = ga_login_test_generate_code($code_on_page, 1);
  $this
    ->drupalPost('user', $edit, t('Log in'));
  $this
    ->assertText(t('Member for'));

  // Delete the code.
  $edit = array();
  $this
    ->drupalPost("user/{$account->uid}/edit", $edit, t('Delete GA Login Code'));
  $this
    ->assertText(t("Delete the GA Login code for"));

  // Confirm.
  $this
    ->drupalPost(NULL, $edit, 'Delete');
  $this
    ->assertUrl("user/{$account->uid}/edit");
  $this
    ->assertText(t("Successfully deleted the GA Login code for"));

  // Delete should no longer be accessible.
  $this
    ->drupalGet("user/{$account->uid}/ga_login/delete");
  $this
    ->assertText(t("You are not authorized to access this page."));

  // Make sure tfa checkbox is not checked.
  $this
    ->drupalGet("user/{$account->uid}/edit");
  $this
    ->assertNoFieldChecked('edit-ga-login-force-tfa');
  $this
    ->drupalLogout();
}