Skip to content

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

License

Notifications You must be signed in to change notification settings

joelmeiller/cypress-msal-login

Repository files navigation

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 V1 for MSAL library <= 2

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

Installation V2 for MSAL library = 3

This package nomore depends on the MSAL browser library and can be added directly

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(
  {
    username: '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

About

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

Resources

License

Stars

Watchers

Forks

Packages