Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpTestTrait - Allow one to easily authenticate subrequests #21261

Merged
merged 1 commit into from
Sep 28, 2021

Conversation

totten
Copy link
Member

@totten totten commented Aug 26, 2021

Overview

Update HttpTestTrait to make it is easier for E2E tests to send authenticated requests on behalf of different users/contacts.

Step towards #21249

Before

The test writer must determine a specific credential (e.g. username+password or api_key or JWT) and send it as part of the request.

After

The test writer may pass the option authx_user or authx_contact_id to Guzzle. This will be converted to a JWT so that the HTTP call includes authentication credentials. A few examples:

// Add JWT credentials to every Guzzle request.
$http = $this->createGuzzle([
  'authx_user' =>  $GLOBALS['_CV']['DEMO_USER'],
]);
$response = $http->get('route://civicrm/dashboard');

// Add JWT credentials to a single request.
$http = $this->createGuzzle();
$response = $http->get('route://civicrm/dashboard', [
  'authx_user' =>  $GLOBALS['_CV']['ADMIN_USER'],
]);

// Perform a stateful login via JWT.
$http = $this->createGuzzle(['cookies' => new CookieJar()]);
$response = $http->get('route://civicrm/authx/login', [
  'authx_contact_id' => 100,
]);

Full list of options:

  • authx_ttl (int): Seconds of validity for JWT's
  • authx_host (string): Only send tokens for the given host. (Default per CIVICRM_UF_BASEURL)
  • authx_contact_id (int): The CiviCRM contact to authenticate with
  • authx_user (string): The CMS user to authenticate with
  • authx_flow (string): How to format the auth token. One of: 'param', 'xheader', 'header'.

@civibot
Copy link

civibot bot commented Aug 26, 2021

(Standard links)

@civibot civibot bot added the master label Aug 26, 2021
```php
// Add JWT credentials to every Guzzle request
$http = $this->createGuzzle([
  'authx_user' => 'admin',
]);
$response = $http->get('route://civicrm/dashboard');

// Add JWT credentials to a single request
$http = $this->createGuzzle();
$response = $http->get('route://civicrm/dashboard', [
  'authx_user' => 'demo',
]);

// Perform a stateful login via JWT
$http = $this->createGuzzle(['cookies' => new CookieJar()]);
$response = $http->get('route://civicrm/authx/login', [
  'authx_contact_id' => 100,
]);
```
@totten totten changed the title HttpTestTrait - Allow one to easily add authenticate subrequests HttpTestTrait - Allow one to easily authenticate subrequests Aug 28, 2021
@eileenmcnaughton
Copy link
Contributor

@seamuslee001 thoughts? This seems 'safe' to me in that it seems to be not otherwise invoked & adds test cover / ease of testing. Maybe that is high enough bar for this one.

@seamuslee001
Copy link
Contributor

This seems fine to me merging

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants