lundi 19 octobre 2020

Setting cookies on browser with cypress

I need some help here, I want to login problematically using cypress for my tests. I'm getting a 200 response, seems to be login in correctly but after that I can't access like logged in to my app. I guess that what I'm missing to set the cookies on my browser to establish that session? Is that right?

Cypress.Commands.add("login", () => {
  cy.request({
    method: 'POST',
    form: true,
    url: 'http://localhost/api/v1/auth/login',
    headers: {
      'Content-Type': 'application/json', 
    body: {       
      "email": "user@gmail.com",
      "password": "pass"
    }
  }).then(response => {
    const target = response.body.email;
  })
})


describe('Set header and cookie', function() { 
  before('set cookie',function(){
    cy.setCookie("Cookie", "the cookie?");
  });
  it.only('tests login', function() {
    cy.login();
    cy.visit('http://localhost/my-documents')
  })
})
})

This is my response. I have never handle cookies before, I'm not sure about what I'm doing. Is "data" the cookie that I have to set on setCookie? I have tried that but clearly is wrong because the cookie changes with every session, I clearly need to study more. But may be someone can help me out here a little.

{
"success":true,
"message":"You are logged in.",
"data":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9hZHZvb2RsZS5sb2NhbGhvc3RcL2FwaVwvdjFcL2F1dGhcL2xvZ2luIiwiaWF0IjoxNjAzMTExNjM3LCJleHAiOjE2MDMxMTUyMzcsIm5iZiI6MTYwMzExMTYzNywianRpIjoiam5rVXZiZlVVNVF6OHdVQiIsInN1YiI6MjgsInBydiI6Ijg3ZTBhZjFlZjlmZDE1ODEyZmRlYzk3MTUzYTE0ZTBiMDQ3NTQ2YWEifQ.0et9Mpl8I_sXLHZi1yY4bMoP5SEiJWM3SdI_ywXvj3M",
"show_message":false
}

This is what my cookie tab on network (dev tools) says when I login into my app (i've erased the domain name)

enter image description here

Aucun commentaire:

Enregistrer un commentaire