@@ -17,6 +17,7 @@ const baseRequest = {
1717
1818describe ( "Route POST /dossiers" , ( ) => {
1919 beforeAll ( async ( ) => {
20+ await tryber . seeds ( ) . bug_types ( ) ;
2021 await tryber . tables . CampaignPhase . do ( ) . insert ( [
2122 { id : 1 , name : "Test Phase" , type_id : 1 } ,
2223 ] ) ;
@@ -116,6 +117,7 @@ describe("Route POST /dossiers", () => {
116117 await tryber . tables . CampaignDossierDataBrowsers . do ( ) . delete ( ) ;
117118 await tryber . tables . CampaignDossierDataLanguages . do ( ) . delete ( ) ;
118119 await tryber . tables . CampaignDossierDataCountries . do ( ) . delete ( ) ;
120+ await tryber . tables . WpAppqCampaignAdditionalFields . do ( ) . delete ( ) ;
119121 } ) ;
120122
121123 it ( "Should create a campaign" , async ( ) => {
@@ -274,6 +276,126 @@ describe("Route POST /dossiers", () => {
274276 expect ( campaign ) . toHaveProperty ( "close_date" , "2021-08-20 14:15:22" ) ;
275277 } ) ;
276278
279+ it ( "Should create a campaign with the specified additional fields" , async ( ) => {
280+ const response = await request ( app )
281+ . post ( "/dossiers" )
282+ . set ( "authorization" , "Bearer admin" )
283+ . send ( {
284+ ...baseRequest ,
285+ additionals : [
286+ {
287+ name : "Regex Field" ,
288+ slug : "regex-field" ,
289+ error : "Regex error" ,
290+ type : "text" ,
291+ regex : "^[a-zA-Z0-9]+$" ,
292+ } ,
293+ {
294+ name : "Select Field" ,
295+ slug : "select-field" ,
296+ error : "Select error" ,
297+ type : "select" ,
298+ showInStats : true ,
299+ options : [ "Option 1" , "Option 2" ] ,
300+ } ,
301+ ] ,
302+ } ) ;
303+ expect ( response . status ) . toBe ( 201 ) ;
304+ expect ( response . body ) . toHaveProperty ( "id" ) ;
305+ const id = response . body . id ;
306+
307+ const fields = await tryber . tables . WpAppqCampaignAdditionalFields . do ( )
308+ . select ( )
309+ . where ( "cp_id" , id ) ;
310+
311+ expect ( fields ) . toHaveLength ( 2 ) ;
312+ expect ( fields ) . toEqual (
313+ expect . arrayContaining ( [
314+ expect . objectContaining ( {
315+ cp_id : id ,
316+ title : "Regex Field" ,
317+ slug : "regex-field" ,
318+ type : "regex" ,
319+ validation : "^[a-zA-Z0-9]+$" ,
320+ error_message : "Regex error" ,
321+ stats : 0 ,
322+ } ) ,
323+ expect . objectContaining ( {
324+ cp_id : id ,
325+ title : "Select Field" ,
326+ slug : "select-field" ,
327+ type : "select" ,
328+ validation : "Option 1;Option 2" ,
329+ error_message : "Select error" ,
330+ stats : 1 ,
331+ } ) ,
332+ ] )
333+ ) ;
334+ } ) ;
335+
336+ it ( "Should create a campaign with the specified bug types" , async ( ) => {
337+ const response = await request ( app )
338+ . post ( "/dossiers" )
339+ . set ( "authorization" , "Bearer admin" )
340+ . send ( {
341+ ...baseRequest ,
342+ bugTypes : [ 1 , 2 , 3 ] ,
343+ } ) ;
344+ expect ( response . status ) . toBe ( 201 ) ;
345+ expect ( response . body ) . toHaveProperty ( "id" ) ;
346+ const id = response . body . id ;
347+
348+ const bugTypes = await tryber . tables . WpAppqAdditionalBugTypes . do ( )
349+ . select ( )
350+ . where ( "campaign_id" , id ) ;
351+
352+ expect ( bugTypes ) . toHaveLength ( 3 ) ;
353+ expect ( bugTypes ) . toEqual (
354+ expect . arrayContaining ( [
355+ expect . objectContaining ( {
356+ bug_type_id : 1 ,
357+ } ) ,
358+ expect . objectContaining ( {
359+ bug_type_id : 2 ,
360+ } ) ,
361+ expect . objectContaining ( {
362+ bug_type_id : 3 ,
363+ } ) ,
364+ ] )
365+ ) ;
366+ } ) ;
367+
368+ it ( "Should create a campaign with default bug types if no specific bug types are selected" , async ( ) => {
369+ const response = await request ( app )
370+ . post ( "/dossiers" )
371+ . set ( "authorization" , "Bearer admin" )
372+ . send ( {
373+ ...baseRequest ,
374+ } ) ;
375+ expect ( response . status ) . toBe ( 201 ) ;
376+ expect ( response . body ) . toHaveProperty ( "id" ) ;
377+ const id = response . body . id ;
378+
379+ const bugTypes = await tryber . tables . WpAppqAdditionalBugTypes . do ( )
380+ . select ( )
381+ . where ( "campaign_id" , id ) ;
382+
383+ expect ( bugTypes ) . toHaveLength ( 0 ) ;
384+ } ) ;
385+
386+ it ( "Should throw an error if invalid bugtype is sent" , async ( ) => {
387+ const response = await request ( app )
388+ . post ( "/dossiers" )
389+ . set ( "authorization" , "Bearer admin" )
390+ . send ( {
391+ ...baseRequest ,
392+ bugTypes : [ 100 ] ,
393+ } ) ;
394+ expect ( response . status ) . toBe ( 406 ) ;
395+ expect ( response . body ) . toHaveProperty ( "id" ) ;
396+ const id = response . body . id ;
397+ } ) ;
398+
277399 it ( "Should create a campaign with the end date as start date + 7 if left unspecified" , async ( ) => {
278400 const response = await request ( app )
279401 . post ( "/dossiers" )
0 commit comments