1+ import Boom from '@hapi/boom'
12import { StatusCodes } from 'http-status-codes'
23
34import { createJoiError } from '~/src/server/helpers/error-helper.js'
@@ -33,7 +34,7 @@ describe('Save-and-exit check routes', () => {
3334 const MAGIC_LINK_ID = 'fd4e6453-fb32-43e4-b4cf-12b381a713de'
3435
3536 describe ( 'GET /resume-form/{formId}/{magicLinkId}' , ( ) => {
36- test ( '/ route forwards correctly on success' , async ( ) => {
37+ test ( 'route forwards correctly on success' , async ( ) => {
3738 jest
3839 . mocked ( getFormMetadataById )
3940 // @ts -expect-error - allow partial objects for tests
@@ -59,7 +60,7 @@ describe('Save-and-exit check routes', () => {
5960 )
6061 } )
6162
62- test ( '/ route forwards correctly on invalid form error' , async ( ) => {
63+ test ( 'route forwards correctly on invalid form error' , async ( ) => {
6364 jest . mocked ( getFormMetadataById ) . mockImplementationOnce ( ( ) => {
6465 throw new Error ( 'form not found' )
6566 } )
@@ -82,7 +83,7 @@ describe('Save-and-exit check routes', () => {
8283 expect ( response . headers . location ) . toBe ( '/resume-form-error' )
8384 } )
8485
85- test ( '/ route forwards correctly on magic link error' , async ( ) => {
86+ test ( 'route forwards correctly on magic link error' , async ( ) => {
8687 jest
8788 . mocked ( getFormMetadataById )
8889 // @ts -expect-error - allow partial objects for tests
@@ -104,7 +105,61 @@ describe('Save-and-exit check routes', () => {
104105 )
105106 } )
106107
107- test ( '/route forwards correctly on magic link error - wrong form id' , async ( ) => {
108+ test ( 'route forwards correctly on magic link consumed but redirects to latest link' , async ( ) => {
109+ jest
110+ . mocked ( getFormMetadataById )
111+ // @ts -expect-error - allow partial objects for tests
112+ . mockResolvedValueOnce ( { slug : 'my-form-to-resume' } )
113+ jest . mocked ( getSaveAndExitDetails ) . mockImplementationOnce ( ( ) => {
114+ const boomError = Boom . resourceGone ( 'magic link consumed' )
115+ boomError . data = {
116+ payload : {
117+ latestId : 'latest-link-id'
118+ }
119+ }
120+ throw boomError
121+ } )
122+
123+ const options = {
124+ method : 'GET' ,
125+ url : `/resume-form/${ FORM_ID } /${ MAGIC_LINK_ID } `
126+ }
127+
128+ const { response } = await renderResponse ( server , options )
129+
130+ expect ( response . statusCode ) . toBe ( StatusCodes . SEE_OTHER )
131+ expect ( response . headers . location ) . toBe (
132+ '/resume-form/eab6ac6c-79b6-439f-bd94-d93eb121b3f1/latest-link-id'
133+ )
134+ } )
135+
136+ test ( 'throws if trying to redirect to latest in group, but none found' , async ( ) => {
137+ jest
138+ . mocked ( getFormMetadataById )
139+ // @ts -expect-error - allow partial objects for tests
140+ . mockResolvedValueOnce ( { slug : 'my-form-to-resume' } )
141+ jest . mocked ( getSaveAndExitDetails ) . mockImplementationOnce ( ( ) => {
142+ const boomError = Boom . resourceGone ( 'magic link consumed' )
143+ boomError . output . payload . custom = {
144+ latestId : undefined
145+ }
146+ throw boomError
147+ } )
148+
149+ const options = {
150+ method : 'GET' ,
151+ url : `/resume-form/${ FORM_ID } /${ MAGIC_LINK_ID } `
152+ }
153+
154+ const { response } = await renderResponse ( server , options )
155+
156+ expect ( response . statusCode ) . toBe ( StatusCodes . SEE_OTHER )
157+ expect ( response . headers . location ) . toBe (
158+ '/resume-form-error/my-form-to-resume'
159+ )
160+ } )
161+
162+ test ( 'route forwards correctly on magic link error - wrong form id' , async ( ) => {
108163 jest
109164 . mocked ( getFormMetadataById )
110165 // @ts -expect-error - allow partial objects for tests
@@ -127,7 +182,7 @@ describe('Save-and-exit check routes', () => {
127182 )
128183 } )
129184
130- test ( '/ route forwards correctly on magic link error 2' , async ( ) => {
185+ test ( 'route forwards correctly on magic link error 2' , async ( ) => {
131186 jest
132187 . mocked ( getFormMetadataById )
133188 // @ts -expect-error - allow partial objects for tests
@@ -149,7 +204,7 @@ describe('Save-and-exit check routes', () => {
149204 } )
150205
151206 describe ( 'GET /resume-form-verify/{formId}/{magicLinkId}/{slug}/state?}' , ( ) => {
152- test ( '/ route renders page' , async ( ) => {
207+ test ( 'route renders page' , async ( ) => {
153208 jest
154209 . mocked ( getFormMetadataById )
155210 // @ts -expect-error - allow partial objects for tests
@@ -178,7 +233,7 @@ describe('Save-and-exit check routes', () => {
178233 expect ( $mastheadHeading ) . toBeInTheDocument ( )
179234 } )
180235
181- test ( '/ route forwards correctly on invalid form error' , async ( ) => {
236+ test ( 'route forwards correctly on invalid form error' , async ( ) => {
182237 jest . mocked ( getFormMetadataById ) . mockImplementationOnce ( ( ) => {
183238 throw new Error ( 'form not found' )
184239 } )
@@ -201,7 +256,7 @@ describe('Save-and-exit check routes', () => {
201256 expect ( response . headers . location ) . toBe ( '/resume-form-error' )
202257 } )
203258
204- test ( '/ route forwards correctly on magic link error' , async ( ) => {
259+ test ( 'route forwards correctly on magic link error' , async ( ) => {
205260 jest
206261 . mocked ( getFormMetadataById )
207262 // @ts -expect-error - allow partial objects for tests
@@ -222,7 +277,7 @@ describe('Save-and-exit check routes', () => {
222277 } )
223278
224279 describe ( 'GET /resume-form-error' , ( ) => {
225- test ( '/ route renders page without slug' , async ( ) => {
280+ test ( 'route renders page without slug' , async ( ) => {
226281 const options = {
227282 method : 'GET' ,
228283 url : '/resume-form-error'
@@ -244,7 +299,7 @@ describe('Save-and-exit check routes', () => {
244299 expect ( $button ) . not . toBeInTheDocument ( )
245300 } )
246301
247- test ( '/ route renders page with slug' , async ( ) => {
302+ test ( 'route renders page with slug' , async ( ) => {
248303 const options = {
249304 method : 'GET' ,
250305 url : '/resume-form-error/my-slug'
@@ -269,7 +324,7 @@ describe('Save-and-exit check routes', () => {
269324 } )
270325
271326 describe ( 'GET /resume-form-success' , ( ) => {
272- test ( '/ route renders page without state' , async ( ) => {
327+ test ( 'route renders page without state' , async ( ) => {
273328 jest
274329 . mocked ( getFormMetadata )
275330 // @ts -expect-error - allow partial objects for tests
@@ -298,7 +353,7 @@ describe('Save-and-exit check routes', () => {
298353 expect ( $button ) . toHaveAttribute ( 'href' , '/form/my-form-to-resume/summary' )
299354 } )
300355
301- test ( '/ route renders page with slug' , async ( ) => {
356+ test ( 'route renders page with slug' , async ( ) => {
302357 jest
303358 . mocked ( getFormMetadata )
304359 // @ts -expect-error - allow partial objects for tests
@@ -332,7 +387,7 @@ describe('Save-and-exit check routes', () => {
332387 } )
333388
334389 describe ( '/resume-form-verify/{formId}/{magicLinkId}/{slug}/{state?}' , ( ) => {
335- test ( '/ route handles invalid password' , async ( ) => {
390+ test ( 'route handles invalid password' , async ( ) => {
336391 jest
337392 . mocked ( getFormMetadataById )
338393 // @ts -expect-error - allow partial objects for tests
@@ -369,7 +424,7 @@ describe('Save-and-exit check routes', () => {
369424 )
370425 } )
371426
372- test ( '/ route handles lockout' , async ( ) => {
427+ test ( 'route handles lockout' , async ( ) => {
373428 jest
374429 . mocked ( getFormMetadataById )
375430 // @ts -expect-error - allow partial objects for tests
@@ -408,7 +463,7 @@ describe('Save-and-exit check routes', () => {
408463 expect ( $errorMessage ) . toBeInTheDocument ( )
409464 } )
410465
411- test ( '/ route handles missing password' , async ( ) => {
466+ test ( 'route handles missing password' , async ( ) => {
412467 jest
413468 . mocked ( getFormMetadataById )
414469 // @ts -expect-error - allow partial objects for tests
@@ -449,7 +504,7 @@ describe('Save-and-exit check routes', () => {
449504 expect ( createJoiError ) . not . toHaveBeenCalled ( )
450505 } )
451506
452- test ( '/ route handles missing password and invalid url' , async ( ) => {
507+ test ( 'route handles missing password and invalid url' , async ( ) => {
453508 jest
454509 . mocked ( getFormMetadataById )
455510 // @ts -expect-error - allow partial objects for tests
0 commit comments