@@ -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 ,
@@ -487,7 +487,7 @@ def test_event_date_in_dref(self):
487487 "title" : "Dref test title" ,
488488 "type_of_onset" : Dref .OnsetType .SLOW .value ,
489489 "disaster_category" : Dref .DisasterCategory .YELLOW .value ,
490- "status" : Dref .Status .IN_PROGRESS .value ,
490+ "status" : Dref .Status .DRAFT .value ,
491491 "national_society" : national_society .id ,
492492 "num_assisted" : 5666 ,
493493 "num_affected" : 23 ,
@@ -606,27 +606,27 @@ def test_filter_dref_status(self):
606606 """
607607 Test to filter dref status
608608 """
609- DrefFactory .create (title = "test" , status = Dref .Status .COMPLETED , date_of_approval = "2020-10-10" , created_by = self .user )
610- DrefFactory .create (status = Dref .Status .COMPLETED , date_of_approval = "2020-10-10" , created_by = self .user )
611- DrefFactory .create (status = Dref .Status .COMPLETED , date_of_approval = "2020-10-10" , created_by = self .user )
612- DrefFactory .create (status = Dref .Status .IN_PROGRESS , created_by = self .user )
613- DrefFactory .create (status = Dref .Status .IN_PROGRESS , created_by = self .user )
609+ DrefFactory .create (title = "test" , status = Dref .Status .APPROVED , date_of_approval = "2020-10-10" , created_by = self .user )
610+ DrefFactory .create (status = Dref .Status .APPROVED , date_of_approval = "2020-10-10" , created_by = self .user )
611+ DrefFactory .create (status = Dref .Status .APPROVED , date_of_approval = "2020-10-10" , created_by = self .user )
612+ DrefFactory .create (status = Dref .Status .DRAFT , created_by = self .user )
613+ DrefFactory .create (status = Dref .Status .DRAFT , created_by = self .user )
614614
615615 # filter by `In Progress`
616- url = f"/api/v2/dref/?status={ Dref .Status .IN_PROGRESS .value } "
616+ url = f"/api/v2/dref/?status={ Dref .Status .DRAFT .value } "
617617 self .client .force_authenticate (self .user )
618618 response = self .client .get (url )
619619 self .assertEqual (response .status_code , 200 )
620- self .assertEqual (len (response .data ["results" ]), 5 )
620+ self .assertEqual (len (response .data ["results" ]), 2 )
621621
622622 def test_dref_country_filter (self ):
623623 country1 = Country .objects .create (name = "country1" )
624624 country2 = Country .objects .create (name = "country2" )
625- DrefFactory .create (title = "test" , status = Dref .Status .COMPLETED , created_by = self .user , country = country1 )
626- DrefFactory .create (status = Dref .Status .COMPLETED , created_by = self .user )
627- DrefFactory .create (status = Dref .Status .COMPLETED , created_by = self .user , country = country2 )
628- DrefFactory .create (status = Dref .Status .IN_PROGRESS , created_by = self .user , country = country1 )
629- DrefFactory .create (status = Dref .Status .IN_PROGRESS , created_by = self .user )
625+ DrefFactory .create (title = "test" , status = Dref .Status .APPROVED , created_by = self .user , country = country1 )
626+ DrefFactory .create (status = Dref .Status .APPROVED , created_by = self .user )
627+ DrefFactory .create (status = Dref .Status .APPROVED , created_by = self .user , country = country2 )
628+ DrefFactory .create (status = Dref .Status .DRAFT , created_by = self .user , country = country1 )
629+ DrefFactory .create (status = Dref .Status .DRAFT , created_by = self .user )
630630 url = f"/api/v2/dref/?country={ country1 .id } "
631631 self .client .force_authenticate (self .user )
632632 response = self .client .get (url )
@@ -636,9 +636,10 @@ def test_dref_country_filter(self):
636636 @mock .patch ("django.utils.timezone.now" )
637637 def test_dref_is_published (self , mock_now ):
638638 """
639- Test for dref if is_published = True
639+ Test DREF publishing flow:
640+ - Can only publish when status=FINALIZED
641+ - Publishing sets status=APPROVED and is_published=True
640642 """
641-
642643 initial_now = datetime .now ()
643644 mock_now .return_value = initial_now
644645
@@ -647,9 +648,12 @@ def test_dref_is_published(self, mock_now):
647648 dref = DrefFactory .create (
648649 title = "test" ,
649650 created_by = self .user ,
651+ country = country ,
652+ status = Dref .Status .DRAFT ,
650653 is_published = False ,
651654 type_of_dref = Dref .DrefType .IMMINENT ,
652655 )
656+ # Normal PATCH
653657 url = f"/api/v2/dref/{ dref .id } /"
654658 data = {
655659 "title" : "New Update Title" ,
@@ -658,7 +662,6 @@ def test_dref_is_published(self, mock_now):
658662 self .client .force_authenticate (self .user )
659663 response = self .client .patch (url , data )
660664 self .assert_200 (response )
661-
662665 # create new dref with is_published = False
663666 not_published_dref = DrefFactory .create (
664667 title = "test" ,
@@ -674,23 +677,28 @@ def test_dref_is_published(self, mock_now):
674677 data ["modified_at" ] = initial_now - timedelta (seconds = 10 )
675678 response = self .client .patch (url , data )
676679 self .assert_400 (response )
677-
678- # test dref published endpoint
679- url = f"/api/v2/dref/{ not_published_dref .id } /publish/"
680+ # ---- Test publishing ----
681+ publish_url = f"/api/v2/dref/{ dref .id } /publish/"
680682 data = {}
681- self .client .force_authenticate (self .user )
682- response = self .client .post (url , data )
683+ response = self .client .post (publish_url , data )
683684 self .assert_403 (response )
684-
685- # add permission to request user
685+ # Add permission to user
686686 self .dref_permission = Permission .objects .create (
687687 codename = "dref_region_admin_0" ,
688688 content_type = ContentType .objects .get_for_model (Region ),
689689 name = "Dref Admin for 0" ,
690690 )
691691 self .user .user_permissions .add (self .dref_permission )
692- response = self .client .post (url , data )
692+ # Try again while DRAFT. Should fail(not finalized yet)
693+ response = self .client .post (publish_url , data )
694+ self .assert_400 (response )
695+ # Update status to FINALIZED, then publish should succeed
696+ dref .status = Dref .Status .FINALIZED
697+ dref .save (update_fields = ["status" ])
698+ response = self .client .post (publish_url , data )
699+ dref .refresh_from_db ()
693700 self .assert_200 (response )
701+ self .assertEqual (response .data ["status" ], Dref .Status .APPROVED )
694702 self .assertEqual (response .data ["is_published" ], True )
695703
696704 def test_dref_operation_update_create (self ):
@@ -922,6 +930,7 @@ def test_final_report_update_once_published(self):
922930 dref = dref ,
923931 country = country ,
924932 type_of_dref = Dref .DrefType .RESPONSE ,
933+ status = Dref .Status .FINALIZED ,
925934 )
926935 final_report .users .set ([user1 ])
927936 # try to publish this report
@@ -941,7 +950,7 @@ def test_final_report_update_once_published(self):
941950 response = self .client .post (url , data )
942951 self .assert_200 (response )
943952 self .assertEqual (response .data ["is_published" ], True )
944-
953+ self . assertEqual ( response . data [ "status" ], Dref . Status . APPROVED )
945954 # now try to patch to the final report
946955 url = f"/api/v2/dref-final-report/{ final_report .id } /"
947956 data = {
@@ -959,7 +968,7 @@ def test_dref_for_assessment_report(self):
959968 "type_of_onset" : Dref .OnsetType .SLOW .value ,
960969 "type_of_dref" : Dref .DrefType .ASSESSMENT ,
961970 "disaster_category" : Dref .DisasterCategory .YELLOW .value ,
962- "status" : Dref .Status .IN_PROGRESS .value ,
971+ "status" : Dref .Status .DRAFT .value ,
963972 "num_assisted" : 5666 ,
964973 "num_affected" : 23 ,
965974 "amount_requested" : 127771111 ,
@@ -1547,7 +1556,7 @@ def test_dref_imminent(self):
15471556 "type_of_onset" : Dref .OnsetType .SUDDEN .value ,
15481557 "type_of_dref" : Dref .DrefType .IMMINENT ,
15491558 "disaster_category" : Dref .DisasterCategory .YELLOW .value ,
1550- "status" : Dref .Status .IN_PROGRESS .value ,
1559+ "status" : Dref .Status .DRAFT .value ,
15511560 "num_assisted" : 5666 ,
15521561 "num_affected" : 23 ,
15531562 "amount_requested" : 127771111 ,
0 commit comments