赛普拉斯AWS Cognito集成

时间:2020-06-03 18:14:16

标签: amazon-cognito cypress

赛普拉斯是否可以与Cognito集成?我有一个没有登录页面的应用程序,但是使用了另一个具有Cognito登录名的网站的cookie。 (使用Cookie)

反正是否有要通过第三方应用程序进行“登录”而不访问该页面的信息?我还尝试了向登录端点发出API请求,这也给我带来了跨域问题。

任何想法都值得赞赏!

1 个答案:

答案 0 :(得分:4)

Cypress提供了创建custom commands的功能,该功能可用于执行Amplify / Cognito命令。例如,在测试经过身份验证的页面时,我使用登录命令而不是使用UI登录,因为这被认为是anti pattern

如果能够获取认知配置,则可以使用以下自定义命令执行登录。

在cypress / support / commands.js中创建自定义命令

import Amplify, { Auth } from 'aws-amplify';
Amplify.configure({
  Auth: {
    mandatorySignIn: true,
    region: "eu-west-1",
    userPoolId: Cypress.env("userPoolId"),
    identityPoolId: Cypress.env("identityPoolId"),
    userPoolWebClientId: Cypress.env("appClientId"),
    oauth: {
        domain: Cypress.env("domain"),
        scope: ['email', 'profile', 'aws.cognito.signin.user.admin', 'openid'],
        redirectSignIn: Cypress.env("redirect"),
        redirectSignOut: Cypress.env("redirect"),
        responseType: 'code',
        options: {
            AdvancedSecurityDataCollectionFlag: false
        }
    }
  }
});

Cypress.Commands.add("login", (email, password) => {
  return Auth.signIn(email, password)
      .then(user => {
        console.log('===> user', user);

        let session = Auth.currentSession();

        console.log('===> session', session);
      })
      .catch(err => console.log('===> err', err));
})

在规格文件中使用cy.login()命令:

describe('Authenticated page test', () => {
  it('should be logged in', () => {
    cy.login('test.user@email.com', 'Password')
    
    cy.visit('/')
    cy.contains('some page content')
  })
})