test_middleware.py 1.83 KB
Newer Older
1
import django.http
Calen Pennington committed
2
from django.test import TestCase
3
from nose.plugins.attrib import attr
4
import json
5

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

Calen Pennington committed
9

10
@attr('shard_1')
11
class AjaxExceptionTestCase(TestCase):
12
    def setUp(self):
13
        super(AjaxExceptionTestCase, self).setUp()
14 15 16
        self.a = middleware.AjaxExceptionMiddleware()
        self.request1 = django.http.HttpRequest()
        self.request0 = django.http.HttpRequest()
17 18 19
        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!")
20 21
        self.request1.META['HTTP_X_REQUESTED_WITH'] = "XMLHttpRequest"
        self.request0.META['HTTP_X_REQUESTED_WITH'] = "SHADOWFAX"
Calen Pennington committed
22

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

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

40 41 42
        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))