Form files: the visitor is told when the problem is the file — why and what to do; UTF-8 file names
Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
b6f9f34705
commit
97202ee03e
@@ -294,7 +294,7 @@ describe('2 · each allowed format: one submission, the file in the staff e-mail
|
||||
const attachment = staff[0].attachments![0]
|
||||
expect(Buffer.compare(attachment.content, data)).toBe(0)
|
||||
expect(attachment.contentType).toBe(mime)
|
||||
expect(attachment.filename.endsWith(`.${type}`)).toBe(true)
|
||||
expect(attachment.filename).toBe(name.replace(/\.[^.]+$/, `.${type}`)) // the visitor's name (also with Polish letters), the detected extension
|
||||
expect(staff[0].html).toContain(attachment.filename) // {{attachment}} → the readable name
|
||||
expect(staff[0].html).toContain('not checked for viruses')
|
||||
const customer = mails.filter((m) => m.to === '[email protected]')
|
||||
@@ -313,45 +313,62 @@ describe('2 · each allowed format: one submission, the file in the staff e-mail
|
||||
})
|
||||
|
||||
describe('3 · refused files leave no submission and send no e-mail', () => {
|
||||
const refused = async (files: Upload[], expected: RegExp, extra: Post = {}) => {
|
||||
// Every refusal is answered AT the File field, says it is the file, why, and what to do next.
|
||||
const TYPES = 'PDF, DOC, DOCX'
|
||||
const wrongFormat = (name: string) => `The file "${name}" is in a format we do not accept. Choose one of: ${TYPES}. You can also send the form without the file.`
|
||||
const notAcceptable = (name: string) =>
|
||||
`The file "${name}" was refused: it is a program or an archive, it holds macros, or its content does not match its type. Choose another file (${TYPES}). You can also send the form without the file.`
|
||||
const refused = async (files: Upload[], expected: string, extra: Post = {}) => {
|
||||
const before = await count()
|
||||
const { body, status } = await post({ files, ...extra })
|
||||
expect(body.success).toBe(false)
|
||||
expect(status).toBe(422)
|
||||
expect(body.message).toMatch(expected)
|
||||
expect(body.message).toBe(expected)
|
||||
expect(body).toMatchObject({ field: 'attachment', reason: 'validation' })
|
||||
expect(await count()).toBe(before)
|
||||
expect(mails).toEqual([])
|
||||
}
|
||||
|
||||
test('oversized: stopped while reading', async () => {
|
||||
await refused([{ name: 'big.pdf', data: pad(Buffer.from('%PDF-1.7\n'), MB + 200_000) }], /"big\.pdf" is too large\. The limit is 1 MB\./)
|
||||
await refused(
|
||||
[{ name: 'big.pdf', data: pad(Buffer.from('%PDF-1.7\n'), MB + 200_000) }],
|
||||
'The file "big.pdf" is too large. Choose a file of up to 1 MB. You can also send the form without the file.',
|
||||
)
|
||||
expect(scanned).toEqual([])
|
||||
})
|
||||
|
||||
test('mismatched: a program named .pdf, a PDF named .docx, an empty file', async () => {
|
||||
await refused([{ name: 'cv.pdf', data: EXE }], /"cv\.pdf" is not an accepted file type\. Accepted: PDF, DOC, DOCX\./)
|
||||
await refused([{ name: 'cv.docx', data: PDF }], /not an accepted file type/)
|
||||
await refused([{ name: 'cv.pdf', data: Buffer.alloc(0) }], /not an accepted file type|too large/)
|
||||
await refused([{ name: 'cv.pdf', data: EXE }], notAcceptable('cv.pdf'))
|
||||
await refused([{ name: 'cv.docx', data: PDF }], notAcceptable('cv.docx'))
|
||||
await refused([{ name: 'cv.pdf', data: Buffer.alloc(0) }], notAcceptable('cv.pdf'))
|
||||
})
|
||||
|
||||
test('unsupported and dangerous names: other type, program, double extension, archive, macro-enabled Office', async () => {
|
||||
await refused([{ name: 'photo.png', data: Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }], /not an accepted file type/)
|
||||
await refused([{ name: 'cv.exe', data: EXE }], /not an accepted file type/)
|
||||
await refused([{ name: 'cv.pdf.exe', data: EXE }], /not an accepted file type/)
|
||||
await refused([{ name: 'cv.exe.pdf', data: PDF }], /not an accepted file type/)
|
||||
await refused([{ name: 'cv.pdf.docx', data: DOCX }], /not an accepted file type/)
|
||||
await refused([{ name: 'cv.zip', data: ARCHIVE }], /not an accepted file type/)
|
||||
await refused([{ name: 'cv.docm', data: DOCX_MACRO }], /not an accepted file type/)
|
||||
test('another format: the message names the formats the field takes', async () => {
|
||||
await refused([{ name: 'photo.png', data: Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }], wrongFormat('photo.png'))
|
||||
await refused([{ name: 'notes.txt', data: Buffer.from('hello') }], wrongFormat('notes.txt'))
|
||||
await refused([{ name: 'cv', data: PDF }], wrongFormat('cv'))
|
||||
})
|
||||
|
||||
test('refused files: program, double extension, archive, macro-enabled Office — the message says the file was refused', async () => {
|
||||
await refused([{ name: 'cv.exe', data: EXE }], notAcceptable('cv.exe'))
|
||||
await refused([{ name: 'cv.pdf.exe', data: EXE }], notAcceptable('cv.pdf.exe'))
|
||||
await refused([{ name: 'cv.exe.pdf', data: PDF }], notAcceptable('cv.exe.pdf'))
|
||||
await refused([{ name: 'cv.pdf.docx', data: DOCX }], notAcceptable('cv.pdf.docx'))
|
||||
await refused([{ name: 'cv.zip', data: ARCHIVE }], notAcceptable('cv.zip'))
|
||||
await refused([{ name: 'cv.docm', data: DOCX_MACRO }], notAcceptable('cv.docm'))
|
||||
})
|
||||
|
||||
test('the right container with macros inside: DOCX with a macro project, DOC with a macro storage', async () => {
|
||||
await refused([{ name: 'cv.docx', data: DOCX_MACRO }], /not an accepted file type/)
|
||||
await refused([{ name: 'cv.doc', data: DOC_MACRO }], /not an accepted file type/)
|
||||
await refused([{ name: 'cv.docx', data: ARCHIVE }], /not an accepted file type/) // a ZIP that is not a Word file
|
||||
await refused([{ name: 'cv.docx', data: DOCX_MACRO }], notAcceptable('cv.docx'))
|
||||
await refused([{ name: 'cv.doc', data: DOC_MACRO }], notAcceptable('cv.doc'))
|
||||
await refused([{ name: 'cv.docx', data: ARCHIVE }], notAcceptable('cv.docx')) // a ZIP that is not a Word file
|
||||
})
|
||||
|
||||
test('extra files: more than the field allows, a file under a key that is no File field', async () => {
|
||||
await refused([{ name: 'a.pdf', data: PDF }, { name: 'b.pdf', data: PDF }], /Attach at most 1 files in "Attachment \(optional\)"\./)
|
||||
await refused(
|
||||
[{ name: 'a.pdf', data: PDF }, { name: 'b.pdf', data: PDF }],
|
||||
'Too many files in "Attachment (optional)". Remove some: the limit is 1. You can also send the form without the file.',
|
||||
)
|
||||
const before = await count()
|
||||
const { body } = await post({ files: [{ name: 'a.pdf', data: PDF, field: 'name' }], values: { consent: true, email: '[email protected]' } })
|
||||
expect(body.success).toBe(false)
|
||||
@@ -370,7 +387,7 @@ describe('3 · refused files leave no submission and send no e-mail', () => {
|
||||
|
||||
test('the antivirus hook can refuse a file that passed everything else', async () => {
|
||||
scanVerdict = false
|
||||
await refused([{ name: 'cv.pdf', data: PDF }], /not an accepted file type/)
|
||||
await refused([{ name: 'cv.pdf', data: PDF }], notAcceptable('cv.pdf'))
|
||||
expect(scanned).toHaveLength(1)
|
||||
})
|
||||
|
||||
@@ -501,7 +518,14 @@ describe('6 · failed send, retry, repeated request', () => {
|
||||
const before = await count()
|
||||
const { body, status } = await post({ files: [{ name: 'cv.pdf', data: PDF }] })
|
||||
expect(status).toBe(502)
|
||||
expect(body).toMatchObject({ message: 'We could not send your file. Try again, or send it by e-mail.', reason: 'delivery', success: false })
|
||||
// The visitor is told it is the FILE, at the File field, and what to do — never the mail server's words.
|
||||
expect(body).toEqual({
|
||||
field: 'attachment',
|
||||
message: 'We could not send the file "cv.pdf". Choose a smaller file or another format and try again. You can also send the form without the file.',
|
||||
reason: 'delivery',
|
||||
success: false,
|
||||
})
|
||||
expect(JSON.stringify(body)).not.toMatch(/552|size exceeds|smtp|ECONN/i)
|
||||
|
||||
const stored = (await submissions()).at(-1)!
|
||||
expect(await count()).toBe(before + 1)
|
||||
@@ -527,7 +551,8 @@ describe('6 · failed send, retry, repeated request', () => {
|
||||
test('an adapter that throws is a failed send too', async () => {
|
||||
mailMode = 'throw'
|
||||
const { body } = await post({ files: [{ name: 'cv.pdf', data: PDF }] })
|
||||
expect(body.reason).toBe('delivery')
|
||||
expect(body).toMatchObject({ field: 'attachment', reason: 'delivery' })
|
||||
expect(body.message).not.toMatch(/ECONNREFUSED/)
|
||||
expect((await submissions()).at(-1)!.filesDelivery).toBe('failed')
|
||||
})
|
||||
|
||||
@@ -564,7 +589,8 @@ describe('6 · failed send, retry, repeated request', () => {
|
||||
mails.length = 0
|
||||
mailMode = 'ok'
|
||||
const again = await post({ attempt, files: [{ name: 'cv.pdf', data: PDF }] })
|
||||
expect(again.body.reason).toBe('delivery')
|
||||
expect(again.body).toMatchObject({ field: 'attachment', reason: 'delivery' })
|
||||
expect(again.body.message).toContain('We could not send the file "cv.pdf".')
|
||||
expect(await count()).toBe(before)
|
||||
expect(mails).toEqual([])
|
||||
})
|
||||
@@ -572,16 +598,35 @@ describe('6 · failed send, retry, repeated request', () => {
|
||||
|
||||
describe('7 · languages, the panel, limits', () => {
|
||||
test('messages follow the page language: Polish and English defaults, German and Dutch from the panel', async () => {
|
||||
await payload.updateGlobal({ slug: 'notifications' as never, data: { form: { fileTooLarge: 'Die Datei „{file}“ ist zu groß. Maximal {max} MB.' } } as never, locale: 'de' })
|
||||
await payload.updateGlobal({ slug: 'notifications' as never, data: { form: { fileTooLarge: 'Het bestand "{file}" is te groot. Maximaal {max} MB.' } } as never, locale: 'nl' })
|
||||
await payload.updateGlobal({
|
||||
slug: 'notifications' as never,
|
||||
data: { form: { fileOptional: 'Sie können das Formular auch ohne Datei senden.', fileTooLarge: 'Die Datei „{file}“ ist zu groß. Wählen Sie eine Datei bis {max} MB.' } } as never,
|
||||
locale: 'de',
|
||||
})
|
||||
await payload.updateGlobal({ slug: 'notifications' as never, data: { form: { fileOptional: 'U kunt het formulier ook zonder bestand versturen.', fileTooLarge: 'Het bestand "{file}" is te groot. Kies een bestand tot {max} MB.' } } as never, locale: 'nl' })
|
||||
const big = [{ name: 'big.pdf', data: pad(Buffer.from('%PDF-1.7\n'), MB + 10) }]
|
||||
expect((await post({ files: big, locale: 'pl' })).body.message).toBe('Plik „big.pdf” jest za duży. Limit to 1 MB.')
|
||||
expect((await post({ files: big, locale: 'en' })).body.message).toBe('"big.pdf" is too large. The limit is 1 MB.')
|
||||
expect((await post({ files: big, locale: 'de' })).body.message).toBe('Die Datei „big.pdf“ ist zu groß. Maximal 1 MB.')
|
||||
expect((await post({ files: big, locale: 'nl' })).body.message).toBe('Het bestand "big.pdf" is te groot. Maximaal 1 MB.')
|
||||
// The field's label comes from the panel in the page language.
|
||||
const two = [{ name: 'a.pdf', data: PDF }, { name: 'b.pdf', data: PDF }]
|
||||
expect((await post({ files: two, locale: 'pl' })).body.message).toBe('W polu „Załącznik (opcjonalnie)” można dodać najwyżej 1 plików.')
|
||||
expect((await post({ files: big, locale: 'pl' })).body.message).toBe('Plik „big.pdf” jest za duży. Wybierz plik do 1 MB. Możesz też wysłać formularz bez pliku.')
|
||||
expect((await post({ files: big, locale: 'en' })).body.message).toBe('The file "big.pdf" is too large. Choose a file of up to 1 MB. You can also send the form without the file.')
|
||||
expect((await post({ files: big, locale: 'de' })).body.message).toBe('Die Datei „big.pdf“ ist zu groß. Wählen Sie eine Datei bis 1 MB. Sie können das Formular auch ohne Datei senden.')
|
||||
expect((await post({ files: big, locale: 'nl' })).body.message).toBe('Het bestand "big.pdf" is te groot. Kies een bestand tot 1 MB. U kunt het formulier ook zonder bestand versturen.')
|
||||
|
||||
// Every file message in Polish too; the field's label comes from the panel in the page language.
|
||||
const pl = async (files: Upload[]) => (await post({ files, locale: 'pl' })).body.message
|
||||
expect(await pl([{ name: 'a.pdf', data: PDF }, { name: 'b.pdf', data: PDF }])).toBe('Za dużo plików w polu „Załącznik (opcjonalnie)”. Usuń część: limit to 1. Możesz też wysłać formularz bez pliku.')
|
||||
expect(await pl([{ name: 'zdjęcie.png', data: Buffer.alloc(32, 1) }])).toBe('Plik „zdjęcie.png” ma format, którego nie przyjmujemy. Wybierz jeden z: PDF, DOC, DOCX. Możesz też wysłać formularz bez pliku.')
|
||||
expect(await pl([{ name: 'cv.pdf', data: EXE }])).toBe(
|
||||
'Plik „cv.pdf” został odrzucony: to program lub archiwum, zawiera makra albo jego zawartość nie odpowiada typowi. Wybierz inny plik (PDF, DOC, DOCX). Możesz też wysłać formularz bez pliku.',
|
||||
)
|
||||
mailMode = 'refuse'
|
||||
expect(await pl([{ name: 'cv.pdf', data: PDF }])).toBe('Nie udało się wysłać pliku „cv.pdf”. Wybierz mniejszy plik lub inny format i spróbuj ponownie. Możesz też wysłać formularz bez pliku.')
|
||||
mailMode = 'ok'
|
||||
|
||||
// A failure that is not about the file keeps its own message — no file wording.
|
||||
const consent = (await post({ files: [{ name: 'cv.pdf', data: PDF }], locale: 'en', values: { ...answers, consent: false } })).body
|
||||
expect(consent).toMatchObject({ field: 'consent', reason: 'consent' })
|
||||
expect(consent.message).not.toMatch(/file/i)
|
||||
const missing = (await post({ files: [{ name: 'cv.pdf', data: PDF }], locale: 'en', values: { consent: true, email: '[email protected]' } })).body
|
||||
expect(missing).toMatchObject({ field: 'name', message: 'Check the field "Name".' })
|
||||
})
|
||||
|
||||
test('the form the browser gets carries the limits that really apply, and where to post', async () => {
|
||||
@@ -632,6 +677,13 @@ describe('7 · languages, the panel, limits', () => {
|
||||
expect(none.body).toMatchObject({ field: 'cv', reason: 'validation', success: false })
|
||||
expect(await submitForm({ data: {}, formId: String(required.id), maxPerMinute: 0, payload })).toMatchObject({ field: 'cv', kind: 'required' })
|
||||
expect(await count()).toBe(before)
|
||||
// A required field: the same messages, without "send the form without the file".
|
||||
const big = await post({ files: [{ name: 'cv.pdf', data: pad(Buffer.from('%PDF-1.7\n'), MB + 10), field: 'cv' }], form: String(required.id), values: {} })
|
||||
expect(big.body).toMatchObject({ field: 'cv', message: 'The file "cv.pdf" is too large. Choose a file of up to 1 MB.' })
|
||||
mailMode = 'refuse'
|
||||
const unsent = await post({ files: [{ name: 'cv.pdf', data: PDF, field: 'cv' }], form: String(required.id), values: {} })
|
||||
expect(unsent.body).toMatchObject({ field: 'cv', message: 'We could not send the file "cv.pdf". Choose a smaller file or another format and try again.' })
|
||||
mailMode = 'ok'
|
||||
const ok = await post({ files: [{ name: 'cv.pdf', data: PDF, field: 'cv' }], form: String(required.id), values: {} })
|
||||
expect(ok.body.success).toBe(true)
|
||||
})
|
||||
@@ -640,7 +692,14 @@ describe('7 · languages, the panel, limits', () => {
|
||||
const good = await submitForm({ data: answers, files: { attachment: [{ name: 'cv.pdf', data: PDF }] }, formId, locale: 'en', maxPerMinute: 0, payload })
|
||||
expect(good.success).toBe(true)
|
||||
expect(withFile()).toHaveLength(1)
|
||||
expect(await submitForm({ data: answers, files: { attachment: [{ name: 'cv.pdf', data: EXE }] }, formId, maxPerMinute: 0, payload })).toMatchObject({ kind: 'file_type' })
|
||||
expect(await submitForm({ data: answers, files: { attachment: [{ name: 'cv.pdf', data: EXE }] }, formId, maxPerMinute: 0, payload })).toMatchObject({
|
||||
field: 'attachment',
|
||||
file: 'cv.pdf',
|
||||
kind: 'file_refused',
|
||||
optional: true,
|
||||
types: 'PDF, DOC, DOCX',
|
||||
})
|
||||
expect(await submitForm({ data: answers, files: { attachment: [{ name: 'cv.png', data: EXE }] }, formId, maxPerMinute: 0, payload })).toMatchObject({ kind: 'file_type' })
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
+39
-13
@@ -175,12 +175,15 @@ describe('the form in the browser', () => {
|
||||
|
||||
test('instant checks use the same messages as the server', () => {
|
||||
const pdf = (name: string, size: number) => ({ name, size })
|
||||
const hint = ' You can also send the form without the file.'
|
||||
expect(fileFieldError(field, [pdf('cv.pdf', MB)], texts)).toBeUndefined()
|
||||
expect(fileFieldError(field, [pdf('cv.pdf', 3 * MB)], texts)).toBe('"cv.pdf" is too large. The limit is 2 MB.')
|
||||
expect(fileFieldError(field, [pdf('cv.png', 10)], texts)).toBe('"cv.png" is not an accepted file type. Accepted: PDF, DOCX.')
|
||||
expect(fileFieldError(field, [pdf('a.pdf', 1), pdf('b.pdf', 1), pdf('c.pdf', 1)], texts)).toBe('Attach at most 2 files in "Attachment (optional)".')
|
||||
expect(fileFieldError(field, [pdf('a.pdf', 1.6 * MB), pdf('b.pdf', 1.6 * MB)], texts)).toBe('"b.pdf" is too large. The limit is 3 MB.')
|
||||
expect(fileFieldError(field, [pdf('cv.pdf', 3 * MB)], NOTIFICATION_FALLBACK_PL.form)).toBe('Plik „cv.pdf” jest za duży. Limit to 2 MB.')
|
||||
expect(fileFieldError(field, [pdf('cv.pdf', 3 * MB)], texts)).toBe(`The file "cv.pdf" is too large. Choose a file of up to 2 MB.${hint}`)
|
||||
expect(fileFieldError(field, [pdf('cv.png', 10)], texts)).toBe(`The file "cv.png" is in a format we do not accept. Choose one of: PDF, DOCX.${hint}`)
|
||||
expect(fileFieldError(field, [pdf('a.pdf', 1), pdf('b.pdf', 1), pdf('c.pdf', 1)], texts)).toBe(`Too many files in "Attachment (optional)". Remove some: the limit is 2.${hint}`)
|
||||
expect(fileFieldError(field, [pdf('a.pdf', 1.6 * MB), pdf('b.pdf', 1.6 * MB)], texts)).toBe(`The file "b.pdf" is too large. Choose a file of up to 3 MB.${hint}`)
|
||||
expect(fileFieldError(field, [pdf('cv.pdf', 3 * MB)], NOTIFICATION_FALLBACK_PL.form)).toBe('Plik „cv.pdf” jest za duży. Wybierz plik do 2 MB. Możesz też wysłać formularz bez pliku.')
|
||||
// A required field: no "send without the file".
|
||||
expect(fileFieldError({ ...field, required: true }, [pdf('cv.pdf', 3 * MB)], texts)).toBe('The file "cv.pdf" is too large. Choose a file of up to 2 MB.')
|
||||
// Older text objects without the file keys still give a message.
|
||||
expect(fileFieldError(field, [pdf('cv.png', 10)], { validation: 'Check the field "{field}".' })).toBe('Check the field "Attachment (optional)".')
|
||||
})
|
||||
@@ -189,7 +192,7 @@ describe('the form in the browser', () => {
|
||||
expect(initialBuilderValues(form.fields)).toEqual({ name: '', attachment: [] })
|
||||
expect(validateBuilderFields([field], { attachment: [] }, texts)).toEqual({})
|
||||
expect(validateBuilderFields([{ ...field, required: true }], { attachment: [] }, texts)).toEqual({ attachment: 'Check the field "Attachment (optional)".' })
|
||||
expect(validateBuilderFields([field], { attachment: [{ name: 'x.exe', size: 5 }] }, texts).attachment).toContain('not an accepted file type')
|
||||
expect(validateBuilderFields([field], { attachment: [{ name: 'x.exe', size: 5 }] }, texts).attachment).toContain('is in a format we do not accept')
|
||||
})
|
||||
|
||||
test('markup: a native file input with a label, help text tied to it, accept and multiple', () => {
|
||||
@@ -225,15 +228,38 @@ describe('messages and helpers', () => {
|
||||
const texts = NOTIFICATION_FALLBACK.form
|
||||
|
||||
test('every file outcome has its own text; the placeholders are filled', () => {
|
||||
const fail = (extra: Record<string, unknown>) => resolveFormMessage({ field: 'cv', label: 'CV', reason: 'validation', success: false, ...extra } as never, texts)
|
||||
expect(fail({ file: 'a.pdf', kind: 'file_too_large', max: 10 })).toBe('"a.pdf" is too large. The limit is 10 MB.')
|
||||
expect(fail({ file: 'a.exe', kind: 'file_type', types: 'PDF, DOCX' })).toBe('"a.exe" is not an accepted file type. Accepted: PDF, DOCX.')
|
||||
expect(fail({ kind: 'file_count', max: 2 })).toBe('Attach at most 2 files in "CV".')
|
||||
const fail = (extra: Record<string, unknown>, t = texts) => resolveFormMessage({ field: 'cv', label: 'CV', reason: 'validation', success: false, ...extra } as never, t)
|
||||
// English: which file, why, what to do
|
||||
expect(fail({ file: 'a.pdf', kind: 'file_too_large', max: 10 })).toBe('The file "a.pdf" is too large. Choose a file of up to 10 MB.')
|
||||
expect(fail({ file: 'a.txt', kind: 'file_type', types: 'PDF, DOCX' })).toBe('The file "a.txt" is in a format we do not accept. Choose one of: PDF, DOCX.')
|
||||
expect(fail({ file: 'a.exe', kind: 'file_refused', types: 'PDF, DOCX' })).toBe(
|
||||
'The file "a.exe" was refused: it is a program or an archive, it holds macros, or its content does not match its type. Choose another file (PDF, DOCX).',
|
||||
)
|
||||
expect(fail({ kind: 'file_count', max: 2 })).toBe('Too many files in "CV". Remove some: the limit is 2.')
|
||||
expect(resolveFormMessage({ field: 'cv', file: 'a.pdf', reason: 'delivery', success: false }, texts)).toBe(
|
||||
'We could not send the file "a.pdf". Choose a smaller file or another format and try again.',
|
||||
)
|
||||
// an optional field: every file message adds that the form can go without the file
|
||||
for (const extra of [{ file: 'a.pdf', kind: 'file_too_large', max: 10 }, { file: 'a.txt', kind: 'file_type', types: 'PDF' }, { file: 'a.exe', kind: 'file_refused', types: 'PDF' }, { kind: 'file_count', max: 2 }]) {
|
||||
expect(fail({ ...extra, optional: true })).toMatch(/ You can also send the form without the file\.$/)
|
||||
}
|
||||
expect(resolveFormMessage({ field: 'cv', file: 'a.pdf', optional: true, reason: 'delivery', success: false }, texts)).toMatch(/try again\. You can also send the form without the file\.$/)
|
||||
// Polish
|
||||
const pl = NOTIFICATION_FALLBACK_PL.form
|
||||
expect(fail({ file: 'a.pdf', kind: 'file_too_large', max: 10 }, pl)).toBe('Plik „a.pdf” jest za duży. Wybierz plik do 10 MB.')
|
||||
expect(fail({ file: 'a.txt', kind: 'file_type', types: 'PDF, DOCX' }, pl)).toBe('Plik „a.txt” ma format, którego nie przyjmujemy. Wybierz jeden z: PDF, DOCX.')
|
||||
expect(fail({ file: 'a.exe', kind: 'file_refused', types: 'PDF, DOCX' }, pl)).toBe(
|
||||
'Plik „a.exe” został odrzucony: to program lub archiwum, zawiera makra albo jego zawartość nie odpowiada typowi. Wybierz inny plik (PDF, DOCX).',
|
||||
)
|
||||
expect(fail({ kind: 'file_count', max: 2, optional: true }, pl)).toBe('Za dużo plików w polu „CV”. Usuń część: limit to 2. Możesz też wysłać formularz bez pliku.')
|
||||
expect(resolveFormMessage({ field: 'cv', file: 'a.pdf', reason: 'delivery', success: false }, pl)).toBe('Nie udało się wysłać pliku „a.pdf”. Wybierz mniejszy plik lub inny format i spróbuj ponownie.')
|
||||
// Failures that are not about a file keep their own texts
|
||||
expect(fail({ kind: 'required' })).toBe('Check the field "CV".')
|
||||
expect(resolveFormMessage({ reason: 'delivery', success: false }, texts)).toBe('We could not send your file. Try again, or send it by e-mail.')
|
||||
expect(resolveFormMessage({ reason: 'delivery', success: false }, NOTIFICATION_FALLBACK_PL.form)).toBe('Nie udało się wysłać pliku. Spróbuj ponownie lub wyślij go e-mailem.')
|
||||
expect(fail({ kind: 'invalid_format', optional: true })).toBe('Check the field "CV".')
|
||||
expect(resolveFormMessage({ reason: 'error', success: false }, texts)).toBe(texts.error)
|
||||
expect(resolveFormMessage({ reason: 'rate_limited', success: false }, texts)).toBe(texts.rateLimited)
|
||||
// Texts written before the file keys existed fall back to the general ones.
|
||||
const { fileCount: _a, fileNotSent: _b, fileRemove: _c, fileTooLarge: _d, fileType: _e, ...old } = texts
|
||||
const { fileCount: _a, fileNotSent: _b, fileOptional: _f, fileRefused: _g, fileRemove: _c, fileTooLarge: _d, fileType: _e, ...old } = texts
|
||||
expect(resolveFormMessage({ reason: 'delivery', success: false }, old)).toBe(texts.error)
|
||||
expect(resolveFormMessage({ field: 'cv', file: 'a.pdf', kind: 'file_too_large', max: 1, reason: 'validation', success: false }, old)).toBe('Check the field "cv".')
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user