Skip to content

Commit bb6870e

Browse files
code coverage and code smells
1 parent 306576b commit bb6870e

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

server/routes/your-photos.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ const handlers = {
2121
},
2222

2323
post: async (request, h) => {
24-
const imageIndex = parseInt(request.payload.imageIndex, 10)
24+
const imageIndex = Number.parseInt(request.payload.imageIndex, 10)
2525
const thumbnails = request.yar.get('thumbnails') || []
2626

27-
if (!isNaN(imageIndex) && imageIndex >= 0 && imageIndex < thumbnails.length) {
27+
if (!Number.isNaN(imageIndex) && imageIndex >= 0 && imageIndex < thumbnails.length) {
2828
const imageToRemove = thumbnails[imageIndex]
2929

3030
try {

server/services/__tests__/image-checker.spec.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ describe('image-checker', () => {
108108
expect(result).toEqual({ success: true, skipped: true })
109109
})
110110

111+
it('returns skipped result when thumbnails argument is omitted', async () => {
112+
const result = await imageChecker.validate()
113+
114+
expect(result).toEqual({ success: true, skipped: true })
115+
})
116+
111117
it('does not call post when no thumbnails are supplied', async () => {
112118
await imageChecker.validate([])
113119

@@ -122,6 +128,66 @@ describe('image-checker', () => {
122128
expect(result).toEqual({ success: true, skipped: true })
123129
})
124130

131+
it.each([
132+
{
133+
label: 'endpoint is missing',
134+
endpoint: '',
135+
key: 'test-content-safety-key'
136+
},
137+
{
138+
label: 'key is missing',
139+
endpoint: 'https://example.cognitiveservices.azure.com/',
140+
key: ''
141+
}
142+
])('returns skipped result when content safety config is incomplete: $label', async ({ endpoint, key }) => {
143+
process.env.CONTENT_SAFETY_ENDPOINT = endpoint
144+
process.env.CONTENT_SAFETY_KEY = key
145+
146+
const result = await imageChecker.validate([{ finalFilename: 'upload-id/photo1.jpg' }])
147+
148+
expect(result).toEqual({ success: true, skipped: true })
149+
})
150+
151+
it.each([
152+
{
153+
label: 'endpoint is missing',
154+
endpoint: '',
155+
key: 'test-content-safety-key'
156+
},
157+
{
158+
label: 'key is missing',
159+
endpoint: 'https://example.cognitiveservices.azure.com/',
160+
key: ''
161+
}
162+
])('does not create content safety client when config is incomplete: $label', async ({ endpoint, key }) => {
163+
process.env.CONTENT_SAFETY_ENDPOINT = endpoint
164+
process.env.CONTENT_SAFETY_KEY = key
165+
166+
await imageChecker.validate([{ finalFilename: 'upload-id/photo1.jpg' }])
167+
168+
expect(ContentSafetyClient).not.toHaveBeenCalled()
169+
})
170+
171+
it.each([
172+
{
173+
label: 'endpoint is missing',
174+
endpoint: '',
175+
key: 'test-content-safety-key'
176+
},
177+
{
178+
label: 'key is missing',
179+
endpoint: 'https://example.cognitiveservices.azure.com/',
180+
key: ''
181+
}
182+
])('does not fetch blob container when config is incomplete: $label', async ({ endpoint, key }) => {
183+
process.env.CONTENT_SAFETY_ENDPOINT = endpoint
184+
process.env.CONTENT_SAFETY_KEY = key
185+
186+
await imageChecker.validate([{ finalFilename: 'upload-id/photo1.jpg' }])
187+
188+
expect(blobStorage.getUploadContainerClient).not.toHaveBeenCalled()
189+
})
190+
125191
it('does not call post when blob container client is unavailable', async () => {
126192
blobStorage.getUploadContainerClient.mockResolvedValue(null)
127193
isUnexpected.mockReturnValue(false)

0 commit comments

Comments
 (0)