@@ -61,7 +61,7 @@ describe('api: /projects/:id/forms (drafts)', () => {
6161 body . version . should . equal ( 'drafty' ) ;
6262 } ) ) ) ) ) ;
6363
64- it ( 'should create a new draft token setting a new draft version ' , testService ( ( service ) =>
64+ it ( 'should create a new draft token while setting a new draft' , testService ( ( service ) =>
6565 service . login ( 'alice' , ( asAlice ) =>
6666 asAlice . post ( '/v1/projects/1/forms/simple/draft' )
6767 . expect ( 200 )
@@ -83,22 +83,81 @@ describe('api: /projects/:id/forms (drafts)', () => {
8383 } ) ) ;
8484 } ) ) ) ) ) ;
8585
86- it ( 'should worker-process the draft form over to enketo' , testService ( ( service , container ) =>
87- service . login ( 'alice' , ( asAlice ) =>
88- asAlice . post ( '/v1/projects/1/forms/simple/draft' )
89- . expect ( 200 )
90- . then ( ( { body } ) => {
91- should . not . exist ( body . enketoId ) ;
92- } )
93- . then ( ( ) => exhaust ( container ) )
94- . then ( ( ) => asAlice . get ( '/v1/projects/1/forms/simple/draft' )
95- . expect ( 200 )
96- . then ( ( { body } ) => {
97- body . enketoId . should . equal ( '::abcdefgh' ) ;
98- should . not . exist ( body . enketoOnceId ) ;
99- global . enketoReceivedUrl . startsWith ( container . env . domain ) . should . equal ( true ) ;
100- global . enketoReceivedUrl . should . match ( / \/ v 1 \/ t e s t \/ [ a - z 0 - 9 $ ! ] { 64 } \/ p r o j e c t s \/ 1 \/ f o r m s \/ s i m p l e \/ d r a f t / i) ;
101- } ) ) ) ) ) ;
86+ it ( 'should request an enketoId while setting a new draft' , testService ( async ( service , { env } ) => {
87+ const asAlice = await service . login ( 'alice' ) ;
88+ global . enketo . token = '::ijklmnop' ;
89+ await asAlice . post ( '/v1/projects/1/forms/simple/draft' ) . expect ( 200 ) ;
90+ global . enketo . callCount . should . equal ( 1 ) ;
91+ global . enketo . receivedUrl . startsWith ( env . domain ) . should . be . true ( ) ;
92+ const match = global . enketo . receivedUrl . match ( / \/ v 1 \/ t e s t \/ ( [ a - z 0 - 9 $ ! ] { 64 } ) \/ p r o j e c t s \/ 1 \/ f o r m s \/ s i m p l e \/ d r a f t $ / i) ;
93+ should . exist ( match ) ;
94+ const { body } = await asAlice . get ( '/v1/projects/1/forms/simple/draft' )
95+ . expect ( 200 ) ;
96+ match [ 1 ] . should . equal ( body . draftToken ) ;
97+ body . enketoId . should . equal ( '::ijklmnop' ) ;
98+ } ) ) ;
99+
100+ it ( 'should request a new enketoId while setting each new draft' , testService ( async ( service , { env } ) => {
101+ const asAlice = await service . login ( 'alice' ) ;
102+ await asAlice . post ( '/v1/projects/1/forms/simple/draft' ) . expect ( 200 ) ;
103+ await asAlice . post ( '/v1/projects/1/forms/simple/draft/publish?version=two' )
104+ . expect ( 200 ) ;
105+ global . enketo . callCount . should . equal ( 1 ) ;
106+ global . enketo . token = '::ijklmnop' ;
107+ await asAlice . post ( '/v1/projects/1/forms/simple/draft' ) . expect ( 200 ) ;
108+ global . enketo . callCount . should . equal ( 2 ) ;
109+ global . enketo . receivedUrl . startsWith ( env . domain ) . should . be . true ( ) ;
110+ const match = global . enketo . receivedUrl . match ( / \/ v 1 \/ t e s t \/ ( [ a - z 0 - 9 $ ! ] { 64 } ) \/ p r o j e c t s \/ 1 \/ f o r m s \/ s i m p l e \/ d r a f t $ / i) ;
111+ should . exist ( match ) ;
112+ const { body } = await asAlice . get ( '/v1/projects/1/forms/simple/draft' )
113+ . expect ( 200 ) ;
114+ match [ 1 ] . should . equal ( body . draftToken ) ;
115+ body . enketoId . should . equal ( '::ijklmnop' ) ;
116+ } ) ) ;
117+
118+ it ( 'should return with success even if the request to Enketo fails' , testService ( async ( service ) => {
119+ const asAlice = await service . login ( 'alice' ) ;
120+ global . enketo . state = 'error' ;
121+ await asAlice . post ( '/v1/projects/1/forms/simple/draft' ) . expect ( 200 ) ;
122+ const { body } = await asAlice . get ( '/v1/projects/1/forms/simple/draft' )
123+ . expect ( 200 ) ;
124+ should . not . exist ( body . enketoId ) ;
125+ } ) ) ;
126+
127+ it ( 'should stop waiting for Enketo after 0.5 seconds @slow' , testService ( async ( service ) => {
128+ const asAlice = await service . login ( 'alice' ) ;
129+ global . enketo . wait = ( f ) => { setTimeout ( f , 501 ) ; } ;
130+ await asAlice . post ( '/v1/projects/1/forms/simple/draft' ) . expect ( 200 ) ;
131+ const { body } = await asAlice . get ( '/v1/projects/1/forms/simple/draft' )
132+ . expect ( 200 ) ;
133+ should . not . exist ( body . enketoId ) ;
134+ } ) ) ;
135+
136+ it ( 'should request an enketoId from the worker if the request from the endpoint fails' , testService ( async ( service , container ) => {
137+ const asAlice = await service . login ( 'alice' ) ;
138+ global . enketo . state = 'error' ;
139+ await asAlice . post ( '/v1/projects/1/forms/simple/draft' ) . expect ( 200 ) ;
140+ global . enketo . callCount . should . equal ( 1 ) ;
141+ global . enketo . token = '::ijklmnop' ;
142+ await exhaust ( container ) ;
143+ global . enketo . callCount . should . equal ( 2 ) ;
144+ global . enketo . receivedUrl . startsWith ( container . env . domain ) . should . be . true ( ) ;
145+ const match = global . enketo . receivedUrl . match ( / \/ v 1 \/ t e s t \/ ( [ a - z 0 - 9 $ ! ] { 64 } ) \/ p r o j e c t s \/ 1 \/ f o r m s \/ s i m p l e \/ d r a f t $ / i) ;
146+ should . exist ( match ) ;
147+ const { body } = await asAlice . get ( '/v1/projects/1/forms/simple/draft' )
148+ . expect ( 200 ) ;
149+ match [ 1 ] . should . equal ( body . draftToken ) ;
150+ body . enketoId . should . equal ( '::ijklmnop' ) ;
151+ should . not . exist ( body . enketoOnceId ) ;
152+ } ) ) ;
153+
154+ it ( 'should not request an enketoId from the worker if the request from the endpoint succeeds' , testService ( async ( service , container ) => {
155+ const asAlice = await service . login ( 'alice' ) ;
156+ await asAlice . post ( '/v1/projects/1/forms/simple/draft' ) . expect ( 200 ) ;
157+ global . enketo . callCount . should . equal ( 1 ) ;
158+ await exhaust ( container ) ;
159+ global . enketo . callCount . should . equal ( 1 ) ;
160+ } ) ) ;
102161
103162 it ( 'should manage draft/published enketo tokens separately' , testService ( ( service , container ) =>
104163 service . login ( 'alice' , ( asAlice ) =>
@@ -108,10 +167,9 @@ describe('api: /projects/:id/forms (drafts)', () => {
108167 . expect ( 200 )
109168 . then ( ( ) => exhaust ( container ) )
110169 . then ( ( ) => {
111- global . enketoToken = '::ijklmnop' ;
170+ global . enketo . token = '::ijklmnop' ;
112171 return asAlice . post ( '/v1/projects/1/forms/simple2/draft' )
113172 . expect ( 200 )
114- . then ( ( ) => exhaust ( container ) )
115173 . then ( ( ) => Promise . all ( [
116174 asAlice . get ( '/v1/projects/1/forms/simple2' )
117175 . expect ( 200 )
@@ -138,7 +196,7 @@ describe('api: /projects/:id/forms (drafts)', () => {
138196 body . version . should . equal ( 'drafty2' ) ;
139197 } ) ) ) ) ) ;
140198
141- it ( 'should keep the draft token while replacing the draft version ' , testService ( ( service ) =>
199+ it ( 'should keep the draft token while replacing the draft' , testService ( ( service ) =>
142200 service . login ( 'alice' , ( asAlice ) =>
143201 asAlice . post ( '/v1/projects/1/forms/simple/draft' )
144202 . send ( testData . forms . simple . replace ( 'id="simple"' , 'id="simple" version="drafty"' ) )
@@ -161,6 +219,41 @@ describe('api: /projects/:id/forms (drafts)', () => {
161219 } ) ) ;
162220 } ) ) ) ) ) ;
163221
222+ it ( 'should keep the enketoId while replacing the draft' , testService ( async ( service ) => {
223+ const asAlice = await service . login ( 'alice' ) ;
224+ await asAlice . post ( '/v1/projects/1/forms/simple/draft' )
225+ . send ( testData . forms . simple . replace ( 'id="simple"' , 'id="simple" version="drafty"' ) )
226+ . set ( 'Content-Type' , 'application/xml' )
227+ . expect ( 200 ) ;
228+ global . enketo . callCount . should . equal ( 1 ) ;
229+ const { body : draft1 } = await asAlice . get ( '/v1/projects/1/forms/simple/draft' )
230+ . expect ( 200 ) ;
231+ draft1 . enketoId . should . equal ( '::abcdefgh' ) ;
232+ await asAlice . post ( '/v1/projects/1/forms/simple/draft' )
233+ . send ( testData . forms . simple . replace ( 'id="simple"' , 'id="simple" version="drafty2"' ) )
234+ . set ( 'Content-Type' , 'application/xml' )
235+ . expect ( 200 ) ;
236+ global . enketo . callCount . should . equal ( 1 ) ;
237+ const { body : draft2 } = await asAlice . get ( '/v1/projects/1/forms/simple/draft' )
238+ . expect ( 200 ) ;
239+ draft2 . enketoId . should . equal ( '::abcdefgh' ) ;
240+ } ) ) ;
241+
242+ it ( 'should not request an enketoId from the worker while replacing the draft' , testService ( async ( service , container ) => {
243+ const asAlice = await service . login ( 'alice' ) ;
244+ await asAlice . post ( '/v1/projects/1/forms/simple/draft' )
245+ . send ( testData . forms . simple . replace ( 'id="simple"' , 'id="simple" version="drafty"' ) )
246+ . set ( 'Content-Type' , 'application/xml' )
247+ . expect ( 200 ) ;
248+ global . enketo . callCount . should . equal ( 1 ) ;
249+ await asAlice . post ( '/v1/projects/1/forms/simple/draft' )
250+ . send ( testData . forms . simple . replace ( 'id="simple"' , 'id="simple" version="drafty2"' ) )
251+ . set ( 'Content-Type' , 'application/xml' )
252+ . expect ( 200 ) ;
253+ await exhaust ( container ) ;
254+ global . enketo . callCount . should . equal ( 1 ) ;
255+ } ) ) ;
256+
164257 it ( 'should copy the published form definition if not given one' , testService ( ( service ) =>
165258 service . login ( 'alice' , ( asAlice ) =>
166259 asAlice . post ( '/v1/projects/1/forms/simple/draft' )
@@ -814,10 +907,9 @@ describe('api: /projects/:id/forms (drafts)', () => {
814907 . expect ( 200 )
815908 . then ( ( ) => exhaust ( container ) )
816909 . then ( ( ) => {
817- global . enketoToken = '::ijklmnop' ;
910+ global . enketo . token = '::ijklmnop' ;
818911 return asAlice . post ( '/v1/projects/1/forms/simple2/draft' )
819912 . expect ( 200 )
820- . then ( ( ) => exhaust ( container ) )
821913 . then ( ( ) => asAlice . get ( '/v1/projects/1/forms/simple2/draft' )
822914 . set ( 'X-Extended-Metadata' , true )
823915 . expect ( 200 )
0 commit comments