-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadd-a-photo.spec.js
More file actions
203 lines (166 loc) · 6.57 KB
/
add-a-photo.spec.js
File metadata and controls
203 lines (166 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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')
})
})
})
})