'Is it possible to run Cypress tests using AWS assumerole?
I have created some Automated Tests to test a public facing API endpoint. There is a Test Data Creation step which runs and creates data directly in our AWS environment (ie. directly executing Lambdas, adding entries to the database). This is integrated into Cypress, and added a package in the package.json
To perform this, it is first necessary to use the aws-sdk
, specifically assumerole
. When this process is run directly using Node, it runs and populates ~/.aws/credentials
with aws_access_key_id
, aws_secret_access_key
and aws_session_token
.
Once this role has been assumed the Test Data Creation can be run, and data is created for testing.
Using Node and the Node environment, this works correctly, and data is created.
But when this is run in Cypress, it fails.
This is my plugins/index.js
:
const AWS = require('aws-sdk');
module.exports = (on, config) => {
AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
sessionToken: process.env.AWS_SESSION_TOKEN,
region: process.env.AWS_REGION
});
return { on, config };
};
And as the Test Data Creation process is async, I have added this async command in support/commands.js
:
const testDataCreation = require('test-data-creation');
Cypress.Commands.add("generateOrganisation", async () => {
await testDataCreation.generateOrganisation();
});
And in my test:
describe('Test', () => {
before(function () {
cy.generateOrganisation().then(response => response.organisationId).as("orgId");
});
it('should visit the page', function () {
cy.visit(`https://${this.orgId}.website.com/`);
cy.get('#cookie-box').should('have.class', 'my-cookie');
});
});
But when the test runs, the test fails with the message "ConfigError: Missing region in config". This is a typical message for when the AWS config is not set correctly. Which makes me think that Cypress is not able to run tests correctly with the assumed AWS role. Or maybe that I have done something wrong. I was guessing that it should "just work" (like how the Test Data Creation script can be run using Node). How can the setup steps requiring AWS credentials (ie Test Data Creation) be run from Cypress?
Solution 1:[1]
After various attempts at finding a solution - it turned out to be pretty straightforward.
This part wasn't needed. assumerole
was working and Cypress was running with the Assumed Role and so it was not necessary to set keys again.
const AWS = require('aws-sdk');
module.exports = (on, config) => {
AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
sessionToken: process.env.AWS_SESSION_TOKEN,
region: process.env.AWS_REGION
});
return { on, config };
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | nuclearcat |