test_middleware.py 1.73 KB
Newer Older
1
import django.http
Calen Pennington committed
2
from django.test import TestCase
3
import json
4

5
import lms.lib.comment_client
6 7
import django_comment_client.middleware as middleware

Calen Pennington committed
8

9
class AjaxExceptionTestCase(TestCase):
10 11 12 13
    def setUp(self):
        self.a = middleware.AjaxExceptionMiddleware()
        self.request1 = django.http.HttpRequest()
        self.request0 = django.http.HttpRequest()
14 15 16
        self.exception1 = lms.lib.comment_client.CommentClientRequestError('{}', 401)
        self.exception2 = lms.lib.comment_client.CommentClientRequestError('Foo!', 404)
        self.exception0 = lms.lib.comment_client.CommentClient500Error("Holy crap the server broke!")
17 18
        self.request1.META['HTTP_X_REQUESTED_WITH'] = "XMLHttpRequest"
        self.request0.META['HTTP_X_REQUESTED_WITH'] = "SHADOWFAX"
Calen Pennington committed
19

20
    def test_process_exception(self):
21 22
        response1 = self.a.process_exception(self.request1, self.exception1)
        self.assertIsInstance(response1, middleware.JsonError)
23 24 25 26 27
        self.assertEqual(self.exception1.status_code, response1.status_code)
        self.assertEqual(
            {"errors": json.loads(self.exception1.message)},
            json.loads(response1.content)
        )
28 29 30

        response2 = self.a.process_exception(self.request1, self.exception2)
        self.assertIsInstance(response2, middleware.JsonError)
31 32 33 34 35
        self.assertEqual(self.exception2.status_code, response2.status_code)
        self.assertEqual(
            {"errors": [self.exception2.message]},
            json.loads(response2.content)
        )
36

37 38 39
        self.assertIsNone(self.a.process_exception(self.request1, self.exception0))
        self.assertIsNone(self.a.process_exception(self.request0, self.exception1))
        self.assertIsNone(self.a.process_exception(self.request0, self.exception0))