From 3ccd6446c2b07420b4a1e05ac535c778caf10eef Mon Sep 17 00:00:00 2001 From: Sadiq Khoja Date: Wed, 8 Jan 2025 17:32:43 -0500 Subject: [PATCH] fixes #869: return warnings if there are any --- lib/model/query/forms.js | 6 ++++-- test/integration/api/forms/forms.js | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/model/query/forms.js b/lib/model/query/forms.js index 97f04f8e3..508cc37e3 100644 --- a/lib/model/query/forms.js +++ b/lib/model/query/forms.js @@ -242,14 +242,16 @@ const createVersion = (partial, form, publish, duplicating = false) => async ({ // skip checking for structural change if duplicating because user has already // been warning at the time of form definition upload if (!duplicating) { - await Forms.checkStructuralChange(prevFields, fields) - .then(Forms.rejectIfWarnings); + await Forms.checkStructuralChange(prevFields, fields); } // If we haven't been rejected or warned yet, make a new schema id schemaId = await Forms._newSchema(); } } + // Let's check for warnings before pushing to Enketo or to DB + await Forms.rejectIfWarnings(); + // If not publishing, check whether there is an existing draft that we have access to. // If not, generate a draft token and enketoId. let { draftToken, enketoId } = form.def; diff --git a/test/integration/api/forms/forms.js b/test/integration/api/forms/forms.js index 635eb3e76..c649fd5f0 100644 --- a/test/integration/api/forms/forms.js +++ b/test/integration/api/forms/forms.js @@ -158,6 +158,25 @@ describe('api: /projects/:id/forms (create, read, update)', () => { })); })); + it('should fail on warnings even for valid xlsx files', testService(async (service) => { + await service.login('alice', (asAlice) => + asAlice.post('/v1/projects/1/forms') + .send(readFileSync(appRoot + '/test/data/simple.xlsx')) + .set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') + .expect(200)); + + global.xlsformTest = 'warning'; // set up the mock service to warn. + return service.login('alice', (asAlice) => + asAlice.post('/v1/projects/1/forms/simple2/draft') + .send(readFileSync(appRoot + '/test/data/simple.xlsx')) + .set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') + .expect(400) + .then(({ body }) => { + body.code.should.equal(400.16); + body.details.warnings.xlsFormWarnings.should.eql([ 'warning 1', 'warning 2' ]); + })); + })); + it('should create the form for xlsx files with warnings given ignoreWarnings', testService((service) => { global.xlsformTest = 'warning'; // set up the mock service to warn. return service.login('alice', (asAlice) =>