You are here

protected function AuthenticationFormJsTest::validateForm in Apigee Edge 8

Validates the visited form.

Parameters

callable $visitFormAsAdmin: The function that visits the form as an admin user that we would like to validate.

2 calls to AuthenticationFormJsTest::validateForm()
AuthenticationFormJsTest::testAuthenticationForm in tests/src/FunctionalJavascript/Form/AuthenticationFormJsTest.php
Tests the Authentication form.
AuthenticationFormJsTest::testKeyAddForm in tests/src/FunctionalJavascript/Form/AuthenticationFormJsTest.php
Tests the Key add form.

File

tests/src/FunctionalJavascript/Form/AuthenticationFormJsTest.php, line 259

Class

AuthenticationFormJsTest
Apigee Edge API credentials, authentication form, key integration test.

Namespace

Drupal\Tests\apigee_edge\FunctionalJavascript\Form

Code

protected function validateForm(callable $visitFormAsAdmin) : void {
  $web_assert = $this
    ->assertSession();
  $page = $this
    ->getSession()
    ->getPage();

  // Unset private file path and invalidate the active key.
  $settings['settings']['file_private_path'] = (object) [
    'value' => '',
    'required' => TRUE,
  ];
  $this
    ->writeSettings($settings);
  $this
    ->invalidateKey();

  // Ensure that the private file key provider is the default on the
  // Authentication form and form detects the problem caused by the
  // missing private filesystem configuration.
  $visitFormAsAdmin();
  $this
    ->assertSession()
    ->pageTextContains('The requirements of the selected Apigee Edge: Private File key provider are not fulfilled. Fix errors described below or change the key provider.');
  $this
    ->assertSession()
    ->pageTextContains('Private filesystem has not been configured yet. Learn more');

  // Set private file path.
  $settings['settings']['file_private_path'] = (object) [
    'value' => "{$this->siteDirectory}/private",
    'required' => TRUE,
  ];
  $this
    ->writeSettings($settings);

  // Reload the page, the key input form should be visible.
  $visitFormAsAdmin();

  // Make sure the default fields are visible and empty.
  $web_assert
    ->fieldValueEquals('Authentication type', 'oauth');
  $web_assert
    ->fieldValueEquals('Username', '');
  $web_assert
    ->fieldValueEquals('Password', '');
  $web_assert
    ->fieldValueEquals('Organization', '');
  $web_assert
    ->fieldValueEquals('Apigee Edge endpoint', '');

  // Select basic auth.
  $page
    ->selectFieldOption('key_input_settings[auth_type]', EdgeKeyTypeInterface::EDGE_AUTH_TYPE_BASIC);

  // Make sure the oauth fields are hidden.
  $this
    ->assertFalse($this
    ->cssSelect('input[name="key_input_settings[authorization_server]"]')[0]
    ->isVisible());
  $this
    ->assertFalse($this
    ->cssSelect('input[name="key_input_settings[client_id]"]')[0]
    ->isVisible());
  $this
    ->assertFalse($this
    ->cssSelect('input[name="key_input_settings[client_secret]"]')[0]
    ->isVisible());

  // Test the connection with basic auth.
  $page
    ->selectFieldOption('key_input_settings[instance_type]', EdgeKeyTypeInterface::INSTANCE_TYPE_PUBLIC);
  $page
    ->fillField('Username', $this->username);
  $page
    ->fillField('Password', $this->password);
  $page
    ->fillField('Organization', $this->organization);
  $this
    ->assertSession()
    ->pageTextContains('Send request using the given API credentials.');
  $this
    ->assertSendRequestMessage('.messages--status', 'Connection successful.');
  $web_assert
    ->elementNotExists('css', 'details[data-drupal-selector="edit-debug"]');

  // Switch to oauth.
  $this
    ->cssSelect('select[name="key_input_settings[auth_type]"]')[0]
    ->setValue('oauth');

  // Make sure the oauth fields are visible.
  $this
    ->assertTrue($this
    ->cssSelect('input[name="key_input_settings[authorization_server_type]"]')[0]
    ->isVisible());
  $this
    ->assertTrue($this
    ->cssSelect('input[name="key_input_settings[client_id]"]')[0]
    ->isVisible());
  $this
    ->assertTrue($this
    ->cssSelect('input[name="key_input_settings[client_secret]"]')[0]
    ->isVisible());

  // Make sure that test connection is disabled without a password.
  $page
    ->fillField('Password', '');
  $this
    ->assertTrue($this
    ->cssSelect('input[name="test_connection"]')[0]
    ->hasAttribute('disabled'));

  // Make sure that test connection is now enabled.
  $page
    ->fillField('Password', $this->password);
  $this
    ->assertFalse($this
    ->cssSelect('input[name="test_connection"]')[0]
    ->hasAttribute('disabled'));

  // Test the connection with oauth.
  $this
    ->assertSendRequestMessage('.messages--status', 'Connection successful.');
  $web_assert
    ->elementNotExists('css', 'details[data-drupal-selector="edit-debug"]');

  // Make sure the token file has not been left behind.
  $token_file_path = $this->container
    ->get('file_system')
    ->realpath(OauthTokenFileStorage::DEFAULT_DIRECTORY . '/oauth.dat');
  $this
    ->assertFileNotExists($token_file_path);

  // Switch back to basic auth.
  $this
    ->cssSelect('select[name="key_input_settings[auth_type]"]')[0]
    ->setValue('basic');

  // Test the connection with basic auth.
  $page
    ->fillField('Password', $this->password);
  $this
    ->assertSendRequestMessage('.messages--status', 'Connection successful.');
  $web_assert
    ->elementNotExists('css', 'details[data-drupal-selector="edit-debug"]');
  $page
    ->fillField('Password', $this->password);

  // Press the Save/Save configuration button.
  $page
    ->pressButton('op');
  $this
    ->assertSession()
    ->pageTextContains('Connection successful.');

  // Because Key add/edit form redirects the user to the Key entity listing
  // page on success therefore we have to re-visit the form again.
  $visitFormAsAdmin();

  // Setup valid credentials again.
  $page
    ->selectFieldOption('key_input_settings[instance_type]', EdgeKeyTypeInterface::INSTANCE_TYPE_PUBLIC);
  $page
    ->fillField('Username', $this->username);
  $page
    ->fillField('Password', $this->password);
  $page
    ->fillField('Organization', $this->organization);
  $page
    ->selectFieldOption('key_input_settings[auth_type]', EdgeKeyTypeInterface::EDGE_AUTH_TYPE_BASIC);

  // Test invalid password.
  $random_pass = $this
    ->randomString();
  $page
    ->fillField('Password', $random_pass);
  $this
    ->assertSendRequestMessage('.messages--error', "Failed to connect to Apigee Edge. The given username ({$this->username}) or password is incorrect. Error message: ");

  // TODO Re-add this assert later. It had to be disabled because of a
  // regression bug in the Apigee Edge for Public Cloud 19.03.01 release. If
  // valid organization name and username provided with an invalid password
  // the MGMT server returns HTTP 500 with an error instead of HTTP 401.
  // $web_assert->elementContains('css', 'textarea[data-drupal-selector="edit-debug-text"]', 'HTTP/1.1 401 Unauthorized');.
  $web_assert
    ->elementContains('css', 'textarea[data-drupal-selector="edit-debug-text"]', '***credentials***');
  $web_assert
    ->elementNotContains('css', 'textarea[data-drupal-selector="edit-debug-text"]', $random_pass);
  $page
    ->fillField('Password', $this->password);

  // Test invalid username when using public cloud endpoint.
  $page
    ->selectFieldOption('key_input_settings[instance_type]', EdgeKeyTypeInterface::INSTANCE_TYPE_PUBLIC);
  $page
    ->fillField('Username', $this
    ->randomMachineName());
  $this
    ->assertSendRequestMessage('.messages--error', "Failed to connect to Apigee Edge. The organization username should be a valid email. Error message: ");
  $page
    ->fillField('Username', $this->username);

  // Test invalid organization.
  $random_org = $this->randomGenerator
    ->word(16);
  $page
    ->fillField('Organization', $random_org);
  $this
    ->assertSendRequestMessage('.messages--error', "Failed to connect to Apigee Edge. The given organization name ({$random_org}) is incorrect. Error message: ");
  $web_assert
    ->elementContains('css', 'textarea[data-drupal-selector="edit-debug-text"]', 'HTTP/1.1 404 Not Found');
  $web_assert
    ->elementContains('css', 'textarea[data-drupal-selector="edit-debug-text"]', "\"organization\": \"{$random_org}\"");
  $page
    ->fillField('Organization', $this->organization);

  // Test invalid endpoint.
  $page
    ->selectFieldOption('key_input_settings[instance_type]', EdgeKeyTypeInterface::INSTANCE_TYPE_PRIVATE);
  $invalid_domain = "{$this->randomGenerator->word(16)}.example.com";
  $page
    ->fillField('Apigee Edge endpoint', "http://{$invalid_domain}/");
  $this
    ->assertSendRequestMessage('.messages--error', "Failed to connect to Apigee Edge. The given endpoint (http://{$invalid_domain}/) is incorrect or something is wrong with the connection. Error message: ");
  $web_assert
    ->elementContains('css', 'textarea[data-drupal-selector="edit-debug-text"]', "\"endpoint\": \"http:\\/\\/{$invalid_domain}\\/\"");
  $web_assert
    ->fieldValueEquals('Apigee Edge endpoint', "http://{$invalid_domain}/");
  $page
    ->fillField('Apigee Edge endpoint', '');
  $page
    ->selectFieldOption('key_input_settings[instance_type]', EdgeKeyTypeInterface::INSTANCE_TYPE_PUBLIC);

  // Test another invalid endpoint scenario:
  // Some endpoints are not a Management API endpoint, but still return
  // HTTP 200 with a JSON response.
  // Only test with mock client.
  if (static::$mock_api_client_ready && !$this->integration_enabled) {
    $invalid_endpoint = 'enterprise.apigee.com/platform/orgname';
    $page
      ->selectFieldOption('key_input_settings[instance_type]', EdgeKeyTypeInterface::INSTANCE_TYPE_PRIVATE);
    $page
      ->fillField('Apigee Edge endpoint', "https://{$invalid_endpoint}/");
    $this
      ->assertSendRequestMessage('.messages--error', "Failed to connect to Apigee Edge. The given endpoint (https://{$invalid_endpoint}/) is incorrect or something is wrong with the connection. Error message: ");
    $invalid_endpoint_escaped = str_replace('/', '\\/', $invalid_endpoint);
    $web_assert
      ->elementContains('css', 'textarea[data-drupal-selector="edit-debug-text"]', "\"endpoint\": \"https:\\/\\/{$invalid_endpoint_escaped}\\/\"");
    $web_assert
      ->fieldValueEquals('Apigee Edge endpoint', "https://{$invalid_endpoint}/");
    $page
      ->fillField('Apigee Edge endpoint', '');
    $page
      ->selectFieldOption('key_input_settings[instance_type]', EdgeKeyTypeInterface::INSTANCE_TYPE_PUBLIC);
  }

  // Test invalid authorization server.
  $this
    ->cssSelect('select[data-drupal-selector="edit-key-input-settings-auth-type"]')[0]
    ->setValue('oauth');
  $invalid_domain = "{$this->randomGenerator->word(16)}.example.com";
  $page
    ->selectFieldOption('key_input_settings[authorization_server_type]', 'custom');
  $page
    ->fillField('Custom authorization server', "http://{$invalid_domain}/");
  $this
    ->assertSendRequestMessage('.messages--error', "Failed to connect to the OAuth authorization server. The given authorization server (http://{$invalid_domain}/) is incorrect or something is wrong with the connection. Error message: ");
  $web_assert
    ->fieldValueEquals('Custom authorization server', "http://{$invalid_domain}/");
  $web_assert
    ->elementContains('css', 'textarea[data-drupal-selector="edit-debug-text"]', '"auth_type": "oauth"');
  $web_assert
    ->elementContains('css', 'textarea[data-drupal-selector="edit-debug-text"]', "\"authorization_server\": \"http:\\/\\/{$invalid_domain}\\/\"");
  $web_assert
    ->elementContains('css', 'textarea[data-drupal-selector="edit-debug-text"]', '"client_id": "edgecli"');
  $web_assert
    ->elementContains('css', 'textarea[data-drupal-selector="edit-debug-text"]', '"client_secret": "edgeclisecret"');
  $page
    ->fillField('Custom authorization server', '');
  $page
    ->selectFieldOption('key_input_settings[authorization_server_type]', 'default');

  // Test invalid client secret.
  $random_secret = $this->randomGenerator
    ->word(16);
  $page
    ->fillField('Client secret', $random_secret);
  $this
    ->assertSendRequestMessage('.messages--error', "Failed to connect to the OAuth authorization server. The given username ({$this->username}) or password or client ID (edgecli) or client secret is incorrect. Error message: ");
  $web_assert
    ->elementContains('css', 'textarea[data-drupal-selector="edit-debug-text"]', '"authorization_server": "https:\\/\\/login.apigee.com\\/oauth\\/token"');
  $web_assert
    ->elementContains('css', 'textarea[data-drupal-selector="edit-debug-text"]', '"client_id": "edgecli"');
  $web_assert
    ->elementContains('css', 'textarea[data-drupal-selector="edit-debug-text"]', '"client_secret": "***client-secret***"');
  $web_assert
    ->elementNotContains('css', 'textarea[data-drupal-selector="edit-debug-text"]', $random_secret);
  $page
    ->fillField('Client secret', '');

  // Test invalid client id.
  $client_id = $this->randomGenerator
    ->word(8);
  $page
    ->fillField('Client ID', $client_id);
  $this
    ->assertSendRequestMessage('.messages--error', "Failed to connect to the OAuth authorization server. The given username ({$this->username}) or password or client ID ({$client_id}) or client secret is incorrect. Error message: ");
  $web_assert
    ->elementContains('css', 'textarea[data-drupal-selector="edit-debug-text"]', '"authorization_server": "https:\\/\\/login.apigee.com\\/oauth\\/token"');
  $web_assert
    ->elementContains('css', 'textarea[data-drupal-selector="edit-debug-text"]', "\"client_id\": \"{$client_id}\"");
  $web_assert
    ->elementContains('css', 'textarea[data-drupal-selector="edit-debug-text"]', '"client_secret": "edgeclisecret"');
  $page
    ->fillField('Client ID', '');
}