cypress-msal-login
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

cypress-msal-login

Cypress command extension to log into a SSO App through the Microsoft Authentication flow using the MSAL library in the app.

Installation

This package depends on the MSAL browser library to be installed in your application (peerDependency)

npm install @azure/msal-browser

Add the cypress package as a dev dependency

npm install --dev cypress-msal-login

## Login with MSAL account

The login command requires three parameters. Here is a minimal example:

cy.msalLogin(
  {
    email: 'my-test-user@cypress.com',
    password: 'whatever'
  },
  {
    auth: {
    clientId: '<application-client-id>',
    authority: 'https://login.microsoftonline.com/<application-tenant-id>',
    },
    cache: {
      cacheLocation: BrowserCacheLocation.LocalStorage,
    },
  },
  ['openid', 'profile', 'user', 'offline_access'],
)
  • User Credentials The cypess test user's credentials (email and password). I strongly recommend to create a dedicated Cypress test user with specific rights.

  • Configuration The configuration as provided to your msal instance. If you set the settings using envirenment variables (which you probably should), you might need to adjust the configuration accordingly for Cypress. See example below and take a look at the documentation cypress/environment-variables

import { Configuration, BrowserCacheLocation } from '@azure/msal-browser'

const msalConfig: Configuration = {
  auth: {
    clientId: Cypress.env('MSAL_CLIENT_ID'),
    authority: `https://login.microsoftonline.com/${Cypress.env('MSAL_TENANT_ID')}`,
  },
  cache: {
    cacheLocation: BrowserCacheLocation.LocalStorage,
    storeAuthStateInCookie: true,
  },
}

Session token

The session token (access_token) is exposes as alias sessionToken and you can get it like this:

  cy.get('@sessionToken').then(sessionToken => {
    // ... do whatever you want with it.
  })

Trouble-shoot

  • Scope The scope of the token. Make sure you include at least the openid, profile, user and offline_access

Package Sidebar

Install

npm i cypress-msal-login

Weekly Downloads

1,104

Version

2.0.1

License

MIT

Unpacked Size

854 kB

Total Files

183

Last publish

Collaborators

  • joel.meiller