diff --git a/site/gatsby-site/cypress/e2e/integration/apps/checklistsForm.cy.js b/site/gatsby-site/cypress/e2e/integration/apps/checklistsForm.cy.js index 89dee79b86..1875c438b9 100644 --- a/site/gatsby-site/cypress/e2e/integration/apps/checklistsForm.cy.js +++ b/site/gatsby-site/cypress/e2e/integration/apps/checklistsForm.cy.js @@ -4,6 +4,18 @@ const { gql } = require('@apollo/client'); describe('Checklists App Form', () => { const url = '/apps/checklists?id=testChecklist'; + const defaultChecklist = { + __typename: 'Checklist', + about: '', + id: 'testChecklist', + name: 'Test Checklist', + owner_id: 'a-fake-user-id-that-does-not-exist', + risks: [], + tags_goals: [], + tags_methods: [], + tags_other: [], + }; + const usersQuery = { query: gql` { @@ -19,27 +31,36 @@ describe('Checklists App Form', () => { timeout: 120000, // mongodb admin api is extremely slow }; - it('Should have read-only access for non-logged-in users', () => { + const withLogin = (callback) => { + cy.login(Cypress.env('e2eUsername'), Cypress.env('e2ePassword')); + + cy.query(usersQuery).then(({ data: { users } }) => { + const user = users.find((user) => user.adminData.email == Cypress.env('e2eUsername')); + + callback({ user }); + }); + }; + + const interceptFindChecklist = (checklist) => { cy.conditionalIntercept( '**/graphql', (req) => req.body.operationName == 'findChecklist', 'findChecklist', - { - data: { - checklist: { - __typename: 'Checklist', - about: '', - id: 'testChecklist', - name: 'Test Checklist', - owner_id: 'a-fake-user-id-that-does-not-exist', - risks: [], - tags_goals: [], - tags_methods: [], - tags_other: [], - }, - }, - } + { data: { checklist } } ); + }; + + const interceptUpsertChecklist = (checklist) => { + cy.conditionalIntercept( + '**/graphql', + (req) => req.body.operationName == 'upsertChecklist', + 'upsertChecklist', + { data: { checklist } } + ); + }; + + it('Should have read-only access for non-logged-in users', () => { + interceptFindChecklist(defaultChecklist); cy.visit(url); @@ -55,26 +76,7 @@ describe('Checklists App Form', () => { maybeIt('Should have read-only access for logged-in non-owners', () => { cy.login(Cypress.env('e2eUsername'), Cypress.env('e2ePassword')); - cy.conditionalIntercept( - '**/graphql', - (req) => req.body.operationName == 'findChecklist', - 'findChecklist', - { - data: { - checklist: { - __typename: 'Checklist', - about: '', - id: 'testChecklist', - name: 'Test Checklist', - owner_id: 'a-fake-user-id-that-does-not-exist', - risks: [], - tags_goals: [], - tags_methods: [], - tags_other: [], - }, - }, - } - ); + interceptFindChecklist(defaultChecklist); cy.visit(url); @@ -88,51 +90,13 @@ describe('Checklists App Form', () => { }); maybeIt('Should allow editing for owner', () => { - cy.login(Cypress.env('e2eUsername'), Cypress.env('e2ePassword')); - - cy.query(usersQuery).then(({ data: { users } }) => { - const user = users.find((user) => user.adminData.email == Cypress.env('e2eUsername')); - - cy.conditionalIntercept( - '**/graphql', - (req) => req.body.operationName == 'findChecklist', - 'findChecklist', - { - data: { - checklist: { - __typename: 'Checklist', - about: '', - id: 'testChecklist', - name: 'Test Checklist', - owner_id: user.userId, - risks: [], - tags_goals: [], - tags_methods: [], - tags_other: [], - }, - }, - } - ); - cy.conditionalIntercept( - '**/graphql', - (req) => req.body.operationName == 'upsertChecklist', - 'upsertChecklist', - { - data: { - checklist: { - __typename: 'Checklist', - about: "It's a system that does something probably.", - id: 'testChecklist', - name: 'Test Checklist', - owner_id: user.userId, - risks: [], - tags_goals: [], - tags_methods: [], - tags_other: [], - }, - }, - } - ); + withLogin(({ user }) => { + interceptFindChecklist({ ...defaultChecklist, owner_id: user.userId }); + interceptUpsertChecklist({ + ...defaultChecklist, + owner_id: user.userId, + about: "It's a system that does something probably.", + }); cy.visit(url);