oauth-2.0azure-active-directoryautomated-testscypressweb-testing

Cypress does not log in by cy.setCookie() in AzureDevOps project


I am trying to run automation tests with cypress in a project set up by interns at the company I myself is now an intern at. The website uses OAuth 2.0 and AAD for the login process and there is currently no API POST endpoint for the login. When I try to reach the login page I get caught in an infinite redirection loop that ends in error 414, so to avoid this I am trying to set the cookie with the JWT token that the login process creates before getting to the website. This is my code

describe('Mock authentication so I can test the website', () => {
  beforeEach(() =>{
    cy.clearCookies();
  })
  it('Visits BoostApps dev site', () => {
    cy.viewport(1200,1800)
    
    cy.setCookie('access_token', 'Token is placed here', {
      domain: 'url to website',
      path: '/',
      secure: false,
      httpOnly: false,
    });
    
    cy.debug().getCookie('access_token')
    
    cy.visit('url to website', {
      failOnStatusCode: false,
    }); 

    cy.contains('Competitions').click() //Competitions are only available for logged in users
  });
});

When running this with a newly generated token from Postman I get the following error

! CypressError

cy.setCookie() had an unexpected error setting the requested cookie in Chrome.

> Error: Sanitizing cookie failed

I have set chromeWebSecurity: false in my config file

Tried solution 1

I have tried using different browsers with third party cookies allowed

same results

Tried solution 2

I have tried using c.origin() in different ways

same results

I have also tried a huge number of other small solutions in code differences supplied to me by Chatgpt. I am fairly new to testing and very new to cypress. My coding knowledge is fairly low so I am wondering if anyone else have encountered this issue or if there is a good way to go through with the authorization without using the login ui.


Solution

  • It will be some bad formatting in the token.

    Take a look at these tests:

    Well formed token

    it('sets a well-formed token in a cookie', () => {
      const token = '123'
      cy.setCookie('test', token)
    })
    

    enter image description here


    Malformed token

    it('sets a badly-formed token in a cookie', () => {
    
      // multi-line token string 
      const token = `  
      123`
      cy.setCookie('test', token)
    })
    

    This throws the same error you have:

    enter image description here