forked from Rhumbix/django-request-logging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
65 lines (53 loc) · 2.49 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import io, sys, os
sys.path.append(os.environ['DE_PATH'])
from django.test import RequestFactory
from django.conf import settings
from request_logging.middleware import MAX_BODY_LENGTH, LoggingMiddleware
from request_logging.middleware import match_ignored
import request_logging
import unittest
import mock
#settings.configure()
@mock.patch.object(request_logging.middleware, "request_logger")
class ChunkedLogTestCase(unittest.TestCase):
def setUp(self):
self.factory = RequestFactory()
self.middleware = LoggingMiddleware()
def test_does_not_error_with_binary_content_larger_than_chunk_size(self, mock_log):
body = MAX_BODY_LENGTH * "0" + "1"
datafile = io.StringIO(body)
request = self.factory.post("/somewhere", data={"file": datafile})
self.middleware.process_request(request)
self.assert_logged(mock_log, str(request.body[:MAX_BODY_LENGTH]))
self.assert_not_logged(mock_log, body)
def test_request_body_logged(self, mock_log):
body = "some body"
datafile = io.StringIO(body)
request = self.factory.post("/somewhere", data={"file": datafile})
self.middleware.process_request(request)
self.assert_logged(mock_log, "some body")
def assert_logged(self, mock_log, expected_entry):
calls = mock_log.log.call_args_list
text = " ".join([call[0][1] for call in calls])
self.assertTrue(expected_entry in text)
def assert_not_logged(self, mock_log, unexpected_entry):
calls = mock_log.log.call_args_list
text = " ".join([call[0][1] for call in calls])
self.assertTrue(unexpected_entry not in text)
class DigitalEventsTestCase(unittest.TestCase):
"""
First argument is path to match.
Second argument is a list of paths to match exactly.
Third argument is a list of paths to match only if initial starts with
the pattern.
"""
def test_match_ignored_path_exact(self):
self.assertTrue (match_ignored('/foo', ['/foo'], []))
self.assertTrue (match_ignored('/foo', ['/foo', '/bar'], []))
self.assertTrue (match_ignored('/foo/$?=', ['/bar/', '/foo/$?='], []))
def test_match_ignored_path_startswith(self):
self.assertTrue (match_ignored('/foobarbar', [], ['/foo']))
self.assertTrue (match_ignored('/foo/bar/foobar', [], ['/foo/']))
def test_no_match(self):
self.assertFalse (match_ignored('/foobarbar', [], ['/oo']))
self.assertFalse (match_ignored('/foobarbar', ['/foobar'], ['/oo']))