From 164822c7986fc4f47f29e9fa1572f58718245c25 Mon Sep 17 00:00:00 2001 From: Richard Pentecost Date: Thu, 22 Feb 2024 14:36:44 +0000 Subject: [PATCH] add tests for adding evidence document for an investment project --- .../investments/project-add-evidence-spec.js | 155 ++++++++++++++++++ .../investment/project-upload-evidence.json | 34 ++++ .../v3/investment/investment-projects.js | 13 ++ test/sandbox/server.js | 15 ++ 4 files changed, 217 insertions(+) create mode 100644 test/functional/cypress/specs/investments/project-add-evidence-spec.js create mode 100644 test/sandbox/fixtures/v3/investment/project-upload-evidence.json diff --git a/test/functional/cypress/specs/investments/project-add-evidence-spec.js b/test/functional/cypress/specs/investments/project-add-evidence-spec.js new file mode 100644 index 00000000000..1e50cd02ab2 --- /dev/null +++ b/test/functional/cypress/specs/investments/project-add-evidence-spec.js @@ -0,0 +1,155 @@ +const urls = require('../../../../../src/lib/urls') +const fixtures = require('../../fixtures') +const { assertPayload } = require('../../support/assertions') + +describe('Investment project add evidence', () => { + context('When adding evidence for a project', () => { + const projectId = fixtures.investment.investmentWithLink.id + + beforeEach(() => { + cy.intercept('GET', '/api-proxy/v4/metadata/evidence-tag').as( + 'getEvidenceTagsApiRequest' + ) + cy.visit(urls.investments.projects.evidence.add(projectId)) + cy.wait('@getEvidenceTagsApiRequest') + }) + + context('form layout', () => { + it('should render the heading', () => { + cy.get('[data-test="heading"]') + .should('exist') + .should('have.text', 'Add evidence') + }) + + it('should render the form with the correct form fields', () => { + cy.get('form').should('be.visible') + cy.get('[data-test="field-file_upload"]').should('exist') + cy.get('[data-test="criteria_field_0"]') + .should('exist') + .should('not.contain', 'Remove') + cy.get('[data-test="add-another"]') + .should('exist') + .should('have.text', 'Add another') + cy.get('[data-test="textarea"]').should('exist') + }) + + it('should render an upload and cancel button', () => { + cy.get('[data-test="submit-button"]') + .should('exist') + .and('have.text', 'Upload') + cy.get('[data-test="cancel-button"]') + .should('exist') + .and( + 'have.attr', + 'href', + `/investments/projects/${projectId}/evidence` + ) + .and('have.text', 'Cancel') + }) + + it('should add a second verification criteria field when add another button is clicked', () => { + cy.get('[data-test="add-another"]').click() + cy.get('[data-test="criteria_field_0"]') + .should('exist') + .contains('Remove') + cy.get('[data-test="criteria_field_1"]') + .should('exist') + .contains('Remove') + }) + + it('should remove the second verification criteria field when the corresponding remove button is clicked', () => { + cy.get('[data-test="add-another"]').click() + cy.get('[data-test="criteria_field_1"]').contains('Remove').click() + cy.get('[data-test="criterial_field_1').should('not.exist') + cy.get('[data-test="criteria_field_0"]') + .should('exist') + .contains('Remove') + .should('not.exist') + }) + }) + + context('submitting form', () => { + it('should show errors when the upload field and verification criteria do not have values and the form is submitted', () => { + cy.get('[data-test="submit-button"').click() + cy.get('[data-test="summary-form-errors') + .should('exist') + .and('contain', 'Choose a document') + .and('contain', 'Select a criteria') + cy.get('[data-test="field-file_upload"]').contains('Choose a document') + cy.get('[data-test="criteria_field_0"]').contains('Select a criteria') + }) + + it('should show upload field error when just the upload field does not have a value and the form is submitted', () => { + cy.get('[data-test="criteria_field_0"]').find('select').select(1) + cy.get('[data-test="submit-button"').click() + cy.get('[data-test="summary-form-errors') + .should('exist') + .and('contain', 'Choose a document') + cy.get('[data-test="field-file_upload"]').contains('Choose a document') + cy.get('[data-test="criteria_field_0"]').should( + 'not.contain', + 'Select a criteria' + ) + }) + + it('should show verification criteria field error when just the verification criteria field does not have a value and the form is submitted', () => { + cy.get('input[type=file]').selectFile( + 'test/functional/cypress/fixtures/default.json' + ) + cy.get('[data-test="submit-button"').click() + cy.get('[data-test="summary-form-errors') + .should('exist') + .and('contain', 'Select a criteria') + cy.get('[data-test="criteria_field_0"]').contains('Select a criteria') + cy.get('[data-test="field-file_upload"]').should( + 'not.contain', + 'Choose a document' + ) + }) + + it('should call the endpoint with the form data when valid form is submitted and redirect to evidence page with flash message', () => { + cy.intercept( + 'POST', + `/api-proxy/v3/investment/${projectId}/evidence-document` + ).as('postUpload') + cy.intercept('PUT', 'https://s3.amazon.com/documentId', { + statusCode: 200, + }) + cy.get('input[type=file]').selectFile( + 'test/functional/cypress/fixtures/default.json' + ) + cy.get('[data-test="criteria_field_0"]').find('select').select(1) + cy.get('[data-test="submit-button"').click() + assertPayload('@postUpload', { + comment: '', + original_filename: 'default.json', + tags: ['d5145304-cacd-47ec-b808-4306a151b7d5'], + }) + cy.get('[data-test="evidence-heading"]') + .should('exist') + .and('have.text', 'Evidence') + cy.get('[data-test="status-message"]') + .should('exist') + .and('have.text', '1 File Uploaded') + }) + + it('should call the delete endpoint to rollback the upload if there is an error when uploading file to s3', () => { + cy.intercept('PUT', 'https://s3.amazon.com/documentId', { + statusCode: 404, + }) + cy.intercept( + 'DELETE', + `/api-proxy/v3/investment/${projectId}/evidence-document/7cbe3d94-4d02-49fb-8ccb-9f70be849206` + ).as('rollbackUpload') + cy.get('input[type=file]').selectFile( + 'test/functional/cypress/fixtures/default.json' + ) + cy.get('[data-test="criteria_field_0"]').find('select').select(1) + cy.get('[data-test="submit-button"').click() + cy.wait('@rollbackUpload').then(({ response }) => { + expect(response.statusCode).to.equal(200) + }) + }) + }) + }) +}) diff --git a/test/sandbox/fixtures/v3/investment/project-upload-evidence.json b/test/sandbox/fixtures/v3/investment/project-upload-evidence.json new file mode 100644 index 00000000000..e7a555aeda4 --- /dev/null +++ b/test/sandbox/fixtures/v3/investment/project-upload-evidence.json @@ -0,0 +1,34 @@ +{ + "id": "7cbe3d94-4d02-49fb-8ccb-9f70be849206", + "tags": [ + { + "name": "Check A - FDI eligibility: New permanent jobs", + "id": "e8bc969c-d675-4282-acae-1a0535626896" + }, + { + "name": "Check A - FDI eligibility: Safeguarded jobs", + "id": "c1e208ea-c9a6-4fd2-ac5d-a09dd86d7758" + } + ], + "comment": "A comment on this piece of valuable evidence", + "av_clean": null, + "created_by": { + "name": "Dean Cox", + "firstName": "Dean", + "lastName": "Cox", + "id": "681e4af7-707d-4913-aa44-3551dd83afad" + }, + "created_on": "2023-08-25T15:47:59.374756Z", + "modified_by": null, + "modified_on": "2023-08-25T15:47:59.374782Z", + "uploaded_on": null, + "investmentProject": { + "name": "New Hotel (commitment to invest)", + "projectCode": "DHP-00000003", + "id": "fb5b5006-56af-40e0-8615-7aba53e0e4bf" + }, + "originalFilename": "default.json", + "url": "/v3/investment/fb5b5006-56af-40e0-8615-7aba53e0e4bf/evidence-document/7cbe3d94-4d02-49fb-8ccb-9f70be849206/download", + "status": "not_virus_scanned", + "signed_upload_url": "https://s3.amazon.com/documentId" +} diff --git a/test/sandbox/routes/v3/investment/investment-projects.js b/test/sandbox/routes/v3/investment/investment-projects.js index fc8698650b5..4e757a88f90 100644 --- a/test/sandbox/routes/v3/investment/investment-projects.js +++ b/test/sandbox/routes/v3/investment/investment-projects.js @@ -4,6 +4,7 @@ import projectAudit from '../../../fixtures/v3/investment/project-audit.json' as import projectEvidence from '../../../fixtures/v3/investment/project-evidence.json' assert { type: 'json' } import projectNoEvidence from '../../../fixtures/v3/investment/project-no-evidence.json' assert { type: 'json' } import projectDocumentDownload from '../../../fixtures/v3/investment/project-document-download.json' assert { type: 'json' } +import projectUploadEvidence from '../../../fixtures/v3/investment/project-upload-evidence.json' assert { type: 'json' } var allProjectsMap = {} allProjects.results.forEach(function (project) { @@ -30,6 +31,18 @@ export const investmentProjectEvidence = function (req, res) { ) } +export const investmentProjectEvidenceUpload = function (req, res) { + res.json(projectUploadEvidence) +} + +export const investmentProjectEvidenceUploadCallback = function (req, res) { + res.sendStatus(200) +} + +export const deleteInvestmentProjectEvidence = function (req, res) { + res.sendStatus(200) +} + export const documentDownload = function (req, res) { res.json(projectDocumentDownload) } diff --git a/test/sandbox/server.js b/test/sandbox/server.js index bcddf56dc2c..17337b1406a 100644 --- a/test/sandbox/server.js +++ b/test/sandbox/server.js @@ -116,6 +116,9 @@ import { documentDownload, postInvestmentProject, postInvestmentProjectEditTeams, + investmentProjectEvidenceUpload, + investmentProjectEvidenceUploadCallback, + deleteInvestmentProjectEvidence, } from './routes/v3/investment/investment-projects.js' import { getCompanies } from './routes/v3/search/company.js' import { contacts } from './routes/v3/search/contact.js' @@ -549,6 +552,18 @@ app.get( '/v3/investment/:investmentId/evidence-document', investmentProjectEvidence ) +app.post( + '/v3/investment/:id/evidence-document', + investmentProjectEvidenceUpload +) +app.post( + '/v3/investment/:id/evidence-document/:documentId/upload-callback', + investmentProjectEvidenceUploadCallback +) +app.delete( + '/v3/investment/:id/evidence-document/:documentId', + deleteInvestmentProjectEvidence +) app.get( '/v3/investment/:investmentId/evidence-document/:documentId/download', documentDownload