lundi 27 juillet 2020

JWT token seems to be changing between "before all" and first test in Cypress

I am running the following test on Cypress

before(() => {
      cy.horizonAuth()
      cy.log(sessionStorage.getItem('JWT'))
    });

//API requests

    it('Sending a regular API request to Welcome Messages', function() {
        cy.log(sessionStorage.getItem('JWT'))
        cy.request({
            method : 'POST',
            url : '/api/v1/organisations/welcome-message?actions=api/v1/organisations/welcome-message/set-welcome-messages',
            body : {"welcome_messages":[{"message":"<p>Test investor welcome message!</p>","type":"User"},{"message":"<p>Token from storage </p>","type":"Internal User"},{"message":"<p>Test entrepreneur welcome message!</p>","type":"Entrepreneur"}]},
            auth: {bearer: sessionStorage.getItem('JWT')}

    })
})

This is the auth command I am using

Cypress.Commands.add('horizonAuth', () => {
    let config = Cypress.config();
  
    cy.request({
      method: 'POST',
      url: Cypress.config('tokenUrl') + '/api/v1?actions=api/v1/login',
      body: {
        username: config.horizon.username,
        password: config.horizon.password,
      }
    }).then(response => {
        cy.setLocal
      window.sessionStorage.setItem('JWT', response.body['api/v1/login'].token)
    })
  })  

I noticed that my tests were failing the first time I ran the above, but if I ran the test a second time and commented out the cy.horizonAuth() then the test would pass. I decided to add the log commands to see the JWT token and it appears that the token is changing between the horizonAuth and the first test. This happens if I close the session and start a new session also as the first time it's run the Log comes back with "Null"

First time tests are run in session Second time tests are run in session Third time tests are run with horizonAuth commented out

Any help on this would be greatly appreciated as I am fairly new to Cypress.

Aucun commentaire:

Enregistrer un commentaire