Skip to content

Commit a6b7957

Browse files
authored
Merge pull request #1530 from python-discord/fix-infraction-active-regression
Do not activate infractions on partial updates
2 parents 5b98812 + cf7b995 commit a6b7957

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

pydis_site/apps/api/tests/test_infractions.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,18 @@ def test_partial_update(self):
327327
self.assertEqual(infraction.type, self.ban_hidden.type)
328328
self.assertEqual(infraction.hidden, self.ban_hidden.hidden)
329329

330+
def test_partial_update_of_inactive_infraction(self):
331+
url = reverse('api:bot:infraction-detail', args=(self.ban_hidden.id,))
332+
data = {
333+
'expires_at': '4143-02-15T21:04:31+00:00',
334+
'reason': "Do not help out Joe with any electricity-related questions"
335+
}
336+
self.assertFalse(self.ban_inactive.active, "Infraction should start out as inactive")
337+
response = self.client.patch(url, data=data)
338+
self.assertEqual(response.status_code, 200)
339+
infraction = Infraction.objects.get(id=self.ban_inactive.id)
340+
self.assertFalse(infraction.active, "Infraction changed to active via patch")
341+
330342
def test_partial_update_returns_400_for_frozen_field(self):
331343
url = reverse('api:bot:infraction-detail', args=(self.ban_hidden.id,))
332344
data = {'user': 6}
@@ -692,6 +704,37 @@ def test_integrity_error_if_missing_active_field(self):
692704
reason='A reason.',
693705
)
694706

707+
def test_accepts_two_warnings(self):
708+
"""Two warnings can be created for a user."""
709+
url = reverse('api:bot:infraction-list')
710+
data = {
711+
'user': self.user.id,
712+
'actor': self.user.id,
713+
'type': 'warning',
714+
'reason': "Thought upgrading DRF is a smart idea",
715+
'active': False,
716+
}
717+
first_response = self.client.post(url, data=data)
718+
self.assertEqual(first_response.status_code, 201)
719+
data['reason'] = "Do not claim King Arthur eats children"
720+
second_response = self.client.post(url, data=data)
721+
self.assertEqual(second_response.status_code, 201)
722+
723+
def test_respects_active_false_of_warnings(self):
724+
"""Infractions can be created as inactive."""
725+
url = reverse('api:bot:infraction-list')
726+
data = {
727+
'user': self.user.id,
728+
'actor': self.user.id,
729+
'type': 'warning',
730+
'reason': "Associate of REDACTED",
731+
'active': False,
732+
}
733+
response = self.client.post(url, data=data)
734+
self.assertEqual(response.status_code, 201)
735+
infraction = Infraction.objects.get(id=response.json()['id'])
736+
self.assertFalse(infraction.active)
737+
695738

696739
class InfractionDeletionTests(AuthenticatedAPITestCase):
697740
@classmethod

pydis_site/apps/api/viewsets/bot/infraction.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,12 @@ class InfractionViewSet(
161161
def partial_update(self, request: HttpRequest, *_args, **_kwargs) -> Response:
162162
"""Method that handles the nuts and bolts of updating an Infraction."""
163163
instance = self.get_object()
164-
request.data.setdefault("active", True)
164+
# DRF presently errors out if we are not specifying all fields here.
165+
# See this issue:
166+
# https://github.com/encode/django-rest-framework/issues/9358. The
167+
# merged PR that closed the issue does not appear to work either, so
168+
# here's a workaround.
169+
request.data.setdefault("active", instance.active)
165170
serializer = self.get_serializer(instance, data=request.data, partial=True)
166171
serializer.is_valid(raise_exception=True)
167172
serializer.save()

0 commit comments

Comments
 (0)