dr.botzo/tests/test_dispatch_api.py

45 lines
1.9 KiB
Python

"""Test the dispatch package's webservice."""
from django.contrib.auth.models import User
from rest_framework.status import HTTP_200_OK, HTTP_403_FORBIDDEN
from rest_framework.test import APITestCase
from dispatch.models import Dispatcher, DispatcherAction
class DispatchAPITest(APITestCase):
"""Test dispatch DRF views."""
def setUp(self):
"""Do pre-test stuff."""
self.client = self.client_class()
self.user = User.objects.create(username='test')
self.client.force_authenticate(user=self.user)
def test_dispatch_object_retrieval(self):
"""Test that the list endpoints returns objects."""
dispatcher = Dispatcher.objects.create()
DispatcherAction.objects.create(dispatcher=dispatcher)
resp = self.client.get('/dispatch/api/dispatchers/')
self.assertEqual(resp.status_code, HTTP_200_OK)
self.assertEqual(len(resp.json()), Dispatcher.objects.count())
resp = self.client.get('/dispatch/api/actions/')
self.assertEqual(resp.status_code, HTTP_200_OK)
self.assertEqual(len(resp.json()), DispatcherAction.objects.count())
def test_unauthed_dispatch_object_retrieval(self):
"""Test that the list endpoints require authentication."""
client = self.client_class()
resp = client.get('/dispatch/api/dispatchers/')
self.assertEqual(resp.status_code, HTTP_403_FORBIDDEN)
resp = client.get('/dispatch/api/dispatchers/111/')
self.assertEqual(resp.status_code, HTTP_403_FORBIDDEN)
resp = client.get('/dispatch/api/dispatchers/fake/')
self.assertEqual(resp.status_code, HTTP_403_FORBIDDEN)
resp = client.get('/dispatch/api/actions/')
self.assertEqual(resp.status_code, HTTP_403_FORBIDDEN)
resp = client.get('/dispatch/api/actions/111/')
self.assertEqual(resp.status_code, HTTP_403_FORBIDDEN)