Skip to content

Commit

Permalink
add tests for adding evidence document for an investment project
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Pentecost authored and Richard Pentecost committed Feb 27, 2024
1 parent 2baa868 commit 164822c
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 0 deletions.
155 changes: 155 additions & 0 deletions test/functional/cypress/specs/investments/project-add-evidence-spec.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
})
})
34 changes: 34 additions & 0 deletions test/sandbox/fixtures/v3/investment/project-upload-evidence.json
Original file line number Diff line number Diff line change
@@ -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"
}
13 changes: 13 additions & 0 deletions test/sandbox/routes/v3/investment/investment-projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down
15 changes: 15 additions & 0 deletions test/sandbox/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 164822c

Please sign in to comment.