test_middleware.py 1.83 KB
Newer Older
1 2
import json

3
import django.http
Calen Pennington committed
4
from django.test import TestCase
5
from nose.plugins.attrib import attr
6 7

import django_comment_client.middleware as middleware
8
import lms.lib.comment_client
9

Calen Pennington committed
10

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

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

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

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