@@ -746,7 +746,7 @@ def test_final_report_update_once_published(self):
746746 # try to publish this report
747747 url = f'/api/v2/dref-final-report/{ final_report .id } /publish/'
748748 data = {}
749- self .client .force_authenticate (self . user )
749+ self .client .force_authenticate (user1 )
750750 response = self .client .post (url , data )
751751 self .assert_200 (response )
752752 self .assertEqual (response .data ['is_published' ], True )
@@ -986,6 +986,7 @@ def test_optimistic_lock_in_final_report(self):
986986 data ['modified_at' ] = datetime .now () + timedelta (days = 2 )
987987 response = self .client .patch (url , data = data )
988988 self .assert_200 (response )
989+
989990 def test_dref_permission (self ):
990991 user1 = UserFactory .create (
991992@@ -1063,7 +1064,7 @@ def test_dref_permission(self):
10631064 self .assertEqual (response .status_code , 200 )
10641065 self .assertEqual (len (response .data ['results' ]), 0 )
10651066
1066- def test_dref_operational_update_permission (self ):
1067+ def test_superuser_permisssion_operational_update (self ):
10671068 super_user = UserFactory .create (
1068106910691070 first_name = 'Test' ,
@@ -1072,56 +1073,105 @@ def test_dref_operational_update_permission(self):
1072107310731074 is_superuser = True ,
10741075 )
1075- user1 , user2 = UserFactory .create_batch (2 )
10761076 dref = DrefFactory .create (
10771077 title = 'Test Title' ,
10781078 is_published = True ,
10791079 )
1080- dref .users .add (user1 )
10811080 self .country1 = Country .objects .create (name = 'abc' )
10821081 self .district1 = District .objects .create (name = 'test district1' , country = self .country1 )
1083- old_op_count = DrefOperationalUpdate .objects .filter (dref = dref ).count ()
10841082 url = '/api/v2/dref-op-update/'
10851083 data = {
10861084 'dref' : dref .id ,
10871085 'country' : self .country1 .id ,
10881086 'district' : [self .district1 .id ],
10891087 }
1090- # authenticate with user for whom dref is not shared
1091- self .authenticate (self .user )
1092- response = self .client .post (url , data = data )
1093- self .assert_403 (response )
1094- # authenticate with user for whom dref is shared
1095- self .authenticate (user1 )
1088+ # authenticate with superuser
1089+ self .authenticate (super_user )
10961090 response = self .client .post (url , data = data )
10971091 self .assert_201 (response )
1098- self .assertEqual (
1099- DrefOperationalUpdate .objects .filter (dref = dref ).count (),
1100- old_op_count + 1
1101- )
11021092
1103- def test_superuser_permisssion_operational_update (self ):
1104- super_user = UserFactory .create (
1105- 1106- first_name = 'Test' ,
1107- last_name = 'User1' ,
1108- password = 'admin123' ,
1109- 1110- is_superuser = True ,
1093+ def test_operational_update_view_permission (self ):
1094+ Dref .objects .all ().delete ()
1095+ user1 , user2 , user3 = UserFactory .create_batch (3 )
1096+ dref = DrefFactory .create (
1097+ title = 'Test Title' ,
1098+ is_published = True ,
1099+ created_by = user1
11111100 )
1101+ operational_update = DrefOperationalUpdateFactory .create (
1102+ dref = dref ,
1103+ )
1104+ operational_update .users .set ([user3 ])
1105+ self .country1 = Country .objects .create (name = 'abc' )
1106+ self .district1 = District .objects .create (name = 'test district1' , country = self .country1 )
1107+
1108+ # Here user1 is able to see the operational_update
1109+ url = '/api/v2/dref-op-update/'
1110+ self .authenticate (user1 )
1111+ response = self .client .get (url )
1112+ self .assert_200 (response )
1113+
1114+ # authenticate with the user3
1115+ # here user3 should be able to view dref
1116+ # since user3 is assigned to op-update created from dref
1117+ dref_url = '/api/v2/dref/'
1118+ self .authenticate (user3 )
1119+ response = self .client .get (dref_url )
1120+ self .assert_200 (response )
1121+ self .assertEqual (len (response .data ['results' ]), 1 )
1122+ self .assertEqual (response .data ['results' ][0 ]['id' ], dref .id )
1123+
1124+ # authenticate with user2
1125+ # here user2 is not able to view dref
1126+ self .authenticate (user2 )
1127+ response = self .client .get (dref_url )
1128+ self .assert_200 (response )
1129+ self .assertEqual (len (response .data ['results' ]), 0 )
1130+
1131+ def test_concurrent_dref_operational_update (self ):
1132+ user1 , user2 , user3 = UserFactory .create_batch (3 )
1133+ print (f'{ user1 .id } { user2 .id } { user3 .id } ' )
11121134 dref = DrefFactory .create (
11131135 title = 'Test Title' ,
11141136 is_published = True ,
1137+ created_by = user1
1138+ )
1139+ operational_update = DrefOperationalUpdateFactory .create (
1140+ dref = dref ,
1141+ operational_update_number = 1 ,
1142+ created_by = user3
11151143 )
1144+ operational_update .users .set ([user3 , user2 ])
11161145 self .country1 = Country .objects .create (name = 'abc' )
11171146 self .district1 = District .objects .create (name = 'test district1' , country = self .country1 )
1147+
1148+ # Here user1 is able to see the operational_update
11181149 url = '/api/v2/dref-op-update/'
1119- data = {
1120- 'dref' : dref .id ,
1121- 'country' : self .country1 .id ,
1122- 'district' : [self .district1 .id ],
1123- }
1124- # authenticate with superuser
1125- self .authenticate (super_user )
1126- response = self .client .post (url , data = data )
1127- self .assert_201 (response )
1150+ self .authenticate (user1 )
1151+ response = self .client .get (url )
1152+ self .assert_200 (response )
1153+
1154+ # create another operational update from corresponding dref
1155+ operation_update2 = DrefOperationalUpdateFactory .create (
1156+ dref = dref ,
1157+ operational_update_number = 2
1158+ )
1159+ operation_update2 .users .set ([user2 ])
1160+
1161+ # authenticate with user2
1162+ url = '/api/v2/dref-op-update/'
1163+ self .authenticate (user2 )
1164+ response = self .client .get (url )
1165+ self .assert_200 (response )
1166+
1167+ # user2 should also be able to view dref
1168+ dref_url = '/api/v2/dref/'
1169+ self .authenticate (user2 )
1170+ response = self .client .get (dref_url )
1171+ self .assert_200 (response )
1172+ self .assertEqual (len (response .data ['results' ]), 1 )
1173+
1174+ # authenticate with user1
1175+ self .authenticate (user1 )
1176+ response = self .client .get (dref_url )
1177+ self .assert_200 (response )
0 commit comments