Debian Codegraph @

debreach/tests.py

raw
1 # -*- coding: utf-8 -*-
2 import os
3 import unittest
4
5 from django.http import HttpResponse
6 from django.test import TestCase
7 from django.test.client import RequestFactory
8 from django.urls import reverse
9 from django.utils.encoding import force_str
10
11 from debreach.decorators import append_random_comment, random_comment_exempt
12 from debreach.middleware import RandomCommentMiddleware
13
14
15 def test_view(request):
16 return HttpResponse()
17
18
19 class TestRandomCommentMiddleware(TestCase):
20
21 def test_noop_on_wrong_content_type(self):
22 response = HttpResponse('abc', content_type='text/plain')
23 request = RequestFactory().get('/')
24 middleware = RandomCommentMiddleware(lambda request: response)
25 response = middleware.process_response(request, response)
26 self.assertEqual(response.content, b'abc')
27
28 def test_html_content_type(self):
29 html = '''<!doctype html>
30 <html>
31 <head>
32 <title>Page title</title>
33 </head>
34 <body>
35 <h1>Test</h1>
36 <p>Lorem ipsum</p>
37 </body>
38 </html>'''
39 response = HttpResponse(html, content_type='text/html')
40 request = RequestFactory().get('/')
41 middleware = RandomCommentMiddleware(lambda request: response)
42 response = middleware.process_response(request, response)
43 self.assertNotEqual(response.content, html)
44
45 def test_unicode_characters(self):
46 html = '''<!doctype html>
47 <html>
48 <head>
49 <title>Page title</title>
50 </head>
51 <body>
52 <h1>Test</h1>
53 <p>{0}</p>
54 </body>
55 </html>'''.format(''.join(chr(x) for x in range(9999)))
56 response = HttpResponse(html, content_type='text/html')
57 request = RequestFactory().get('/')
58 middleware = RandomCommentMiddleware(lambda request: response)
59 response = middleware.process_response(request, response)
60 self.assertNotEqual(force_str(response.content), force_str(html))
61
62 def test_exemption(self):
63 html = '''<html>
64 <head><title>Test</title></head>
65 <body><p>Test body.</p></body>
66 </html>'''
67 response = HttpResponse(html)
68 response._random_comment_exempt = True
69 request = RequestFactory().get('/')
70 middleware = RandomCommentMiddleware(lambda request: response)
71 response = middleware.process_response(request, response)
72 self.assertEqual(force_str(response.content), html)
73
74 def test_missing_content_type(self):
75 request = RequestFactory().get('/')
76 response = HttpResponse('')
77 del response['Content-Type']
78 middleware = RandomCommentMiddleware(lambda request: response)
79 processed_response = middleware.process_response(request, response)
80 self.assertEqual(response, processed_response)
81
82 def test_empty_response_body_ignored(self):
83 request = RequestFactory().get('/')
84 response = HttpResponse('')
85 middleware = RandomCommentMiddleware(lambda request: response)
86 processed_response = middleware.process_response(request, response)
87 self.assertEqual(len(processed_response.content), 0)
88
89
90 class TestDecorators(TestCase):
91
92 def test_append_random_comment(self):
93 html = '''<html>
94 <head><title>Test</title></head>
95 <body><p>Test body.</p></body>
96 </html>'''
97
98 @append_random_comment
99 def test_view(request):
100 return HttpResponse(html)
101
102 request = RequestFactory().get('/')
103 response = test_view(request)
104 self.assertNotEqual(force_str(response.content), html)
105 self.assertIn('<!-- ', force_str(response.content))
106 self.assertIn(' -->', force_str(response.content))
107
108 def test_random_comment_exempt(self):
109 html = '''<html>
110 <head><title>Test</title></head>
111 <body><p>Test body.</p></body>
112 </html>'''
113
114 @random_comment_exempt
115 def test_view(request):
116 return HttpResponse(html)
117
118 request = RequestFactory().get('/')
119 response = test_view(request)
120 self.assertTrue(response._random_comment_exempt)
121
122
123 @unittest.skipUnless(
124 'test_project' in os.environ.get('DJANGO_SETTINGS_MODULE', ''),
125 'Not running in test_project'
126 )
127 class IntegrationTests(TestCase):
128
129 def test_adds_comment(self):
130 resp = self.client.get(reverse('home'))
131 self.assertFalse(resp.content.endswith(b'</html>'))