Skip to content
Snippets Groups Projects
Commit 05263c1f authored by Aaron Bauman's avatar Aaron Bauman
Browse files

Testing tests

parent bb5aacf2
No related branches found
No related tags found
No related merge requests found
...@@ -125,6 +125,7 @@ class SalesforceJWTPlugin extends SalesforceAuthProviderPluginBase { ...@@ -125,6 +125,7 @@ class SalesforceJWTPlugin extends SalesforceAuthProviderPluginBase {
$form['encrypt_key'] = [ $form['encrypt_key'] = [
'#title' => 'Private Key', '#title' => 'Private Key',
'#type' => 'select', '#type' => 'select',
'#empty_option' => $this->t('- Select -'),
'#options' => $this->keyRepository->getKeyNamesAsOptions(['type' => 'authentication']), '#options' => $this->keyRepository->getKeyNamesAsOptions(['type' => 'authentication']),
'#required' => TRUE, '#required' => TRUE,
'#default_value' => $this->getCredentials()->getKeyId(), '#default_value' => $this->getCredentials()->getKeyId(),
...@@ -138,6 +139,10 @@ class SalesforceJWTPlugin extends SalesforceAuthProviderPluginBase { ...@@ -138,6 +139,10 @@ class SalesforceJWTPlugin extends SalesforceAuthProviderPluginBase {
*/ */
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::validateConfigurationForm($form, $form_state); parent::validateConfigurationForm($form, $form_state);
if (empty($form_state->getValue('provider_settings')) && $form_state->getValue('provider_settings') == self::defaultConfiguration()) {
$form_state->setError($form, $this->t('Please fill in JWT provider settings.'));
return;
}
$this->setConfiguration($form_state->getValue('provider_settings')); $this->setConfiguration($form_state->getValue('provider_settings'));
try { try {
// Bootstrap here by setting ID to provide a key to token storage. // Bootstrap here by setting ID to provide a key to token storage.
......
<?php
namespace Drupal\Tests\salesforce_jwt\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\simpletest\WebTestBase;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\key\Functional\KeyTestTrait;
/**
* Test JWT Auth.
*
* @group salesforce_jwt
*/
class SalesforceJwtTest extends WebDriverTestBase {
use KeyTestTrait;
public static $modules = ['key', 'typed_data', 'dynamic_entity_reference', 'salesforce', 'salesforce_test_rest_client', 'salesforce_jwt'];
/**
* Admin user to test form.
*
* @var \Drupal\user\Entity\User
*/
protected $adminUser;
/**
* Id of shared cert key.
*/
const KEY_ID = 'salesforce_jwt_test_key';
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->adminUser = $this->drupalCreateUser(['authorize salesforce']);
$this->drupalLogin($this->adminUser);
$this->createTestKey(self::KEY_ID);
}
/**
* Test that saving mapped nodes enqueues them for push to Salesforce.
*/
public function testJwtAuth() {
$assert_session = $this->assertSession();
$page = $this->getSession()->getPage();
$this->drupalGet('admin/config/salesforce/authorize/add');
$page->fillField('provider', 'jwt');
$assert_session->assertWaitOnAjaxRequest();
$page->fillField('label', $this->randomString());
$page->fillField('label', $this->randomString());
$edit = [
'provider_settings[consumer_key]' => 'foo',
'provider_settings[login_user]' => 'bar',
'provider_settings[login_url]' => 'zee',
'provider_settings[encrypt_key]' => self::KEY_ID,
];
foreach ($edit as $key => $value) {
$page->fillField($key, $value);
}
$page->pressButton('Save');
// $this->assertUrl('admin/config/salesforce/authorize/add');
// $this->assertTrue(FALSE);
$assert_session->addressEquals('foo');
$assert_session->addressEquals('bar');
// $this->assertText('Salesforce consumer key field is required.');
// $this->assertText('Salesforce login user field is required.');
// $this->assertText('Private Key field is required.');
// $edit = [
// 'label' => $this->randomString(),
// 'id' => strtolower($this->randomMachineName()),
// 'provider' => 'jwt',
// 'save_default' => 1,
// ];
// $edit['provider_settings'] += [
// 'consumer_key' => 'foo',
// 'login_user' => 'bar',
// 'login_url' => 'zee',
// 'encrypt_key' => self::KEY_ID,
// ];
// $this->drupalPostForm('admin/config/salesforce/authorize/add', $edit, 'submit');
// $this->assertUrl('admin/config/salesforce/authorize/add');
//// $this->assertTrue(FALSE);
// $this->assertUrl('admin/config/salesforce/authorize/list');
// $this->drupalGet('admin/config/salesforce/authorize/list');
// /** @var \Drupal\salesforce_push\PushQueue $queue */
// $queue = \Drupal::service('queue.salesforce_push');
// $this->assertEquals(0, $queue->numberOfItems());
//
// Node::create([
// 'type' => 'salesforce_mapping_test_content',
// 'title' => 'Test Example',
// ]
// )->save();
// $this->assertEquals(1, $queue->numberOfItems());
//
// Node::create([
// 'type' => 'salesforce_mapping_test_content',
// 'title' => 'Test Example 2',
// ]
// )->save();
// $ this->assertEquals(2, $queue->numberOfItems());
}
}
\ No newline at end of file
...@@ -34,6 +34,7 @@ class SalesforceAuthForm extends EntityForm { ...@@ -34,6 +34,7 @@ class SalesforceAuthForm extends EntityForm {
'#type' => 'textfield', '#type' => 'textfield',
'#description' => $this->t('User-facing label for this project, e.g. "OAuth Full Sandbox"'), '#description' => $this->t('User-facing label for this project, e.g. "OAuth Full Sandbox"'),
'#default_value' => $auth->label(), '#default_value' => $auth->label(),
'#required' => TRUE,
]; ];
$form['id'] = [ $form['id'] = [
...@@ -44,6 +45,7 @@ class SalesforceAuthForm extends EntityForm { ...@@ -44,6 +45,7 @@ class SalesforceAuthForm extends EntityForm {
'exists' => [$this, 'exists'], 'exists' => [$this, 'exists'],
'source' => ['label'], 'source' => ['label'],
], ],
'#required' => TRUE,
]; ];
// This is the element that contains all of the dynamic parts of the form. // This is the element that contains all of the dynamic parts of the form.
...@@ -82,6 +84,14 @@ class SalesforceAuthForm extends EntityForm { ...@@ -82,6 +84,14 @@ class SalesforceAuthForm extends EntityForm {
$plugin = $this->entity->authManager()->createInstance($form_state->getValue('provider')); $plugin = $this->entity->authManager()->createInstance($form_state->getValue('provider'));
$form['settings']['provider_settings'] += $plugin->buildConfigurationForm([], $form_state); $form['settings']['provider_settings'] += $plugin->buildConfigurationForm([], $form_state);
} }
elseif ($form_state->getUserInput()) {
$input = $form_state->getUserInput();
if (!empty($input['provider'])) {
$plugin = $this->entity->authManager()
->createInstance($input['provider']);
$form['settings']['provider_settings'] += $plugin->buildConfigurationForm([], $form_state);
}
}
$form['save_default'] = [ $form['save_default'] = [
'#type' => 'checkbox', '#type' => 'checkbox',
'#title' => 'Save and set default', '#title' => 'Save and set default',
...@@ -115,6 +125,11 @@ class SalesforceAuthForm extends EntityForm { ...@@ -115,6 +125,11 @@ class SalesforceAuthForm extends EntityForm {
return; return;
} }
if (!empty($form_state->getErrors())) {
// Don't bother processing plugin validation if we already have errors.
return;
}
$this->entity->getPlugin()->validateConfigurationForm($form, $form_state); $this->entity->getPlugin()->validateConfigurationForm($form, $form_state);
} }
......
...@@ -82,7 +82,7 @@ abstract class SalesforceAuthProviderPluginBase extends Salesforce implements Sa ...@@ -82,7 +82,7 @@ abstract class SalesforceAuthProviderPluginBase extends Salesforce implements Sa
* Comment. * Comment.
*/ */
public function __construct(array $configuration, $plugin_id, $plugin_definition, ClientInterface $httpClient, SalesforceAuthTokenStorageInterface $storage) { public function __construct(array $configuration, $plugin_id, $plugin_definition, ClientInterface $httpClient, SalesforceAuthTokenStorageInterface $storage) {
$this->id = $configuration['id']; $this->id = !empty($configuration['id']) ? $configuration['id'] : NULL;
$this->configuration = $configuration; $this->configuration = $configuration;
$this->pluginDefinition = $plugin_definition; $this->pluginDefinition = $plugin_definition;
$this->pluginId = $plugin_id; $this->pluginId = $plugin_id;
......
<?php
namespace Drupal\salesforce\Tests;
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use OAuth\Common\Http\Client\ClientInterface;
use OAuth\Common\Http\Uri\UriInterface;
/**
* Wraps Guzzle HTTP client for an OAuth ClientInterface.
*/
class TestHttpClientWrapper implements ClientInterface {
/**
* Guzzle HTTP Client service.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* HttpClientWrapper constructor.
*
* @param \GuzzleHttp\ClientInterface $httpClient
* Guzzle HTTP client service, from core http_client.
*/
public function __construct(GuzzleClientInterface $httpClient) {
$this->httpClient = $httpClient;
}
/**
* {@inheritdoc}
*/
public function retrieveResponse(
UriInterface $endpoint,
$requestBody,
array $extraHeaders = [],
$method = 'POST'
) {
// This method is only used to Salesforce OAuth. Based on the given args,
// return a hard-coded version of the expected response.
if (is_array($requestBody) && array_key_exists('grant_type', $requestBody)) {
switch ($requestBody['grant_type']) {
case 'authorization_code':
return file_get_contents(__DIR__ . './oauthResponse.json');
case 'urn:ietf:params:oauth:grant-type:jwt-bearer':
return file_get_contents(__DIR__ . './jwtAuthResponse.json');
}
}
return '';
}
}
{"access_token":"XXXXXXXXXXXXXXX!XXXXXXXXXXXXXXXXXXX_XXXXXXXXXXXXXX.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","scope":"visualforce wave_api custom_permissions web openid chatter_api api id eclair_api full","instance_url":"https://na46.salesforce.com","id":"https://login.salesforce.com/id/XXXXXXXXXXXXXXXXXX/XXXXXXXXXXXXXXXXXX","token_type":"Bearer"}
\ No newline at end of file
{"access_token":"XXXXXXXXXXXXXXX!XXXXXXXX.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","refresh_token":"XXXXXXXXXXXXXXXXXXX.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","signature":"XXXXXXXXXXXXXXX+XXXX+XXXXXXXXXXXXXXXXXXXX/X=","scope":"refresh_token visualforce wave_api web custom_permissions openid chatter_api id api full","id_token":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXX","instance_url":"https://na46.salesforce.com","id":"https://login.salesforce.com/id/XXXXXXXXXXXXXXXEAS/XXXXXXXXXXXXXXXXXX","token_type":"Bearer","issued_at":"1566589321680"}
\ No newline at end of file
...@@ -4,6 +4,7 @@ namespace Drupal\salesforce_test_rest_client; ...@@ -4,6 +4,7 @@ namespace Drupal\salesforce_test_rest_client;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase; use Drupal\Core\DependencyInjection\ServiceProviderBase;
use Drupal\salesforce\Tests\TestHttpClientWrapper;
use Drupal\salesforce\Tests\TestRestClient; use Drupal\salesforce\Tests\TestRestClient;
use Drupal\salesforce\Tests\TestHttpClientFactory; use Drupal\salesforce\Tests\TestHttpClientFactory;
use Drupal\salesforce\Tests\TestSalesforceAuthProviderPluginManager; use Drupal\salesforce\Tests\TestSalesforceAuthProviderPluginManager;
...@@ -26,6 +27,8 @@ class SalesforceTestRestClientServiceProvider extends ServiceProviderBase { ...@@ -26,6 +27,8 @@ class SalesforceTestRestClientServiceProvider extends ServiceProviderBase {
->setClass(TestRestClient::class); ->setClass(TestRestClient::class);
$container->getDefinition('plugin.manager.salesforce.auth_providers') $container->getDefinition('plugin.manager.salesforce.auth_providers')
->setClass(TestSalesforceAuthProviderPluginManager::class); ->setClass(TestSalesforceAuthProviderPluginManager::class);
$container->getDefinition('salesforce.http_client_wrapper')
->setClass(TestHttpClientWrapper::class);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment