diff options
Diffstat (limited to '')
| -rw-r--r-- | api/tests/test_off_topic_channel_names.py | 29 | ||||
| -rw-r--r-- | api/viewsets.py | 21 | 
2 files changed, 49 insertions, 1 deletions
diff --git a/api/tests/test_off_topic_channel_names.py b/api/tests/test_off_topic_channel_names.py index 96cb3b34..1f6e4ee3 100644 --- a/api/tests/test_off_topic_channel_names.py +++ b/api/tests/test_off_topic_channel_names.py @@ -112,3 +112,32 @@ class CreationTests(APISubdomainTestCase):              self.assertEqual(response.json(), {                  'name': ["Enter a valid value."]              }) + + +class DeletionTests(APISubdomainTestCase): +    @classmethod +    def setUpTestData(cls): +        cls.test_name = OffTopicChannelName.objects.create(name='lemons-lemonade-stand') +        cls.test_name_2 = OffTopicChannelName.objects.create(name='bbq-with-bisk') + +    def test_deleting_unknown_name_returns_404(self): +        url = reverse('bot:offtopicchannelname-detail', args=('unknown-name',), host='api') +        response = self.client.delete(url) + +        self.assertEqual(response.status_code, 404) + +    def test_deleting_known_name_returns_204(self): +        url = reverse('bot:offtopicchannelname-detail', args=(self.test_name.name,), host='api') +        response = self.client.delete(url) + +        self.assertEqual(response.status_code, 204) + +    def test_name_gets_deleted(self): +        url = reverse('bot:offtopicchannelname-detail', args=(self.test_name_2.name,), host='api') +        response = self.client.delete(url) + +        self.assertEqual(response.status_code, 204) + +        url = reverse('bot:offtopicchannelname-list', host='api') +        response = self.client.get(url) +        self.assertNotIn(self.test_name_2.name, response.json()) diff --git a/api/viewsets.py b/api/viewsets.py index 0bbb70f2..82f8aea9 100644 --- a/api/viewsets.py +++ b/api/viewsets.py @@ -1,3 +1,4 @@ +from django.shortcuts import get_object_or_404  from rest_framework.exceptions import ParseError  from rest_framework.mixins import CreateModelMixin, DestroyModelMixin, ListModelMixin, RetrieveModelMixin  from rest_framework.response import Response @@ -70,7 +71,7 @@ class DocumentationLinkViewSet(CreateModelMixin, DestroyModelMixin, ListModelMix      lookup_field = 'package' -class OffTopicChannelNameViewSet(ViewSet): +class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet):      """      View of off-topic channel names used by the bot      to rotate our off-topic names on a daily basis. @@ -102,12 +103,30 @@ class OffTopicChannelNameViewSet(ViewSet):      - 201: returned on success      - 400: if the request body has invalid fields, see the response for details +    ### DELETE /bot/off-topic-channel-names/<name:str> +    Delete the off-topic-channel name with the given `name`. + +    #### Status codes +    - 204: returned on success +    - 404: returned when the given `name` was not found +      ## Authentication      Requires a API token.      """ +    lookup_field = 'name'      serializer_class = OffTopicChannelNameSerializer +    def get_object(self): +        queryset = self.get_queryset() +        if self.lookup_field not in self.kwargs: +            raise ParseError(detail={ +                'name': ["This query parameter is required."] +            }) + +        name = self.kwargs[self.lookup_field] +        return get_object_or_404(queryset, name=name) +      def get_queryset(self):          return OffTopicChannelName.objects.all()  |