-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test that Enketo IDs are requested during request to create form #1001
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ const { DateTime } = require('luxon'); | |
const { testService } = require('../../setup'); | ||
const testData = require('../../../data/xml'); | ||
const { exhaust } = require(appRoot + '/lib/worker/worker'); | ||
const { without } = require(appRoot + '/lib/util/util'); | ||
|
||
describe('api: /projects/:id/forms (create, read, update)', () => { | ||
|
||
|
@@ -266,34 +267,6 @@ describe('api: /projects/:id/forms (create, read, update)', () => { | |
body.draftToken.should.be.a.token(); | ||
}))))); | ||
|
||
it('should worker-process the form draft over to enketo', testService((service, container) => | ||
service.login('alice', (asAlice) => | ||
asAlice.post('/v1/projects/1/forms') | ||
.set('Content-Type', 'application/xml') | ||
.send(testData.forms.simple2) | ||
.expect(200) | ||
.then(() => exhaust(container)) | ||
.then(() => asAlice.get('/v1/projects/1/forms/simple2/draft') | ||
.expect(200) | ||
.then(({ body }) => { | ||
body.enketoId.should.equal('::abcdefgh'); | ||
should.not.exist(body.enketoOnceId); | ||
}))))); | ||
|
||
it('should worker-process the published form over to enketo', testService((service, container) => | ||
service.login('alice', (asAlice) => | ||
asAlice.post('/v1/projects/1/forms?publish=true') | ||
.set('Content-Type', 'application/xml') | ||
.send(testData.forms.simple2) | ||
.expect(200) | ||
.then(() => exhaust(container)) | ||
.then(() => asAlice.get('/v1/projects/1/forms/simple2') | ||
.expect(200) | ||
.then(({ body }) => { | ||
body.enketoId.should.equal('::abcdefgh'); | ||
body.enketoOnceId.should.equal('::::abcdefgh'); | ||
}))))); | ||
|
||
it('should if flagged save the given definition as published', testService((service) => | ||
service.login('alice', (asAlice) => | ||
asAlice.post('/v1/projects/1/forms?publish=true') | ||
|
@@ -324,6 +297,156 @@ describe('api: /projects/:id/forms (create, read, update)', () => { | |
body.publishedAt.should.be.a.recentIsoDate(); | ||
}))))); | ||
|
||
describe('Enketo ID for draft', () => { | ||
it('should request an enketoId', testService(async (service, { env }) => { | ||
const asAlice = await service.login('alice'); | ||
const { body } = await asAlice.post('/v1/projects/1/forms') | ||
.set('Content-Type', 'application/xml') | ||
.send(testData.forms.simple2) | ||
.expect(200); | ||
global.enketo.callCount.should.equal(1); | ||
global.enketo.createData.should.eql({ | ||
openRosaUrl: `${env.domain}/v1/test/${body.draftToken}/projects/1/forms/simple2/draft`, | ||
xmlFormId: 'simple2', | ||
token: undefined | ||
}); | ||
body.enketoId.should.equal('::abcdefgh'); | ||
should.not.exist(body.enketoOnceId); | ||
})); | ||
|
||
it('should return with success even if request to Enketo fails', testService(async (service) => { | ||
const asAlice = await service.login('alice'); | ||
global.enketo.state = 'error'; | ||
const { body } = await asAlice.post('/v1/projects/1/forms') | ||
.set('Content-Type', 'application/xml') | ||
.send(testData.forms.simple2) | ||
.expect(200); | ||
should.not.exist(body.enketoId); | ||
should.not.exist(body.enketoOnceId); | ||
})); | ||
|
||
it('should wait for Enketo only briefly @slow', testService(async (service) => { | ||
const asAlice = await service.login('alice'); | ||
global.enketo.wait = (done) => { setTimeout(done, 600); }; | ||
const { body } = await asAlice.post('/v1/projects/1/forms') | ||
.set('Content-Type', 'application/xml') | ||
.send(testData.forms.simple2) | ||
.expect(200); | ||
should.not.exist(body.enketoId); | ||
should.not.exist(body.enketoOnceId); | ||
})); | ||
|
||
it('should request an enketoId from worker if request from endpoint fails', testService(async (service, container) => { | ||
const asAlice = await service.login('alice'); | ||
|
||
// First request to Enketo, from the endpoint | ||
global.enketo.state = 'error'; | ||
await asAlice.post('/v1/projects/1/forms') | ||
.set('Content-Type', 'application/xml') | ||
.send(testData.forms.simple2) | ||
.expect(200); | ||
|
||
// Second request, from the worker | ||
global.enketo.callCount.should.equal(1); | ||
await exhaust(container); | ||
global.enketo.callCount.should.equal(2); | ||
const { body } = await asAlice.get('/v1/projects/1/forms/simple2') | ||
.expect(200); | ||
global.enketo.createData.should.eql({ | ||
openRosaUrl: `${container.env.domain}/v1/test/${body.draftToken}/projects/1/forms/simple2/draft`, | ||
xmlFormId: 'simple2', | ||
token: undefined | ||
}); | ||
body.enketoId.should.equal('::abcdefgh'); | ||
should.not.exist(body.enketoOnceId); | ||
})); | ||
|
||
it('should not request an enketoId from worker if request from endpoint succeeds', testService(async (service, container) => { | ||
const asAlice = await service.login('alice'); | ||
await asAlice.post('/v1/projects/1/forms') | ||
.set('Content-Type', 'application/xml') | ||
.send(testData.forms.simple2) | ||
.expect(200); | ||
global.enketo.callCount.should.equal(1); | ||
await exhaust(container); | ||
global.enketo.callCount.should.equal(1); | ||
})); | ||
}); | ||
|
||
describe('Enketo IDs for published form', () => { | ||
it('should request Enketo IDs', testService(async (service, { env }) => { | ||
const asAlice = await service.login('alice'); | ||
const { body } = await asAlice.post('/v1/projects/1/forms?publish=true') | ||
.set('Content-Type', 'application/xml') | ||
.send(testData.forms.simple2) | ||
.expect(200); | ||
global.enketo.callCount.should.equal(1); | ||
without(['token'], global.enketo.createData).should.eql({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need to test the |
||
openRosaUrl: `${env.domain}/v1/projects/1`, | ||
xmlFormId: 'simple2' | ||
}); | ||
body.enketoId.should.equal('::abcdefgh'); | ||
body.enketoOnceId.should.equal('::::abcdefgh'); | ||
})); | ||
|
||
it('should return with success even if request to Enketo fails', testService(async (service) => { | ||
const asAlice = await service.login('alice'); | ||
global.enketo.state = 'error'; | ||
const { body } = await asAlice.post('/v1/projects/1/forms?publish=true') | ||
.set('Content-Type', 'application/xml') | ||
.send(testData.forms.simple2) | ||
.expect(200); | ||
should.not.exist(body.enketoId); | ||
should.not.exist(body.enketoOnceId); | ||
})); | ||
|
||
it('should wait for Enketo only briefly @slow', testService(async (service) => { | ||
const asAlice = await service.login('alice'); | ||
global.enketo.wait = (done) => { setTimeout(done, 600); }; | ||
const { body } = await asAlice.post('/v1/projects/1/forms?publish=true') | ||
.set('Content-Type', 'application/xml') | ||
.send(testData.forms.simple2) | ||
.expect(200); | ||
should.not.exist(body.enketoId); | ||
should.not.exist(body.enketoOnceId); | ||
})); | ||
|
||
it('should request Enketo IDs from worker if request from endpoint fails', testService(async (service, container) => { | ||
const asAlice = await service.login('alice'); | ||
|
||
// First request to Enketo, from the endpoint | ||
global.enketo.state = 'error'; | ||
await asAlice.post('/v1/projects/1/forms?publish=true') | ||
.set('Content-Type', 'application/xml') | ||
.send(testData.forms.simple2) | ||
.expect(200); | ||
|
||
// Second request, from the worker | ||
global.enketo.callCount.should.equal(1); | ||
await exhaust(container); | ||
global.enketo.callCount.should.equal(2); | ||
const { body } = await asAlice.get('/v1/projects/1/forms/simple2') | ||
.expect(200); | ||
without(['token'], global.enketo.createData).should.eql({ | ||
openRosaUrl: `${container.env.domain}/v1/projects/1`, | ||
xmlFormId: 'simple2' | ||
}); | ||
body.enketoId.should.equal('::abcdefgh'); | ||
body.enketoOnceId.should.equal('::::abcdefgh'); | ||
})); | ||
|
||
it('should not request Enketo IDs from worker if request from endpoint succeeds', testService(async (service, container) => { | ||
const asAlice = await service.login('alice'); | ||
await asAlice.post('/v1/projects/1/forms?publish=true') | ||
.set('Content-Type', 'application/xml') | ||
.send(testData.forms.simple2) | ||
.expect(200); | ||
global.enketo.callCount.should.equal(1); | ||
await exhaust(container); | ||
global.enketo.callCount.should.equal(1); | ||
})); | ||
}); | ||
|
||
it('should log the action in the audit log', testService((service) => | ||
service.login('alice', (asAlice) => | ||
asAlice.post('/v1/projects/1/forms') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ const defaults = { | |
// The number of times that the mock has been called during the test, that is, | ||
// the number of requests that would be sent to Enketo | ||
callCount: 0, | ||
// An object with a property for each argument passed to the create() method | ||
createData: undefined, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added
|
||
// The OpenRosa URL that was passed to the create() method | ||
receivedUrl: undefined, | ||
// An object with a property for each argument passed to the edit() method | ||
|
@@ -57,8 +59,9 @@ const request = () => { | |
}); | ||
}; | ||
|
||
const create = async (openRosaUrl) => { | ||
const create = async (openRosaUrl, xmlFormId, token) => { | ||
const { enketoId = '::abcdefgh' } = await request(); | ||
global.enketo.createData = { openRosaUrl, xmlFormId, token }; | ||
global.enketo.receivedUrl = openRosaUrl; | ||
return { enketoId, enketoOnceId: '::::abcdefgh' }; | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change from 501 to 600 is meant to address this intermittent test failure: #588 (comment). Note that this test was not introduced in #989, but rather #782. I don't think I did anything in #989 that would have changed the POST/GET …/draft endpoints. However, if this test is failing, it certainly seems possible that new tests in #989 could fail, so I think it's worth addressing now.
Here's one possible explanation for the failure:
Another possible explanation:
setTimeout()
doesn't run callbacks after an exact number of milliseconds, but after at least the specified number of milliseconds. So the Enketo mock could respond after 502 ms instead of after 501 ms.I'm not sure what the exact explanation is, but the fact that the test is failing intermittently doesn't make me think that there's anything wrong with the functionality here. I think we just need to provide a little more wiggle room for the test.