Cypress 与 amazon cognito 的集成测试

arn*_*arn 5 amazon-cognito cypress

我正在尝试为我们的客户端应用程序创建一个 e2e 测试,该应用程序使用 amazon cognito 进行登录。我正在尝试按照有关登录的 aws 文档进行操作。我收到网络错误。我不确定这是否是正确的方法。我收到网络错误,因为它可能是通过我们的 VPN 传输的。我应该如何使用 aws 库解决这个问题?下面是我从 AWS 文档中使用的示例代码。

var authenticationData = {
        Username : 'username',
        Password : 'password',
    };
    var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
    var poolData = { UserPoolId : 'us-east-1_ExaMPle',
        ClientId : '1example23456789'
    };
    var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
    var userData = {
        Username : 'username',
        Pool : userPool
    };
    var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {
            var accessToken = result.getAccessToken().getJwtToken();

            /* Use the idToken for Logins Map when Federating User Pools with identity pools or when passing through an Authorization Header to an API Gateway Authorizer */
            var idToken = result.idToken.jwtToken;
        },

        onFailure: function(err) {
            alert(err);
        },

});
Run Code Online (Sandbox Code Playgroud)

Aid*_*daM 1

我也在使用 VPN,以下内容对我有用。我向commands.js 文件添加了不同的登录命令来处理用户角色,例如管理员等。

Cypress.Commands.add('loginAdmin', (overrides = {}) => {
Cypress.log({
  name: 'loginAdminViaCognito',
});  

var authenticationData = {
    Username: 'AdminUsername',
    Password: 'AdminPassword'
};
return doLogin(authenticationData);
});
Run Code Online (Sandbox Code Playgroud)

我在调用并传递不同身份验证数据的同一个文件中有通用的 doLogin 函数

function doLogin(authData){
var poolData = {
    UserPoolId: 'aws-cognito-userpoolid', 
    ClientId: 'aws-cognito-app-clientid',
};
   
var userPool = new CognitoUserPool(poolData);
var cognitoUser = new AmazonCognitoIdentity.CognitoUser({Username: authData.Username, Pool: userPool});
       
const authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(
    authData
);

return new Promise((resolve, reject) =>
    cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function(result){ 
        let token = result.getIdToken().getJwtToken();
        //let accessToken = result.getAccessToken().jwtToken;
        //let refreshToken = result.getRefreshToken().token;
        
        resolve(token);
    },
    onFailure: function(err){            
        reject("Unable to get token. " + JSON.stringify(err));
    }
})
);}
Run Code Online (Sandbox Code Playgroud)