1

I am trying to save a token from DB and pass the token in the next API request in the body I have written the below code but it's not working, please help.

it.only('Change Password', () => {
    cy.ResetPassAPI(globalThis.data.dradminemail);// this generates a token in DB
    const resetoken = cy.task(
      'queryDb',
      `select _Token from tokens WHERE _Member_Id = 25372 and _Token_Type = "resetPassword" ORDER BY _id DESC LIMIT 1`,); // saving the token from DB generate from above

    

    cy.request({
      method: 'PATCH',
      url: 'https://xxx/xx/xx/changePasswordWithToken',
      body: {
        confirmPassword: 'xxx',
        password: 'xx',
        token: resetoken,  //passing the saved token from database
        uid: 1234,
      },
    });
  });
Fody
  • 23,754
  • 3
  • 20
  • 37
ruffainn
  • 83
  • 1
  • 14

3 Answers3

1

You can use Cypress.env() for this.

it.only('Change Password', () => {
  cy.ResetPassAPI(globalThis.data.dradminemail) // this generates a token in DB
  const resetoken = cy.task(
    'queryDb',
    `select _Token from tokens WHERE _Member_Id = 25372 and _Token_Type = "resetPassword" ORDER BY _id DESC LIMIT 1`
  ) // saving the token from DB generate from above
  Cypress.env('token', resetoken.slice(12,48))

  cy.request({
    method: 'PATCH',
    url: 'https://xxx/xx/xx/changePasswordWithToken',
    body: {
      confirmPassword: 'xxx',
      password: 'xx',
      token: Cypress.env('token'), //passing the saved token from database
      uid: 1234,
    },
  })
})

After Slicing: after slicing

Alapan Das
  • 17,144
  • 3
  • 29
  • 52
  • Thanks for the help but the request body its sending back looks like this Body: {"confirmPassword":"cbrex1","password":"cbrex1","token":[{"_Token":"d20a784d-9ca3-4b4f-bdfa-c35051c6d8d8"}],"uid":25372} in token i only then string like "d20a784d-9ca3-4b4f-bdfa-c35051c6d8d8" but not the other part can you help me with that – ruffainn Jul 11 '22 at 19:19
  • 1
    Updated the answer with the `slice` method. – Alapan Das Jul 11 '22 at 19:57
1

You have the wrong syntax for cy.task()

Instead of

const resetoken = cy.task(...)

it should be

cy.task(...).then(resetoken => {

Full test:

it('Change Password', () => {
  cy.ResetPassAPI(globalThis.data.dradminemail);
  const query = 'select _Token from tokens WHERE _Member_Id = 25372 and _Token_Type = "resetPassword" ORDER BY _id DESC LIMIT 1';
  cy.task('queryDb', query)
    .then(queryResult => {
      const resetoken = queryResult._Token;
      cy.request({
        method: 'PATCH',
        url: 'https://xxx/xx/xx/changePasswordWithToken',
        body: {
          confirmPassword: 'xxx',
          password: 'xx',
          token: resetoken,  
          uid: 1234,
        },
      });
    });
});

You probably don't need the token anywhere else, once the PATCH has been sent.

Fody
  • 23,754
  • 3
  • 20
  • 37
  • Thanks a lot its one part is working but my problem now is the reset token I am getting is also coming with a column name like {_Token: 'c474f281-271b-4448-a447-7b2eabc6fb1a'} when I am passing this in the body its invalid as I need to only pass the key and not the whole part. Can you please with that. – ruffainn Jul 09 '22 at 20:30
  • 1
    Essentially the results of the select query are returned in an object, to access it use the property notation `result._Token`. I will update the answer. – Fody Jul 09 '22 at 20:39
  • Thank you for the response I have tried the above but its not returning any token in the body pasting the request Body: {"confirmPassword":"cbrex1","password":"cbrex1","uid":25372}. I wont wondering if we can use slice or split. – ruffainn Jul 11 '22 at 18:55
  • Sorry, no idea. From what you said in the first comment, this should work. – Fody Jul 11 '22 at 19:59
  • 1
    Thanks, fody for all your help it finally worked using this result[0]._Token – ruffainn Jul 11 '22 at 20:11
0

You can save it as an alias and then access it later with calling alias or using this keyword within function(){} instead of arrow function () => {}

// use function{} instead of () => {}
it.only('Change Password', function() {
    cy.ResetPassAPI(globalThis.data.dradminemail)
    cy.task(
      'queryDb',
      `select _Token from tokens WHERE _Member_Id = 25372 and _Token_Type = "resetPassword" ORDER BY _id DESC LIMIT 1`,)
      // task returns token so we save it with alias
      .as('resetToken')

    

    cy.request({
      method: 'PATCH',
      url: 'https://xxx/xx/xx/changePasswordWithToken',
      body: {
        confirmPassword: 'xxx',
        password: 'xx',
        token: this.resetToken, 
        uid: 1234,
      },
    })
  })
jjhelguero
  • 2,281
  • 5
  • 13