Authentication Tools
This package allows a user to login to Crossroads.net domains without using the login page.
Tools provided include:
Install with
npm install crds-cypress-login
MP Login
Allows an existing user to login to crossroads.net through Ministry Platform authentication, without using the UI.
Create Login Custom Command
Create a custom Cypress login command in your cypress/support/commands.js
file so it can be configured once and reused by any test.
The MinistryPlatformLoginPlugin constructor requires an environment: int
, demo
or an empty string for prod. No other environment variables are required.
// Import and configure the plugin
import { MinistryPlatformLoginPlugin } from 'crds-cypress-login';
const mpPlugin = MinistryPlatformLoginPlugin('int');
// Create custom login command
Cypress.Commands.add('mpLogin', (email, password) => {
return cy.wrap(mpPlugin.GetLoginCookies(email, password), {timeout: cy.responseTimeout})
.then((cookies) => {
cookies.forEach((c) => {
cy.setCookie(c.name, c.value);
});
return cy.reload();
});
});
Now it can be used in your test:
it('Tests logged in functionality', () => {
cy.mpLogin('test@email.com', 'password123')
.then(() => {
//Test stuff inside a .then() clause
//because logging in and reloading the page takes a bit
//and you want it to be ready before continuing to test
});
});
Create Stay Logged In Custom Command
If you want a user to stay logged in between tests, create a stay logged in command in your cypress/support/commands.js
.
This example assumes you've already configured the mpPlugin
variable above.
// Create custom stay logged in command
Cypress.Commands.add('mpStayLoggedIn', () => {
mpPlugin.GetLoginCookieNames()
.forEach((c) => {
Cypress.Cookies.preserveOnce(c);
});
});
Now it can be used in your tests like:
before(() => {
cy.mpLogin('test@email.com', 'password123');
});
beforeEach(() => {
cy.mpStayLoggedIn();
});
it('Logged in test', () => {
//User is logged in
});
it('Still logged in', () => {
//User is still logged in
});
Details for the curious
The MinistryPlatformLoginPlugin does not use Cypress commands so its functions need to be manipulated a bit for them to work correctly with Cypress tests.
- The
mpPlugin.GetLoginCookies
returns a Promise that is not a native Cypress Promise, so it must be called inside acy.wrap
clause so Cypress can access its results in sync with the test.mpPlugin.GetLoginCookies
makes an API request, so we also need to configure its timeout to match thecy.request()
timeout by adding the{timeout: 30000}
option. - The
mpPlugin.GetLoginCookieNames
returns an array of strings directly, so it does not need to be wrapped to be in sync with Cypress tests. Unfortunately theCypress.Cookies.preserveOnce()
function does not seem to accept a list of strings containing cookie names, so each must be configured individually.
Okta Login
Allows an existing user to sign in to crossroads.net through Okta authentication, without using the UI.
Create Login Custom Command
Create a custom Cypress login command in your cypress/support/commands.js
file so it can be configured once and reused by any test.
The OktaLoginPlugin constructor requires two parameters; the Okta domain used for authentication (ex. https://authpreview.crossroads.net
) and an Okta Application Client ID with PKCE enabled. No other environment variables are required.
// Import and configure the plugin
import { OktaLoginPlugin } from 'crds-cypress-login';
const oktaPlugin = OktaLoginPlugin(`https://authpreview.crossroads.net`, `exampleClientId`);
// Create custom command
Cypress.Commands.add('oktaLogin', (email, password) => {
return cy.wrap(oktaPlugin.login(email, password), {timeout: 30000});
});
Now it can be used in your tests:
it('Tests logged in functionality', () => {
cy.oktaLogin('test@email.com', 'password123')
.then(() => {
// The login function does not navigate anywhere, so navigate somewhere first
cy.visit('/');
// test stuff
});
});
The Okta login process does not navigate anywhere or reload a page when it is complete, so tests will need handle this themselves.
Log out between tests
Okta stores session information in localstorage, not cookies, so a user will stay logged in between tests automatically. This is probably not what you want, so use cy.clearLocalStorage() between tests if you would like to log a user out.
Details for the curious
Like the MinistryPlatformLoginPlugin, the OktaLoginPlugin does not use Cypress commands so its functions need to be manipulated a bit for them to work correctly with Cypress tests.
- The
oktaPlugin.login
returns a Promise that is not a native Cypress Promise, so it must be called inside acy.wrap
clause so Cypress can access its results in sync with the test. It also makes an API request, so we need to configure its timeout to match thecy.request()
timeout by adding the{timeout: 30000}
option.