View source
<?php
namespace Drupal\Tests\apigee_edge\FunctionalJavascript\Form;
use Drupal\apigee_edge\Form\AuthenticationForm;
use Drupal\apigee_edge\OauthTokenFileStorage;
use Drupal\apigee_edge\Plugin\EdgeKeyTypeInterface;
use Drupal\Core\Url;
use Drupal\key\Entity\Key;
use Drupal\Tests\apigee_edge\FunctionalJavascript\ApigeeEdgeFunctionalJavascriptTestBase;
class AuthenticationFormJsTest extends ApigeeEdgeFunctionalJavascriptTestBase {
protected $defaultTheme = 'classy';
private $username;
private $password;
private $organization;
private $endpoint;
private $instanceType;
private $account_key;
protected function setUp() {
parent::setUp();
$test_key = Key::load($this
->config(AuthenticationForm::CONFIG_NAME)
->get('active_key'));
$test_key_type = $test_key
->getKeyType();
$this->instanceType = $test_key_type
->getInstanceType($test_key);
$this->organization = $test_key_type
->getOrganization($test_key);
if ($this->instanceType != EdgeKeyTypeInterface::INSTANCE_TYPE_HYBRID) {
$this->username = $test_key_type
->getUsername($test_key);
$this->password = $test_key_type
->getPassword($test_key);
$this->endpoint = $test_key_type
->getEndpoint($test_key);
}
else {
$this->account_key = $test_key_type
->getAccountKey($test_key);
}
$this
->config('apigee_edge.client')
->set('http_client_timeout', 30)
->save();
}
public function testAuthenticationForm() {
if ($this->instanceType == EdgeKeyTypeInterface::INSTANCE_TYPE_HYBRID) {
$this
->markTestSkipped('Skipping "testAuthenticationForm": can only be tested with public/private cloud credentials.');
}
$web_assert = $this
->assertSession();
$this
->drupalLogin($this->rootUser);
$this
->drupalGet(Url::fromRoute('apigee_edge.settings'));
$this
->getSession()
->getPage()
->fillField('Password', $this->password);
$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"]');
$page = $this
->getSession()
->getPage();
$page
->find('css', 'details[data-drupal-selector="edit-provider-section"] > summary')
->click();
$page
->selectFieldOption('key_provider', 'apigee_edge_environment_variables');
$this
->assertSession()
->waitForText("The selected key provider does not accept a value. See the provider's description for instructions on how and where to store the key value.");
$this
->validateForm([
$this,
'visitAuthenticationForm',
]);
$this
->visitAuthenticationForm();
$web_assert
->fieldValueEquals('Organization', $this->organization);
$web_assert
->fieldValueEquals('Username', $this->username);
$web_assert
->fieldValueEquals('Password', '');
}
public function testKeyAddForm() {
if ($this->instanceType == EdgeKeyTypeInterface::INSTANCE_TYPE_HYBRID) {
$this
->markTestSkipped('Skipping "testKeyAddForm": can only be tested with public/private cloud credentials.');
}
$web_assert = $this
->assertSession();
$this
->drupalLogin($this->rootUser);
$this
->drupalGet(Url::fromRoute('entity.key.add_form'));
$web_assert
->elementNotExists('css', 'input[name="test_connection"]');
$this
->cssSelect('select[name="key_type"]')[0]
->setValue('apigee_auth');
$web_assert
->waitForElementVisible('css', 'input[name="test_connection"]');
$this
->config('apigee_edge.dangerzone')
->set('do_not_alter_key_entity_forms', TRUE)
->save();
$this
->drupalGet(Url::fromRoute('entity.key.add_form'));
$this
->cssSelect('select[name="key_type"]')[0]
->setValue('apigee_auth');
$web_assert
->assertWaitOnAjaxRequest();
$web_assert
->elementNotExists('css', 'input[name="test_connection"]');
$this
->config('apigee_edge.dangerzone')
->set('do_not_alter_key_entity_forms', FALSE)
->save();
$this
->validateForm([
$this,
'visitKeyAddForm',
]);
}
public function testUsingHybridForm() {
$organization = getenv('APIGEE_EDGE_HYBRID_ORGANIZATION') ?: $this->organization;
$account_key = $this->account_key ? json_encode($this->account_key) : getenv('APIGEE_EDGE_ACCOUNT_JSON_KEY');
if (!$organization || !$account_key) {
$this
->markTestSkipped('Skipping "testUsingHybridForm": missing test environment variables APIGEE_EDGE_HYBRID_ORGANIZATION and/or APIGEE_EDGE_ACCOUNT_JSON_KEY.');
}
$web_assert = $this
->assertSession();
$this
->drupalLogin($this->rootUser);
$this
->drupalGet(Url::fromRoute('entity.key.add_form'));
$this
->visitKeyAddForm();
$page = $this
->getSession()
->getPage();
$page
->selectFieldOption('key_input_settings[instance_type]', EdgeKeyTypeInterface::INSTANCE_TYPE_HYBRID);
$page
->fillField('Organization', $organization);
$page
->fillField('GCP service account key', $account_key);
$this
->assertSendRequestMessage('.messages--status', 'Connection successful.');
$web_assert
->elementNotExists('css', 'details[data-drupal-selector="edit-debug"]');
}
protected function visitAuthenticationForm() : void {
if ($this->loggedInUser
->id() !== $this->rootUser) {
$this
->drupalLogin($this->rootUser);
}
$this
->drupalGet(Url::fromRoute('apigee_edge.settings'));
}
protected function visitKeyAddForm() : void {
$web_assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
if ($this->loggedInUser
->id() !== $this->rootUser) {
$this
->drupalLogin($this->rootUser);
}
$this
->drupalGet(Url::fromRoute('entity.key.add_form'));
$page
->fillField('Key name', $this
->randomMachineName());
$this
->cssSelect('select[name="key_type"]')[0]
->setValue('apigee_auth');
$web_assert
->waitForElementVisible('css', 'input[name="test_connection"]');
$this
->cssSelect('select[name="key_provider"]')[0]
->setValue('apigee_edge_private_file');
$web_assert
->waitForElementVisible('css', 'key_input_settings[organization]');
}
protected function validateForm(callable $visitFormAsAdmin) : void {
$web_assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$settings['settings']['file_private_path'] = (object) [
'value' => '',
'required' => TRUE,
];
$this
->writeSettings($settings);
$this
->invalidateKey();
$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');
$settings['settings']['file_private_path'] = (object) [
'value' => "{$this->siteDirectory}/private",
'required' => TRUE,
];
$this
->writeSettings($settings);
$visitFormAsAdmin();
$web_assert
->fieldValueEquals('Authentication type', 'oauth');
$web_assert
->fieldValueEquals('Username', '');
$web_assert
->fieldValueEquals('Password', '');
$web_assert
->fieldValueEquals('Organization', '');
$web_assert
->fieldValueEquals('Apigee Edge endpoint', '');
$page
->selectFieldOption('key_input_settings[auth_type]', EdgeKeyTypeInterface::EDGE_AUTH_TYPE_BASIC);
$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());
$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"]');
$this
->cssSelect('select[name="key_input_settings[auth_type]"]')[0]
->setValue('oauth');
$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());
$page
->fillField('Password', '');
$this
->assertTrue($this
->cssSelect('input[name="test_connection"]')[0]
->hasAttribute('disabled'));
$page
->fillField('Password', $this->password);
$this
->assertFalse($this
->cssSelect('input[name="test_connection"]')[0]
->hasAttribute('disabled'));
$this
->assertSendRequestMessage('.messages--status', 'Connection successful.');
$web_assert
->elementNotExists('css', 'details[data-drupal-selector="edit-debug"]');
$token_file_path = $this->container
->get('file_system')
->realpath(OauthTokenFileStorage::DEFAULT_DIRECTORY . '/oauth.dat');
$this
->assertFileNotExists($token_file_path);
$this
->cssSelect('select[name="key_input_settings[auth_type]"]')[0]
->setValue('basic');
$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);
$page
->pressButton('op');
$this
->assertSession()
->pageTextContains('Connection successful.');
$visitFormAsAdmin();
$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);
$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: ");
$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);
$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);
$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);
$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);
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);
}
$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');
$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', '');
$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', '');
}
public function assertSendRequestMessage($message_selector, $message) {
$web_assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$page
->pressButton('Send request');
$this
->assertNotNull($web_assert
->waitForElementVisible('css', '.ajax-progress.ajax-progress-throbber', 30000));
$web_assert
->assertWaitOnAjaxRequest(30000);
$web_assert
->elementTextContains('css', $message_selector, $message);
}
}