cypress: Problems with origin on login when immediately redirecting

Current behavior

A single page web app is hosted on server A, and the login page is hosted on another server B using Authorization Code Grant with PKCE. The web app on server A does not have a public landing page with a login button, but instead immediately redirects to the login page on server B if the user has not been authenticated yet. After login using server B, the user is redirected to server A again. Authentication itself is handled via certificates not username/password. The certificate is specified in the clientCertificates section of the config file.

The baseUrl is set to domain A.

I tried to implement the login in Cypress using cy.session and cy.origin as suggested https://docs.cypress.io/guides/end-to-end-testing/auth0-authentication#Login-with-cy-origin:

Cypress.Commands.add("login", () => {
      const args = {};
      cy.session(
    args,
    () => {
      cy.visit("/");
      cy.origin("loginPageDomain.com", {}, () => {        
        cy.get("#certificateAuthenticationSelectionButton").click();
        cy.get("#login-button").click();
      });
    },
    {
      cacheAcrossSpecs: true,
    }
  );
});

However, when doing so I get a 401 error message for the first visit call:

The response we received from your web server was:

  > 401: Unauthorized

This was considered a failure because the status code was not `2xx`.

This http request was redirected '4' times to:
  - 302: redirect1
  - 302: redirect2
  - 302: redirect3
  - 302: redirect4
 
If you do not want status codes to cause failures pass the option: `failOnStatusCode: false`

If I add failOnStatusCode: false to the cy.visit method, i.e. cy.visit("/", {failOnStatusCode: false}); then the redirects are called but I get the error:

cy.origin() requires the first argument to be a different domain than top. You passed B to the origin command, while top is at B.

Either the intended page was not visited prior to running the cy.origin block or the cy.origin block may not be needed at all.

I also tried to leave out cy.origin but then when being redirected from server B to server A, cookies (sessionId and XSRF-Token) are provided for the session, however those are not set because those cookies are for domain A but they appears to be cross domain in the browser and since the sameSite property is not set, the cookies are not stored.

This is a problem in Chrome, Edge, Electron. When running in Firefox, the login works without using cy.origin but then the first cy.get in the first test fails with

Timed out retrying after 10000ms: The command was expected to run against origin https://B but the application is at origin https://A.

All further tests run without problems in Firefox. So, my workaround at the moment is to add a dummy test without any body which gets executed first. Then the other tests all run. However, I really need to test Chrome and Edge as well, so this is not a viable solution.

It appears to me that the base origin is set to the redirected domain B instead of the domain that I provided in the baseUrl.

Desired behavior

Overall I expect the login to work as in https://docs.cypress.io/guides/end-to-end-testing/auth0-authentication#Login-with-cy-origin

So, I would expect that

  1. I don’t get an error for the redirections which forces me to add the failOnStatusCode: false option
  2. Even though the first call is immediately redirected, Cypress should set as base origin the web app domain A not the redirected domain B.

Test code to reproduce

see in issue. Cannot provide more details since this is a company internal web app.

Cypress Version

12.3.0

Node version

v16.18.1

Operating System

Windows

Debug Logs

see in description

Other

No response

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 7
  • Comments: 18 (7 by maintainers)

Most upvoted comments

Right now there doesn’t seem to be enough information to reproduce the problem on our end. We’ll have to close this issue until we can reproduce it. This does not mean that your issue is not happening - it just means that we do not have a path to move forward.

Please open comment on this issue with a reproducible example, and we can look into it further. Here are some tips for providing a Short, Self Contained, Correct, Example and our own Troubleshooting Cypress guide.

I am experiencing the same (similar?) issues when trying to login into Okta and then immediately checking some condition. This is a simple snippet that fails for me after upgrading to Cypress 14.0.1.

cy.session(sessionIdentification, () => {
  cy.visit('/');
  cy.origin('{{okta domain url}}', { args: { username, password } }, ({ username, password }) => {
    cy.get('#idp-discovery-username').type(username);
    cy.get('#idp-discovery-submit').click();
    cy.get('#okta-signin-password').type(password);
    cy.get('#okta-signin-submit').click();
  });
  cy.url().should('contain', '/dashboard');
});

with the error:

The command was expected to run against origin {{ **original** app url }} but the application is at origin {{ **okta** domain url }}.

This commonly happens when you have either not navigated to the expected origin or have navigated away unexpectedly.

Using cy.origin() to wrap the commands run on {{ **okta** domain url }} will likely fix this issue.

cy.origin('{{ **okta** domain url }}', () => {
  <commands targeting {{ **okta** domain url }} go here>
})

It fails at the cy.url().should('contain', '/dashboard'); fragment, and Cypress suggests that I should use Okta’s domain instead of my original one, which is wrong. Adding simple cy.wait(5000) fixes issues as it forces Cypress to wait 5 seconds after clicking sign in so it lets the app to properly finish loading, but in my opinion this is a bad workaround rather than a proper solution.

  cy.session(sessionIdentification, () => {
    cy.visit('/');
    cy.origin('{{ **okta** domain url }}', { args: { username, password } }, ({ username, password }) => {
      cy.get('#idp-discovery-username').type(username);
      cy.get('#idp-discovery-submit').click();
      cy.get('#okta-signin-password').type(password);
      cy.get('#okta-signin-submit').click();
      cy.wait(5000);
    });
    cy.url().should('contain', '/dashboard');
  });

I have same issue, why does cypress care if page redirect to login page. Is cypress suppose to be test framework rather than testing browser security vulnerability?

I can’t make an example unfortunately as it’s not a public application I’m working on. But I got something working today but with some hacks. I’m planning on submitting a new issue on Monday so we can hopefully discuss.