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

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

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:

  1. generateTokenAndLogin(email: string) {
  2. cy.request({
  3. url: "https://xxx/get-access-key",
  4. }).then((access) => {
  5. this.accessKey = access.body.value;
  6. cy.request({
  7. url: "https://xxx/get-secret-key",
  8. }).then((secret) => {
  9. this.secretKey = secret.body.value;
  10. //this is returning secret key correctly
  11. this.getSecretKey().then((response) => {
  12. //fetch secret from aws
  13. cy.task("jwt", {
  14. payload: {
  15. email: email,
  16. },
  17. privateKey: response.SecretString as string,
  18. }).then((token) => {
  19. cy.visit(`myweb.com/login?id_token=${token}`);
  20. });
  21. });
  22. });
  23. });
  24. }

inside module.exports

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

cypress test:

  1. describe('Testing', () => {
  2. const jwtHelper = new JWTHelper();
  3. before(()=> {
  4. jwtHelper.generateToken();
  5. })
  6. it('JWT', () => {
  7. cy.get('left-nav').should('be.visible'); //failing on all subsequent commands
  8. });
  9. });

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:

  1. generateTokenAndLogin(email: string) {
  2. cy.request({
  3. url: "https://xxx/get-access-key",
  4. }).then((access) => {
  5. this.accessKey = access.body.value;
  6. cy.request({
  7. url: "https://xxx/get-secret-key",
  8. }).then((secret) => {
  9. this.secretKey = secret.body.value;
  10. //this is returning secret key correctly
  11. this.getSecretKey().then((response) => {
  12. //fetch secret from aws
  13. cy.task("jwt", {
  14. payload: {
  15. email: email,
  16. },
  17. privateKey: response.SecretString as string,
  18. }).then((token) => {
  19. cy.visit(`myweb.com/login?id_token=${token}`);
  20. });
  21. });
  22. });
  23. });
  24. }

inside module.exports

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

cypress test:

  1. describe('Testing', () => {
  2. const jwtHelper = new JWTHelper();
  3. before(()=> {
  4. jwtHelper.generateToken();
  5. })
  6. it('JWT', () => {
  7. cy.get('left-nav').should('be.visible'); //failing on all subsequent commands
  8. });
  9. });

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

答案1

得分: 2

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

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

  1. generateTokenAndLogin(email: string) {
  2. cy.request('https://xxx/get-access-key').then((access) => {
  3. this.accessKey = access.body.value;
  4. cy.request('https://xxx/get-secret-key').then((secret) => {
  5. this.secretKey = secret.body.value;
  6. // 此处正确返回秘钥
  7. this.getSecretKey().then((response) => {
  8. // 从 AWS 获取秘钥
  9. cy.task("jwt", {
  10. payload: {
  11. email: email,
  12. },
  13. privateKey: response.SecretString as string,
  14. })
  15. .then(token => Cypress.env('token', token))
  16. });
  17. });
  18. });
  19. }
  1. const jwtHelper = new JWTHelper();
  2. before(() => {
  3. jwtHelper.generateToken();
  4. })
  5. beforeEach(() => {
  6. cy.visit(`myweb.com/login?id_token=${Cypress.env('token')}`)
  7. })
  8. it('JWT', () => {
  9. cy.get('left-nav').should('be.visible');
  10. })
英文:

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.

  1. generateTokenAndLogin(email: string) {
  2. cy.request('https://xxx/get-access-key').then((access) => {
  3. this.accessKey = access.body.value;
  4. cy.request('https://xxx/get-secret-key').then((secret) => {
  5. this.secretKey = secret.body.value;
  6. //this is returning secret key correctly
  7. this.getSecretKey().then((response) => {
  8. //fetch secret from aws
  9. cy.task("jwt", {
  10. payload: {
  11. email: email,
  12. },
  13. privateKey: response.SecretString as string,
  14. })
  15. .then(token => Cypress.env('token', token))
  16. });
  17. });
  18. });
  19. }
  1. const jwtHelper = new JWTHelper();
  2. before(() => {
  3. jwtHelper.generateToken();
  4. })
  5. beforeEach(() => {
  6. cy.visit(`myweb.com/login?id_token=${Cypress.env('token')}`)
  7. })
  8. it('JWT', () => {
  9. cy.get('left-nav').should('be.visible');
  10. })

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:

确定