Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sample cypress script to bypass SSO

I am setting up new cypress tests to test some functionalities in Dynamics 365 application. But, I'm left with a browser window with the url https://login.microsoftonline.com/__/ and the text Whoops, there is no test to run.

describe('Initial Cypress Tests', () => {
    it('navigate to D365', () => {
        cy.visit('https://wipropoc.crm8.dynamics.com')
    })
})
like image 878
Akhil Tabjula Avatar asked Nov 17 '22 12:11

Akhil Tabjula


1 Answers

Would suggest you to directly do a POST call for getting SSO authentication token and fire cy.visit('https://wipropoc.crm8.dynamics.com') with the obtained token.

Here are the steps to follow from official documentation,

  1. Login when authentication is done on a 3rd party server.
  2. Parse tokens using cy.request().
  3. Manually set tokens on local storage.
  4. Map external hosts and point to local servers.

cy.request('POST', 'https://sso.corp.com/auth', { username: 'foo', password: 'bar' })
    .then((response) => {
    // pull out the location redirect
    const loc = response.headers['Location']

    // parse out the token from the url (assuming its in there)
    const token = parseOutMyToken(loc)

    // do something with the token that your web application expects
    // likely the same behavior as what your SSO does under the hood
    // assuming it handles query string tokens like this
    cy.visit('http://localhost:8080?token=' + token)

    // if you don't need to work with the token you can sometimes
    // just visit the location header directly
    cy.visit(loc)
    })

You can read more about this here - https://docs.cypress.io/guides/guides/web-security.html#Form-Submission-Redirects

Real time example - https://xebia.com/blog/how-to-use-azure-ad-single-sign-on-with-cypress/

like image 109
Kondasamy Jayaraman Avatar answered Dec 06 '22 19:12

Kondasamy Jayaraman