Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
770 changes: 721 additions & 49 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dependencies": {
"@azure/identity": "^4.5.0",
"@azure/service-bus": "^7.9.5",
"@azure/storage-blob": "^12.31.0",
"@friendlycaptcha/sdk": "^0.1.22",
"@hapi/boom": "^10.0.1",
"@hapi/catbox-redis": "^7.0.2",
Expand All @@ -52,7 +53,11 @@
"ajv-formats": "^3.0.1",
"axios": "1.12.2",
"blipp": "^4.0.2",
"copy-webpack-plugin": "^13.0.0",
"cron-parser": "^4.9.0",
"css-loader": "^7.1.2",
"formidable": "^3.5.4",
"fs": "^0.0.1-security",
"govuk-frontend": "^5.11.1",
"hapi-pino": "^12.1.0",
"joi": "^17.13.3",
Expand All @@ -61,22 +66,21 @@
"ol": "^10.3.1",
"postcode-validator": "^3.10.2",
"proj4": "^2.15.0",
"css-loader": "^7.1.2",
"sass": "^1.89.1",
"sharp": "^0.34.5",
"style-loader": "^4.0.0",
"webpack": "^5.97.1",
"copy-webpack-plugin": "^13.0.0",
"webpack-cli": "^5.1.4"
},
"devDependencies": {
"@babel/preset-env": "^7.26.0",
"@hapi/catbox-memory": "^6.0.2",
"concurrently": "^9.1.2",
"dotenv": "^16.5.0",
"husky": "^9.1.7",
"jest": "^29.7.0",
"jest-junit": "^16.0.0",
"node-html-parser": "^6.1.13",
"dotenv": "^16.5.0",
"nodemon": "^3.1.10",
"standard": "^17.1.2"
},
Expand Down
2 changes: 1 addition & 1 deletion server/plugins/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default {
logPayload: true,
level: config.logLevel,
redact: {
paths: ['req.headers.authorization', 'req.headers.cookie', 'res.headers'],
paths: ['req.headers.authorization', 'req.headers.cookie', 'res.headers', 'payload.fileUpload1'],
remove: true
},
ignorePaths: [
Expand Down
203 changes: 203 additions & 0 deletions server/routes/__tests__/add-a-photo.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
import { submitGetRequest, submitPostRequest } from '../../__test-helpers__/server.js'
import constants from '../../utils/constants.js'
import { BlobServiceClient } from '@azure/storage-blob'
import fs from 'node:fs'
import FormData from 'form-data'
import * as addPhoto from '../add-a-photo.js'

jest.mock('@azure/storage-blob', () => ({
BlobServiceClient: jest.fn(),
StorageSharedKeyCredential: jest.fn()
}))

const mockValidPng = Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO7+5e0AAAAASUVORK5CYII=',
'base64'
)

const createForm = (filename = '', content = 'data', contentType = 'image/png') => {
const form = new FormData()
const fileBuffer = Buffer.isBuffer(content) ? content : Buffer.from(content)
form.append('fileUpload1', fileBuffer, { filename, contentType })
return form
}

const url = constants.routes.ADD_A_PHOTO
const header = 'Add a photo'

describe(url, () => {
beforeEach(() => {
BlobServiceClient.mockImplementation(() => ({
getContainerClient: () => ({
createIfNotExists: () => Promise.resolve(),
getBlockBlobClient: () => ({
uploadData: () => Promise.resolve(),
downloadToBuffer: () => Promise.resolve(mockValidPng)
})
})
}))
})

afterEach(() => {
jest.restoreAllMocks()
})

describe('GET', () => {
it('should return correct view', async () => {
const response = await submitGetRequest({ url }, header)
expect(response.result).toContain(header)
})

it('should set upload-id if not present', async () => {
const response = await submitGetRequest({ url }, header)
expect(response.request.yar.get('upload-id')).toBeDefined()
})

it('should keep existing upload-id if already present', async () => {
const existingUploadId = 'existing-upload-id'
const response = await submitGetRequest({ url }, header, 200, { 'upload-id': existingUploadId })

expect(response.request.yar.get('upload-id')).toBe(existingUploadId)
})
})

describe('POST', () => {
describe('empty file', () => {
it('should return correct error message if no file provided', async () => {
const form = new FormData()
const response = await submitPostRequest({
url,
payload: form.getBuffer(),
headers: form.getHeaders()
}, 200)

expect(response.result).toContain('Select a file.')
})

it('should return correct error message if file missing original filename', async () => {
const form = createForm('')
form.append('fileUpload1', Buffer.from('data'), { filename: '' })
const response = await submitPostRequest({
url,
payload: form.getBuffer(),
headers: form.getHeaders()
}, 200)

expect(response.result).toContain('Select a file.')
})
})

it('should return size error if file is over 10MB', async () => {
const form = createForm('big.png', Buffer.alloc(11 * 1024 * 1024), 'image/png')
const response = await submitPostRequest({
url,
payload: form.getBuffer(),
headers: form.getHeaders()
}, 200)

expect(response.result).toContain('The selected file must be smaller than 10MB.')
})

it('should show max selected files content when 5 files already exist', async () => {
const form = createForm('valid.png', mockValidPng, 'image/png')
const thumbnails = Array.from({ length: 5 }, (_, index) => ({
finalFilename: `upload-id/${index}.png`,
thumbLoc: `/public/thumbnails/upload-id-${index}.png`
}))

const response = await submitPostRequest({
url,
payload: form.getBuffer(),
headers: form.getHeaders()
}, 200, { thumbnails })

expect(response.result).toContain('You have added the maximum number of photos allowed')
})

describe('upload failure', () => {
it('should return default error if upload fails', async () => {
jest.spyOn(fs, 'writeFileSync').mockImplementation(() => {
throw new Error('fail')
})

const form = createForm('valid.png', mockValidPng, 'image/png')
const response = await submitPostRequest({
url,
payload: form.getBuffer(),
headers: form.getHeaders()
}, 200)

expect(response.result).toContain('could not be uploaded')
})
})

describe('successful upload', () => {
beforeEach(() => {
jest.spyOn(addPhoto, 'streamToBuffer').mockResolvedValue(mockValidPng)
})

it('should redirect on success', async () => {
const form = createForm('valid.png', mockValidPng, 'image/png')
const response = await submitPostRequest({
url,
payload: form.getBuffer(),
headers: form.getHeaders()
}, 302)

expect(response.headers.location).toBe(constants.routes.YOUR_PHOTOS)
})

it('should store thumbnails in session', async () => {
const form = createForm('valid.png', mockValidPng, 'image/png')
const response = await submitPostRequest({
url,
payload: form.getBuffer(),
headers: form.getHeaders()
}, 302)

const thumbnails = response.request.yar.get('thumbnails')

expect(Array.isArray(thumbnails)).toBe(true)
})

it('should add at least one thumbnail to session on successful upload', async () => {
const form = createForm('valid.png', mockValidPng, 'image/png')
const response = await submitPostRequest({
url,
payload: form.getBuffer(),
headers: form.getHeaders()
}, 302)

const thumbnails = response.request.yar.get('thumbnails')

expect(thumbnails.length).toBeGreaterThan(0)
})

it('should store thumbLoc in session thumbnail entry', async () => {
const form = createForm('valid.png', mockValidPng, 'image/png')
const response = await submitPostRequest({
url,
payload: form.getBuffer(),
headers: form.getHeaders()
}, 302)

const thumbnails = response.request.yar.get('thumbnails')

expect(thumbnails[0]).toHaveProperty('thumbLoc')
})

it('should store finalFilename in session thumbnail entry', async () => {
const form = createForm('valid.png', mockValidPng, 'image/png')
const response = await submitPostRequest({
url,
payload: form.getBuffer(),
headers: form.getHeaders()
}, 302)

const thumbnails = response.request.yar.get('thumbnails')

expect(thumbnails[0]).toHaveProperty('finalFilename')
})
})
})
})
14 changes: 14 additions & 0 deletions server/routes/__tests__/terms-for-uploading-photos.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { submitGetRequest } from '../../__test-helpers__/server.js'
import constants from '../../utils/constants.js'

const url = constants.routes.TERMS_FOR_UPLOADING_PHOTOS
const header = 'Terms for uploading photos'

describe(url, () => {
describe('GET', () => {
it(`Should return success response and correct view for ${url}`, async () => {
const response = await submitGetRequest({ url }, header, constants.statusCodes.OK)
expect(response.payload).toContain('Terms for uploading photos')
})
})
})
13 changes: 12 additions & 1 deletion server/routes/__tests__/upload-photo.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { submitGetRequest } from '../../__test-helpers__/server.js'
import { submitGetRequest, submitPostRequest } from '../../__test-helpers__/server.js'
import constants from '../../utils/constants.js'

const url = constants.routes.UPLOAD_PHOTO
Expand All @@ -11,4 +11,15 @@ describe(url, () => {
expect(response.payload).toContain('Upload photos')
})
})
describe('POST', () => {
it(`Should return redirect response for ${url}`, async () => {
const response = await submitPostRequest({ url }, constants.statusCodes.REDIRECT)
expect(response.statusCode).toBe(constants.statusCodes.REDIRECT)
})

it(`Should redirect to ${constants.routes.ADD_A_PHOTO} for ${url}`, async () => {
const response = await submitPostRequest({ url }, constants.statusCodes.REDIRECT)
expect(response.headers.location).toBe(constants.routes.ADD_A_PHOTO)
})
})
})
14 changes: 14 additions & 0 deletions server/routes/__tests__/your-photos.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { submitGetRequest } from '../../__test-helpers__/server.js'
import constants from '../../utils/constants.js'

const url = constants.routes.YOUR_PHOTOS
const header = 'Your photos'

describe(url, () => {
describe('GET', () => {
it(`Should return success response and correct view for ${url}`, async () => {
const response = await submitGetRequest({ url }, header, constants.statusCodes.OK)
expect(response.payload).toContain('Your photos')
})
})
})
Loading
Loading