From df825e64fb69346c4c4cbe10df648abfcae70f2d Mon Sep 17 00:00:00 2001 From: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com> Date: Fri, 8 Oct 2021 09:05:12 -0600 Subject: [PATCH] Forem Creator Cypress Setup (#14984) * Adds loginCreator(); to commands.js and a creatorUser.js fixture * Update cypress/support/commands.js * Update cypress/support/commands.js * Removes redundant loginUser(); function * Remove superfluous back slashes from commands.js Co-authored-by: Suzanne Aitchison Co-authored-by: Suzanne Aitchison --- cypress/fixtures/users/creatorUser.json | 6 ++++ cypress/support/commands.js | 39 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 cypress/fixtures/users/creatorUser.json diff --git a/cypress/fixtures/users/creatorUser.json b/cypress/fixtures/users/creatorUser.json new file mode 100644 index 000000000..8bf69d51c --- /dev/null +++ b/cypress/fixtures/users/creatorUser.json @@ -0,0 +1,6 @@ +{ + "name": "Creator", + "username": "creator", + "email": "creator@forem.local", + "password": "password" +} diff --git a/cypress/support/commands.js b/cypress/support/commands.js index 33ace7b72..3819c2e2e 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -117,6 +117,45 @@ Cypress.Commands.add('loginUser', ({ email, password }) => { }); }); + +/** + * Logs in a creator with the given name, username, email, and password. + * + * @param credentials + * @param credentials.name {string} A name + * @param credentials.username {string} A username + * @param credentials.email {string} An email address + * @param credentials.password {string} A password + * + * @returns {Cypress.Chainable} A cypress request for signing in a creator. + */ +Cypress.Commands.add('loginCreator', ({ name, username, email, password }) => { + const encodedName = encodeURIComponent(name); + const encodedUsername = encodeURIComponent(username); + const encodedEmail = encodeURIComponent(email); + const encodedPassword = encodeURIComponent(password); + + function getLoginRequest() { + return cy.request( + 'POST', + '/users', + `utf8=%E2%9C%93&user%5Bname%5D=${encodedName}&user%5Busername%5D=${encodedUsername}&user%5Bemail%5D=${encodedEmail}%40forem.local&user%5Bpassword%5D=${encodedPassword}&commit=Create+my+account`, + ); + } + + return getLoginRequest().then((response) => { + if (response.status === 200) { + return response; + } + + cy.log('Login failed. Attempting one more login.'); + + // If we have a login failure, try one more time. + // This is to combat some flaky tests where the login fails occasionnally. + return getLoginRequest(); + }); +}); + /** * Gets an iframe with the given selector (or the first/only iframe if none is passed in), * waits for its content to be loaded, and returns a wrapped reference to the iframe body