# crds-cypress-login

> Cypress methods to log in to Crossroads

Latest version **3.0.0** (published 2021-03-04) · ISC license · 0 weekly downloads

## Install

```sh
npm install crds-cypress-login
pnpm add crds-cypress-login
yarn add crds-cypress-login
bun add crds-cypress-login
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2021-03-04 |
| First published | 2019-07-24 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 10.6 KB |
| Known vulnerabilities | 0 (+23 in 1 direct dependencies) |
| Install scripts | no |
| Author | Sarah Henney |
| Maintainers | crds_npm_org |

## Links

- npm: https://www.npmjs.com/package/crds-cypress-login
- Repository: https://github.com/crdschurch/crds-cypress-tools/tree/master/crds-cypress-login
- npm.io page: https://npm.io/package/crds-cypress-login

## Dependencies (2)

- [axios](https://npm.io/package/axios.md) ^0.21.1
- [@okta/okta-auth-js](https://npm.io/package/@okta/okta-auth-js.md) ^4.7.1

## Recent versions

- 3.0.0 (latest) — 2021-03-04
- 2.0.1 — 2021-01-26
- 2.0.0 — 2020-05-15
- 1.0.1 — 2019-09-20
- 1.0.0 — 2019-07-24

## README

# Authentication Tools

This package allows a user to login to Crossroads.net domains without using the login page.

Tools provided include:
- [MP Login](#mp-login)
- [Okta Login](#okta-login)

Install with
```bash
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.

```javascript
// 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:
```javascript
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.

```javascript
// 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:

```javascript
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 a `cy.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 the `cy.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 the `Cypress.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.

```javascript
// 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:
```javascript
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()](https://docs.cypress.io/api/commands/clearlocalstorage.html#Syntax) 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 a `cy.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 the `cy.request()` timeout by adding the `{timeout: 30000}` option.

---
_Source: https://npm.io/package/crds-cypress-login · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
