fix(transport) : upload décharge différé à l'enregistrement/validation (évite les orphelins) (ERP-171)

This commit is contained in:
2026-06-17 15:22:25 +02:00
parent 1d5110d000
commit 7668d77c78
4 changed files with 99 additions and 20 deletions
@@ -350,6 +350,52 @@ describe('useCarrierForm — champs conditionnels (ERP-166)', () => {
form.main.dischargeDocumentIri = '/api/uploaded_documents/7'
expect(form.buildMainPayload()).toMatchObject({ dischargeDocument: '/api/uploaded_documents/7' })
})
it('RG-4.02 upload différé : selectDischarge ne POST pas ; submitMain upload PUIS crée', async () => {
mockPost.mockReset()
// 1er POST = /uploaded_documents (renvoie l'IRI) ; 2e = /carriers (création).
mockPost
.mockResolvedValueOnce({ '@id': '/api/uploaded_documents/7' })
.mockResolvedValueOnce({ id: 12, name: 'ACME', certificationType: 'AUTRE' })
const form = useCarrierForm()
form.main.name = 'Acme'
form.main.certificationType = 'AUTRE'
// Sélection du fichier : aucun appel réseau (upload différé à l'enregistrement).
form.selectDischarge(new File(['x'], 'decharge.pdf', { type: 'application/pdf' }))
expect(mockPost).not.toHaveBeenCalled()
// La validation est satisfaite par le fichier en attente (pas encore d'IRI).
expect(form.mainErrors.errors.dischargeDocument).toBeUndefined()
const created = await form.submitMain()
expect(created).toBe(true)
// 1er appel : upload multipart ; 2e : création carrier avec l'IRI résolu.
expect(mockPost.mock.calls[0][0]).toBe('/uploaded_documents')
expect(mockPost.mock.calls[1][0]).toBe('/carriers')
expect(mockPost.mock.calls[1][1]).toMatchObject({ dischargeDocument: '/api/uploaded_documents/7' })
})
it('RG-4.02 upload différé : un 422 MIME bloque la création (message inline, pas de POST /carriers)', async () => {
mockPost.mockReset()
// Le POST /uploaded_documents échoue (MIME hors whitelist) → 422.
mockPost.mockRejectedValueOnce(Object.assign(new Error('422'), {
data: { 'hydra:description': 'Type de fichier non autorisé.' },
}))
const form = useCarrierForm()
form.main.name = 'Acme'
form.main.certificationType = 'AUTRE'
form.selectDischarge(new File(['x'], 'malware.exe', { type: 'application/x-msdownload' }))
const created = await form.submitMain()
expect(created).toBe(false)
// Message back affiché inline sous le champ ; aucune création de carrier.
expect(form.mainErrors.errors.dischargeDocument).toBe('Type de fichier non autorisé.')
expect(mockPost).toHaveBeenCalledTimes(1)
expect(mockPost.mock.calls[0][0]).toBe('/uploaded_documents')
})
})
describe('useCarrierForm — copie QUALIMAT (ERP-166)', () => {