You are here

public function KeyAuthTest::testUserAutoKeyGeneration in Key auth 8

Test automatically generating a key for new users.

File

tests/src/Functional/KeyAuthTest.php, line 70

Class

KeyAuthTest
Tests for key authentication provider.

Namespace

Drupal\Tests\key_auth\Functional

Code

public function testUserAutoKeyGeneration() {

  // Enable auto key generation.
  $this->keyAuthConfig
    ->set('auto_generate_keys', TRUE);
  $this->keyAuthConfig
    ->save();

  // Create a user with key authentication access.
  $user = $this
    ->drupalCreateUser([
    'use key authentication',
  ]);

  // Check that a key is present.
  $this
    ->assertNotEmpty($user->api_key->value);

  // Create a user without key authentication access.
  $user = $this
    ->drupalCreateUser([]);

  // Check that a key is not present.
  $this
    ->assertEmpty($user->api_key->value);

  // Disable auto key generation.
  $this->keyAuthConfig
    ->set('auto_generate_keys', FALSE);
  $this->keyAuthConfig
    ->save();

  // Grant access to use key auth to authenticated users.
  $user = $this
    ->drupalCreateUser([
    'use key authentication',
  ]);

  // Check that a key is not present.
  $this
    ->assertEmpty($user->api_key->value);
}