You are here

public function ShieldTest::testShieldKey in Shield 8

Test shield module configuration with key module.

Throws

\Behat\Mink\Exception\ExpectationException

\Behat\Mink\Exception\ResponseTextException

\Drupal\Core\Entity\EntityStorageException

File

tests/src/Functional/ShieldTest.php, line 115

Class

ShieldTest
Tests Shield module.

Namespace

Drupal\Tests\shield\Functional

Code

public function testShieldKey() {
  $path_to_test = 'user';
  $assert_session = $this
    ->assertSession();

  // Assert our key was created and is available.
  $admin = $this
    ->drupalCreateUser([], NULL, TRUE);
  $this
    ->drupalLogin($admin);
  $this
    ->drupalGet('admin/config/system/keys');
  $assert_session
    ->pageTextContains('Shield Test');
  $assert_session
    ->statusCodeEquals(200);

  // Setup shield module using the shield_test key.
  $shield_config = \Drupal::configFactory()
    ->getEditable('shield.settings');
  $shield_config
    ->set('shield_enable', TRUE);
  $shield_config
    ->set('credential_provider', 'multikey');
  $shield_config
    ->set('credentials.multikey.user_pass_key', 'shield_test');
  $shield_config
    ->set('print', 'Hello world!');
  $shield_config
    ->save();

  // Assert we are presented with a http auth prompt.
  $this
    ->drupalGet($path_to_test);
  $assert_session
    ->responseHeaderContains('WWW-Authenticate', 'Basic realm="Hello world!"');
  $assert_session
    ->statusCodeEquals(401);

  // Assert we can authenticate using the credentials from our key.
  $key_values = \Drupal::service('key.repository')
    ->getKey('shield_test')
    ->getKeyValues();
  $user_pass = $key_values['username'] . ':' . $key_values['password'];
  $this
    ->drupalGet($path_to_test, [], [
    'Authorization' => 'Basic ' . base64_encode("{$user_pass}"),
  ]);
  $assert_session
    ->statusCodeEquals(200);

  // Assert our configuration shows correctly in the UI.
  $this
    ->drupalGet('admin/config/system/shield');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->fieldValueEquals('credentials[credential_provider]', 'multikey');
  $assert_session
    ->fieldValueEquals('credentials[providers][multikey][user_pass_key]', 'shield_test');
  $assert_session
    ->fieldValueEquals('shield_print', 'Hello world!');
}