登录用户使用JWT令牌在Cypress中

huangapple go评论53阅读模式
英文:

Login a user by JWT token in cypress

问题

I have a scenario where I need to call multiple APIs to obtain the access key and secret key to fetch an AWS Secret Manager secret key. I then use those keys to generate a JWT token and log in the user. Although I can successfully log in, any command I write after that results in an exception being thrown.

> CypressError: Cypress detected that you returned a promise from a
> command while also invoking one or more cy commands in that promise.

code:
helper class:

  generateTokenAndLogin(email: string) {
    cy.request({
      url: "https://xxx/get-access-key",
    }).then((access) => {
      this.accessKey = access.body.value;
      cy.request({
        url: "https://xxx/get-secret-key",
      }).then((secret) => {
        this.secretKey = secret.body.value;
        //this is returning secret key correctly
        this.getSecretKey().then((response) => {
          //fetch secret from aws
          cy.task("jwt", {
            payload: {
              email: email,
            },
            privateKey: response.SecretString as string,
          }).then((token) => {
            cy.visit(`myweb.com/login?id_token=${token}`);
          });
        });
      });
    });
  }

inside module.exports

on('task', {
    jwt(data: any) {
        const token = jwt.sign(data.payload, data.privateKey, { algorithm: 'RS256', expiresIn: '1h'});
        return token;
    }
});

cypress test:

describe('Testing', () => {
  const jwtHelper = new JWTHelper();
  before(()=> {
    jwtHelper.generateToken();
  })
  it('JWT', () => {
    cy.get('left-nav').should('be.visible'); //failing on all subsequent commands
  });
});

The Cypress login function is successful when there is no command after that, all the subsequent commands are failing.

英文:

I have a scenario where I need to call multiple APIs to obtain the access key and secret key to fetch an AWS Secret Manager secret key. I then use those keys to generate a JWT token and log in the user. Although I can successfully log in, any command I write after that results in an exception being thrown.

> CypressError: Cypress detected that you returned a promise from a
> command while also invoking one or more cy commands in that promise.

code:
helper class:

  generateTokenAndLogin(email: string) {
    cy.request({
      url: "https://xxx/get-access-key",
    }).then((access) => {
      this.accessKey = access.body.value;
      cy.request({
        url: "https://xxx/get-secret-key",
      }).then((secret) => {
        this.secretKey = secret.body.value;
        //this is returning secret key correctly
        this.getSecretKey().then((response) => {
          //fetch secret from aws
          cy.task("jwt", {
            payload: {
              email: email,
            },
            privateKey: response.SecretString as string,
          }).then((token) => {
            cy.visit(`myweb.com/login?id_token=${token}`);
          });
        });
      });
    });
  }

inside module.exports

on('task', {
    jwt(data: any) {
        const token = jwt.sign(data.payload, data.privateKey, { algorithm: 'RS256', expiresIn: '1h'});
        return token;
    }
});

cypress test:

describe('Testing', () => {
  const jwtHelper = new JWTHelper();
  before(()=> {
    jwtHelper.generateToken();
  })
  it('JWT', () => {
    cy.get('left-nav').should('be.visible'); //failing on all subsequent commands
  });
});

The Cypress login function is successful when there is no command after that, all the subsequent commands are failing.

答案1

得分: 2

这可能是深层嵌套的 cy.visit() 导致的问题,你可以在错误消息的正文中进行检查。

如果是这样,尝试将令牌保存为环境变量。

generateTokenAndLogin(email: string) {
  cy.request('https://xxx/get-access-key').then((access) => {
      this.accessKey = access.body.value;
      cy.request('https://xxx/get-secret-key').then((secret) => {
        this.secretKey = secret.body.value;
        // 此处正确返回秘钥
        this.getSecretKey().then((response) => {
          // 从 AWS 获取秘钥
          cy.task("jwt", {
            payload: {
              email: email,
            },
            privateKey: response.SecretString as string,
          })
          .then(token => Cypress.env('token', token))
        });
      });
    });
  }
const jwtHelper = new JWTHelper();

before(() => {
  jwtHelper.generateToken();
})

beforeEach(() => {
  cy.visit(`myweb.com/login?id_token=${Cypress.env('token')}`)
})

it('JWT', () => {
  cy.get('left-nav').should('be.visible'); 
})
英文:

It's probably the deeply nested cy.visit() that causes the problem, you can check in the body of the error message.

If so, try saving the token as an environment variable.

generateTokenAndLogin(email: string) {
  cy.request('https://xxx/get-access-key').then((access) => {
      this.accessKey = access.body.value;
      cy.request('https://xxx/get-secret-key').then((secret) => {
        this.secretKey = secret.body.value;
        //this is returning secret key correctly
        this.getSecretKey().then((response) => {
          //fetch secret from aws
          cy.task("jwt", {
            payload: {
              email: email,
            },
            privateKey: response.SecretString as string,
          })
          .then(token => Cypress.env('token', token))
        });
      });
    });
  }
const jwtHelper = new JWTHelper();

before(() => {
  jwtHelper.generateToken();
})

beforeEach(() => {
  cy.visit(`myweb.com/login?id_token=${Cypress.env('token')}`)
})

it('JWT', () => {
  cy.get('left-nav').should('be.visible'); 
})

huangapple
  • 本文由 发表于 2023年4月7日 00:02:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定