1010 EventFeaturedDocumentFactory ,
1111 EventLinkFactory ,
1212)
13- from api .models import Profile
13+ from api .factories .field_report import FieldReportFactory
14+ from api .models import Profile , VisibilityChoices
1415from deployments .factories .user import UserFactory
1516from main .test_case import APITestCase , SnapshotTestCase
1617
@@ -24,21 +25,34 @@ def setUp(self):
2425 guest_profile .save ()
2526
2627 # Create go user
27- self .go_user = User .objects .create (username = "go-user" )
28+ self .go_user = User .objects .create (username = "go-user" , is_superuser = True , is_staff = True )
2829 go_user_profile = Profile .objects .get (user = self .go_user )
2930 go_user_profile .limit_access_to_guest = False
3031 go_user_profile .save ()
3132
33+ # Create public field reports
34+ FieldReportFactory .create_batch (4 , visibility = VisibilityChoices .PUBLIC )
35+ # Create non-public field reports
36+ FieldReportFactory .create_batch (5 , visibility = VisibilityChoices .IFRC )
37+
3238 def test_guest_user_permission (self ):
3339 body = {}
40+ id = 1 # NOTE: id is used just to test api that requires id, it doesnot indicate real id. It can be any number.
41+
3442 guest_apis = [
3543 "/api/v2/add_subscription/" ,
3644 "/api/v2/del_subscription/" ,
3745 "/api/v2/external-token/" ,
46+ ]
47+ guest_get_apis = [
3848 "/api/v2/user/me/" ,
49+ "/api/v2/field-report/" ,
50+ f"/api/v2/field-report/{ id } /" ,
51+ "/api/v2/language/" ,
52+ f"/api/v2/language/{ id } /" ,
3953 ]
40- id = 1 # NOTE: id is used just to test api that requires id, it doesnot indicate real id. It can be any number.
41- go_apis = [
54+
55+ go_post_apis = [
4256 "/api/v2/dref/" ,
4357 "/api/v2/dref-final-report/" ,
4458 f"/api/v2/dref-final-report/{ id } /publish/" ,
@@ -76,13 +90,9 @@ def test_guest_user_permission(self):
7690 f"/api/v2/dref-final-report/{ id } /" ,
7791 "/api/v2/dref-op-update/" ,
7892 f"/api/v2/dref/{ id } /" ,
79- "/api/v2/field-report/" ,
80- f"/api/v2/field-report/{ id } /" ,
8193 "/api/v2/flash-update/" ,
8294 "/api/v2/flash-update-file/" ,
8395 f"/api/v2/flash-update/{ id } /" ,
84- "/api/v2/language/" ,
85- f"/api/v2/language/{ id } /" ,
8696 "/api/v2/local-units/" ,
8797 f"/api/v2/local-units/{ id } /" ,
8898 "/api/v2/ops-learning/" ,
@@ -106,6 +116,15 @@ def test_guest_user_permission(self):
106116 f"/api/v2/subscription/{ id } /" ,
107117 "/api/v2/users/" ,
108118 f"/api/v2/users/{ id } /" ,
119+ "/api/v2/per-stats/" ,
120+ "/api/v2/per-options/" ,
121+ "/api/v2/per-process-status/" ,
122+ "/api/v2/aggregated-per-process-status/" ,
123+ "/api/v2/completed-dref/" ,
124+ "/api/v2/active-dref/" ,
125+ "/api/v2/dref-share-user/" ,
126+ "/api/v2/personnel_deployment/" ,
127+ f"/api/v2/delegation-office/{ id } /" ,
109128 # Exports
110129 f"/api/v2/export-flash-update/{ 1 } /" ,
111130 ]
@@ -115,56 +134,82 @@ def test_guest_user_permission(self):
115134 f"/api/v2/export-per/{ 1 } /" ,
116135 ]
117136
118- go_apis_req_additional_perm = [
137+ go_post_apis_req_additional_perm = [
119138 "/api/v2/ops-learning/" ,
120139 "/api/v2/per-overview/" ,
121140 f"/api/v2/user/{ id } /accepted_license_terms/" ,
122- f"/api/v2/language/{ id } /bulk-action/" ,
123141 ]
124142
125- self .authenticate (user = self .guest_user )
126-
127143 def _success_check (response ): # NOTE: Only handles json responses
128144 self .assertNotIn (response .status_code , [401 , 403 ], response .content )
129145 self .assertNotIn (response .json ().get ("error_code" ), [401 , 403 ], response .content )
130146
131- def _failure_check (response , is_json = True ):
147+ def _failure_check (response , check_json_error_code = True ):
132148 self .assertIn (response .status_code , [401 , 403 ], response .content )
133- if is_json :
149+ if check_json_error_code :
134150 self .assertIn (response .json ()["error_code" ], [401 , 403 ], response .content )
135151
152+ # check for unauthenticated user
153+ # Unauthenticated user should be able to view public field reports
154+ field_report_pub_response = self .client .get ("/api/v2/field-report/" )
155+ _success_check (field_report_pub_response )
156+ self .assertEqual (len (field_report_pub_response .json ()["results" ]), 4 )
157+
158+ # Unauthenticated user should be not be able to do post operations in field reports
159+ field_report_pub_response = self .client .post ("/api/v2/field-report/" , json = body )
160+ _failure_check (field_report_pub_response , check_json_error_code = False )
161+
162+ # authenticate guest user
163+ self .authenticate (user = self .guest_user )
164+
136165 for api_url in get_custom_negotiation_apis :
137166 headers = {
138167 "Accept" : "text/html" ,
139168 }
140169 response = self .client .get (api_url , headers = headers , stream = True )
141- _failure_check (response , is_json = False )
170+ _failure_check (response , check_json_error_code = False )
142171
143- # Guest user should not be able to access get apis that requires IsAuthenticated permission
172+ # # Guest user should not be able to access get apis that requires IsAuthenticated permission
144173 for api_url in get_apis :
145174 response = self .client .get (api_url )
146175 _failure_check (response )
147176
148- # Guest user should not be able to hit post apis.
149- for api_url in go_apis + go_apis_req_additional_perm :
177+ # # Guest user should not be able to hit post apis.
178+ for api_url in go_post_apis + go_post_apis_req_additional_perm :
150179 response = self .client .post (api_url , json = body )
151180 _failure_check (response )
152181
153- # Guest user should be able to access guest apis
182+ # Guest user should be able to access guest post apis
154183 for api_url in guest_apis :
155184 response = self .client .post (api_url , json = body )
156185 _success_check (response )
157186
158- # Go user should be able to access go_apis
187+ # Guest user should be able to access guest get apis
188+ for api_url in guest_get_apis :
189+ response = self .client .get (api_url )
190+ _success_check (response )
191+
192+ # Guest user should be able to view only public field reports
193+ field_report_pub_response = self .client .get ("/api/v2/field-report/" )
194+ _success_check (field_report_pub_response )
195+ self .assertEqual (len (field_report_pub_response .json ()["results" ]), 4 )
196+
197+ # authenticate ifrc go user
198+ # Go user should be able to access go_post_apis
159199 self .authenticate (user = self .go_user )
160- for api_url in go_apis :
200+ for api_url in go_post_apis :
161201 response = self .client .post (api_url , json = body )
162202 _success_check (response )
163203
164204 for api_url in get_apis :
165205 response = self .client .get (api_url )
166206 _success_check (response )
167207
208+ # Go user should be able to view both public + non-public field reports
209+ field_report_response = self .client .get ("/api/v2/field-report/" )
210+ _success_check (field_report_response )
211+ self .assertEqual (len (field_report_response .json ()["results" ]), 9 )
212+
168213
169214class AuthTokenTest (APITestCase ):
170215 def setUp (self ):
0 commit comments