Skip to content

Commit f416445

Browse files
saadmk11stsewd
andauthored
API V3: Only return notifications for a given organization (#11112)
API V3: Only return notifications for given organization --------- Co-authored-by: Santos Gallegos <[email protected]>
1 parent 57c5c8a commit f416445

File tree

2 files changed

+60
-8
lines changed

2 files changed

+60
-8
lines changed

readthedocs/api/v3/tests/test_organizations.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import django_dynamic_fixture as fixture
2+
from django.contrib.contenttypes.models import ContentType
13
from django.test import override_settings
24
from django.urls import reverse
35
from django.urls.exceptions import NoReverseMatch
46

7+
from readthedocs.notifications.models import Notification
8+
from readthedocs.organizations.models import Organization
59
from readthedocs.subscriptions.constants import TYPE_CONCURRENT_BUILDS
610
from readthedocs.subscriptions.products import RTDProductFeature
711

@@ -51,6 +55,38 @@ def test_organizations_notifications_list(self):
5155
self._get_response_dict("organizations-notifications-list"),
5256
)
5357

58+
def test_organizations_notifications_list_only_given_organization(self):
59+
url = reverse(
60+
"organizations-notifications-list",
61+
kwargs={
62+
"parent_lookup_organization__slug": self.organization.slug,
63+
},
64+
)
65+
other_organization = fixture.get(
66+
Organization,
67+
pub_date=self.created,
68+
modified_date=self.modified,
69+
name="other_organization",
70+
slug="other_organization",
71+
owners=[self.me],
72+
)
73+
74+
fixture.get(
75+
Notification,
76+
attached_to_content_type=ContentType.objects.get_for_model(
77+
other_organization
78+
),
79+
attached_to_id=other_organization.pk,
80+
)
81+
82+
self.client.credentials(HTTP_AUTHORIZATION=f"Token {self.token.key}")
83+
response = self.client.get(url)
84+
self.assertEqual(response.status_code, 200)
85+
self.assertDictEqual(
86+
response.json(),
87+
self._get_response_dict("organizations-notifications-list"),
88+
)
89+
5490
def test_organizations_notifications_list_other_user(self):
5591
url = reverse(
5692
"organizations-notifications-list",
@@ -103,6 +139,28 @@ def test_organizations_notifications_detail(self):
103139
self._get_response_dict("organizations-notifications-detail"),
104140
)
105141

142+
def test_organizations_notifications_detail_other_organization(self):
143+
other_organization = fixture.get(
144+
Organization,
145+
pub_date=self.created,
146+
modified_date=self.modified,
147+
name="new_org",
148+
slug="new_org",
149+
owners=[self.me],
150+
)
151+
152+
url = reverse(
153+
"organizations-notifications-detail",
154+
kwargs={
155+
"parent_lookup_organization__slug": other_organization.slug,
156+
"notification_pk": self.notification_organization.pk,
157+
},
158+
)
159+
160+
self.client.credentials(HTTP_AUTHORIZATION=f"Token {self.token.key}")
161+
response = self.client.get(url)
162+
self.assertEqual(response.status_code, 404)
163+
106164
def test_organizations_notifications_detail_other(self):
107165
url = reverse(
108166
"organizations-notifications-detail",

readthedocs/api/v3/views.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from rest_framework_extensions.mixins import NestedViewSetMixin
2525

2626
from readthedocs.builds.models import Build, Version
27-
from readthedocs.core.permissions import AdminPermission
2827
from readthedocs.core.utils import trigger_build
2928
from readthedocs.core.utils.extend import SettingsOverrideObject
3029
from readthedocs.notifications.models import Notification
@@ -679,10 +678,5 @@ class NotificationsOrganizationViewSet(
679678
permission_classes = [IsAuthenticated & IsOrganizationAdmin]
680679

681680
def get_queryset(self):
682-
content_type = ContentType.objects.get_for_model(Organization)
683-
return self.queryset.filter(
684-
attached_to_content_type=content_type,
685-
attached_to_id__in=AdminPermission.organizations(
686-
self.request.user, owner=True, member=False
687-
).values("id"),
688-
)
681+
organization = self._get_parent_organization()
682+
return organization.notifications.all()

0 commit comments

Comments
 (0)