How to pass basic auth after SSO redirect?

Viewed 1477

I am new to Cypress and JS. The app I am testing redirects to a stub IDP page if not logged in. The app is also behind basic auth. All I am doing in the test is

cy.visit("https://username:password@app_url.com")

This redirects me to the stub IDP page where I am clicking login button as

cy.contains('button', 'Log in').click(). This redirects back to the app home page.

However, upon redirect, it's prompting me to fill in basic auth again. Is there a way to pass basic auth headers that are remembered after the redirect?

The stub IDP is for SAML based SSO and it's too much work or I should say I lack knowledge to create a server request to obtain auth token. So looking for a simple work around for now.

1 Answers

I was having the same problem and I just solved it using cy.intercept()!

You have to use cy.intercept on the new redirected URL and add the basic authorization header to the request.

  1. generate the basic auth header, you can generate it here: https://www.blitter.se/utils/basic-authentication-header-generator/

  2. find the target URL, for you its the app home page.

  • maybe there are several redirects, you can find the final one with basic auth when running the test in open mode and after canceling the popped-out window, you will see this in the runner

redirected url

  • the "new url" is the one you want
  1. in the test, add the intercept() command before clicking the button. Add the authorization header to the outgoing request, that is triggered when the test is being redirected.
cy.intercept(`<TARGET_URL>`, (req) => {
  req.headers['Authorization'] = `<BASIC_AUTH_HEADER_VALUE>`;
});

here you can read more about adding headers to outgoing request and intercept functionality: https://docs.cypress.io/api/commands/intercept#Adding-a-header-to-an-outgoing-request

Related