Skip to content

Commit 063eea3

Browse files
Merge pull request #2253 from IFRCGo/fix/user-guest-permission-event
Allow only events with public visibility for Guest User
2 parents c9be21e + 4f5ef4e commit 063eea3

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

api/drf_views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ def retrieve(self, request, pk=None, *args, **kwargs):
688688
"field_reports",
689689
queryset=FieldReport.objects.prefetch_related("countries", "contacts"),
690690
)
691-
if self.request.user.is_authenticated:
691+
if self.request.user.is_authenticated and not self.request.user.profile.limit_access_to_guest:
692692
if is_user_ifrc(self.request.user):
693693
instance = Event.objects.prefetch_related(FR).get(pk=pk)
694694
else:

api/test_views.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ def setUp(self):
3131
go_user_profile.save()
3232

3333
# Create public field reports
34-
FieldReportFactory.create_batch(4, visibility=VisibilityChoices.PUBLIC)
34+
event_pub = EventFactory.create(visibility=VisibilityChoices.PUBLIC, parent_event=None)
35+
FieldReportFactory.create_batch(4, event=event_pub, visibility=VisibilityChoices.PUBLIC)
3536
# Create non-public field reports
36-
FieldReportFactory.create_batch(5, visibility=VisibilityChoices.IFRC)
37+
event_non_pub = EventFactory.create(visibility=VisibilityChoices.IFRC, parent_event=None)
38+
FieldReportFactory.create_batch(5, event=event_non_pub, visibility=VisibilityChoices.IFRC)
3739

3840
def test_guest_user_permission(self):
3941
body = {}
@@ -50,6 +52,7 @@ def test_guest_user_permission(self):
5052
f"/api/v2/field-report/{id}/",
5153
"/api/v2/language/",
5254
f"/api/v2/language/{id}/",
55+
"/api/v2/event/",
5356
]
5457

5558
go_post_apis = [
@@ -159,6 +162,11 @@ def _failure_check(response, check_json_error_code=True):
159162
field_report_pub_response = self.client.post("/api/v2/field-report/", json=body)
160163
_failure_check(field_report_pub_response, check_json_error_code=False)
161164

165+
# Unauthenticated user should be able to view public events
166+
event_pub_response = self.client.get("/api/v2/event/")
167+
_success_check(event_pub_response)
168+
self.assertEqual(len(event_pub_response.json()["results"]), 1)
169+
162170
# authenticate guest user
163171
self.authenticate(user=self.guest_user)
164172

@@ -194,6 +202,11 @@ def _failure_check(response, check_json_error_code=True):
194202
_success_check(field_report_pub_response)
195203
self.assertEqual(len(field_report_pub_response.json()["results"]), 4)
196204

205+
# Guest user should be able to view public events
206+
event_pub_response = self.client.get("/api/v2/event/")
207+
_success_check(event_pub_response)
208+
self.assertEqual(len(event_pub_response.json()["results"]), 1)
209+
197210
# authenticate ifrc go user
198211
# Go user should be able to access go_post_apis
199212
self.authenticate(user=self.go_user)
@@ -210,6 +223,11 @@ def _failure_check(response, check_json_error_code=True):
210223
_success_check(field_report_response)
211224
self.assertEqual(len(field_report_response.json()["results"]), 9)
212225

226+
# Go user should be able to view both public + non-pubic events
227+
event_response = self.client.get("/api/v2/event/")
228+
_success_check(event_response)
229+
self.assertEqual(len(event_response.json()["results"]), 2)
230+
213231

214232
class AuthTokenTest(APITestCase):
215233
def setUp(self):

0 commit comments

Comments
 (0)