Skip to content

Commit

Permalink
Reject forms which do not have <instanceID>
Browse files Browse the repository at this point in the history
Closes #112
  • Loading branch information
alxndrsn committed Dec 10, 2018
1 parent f8ddb6c commit 8b51f74
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v1.18.15
* Reject forms which do not have <instanceID>
* [compile-contact-summary] make sure test fails when it should
* Update documentation for initialising project layout

## v1.18.14
* [upload-custom-translations] Add tests for webapp v3.4.0's new doc layout
* Change translations structure
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "medic-conf",
"version": "1.18.14",
"version": "1.18.15",
"description": "Configure Medic Mobile deployments",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/abort-promise-chain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = (promiseChain, message) =>
promiseChain.then(() => {
throw new Error(message);
});
10 changes: 9 additions & 1 deletion src/lib/upload-forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const skipFn = require('../lib/skip-fn');
const trace = require('../lib/log').trace;
const warn = require('../lib/log').warn;
const pouch = require('../lib/db');
const abortPromiseChain = require('../lib/abort-promise-chain');

const SUPPORTED_PROPERTIES = ['context', 'icon', 'internalId', 'title'];

Expand All @@ -20,11 +21,11 @@ module.exports = (projectDir, couchUrl, subDirectory, options) => {

const formsDir = `${projectDir}/forms/${subDirectory}`;


if(!fs.exists(formsDir)) {
warn(`Forms dir not found: ${formsDir}`);
return Promise.resolve();
}

return fs.readdir(formsDir)
.filter(name => name.endsWith('.xml'))
.filter(name => !options.forms || options.forms.includes(fs.withoutExtension(name)))
Expand All @@ -40,6 +41,11 @@ module.exports = (projectDir, couchUrl, subDirectory, options) => {

const xml = fs.read(xformPath);

if(!formHasInstanceId(xml)) {
return abortPromiseChain(promiseChain,
`Form at ${xformPath} appears to be missing <meta><instanceID/></meta> node. This form will not work on medic-webapp.`);
}

const internalId = readIdFrom(xml);
if(internalId !== baseDocId) warn('DEPRECATED', 'Form:', fileName, 'Bad ID set in XML. Expected:', baseDocId, 'but saw:', internalId, ' Support for setting these values differently will be dropped. Please see https://github.com/medic/medic-webapp/issues/3342.');

Expand Down Expand Up @@ -93,3 +99,5 @@ const updateFromPropertiesFile = (doc, path) => {
if(ignoredKeys.length) warn('Ignoring property keys', ignoredKeys, 'in', path);
}
};

const formHasInstanceId = xml => xml.includes('<instanceID/>');
19 changes: 19 additions & 0 deletions test/data/lib/upload-forms/no-instance-id/forms/example.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:orx="http://openrosa.org/xforms/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<h:head>
<h:title>No Instance ID</h:title>
<model>
<instance>
<data id="ABC" version="2015-06-05">
<name/>
</data>
</instance>
<bind nodeset="/data/name" type="string" />
</model>
</h:head>
<h:body>
<input ref="/data/name">
<label>What is the name?</label>
</input>
</h:body>
</h:html>
20 changes: 20 additions & 0 deletions test/lib/upload-forms.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const assert = require('chai').assert;
const uploadForms = require('../../src/lib/upload-forms');

const BASE_DIR = 'data/lib/upload-forms';
const COUCH_URL = 'http://example.com/db-name';
const FORMS_SUBDIR = '.';

describe('upload-forms', function() {
it('should reject forms which do not have <meta><instanceID/></meta>', function() {

// when
return uploadForms(`${BASE_DIR}/no-instance-id`, COUCH_URL, FORMS_SUBDIR)

.then(() => assert.fail('Expected Error to be thrown.'))

.catch(e => {
assert.include(e.message, 'This form will not work on medic-webapp.');
});
});
});

0 comments on commit 8b51f74

Please sign in to comment.