Skip to content

Commit b96af13

Browse files
authored
ref(api): Refactored OrganizationUnsubscribe for id_or_slug (#70009)
1 parent 67c76ce commit b96af13

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

src/sentry/api/endpoints/organization_unsubscribe.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,12 @@ def fetch_instance(self, request: Request, organization_slug: str, id: int) -> P
8484
project = Project.objects.select_related("organization").get(id=id)
8585
except Project.DoesNotExist:
8686
raise NotFound()
87-
if project.organization.slug != organization_slug:
88-
raise NotFound()
87+
if str(organization_slug).isdecimal():
88+
if project.organization.id != int(organization_slug):
89+
raise NotFound()
90+
else:
91+
if project.organization.slug != organization_slug:
92+
raise NotFound()
8993
if not OrganizationMember.objects.filter(
9094
user_id=request.user.pk, organization_id=project.organization_id
9195
).exists():
@@ -116,8 +120,13 @@ def fetch_instance(self, request: Request, organization_slug: str, issue_id: int
116120
issue = Group.objects.get_from_cache(id=issue_id)
117121
except Group.DoesNotExist:
118122
raise NotFound()
119-
if issue.organization.slug != organization_slug:
120-
raise NotFound()
123+
if str(organization_slug).isdecimal():
124+
if issue.organization.id != int(organization_slug):
125+
raise NotFound()
126+
else:
127+
if issue.organization.slug != organization_slug:
128+
raise NotFound()
129+
121130
if not OrganizationMember.objects.filter(
122131
user_id=request.user.pk, organization=issue.organization
123132
).exists():

tests/sentry/api/endpoints/test_organization_unsubscribe.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def test_post_no_signature(self):
7272
resp = self.client.get(path)
7373
assert resp.status_code == 404
7474

75-
def test_post_success(self):
75+
def test_post_success_slug(self):
7676
project = self.create_project(organization=self.organization)
7777
path = generate_signed_link(
7878
user=self.user, viewname=self.endpoint, args=[self.organization.slug, project.id]
@@ -88,6 +88,22 @@ def test_post_success(self):
8888
value="never",
8989
).exists()
9090

91+
def test_post_success_id(self):
92+
project = self.create_project(organization=self.organization)
93+
path = generate_signed_link(
94+
user=self.user, viewname=self.endpoint, args=[self.organization.id, project.id]
95+
)
96+
resp = self.client.post(path, data={"cancel": "1"})
97+
assert resp.status_code == 201
98+
with assume_test_silo_mode(SiloMode.CONTROL):
99+
assert NotificationSettingOption.objects.filter(
100+
user_id=self.user.id,
101+
scope_type="project",
102+
scope_identifier=project.id,
103+
type="alerts",
104+
value="never",
105+
).exists()
106+
91107

92108
class OrganizationUnsubscribeIssueTest(APITestCase):
93109
endpoint = "sentry-api-0-organization-unsubscribe-issue"
@@ -139,7 +155,7 @@ def test_post_missing_record(self):
139155
resp = self.client.post(path)
140156
assert resp.status_code == 404
141157

142-
def test_post_success(self):
158+
def test_post_success_slug(self):
143159
group = self.create_group(project=self.project)
144160
path = generate_signed_link(
145161
user=self.user, viewname=self.endpoint, args=[self.organization.slug, group.id]
@@ -149,3 +165,14 @@ def test_post_success(self):
149165

150166
sub = GroupSubscription.objects.get(group=group, user_id=self.user.id)
151167
assert sub.is_active is False
168+
169+
def test_post_success_id(self):
170+
group = self.create_group(project=self.project)
171+
path = generate_signed_link(
172+
user=self.user, viewname=self.endpoint, args=[self.organization.id, group.id]
173+
)
174+
resp = self.client.post(path, data={"cancel": "1"})
175+
assert resp.status_code == 201
176+
177+
sub = GroupSubscription.objects.get(group=group, user_id=self.user.id)
178+
assert sub.is_active is False

0 commit comments

Comments
 (0)