@@ -121,7 +121,7 @@ def test_post_dref_creation(self, send_notification):
121121 "title" : "Dref test title" ,
122122 "type_of_onset" : Dref .OnsetType .SLOW .value ,
123123 "disaster_category" : Dref .DisasterCategory .YELLOW .value ,
124- "status" : Dref .Status .IN_PROGRESS .value ,
124+ "status" : Dref .Status .DRAFT .value ,
125125 "num_assisted" : 5666 ,
126126 "num_affected" : 23 ,
127127 "amount_requested" : 127771111 ,
@@ -237,7 +237,7 @@ def test_event_date_in_dref(self):
237237 "title" : "Dref test title" ,
238238 "type_of_onset" : Dref .OnsetType .SLOW .value ,
239239 "disaster_category" : Dref .DisasterCategory .YELLOW .value ,
240- "status" : Dref .Status .IN_PROGRESS .value ,
240+ "status" : Dref .Status .DRAFT .value ,
241241 "national_society" : national_society .id ,
242242 "num_assisted" : 5666 ,
243243 "num_affected" : 23 ,
@@ -356,27 +356,27 @@ def test_filter_dref_status(self):
356356 """
357357 Test to filter dref status
358358 """
359- DrefFactory .create (title = "test" , status = Dref .Status .COMPLETED , date_of_approval = "2020-10-10" , created_by = self .user )
360- DrefFactory .create (status = Dref .Status .COMPLETED , date_of_approval = "2020-10-10" , created_by = self .user )
361- DrefFactory .create (status = Dref .Status .COMPLETED , date_of_approval = "2020-10-10" , created_by = self .user )
362- DrefFactory .create (status = Dref .Status .IN_PROGRESS , created_by = self .user )
363- DrefFactory .create (status = Dref .Status .IN_PROGRESS , created_by = self .user )
359+ DrefFactory .create (title = "test" , status = Dref .Status .APPROVED , date_of_approval = "2020-10-10" , created_by = self .user )
360+ DrefFactory .create (status = Dref .Status .APPROVED , date_of_approval = "2020-10-10" , created_by = self .user )
361+ DrefFactory .create (status = Dref .Status .APPROVED , date_of_approval = "2020-10-10" , created_by = self .user )
362+ DrefFactory .create (status = Dref .Status .DRAFT , created_by = self .user )
363+ DrefFactory .create (status = Dref .Status .DRAFT , created_by = self .user )
364364
365365 # filter by `In Progress`
366- url = f"/api/v2/dref/?status={ Dref .Status .IN_PROGRESS .value } "
366+ url = f"/api/v2/dref/?status={ Dref .Status .DRAFT .value } "
367367 self .client .force_authenticate (self .user )
368368 response = self .client .get (url )
369369 self .assertEqual (response .status_code , 200 )
370- self .assertEqual (len (response .data ["results" ]), 5 )
370+ self .assertEqual (len (response .data ["results" ]), 2 )
371371
372372 def test_dref_country_filter (self ):
373373 country1 = Country .objects .create (name = "country1" )
374374 country2 = Country .objects .create (name = "country2" )
375- DrefFactory .create (title = "test" , status = Dref .Status .COMPLETED , created_by = self .user , country = country1 )
376- DrefFactory .create (status = Dref .Status .COMPLETED , created_by = self .user )
377- DrefFactory .create (status = Dref .Status .COMPLETED , created_by = self .user , country = country2 )
378- DrefFactory .create (status = Dref .Status .IN_PROGRESS , created_by = self .user , country = country1 )
379- DrefFactory .create (status = Dref .Status .IN_PROGRESS , created_by = self .user )
375+ DrefFactory .create (title = "test" , status = Dref .Status .APPROVED , created_by = self .user , country = country1 )
376+ DrefFactory .create (status = Dref .Status .APPROVED , created_by = self .user )
377+ DrefFactory .create (status = Dref .Status .APPROVED , created_by = self .user , country = country2 )
378+ DrefFactory .create (status = Dref .Status .DRAFT , created_by = self .user , country = country1 )
379+ DrefFactory .create (status = Dref .Status .DRAFT , created_by = self .user )
380380 url = f"/api/v2/dref/?country={ country1 .id } "
381381 self .client .force_authenticate (self .user )
382382 response = self .client .get (url )
@@ -386,9 +386,10 @@ def test_dref_country_filter(self):
386386 @mock .patch ("django.utils.timezone.now" )
387387 def test_dref_is_published (self , mock_now ):
388388 """
389- Test for dref if is_published = True
389+ Test DREF publishing flow:
390+ - Can only publish when status=FINALIZED
391+ - Publishing sets status=APPROVED and is_published=True
390392 """
391-
392393 initial_now = datetime .now ()
393394 mock_now .return_value = initial_now
394395
@@ -397,9 +398,12 @@ def test_dref_is_published(self, mock_now):
397398 dref = DrefFactory .create (
398399 title = "test" ,
399400 created_by = self .user ,
401+ country = country ,
402+ status = Dref .Status .DRAFT ,
400403 is_published = False ,
401404 type_of_dref = Dref .DrefType .IMMINENT ,
402405 )
406+ # Normal PATCH
403407 url = f"/api/v2/dref/{ dref .id } /"
404408 data = {
405409 "title" : "New Update Title" ,
@@ -408,7 +412,6 @@ def test_dref_is_published(self, mock_now):
408412 self .client .force_authenticate (self .user )
409413 response = self .client .patch (url , data )
410414 self .assert_200 (response )
411-
412415 # create new dref with is_published = False
413416 not_published_dref = DrefFactory .create (
414417 title = "test" ,
@@ -424,23 +427,28 @@ def test_dref_is_published(self, mock_now):
424427 data ["modified_at" ] = initial_now - timedelta (seconds = 10 )
425428 response = self .client .patch (url , data )
426429 self .assert_400 (response )
427-
428- # test dref published endpoint
429- url = f"/api/v2/dref/{ not_published_dref .id } /publish/"
430+ # ---- Test publishing ----
431+ publish_url = f"/api/v2/dref/{ dref .id } /publish/"
430432 data = {}
431- self .client .force_authenticate (self .user )
432- response = self .client .post (url , data )
433+ response = self .client .post (publish_url , data )
433434 self .assert_403 (response )
434-
435- # add permission to request user
435+ # Add permission to user
436436 self .dref_permission = Permission .objects .create (
437437 codename = "dref_region_admin_0" ,
438438 content_type = ContentType .objects .get_for_model (Region ),
439439 name = "Dref Admin for 0" ,
440440 )
441441 self .user .user_permissions .add (self .dref_permission )
442- response = self .client .post (url , data )
442+ # Try again while DRAFT. Should fail(not finalized yet)
443+ response = self .client .post (publish_url , data )
444+ self .assert_400 (response )
445+ # Update status to FINALIZED, then publish should succeed
446+ dref .status = Dref .Status .FINALIZED
447+ dref .save (update_fields = ["status" ])
448+ response = self .client .post (publish_url , data )
449+ dref .refresh_from_db ()
443450 self .assert_200 (response )
451+ self .assertEqual (response .data ["status" ], Dref .Status .APPROVED )
444452 self .assertEqual (response .data ["is_published" ], True )
445453
446454 def test_dref_operation_update_create (self ):
@@ -672,6 +680,7 @@ def test_final_report_update_once_published(self):
672680 dref = dref ,
673681 country = country ,
674682 type_of_dref = Dref .DrefType .RESPONSE ,
683+ status = Dref .Status .FINALIZED ,
675684 )
676685 final_report .users .set ([user1 ])
677686 # try to publish this report
@@ -691,7 +700,7 @@ def test_final_report_update_once_published(self):
691700 response = self .client .post (url , data )
692701 self .assert_200 (response )
693702 self .assertEqual (response .data ["is_published" ], True )
694-
703+ self . assertEqual ( response . data [ "status" ], Dref . Status . APPROVED )
695704 # now try to patch to the final report
696705 url = f"/api/v2/dref-final-report/{ final_report .id } /"
697706 data = {
@@ -709,7 +718,7 @@ def test_dref_for_assessment_report(self):
709718 "type_of_onset" : Dref .OnsetType .SLOW .value ,
710719 "type_of_dref" : Dref .DrefType .ASSESSMENT ,
711720 "disaster_category" : Dref .DisasterCategory .YELLOW .value ,
712- "status" : Dref .Status .IN_PROGRESS .value ,
721+ "status" : Dref .Status .DRAFT .value ,
713722 "num_assisted" : 5666 ,
714723 "num_affected" : 23 ,
715724 "amount_requested" : 127771111 ,
@@ -1297,7 +1306,7 @@ def test_dref_imminent(self):
12971306 "type_of_onset" : Dref .OnsetType .SUDDEN .value ,
12981307 "type_of_dref" : Dref .DrefType .IMMINENT ,
12991308 "disaster_category" : Dref .DisasterCategory .YELLOW .value ,
1300- "status" : Dref .Status .IN_PROGRESS .value ,
1309+ "status" : Dref .Status .DRAFT .value ,
13011310 "num_assisted" : 5666 ,
13021311 "num_affected" : 23 ,
13031312 "amount_requested" : 127771111 ,
0 commit comments