Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress AWS Cognito Intergrations

Does Cypress have any way to integrate with Cognito? I have an app that does not have a login page, but uses the cookies from another website that has a Cognito login. (Uses cookies)

Is there anyway to have a 'Login' from a 3rd party app without going to visit that page? I also tried making an API request to the login endpoint and it also gives me a cross domain issue.

Any ideas is appreciated!

like image 621
SetupG Avatar asked Jun 03 '20 18:06

SetupG


1 Answers

Cypress provides the ability to create custom commands, which can be used to execute Amplify/Cognito commands. For example, when testing authenticated pages I use a login command instead of using the UI to log in as this is deemed an anti pattern.

If you are able to obtain the cognito configuration, you can perform a login using a custom command as follows.

Create custom command in 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));
})

Use cy.login() command in spec file:

describe('Authenticated page test', () => {
  it('should be logged in', () => {
    cy.login('[email protected]', 'Password')
    
    cy.visit('/')
    cy.contains('some page content')
  })
})
like image 108
AlexB Avatar answered Oct 02 '22 10:10

AlexB