简体   繁体   English

无法通过使用 cypress 的 amazon cognito 身份验证

[英]Unable to by pass amazon cognito authentication using cypress

I am trying to get login, but login functionality takes place in 'AWS Cognito Authetication', which is making my life little mess.我正在尝试登录,但登录功能发生在“AWS Cognito Authetication”中,这让我的生活变得一团糟。

What happens, when user enters base url on browser, app navigates to 'AWS Cognito' message, where I enter my credentials, after adding credentials, app is showing me an alert message ie会发生什么,当用户在浏览器上输入基本 URL 时,应用程序导航到“AWS Cognito”消息,我在其中输入我的凭据,添加凭据后,应用程序向我显示一条警报消息,即

An error was encountered with the requested page.

Screenshot is attached.附上截图。 I have checked network logs, but it is showing me that:我检查了网络日志,但它告诉我:

{"error":{"name":"UnauthorizedError","message":"No authorization token was found"}}

I need to know , where to start the procedure, I have gone through AWS Cognito Credentials section, but nothing happened yet.我需要知道,从哪里开始这个过程,我已经浏览了 AWS Cognito 凭证部分,但还没有发生任何事情。 Can someone help me there, how to start and how to work with it?有人可以在那里帮助我,如何开始以及如何使用它?

在此处输入图像描述

Cypress documentation has advice on how to authenticate with Cognito.赛普拉斯文档提供了有关如何使用 Cognito 进行身份验证的建议。

It could be more complete though, this blog post by Nick Van Hoof offers a more complete solution.虽然它可能更完整,但Nick Van Hoof 的这篇博客文章提供了更完整的解决方案。

First install aws-amplify and cypress-localstorage-commands libs.首先安装aws-amplifycypress-localstorage-commands库。

Add a Cypress command like:添加一个赛普拉斯命令,如:

import { Amplify, Auth } from 'aws-amplify';
import 'cypress-localstorage-commands';

Amplify.configure({
  Auth: {
    region: 'your aws region',
    userPoolId 'your cognito userPoolId',
    userPoolWebClientId: 'your cognito userPoolWebClientId', 
  },
});

Cypress.Commands.add("signIn", () => {
  cy.then(() => Auth.signIn(username, password)).then((cognitoUser) => {
    const idToken = cognitoUser.signInUserSession.idToken.jwtToken;
    const accessToken = cognitoUser.signInUserSession.accessToken.jwtToken;

    const makeKey = (name) => `CognitoIdentityServiceProvider.${cognitoUser.pool.clientId}.${cognitoUser.username}.${name}`;

    cy.setLocalStorage(makeKey("accessToken"), accessToken);
    cy.setLocalStorage(makeKey("idToken"), idToken);
    cy.setLocalStorage(
      `CognitoIdentityServiceProvider.${cognitoUser.pool.clientId}.LastAuthUser`,
      cognitoUser.username
    );
  });
  cy.saveLocalStorage();
});

Then your test:然后你的测试:

describe("Example test", () => {
  before(() => {
    cy.signIn();
  });

  after(() => {
    cy.clearLocalStorageSnapshot();
    cy.clearLocalStorage();
  });

  beforeEach(() => {
    cy.restoreLocalStorage();
  });

  afterEach(() => {
    cy.saveLocalStorage();
  });

  it("should be logged in", () => {
    cy.visit("/");
    // ...
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM