-
Notifications
You must be signed in to change notification settings - Fork 81
/
tests.py
executable file
·603 lines (508 loc) · 25.5 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#! /usr/bin/env python
import io
import logging
import mock
import re
import unittest
from django.conf import settings
from django.test import RequestFactory, override_settings
from django.http import HttpResponse, StreamingHttpResponse
import request_logging
from request_logging.middleware import (
LoggingMiddleware,
DEFAULT_LOG_LEVEL,
DEFAULT_COLORIZE,
DEFAULT_MAX_BODY_LENGTH,
NO_LOGGING_MSG,
DEFAULT_HTTP_4XX_LOG_LEVEL,
IS_DJANGO_VERSION_GTE_3_2_0,
)
settings.configure()
class BaseLogTestCase(unittest.TestCase):
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.assertIn(expected_entry, text)
def _assert_logged_with_key_value(self, mock_log, expected_key, expected_value):
calls = mock_log.log.call_args_list
text = "".join([call[0][1] for call in calls])
self.assertIn(expected_key, text)
self.assertEqual(expected_value, text.split(expected_key)[1][4 : len(expected_value) + 4])
def _assert_logged_with_level(self, mock_log, level):
calls = mock_log.log.call_args_list
called_levels = set(call[0][0] for call in calls)
self.assertIn(level, called_levels, "{} not in {}".format(level, called_levels))
def _asset_logged_with_additional_args_and_kwargs(self, mock_log, additional_args, kwargs):
calls = mock_log.log.call_args_list
for call_args, call_kwargs in calls:
additional_call_args = call_args[2:]
self.assertTrue(
additional_args == additional_call_args,
"Expected {} to be {}".format(additional_call_args, additional_args),
)
self.assertTrue(kwargs == call_kwargs, "Expected {} to be {}".format(call_kwargs, kwargs))
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.assertNotIn(unexpected_entry, text)
@mock.patch.object(request_logging.middleware, "request_logger")
class MissingRoutes(BaseLogTestCase):
def setUp(self):
self.factory = RequestFactory()
def get_response(request):
response = mock.MagicMock()
response.status_code = 200
response.get.return_value = "application/json"
headers = {"test_headers": "test_headers"}
if IS_DJANGO_VERSION_GTE_3_2_0:
response.headers = headers
else:
response._headers = headers
return response
self.middleware = LoggingMiddleware(get_response)
def test_no_exception_risen(self, mock_log):
body = u"some body"
request = self.factory.post("/a-missing-route-somewhere", data={"file": body})
self.middleware.__call__(request)
self._assert_logged(mock_log, body)
@mock.patch.object(request_logging.middleware, "request_logger")
class LogTestCase(BaseLogTestCase):
def setUp(self):
self.factory = RequestFactory()
def get_response(request):
response = mock.MagicMock()
response.status_code = 200
response.get.return_value = "application/json"
headers = {"test_headers": "test_headers"}
if IS_DJANGO_VERSION_GTE_3_2_0:
response.headers = headers
else:
response._headers = headers
return response
self.middleware = LoggingMiddleware(get_response)
def test_request_body_logged(self, mock_log):
body = u"some body"
request = self.factory.post("/somewhere", data={"file": body})
self.middleware.__call__(request)
self._assert_logged(mock_log, body)
def test_request_binary_logged(self, mock_log):
body = u"some body"
datafile = io.StringIO(body)
request = self.factory.post("/somewhere", data={"file": datafile})
self.middleware.__call__(request)
self._assert_logged(mock_log, "(binary data)")
def test_request_jpeg_logged(self, mock_log):
body = (
b'--BoUnDaRyStRiNg\r\nContent-Disposition: form-data; name="file"; filename="campaign_carousel_img.jp'
b'g"\r\nContent-Type: image/jpeg\r\n\r\n\xff\xd8\xff\xe1\x00\x18Exif\x00\x00II*\x00\x08\x00\x00\x00'
b"\x00\x00\x00\x00\x00\x00\x00\x00\xff\xec\x00\x11Ducky\x00\x01\x00\x04\x00\x00\x00d\x00\x00\xff\xe1"
b"\x03{http://ns.adobe.com/"
)
datafile = io.BytesIO(body)
request = self.factory.post("/somewhere", data={"file": datafile})
self.middleware.__call__(request)
self._assert_logged(mock_log, "(multipart/form)")
def test_request_headers_logged(self, mock_log):
request = self.factory.post("/somewhere", **{"HTTP_USER_AGENT": "silly-human"})
self.middleware.process_request(request, None, request.body)
if IS_DJANGO_VERSION_GTE_3_2_0:
self._assert_logged(mock_log, "User-Agent")
else:
self._assert_logged(mock_log, "HTTP_USER_AGENT")
def test_request_headers_sensitive_logged_default(self, mock_log):
request = self.factory.post(
"/somewhere", **{"HTTP_AUTHORIZATION": "sensitive-token", "HTTP_PROXY_AUTHORIZATION": "proxy-token"}
)
middleware = LoggingMiddleware()
middleware.process_request(request, None, request.body)
if IS_DJANGO_VERSION_GTE_3_2_0:
self._assert_logged(mock_log, "Authorization")
self._assert_logged_with_key_value(mock_log, "Authorization", "*****")
else:
self._assert_logged(mock_log, "HTTP_AUTHORIZATION")
self._assert_logged_with_key_value(mock_log, "HTTP_AUTHORIZATION", "*****")
if IS_DJANGO_VERSION_GTE_3_2_0:
self._assert_logged(mock_log, "Proxy-Authorization")
self._assert_logged_with_key_value(mock_log, "Proxy-Authorization", "*****")
else:
self._assert_logged(mock_log, "HTTP_PROXY_AUTHORIZATION")
self._assert_logged_with_key_value(mock_log, "HTTP_PROXY_AUTHORIZATION", "*****")
@override_settings(
REQUEST_LOGGING_SENSITIVE_HEADERS=["Authorization"]
if IS_DJANGO_VERSION_GTE_3_2_0
else ["HTTP_AUTHORIZATION"]
)
def test_request_headers_sensitive_logged(self, mock_log):
request = self.factory.post(
"/somewhere",
**{
"HTTP_AUTHORIZATION": "sensitive-token",
"HTTP_USER_AGENT": "silly-human",
"HTTP_PROXY_AUTHORIZATION": "proxy-token",
}
)
middleware = LoggingMiddleware()
middleware.process_request(request, None, request.body)
if IS_DJANGO_VERSION_GTE_3_2_0:
self._assert_logged(mock_log, "Authorization")
self._assert_logged_with_key_value(mock_log, "Authorization", "*****")
else:
self._assert_logged(mock_log, "HTTP_AUTHORIZATION")
self._assert_logged_with_key_value(mock_log, "HTTP_AUTHORIZATION", "*****")
if IS_DJANGO_VERSION_GTE_3_2_0:
self._assert_logged(mock_log, "User-Agent")
self._assert_logged_with_key_value(mock_log, "User-Agent", "silly-human")
else:
self._assert_logged(mock_log, "HTTP_USER_AGENT")
self._assert_logged_with_key_value(mock_log, "HTTP_USER_AGENT", "silly-human")
if IS_DJANGO_VERSION_GTE_3_2_0:
self._assert_logged(mock_log, "Proxy-Authorization")
self._assert_logged_with_key_value(mock_log, "Proxy-Authorization", "proxy-token")
else:
self._assert_logged(mock_log, "HTTP_PROXY_AUTHORIZATION")
self._assert_logged_with_key_value(mock_log, "HTTP_PROXY_AUTHORIZATION", "proxy-token")
def test_response_headers_logged(self, mock_log):
request = self.factory.post("/somewhere")
response = mock.MagicMock()
response.get.return_value = "application/json"
headers = {"test_headers": "test_headers"}
if IS_DJANGO_VERSION_GTE_3_2_0:
response.headers = headers
else:
response._headers = headers
self.middleware.process_response(request, response)
self._assert_logged(mock_log, "test_headers")
def test_call_logged(self, mock_log):
body = u"some body"
request = self.factory.post("/somewhere", data={"file": body}, **{"HTTP_USER_AGENT": "silly-human"})
self.middleware.__call__(request)
self._assert_logged(mock_log, body)
self._assert_logged(mock_log, "test_headers")
if IS_DJANGO_VERSION_GTE_3_2_0:
self._assert_logged(mock_log, "User-Agent")
else:
self._assert_logged(mock_log, "HTTP_USER_AGENT")
def test_call_binary_logged(self, mock_log):
body = u"some body"
datafile = io.StringIO(body)
request = self.factory.post("/somewhere", data={"file": datafile}, **{"HTTP_USER_AGENT": "silly-human"})
self.middleware.__call__(request)
self._assert_logged(mock_log, "(binary data)")
self._assert_logged(mock_log, "test_headers")
if IS_DJANGO_VERSION_GTE_3_2_0:
self._assert_logged(mock_log, "User-Agent")
else:
self._assert_logged(mock_log, "HTTP_USER_AGENT")
def test_call_jpeg_logged(self, mock_log):
body = (
b'--BoUnDaRyStRiNg\r\nContent-Disposition: form-data; name="file"; filename="campaign_carousel_img.jp'
b'g"\r\nContent-Type: image/jpeg\r\n\r\n\xff\xd8\xff\xe1\x00\x18Exif\x00\x00II*\x00\x08\x00\x00\x00'
b"\x00\x00\x00\x00\x00\x00\x00\x00\xff\xec\x00\x11Ducky\x00\x01\x00\x04\x00\x00\x00d\x00\x00\xff\xe1"
b"\x03{http://ns.adobe.com/"
)
datafile = io.BytesIO(body)
request = self.factory.post("/somewhere", data={"file": datafile}, **{"HTTP_USER_AGENT": "silly-human"})
self.middleware.__call__(request)
self._assert_logged(mock_log, "(multipart/form)")
self._assert_logged(mock_log, "test_headers")
if IS_DJANGO_VERSION_GTE_3_2_0:
self._assert_logged(mock_log, "User-Agent")
else:
self._assert_logged(mock_log, "HTTP_USER_AGENT")
def test_minimal_logging_when_streaming(self, mock_log):
uri = "/somewhere"
request = self.factory.get(uri)
response = StreamingHttpResponse(status=200, streaming_content=b"OK", content_type="application/json")
self.middleware.process_response(request, response=response)
self._assert_logged(mock_log, "(data_stream)")
@mock.patch.object(request_logging.middleware, "request_logger")
class LoggingContextTestCase(BaseLogTestCase):
def setUp(self):
self.factory = RequestFactory()
self.middleware = LoggingMiddleware()
def test_request_logging_context(self, mock_log):
request = self.factory.post("/somewhere")
self.middleware.process_request(request, None, request.body)
self._asset_logged_with_additional_args_and_kwargs(
mock_log, (), {"extra": {"request": request, "response": None}}
)
def test_response_logging_context(self, mock_log):
request = self.factory.post("/somewhere")
response = mock.MagicMock()
response.get.return_value = "application/json"
headers = {"test_headers": "test_headers"}
if IS_DJANGO_VERSION_GTE_3_2_0:
response.headers = headers
else:
response._headers = headers
self.middleware.process_response(request, response)
self._asset_logged_with_additional_args_and_kwargs(
mock_log, (), {"extra": {"request": request, "response": response}}
)
def test_get_logging_context_extensibility(self, mock_log):
request = self.factory.post("/somewhere")
self.middleware._get_logging_context = lambda request, response: {
"args": (1, True, "Test"),
"kwargs": {"extra": {"REQUEST": request, "middleware": self.middleware}, "exc_info": True},
}
self.middleware.process_request(request, None, request.body)
self._asset_logged_with_additional_args_and_kwargs(
mock_log,
(1, True, "Test"),
{"extra": {"REQUEST": request, "middleware": self.middleware}, "exc_info": True},
)
class BaseLogSettingsTestCase(BaseLogTestCase):
def setUp(self):
body = u"some body"
datafile = io.StringIO(body)
self.request = RequestFactory().post(
"/somewhere", data={"file": datafile}, **{"HTTP_USER_AGENT": "silly-human"}
)
@mock.patch.object(request_logging.middleware, "request_logger")
class LogSettingsLogLevelTestCase(BaseLogSettingsTestCase):
def test_logging_default_debug_level(self, mock_log):
middleware = LoggingMiddleware()
middleware.process_request(self.request, None, self.request.body)
self._assert_logged_with_level(mock_log, DEFAULT_LOG_LEVEL)
@override_settings(REQUEST_LOGGING_DATA_LOG_LEVEL=logging.INFO)
def test_logging_with_customized_log_level(self, mock_log):
middleware = LoggingMiddleware()
middleware.process_request(self.request, None, self.request.body)
self._assert_logged_with_level(mock_log, logging.INFO)
@override_settings(REQUEST_LOGGING_DATA_LOG_LEVEL=None)
def test_invalid_log_level(self, mock_log):
with self.assertRaises(ValueError):
LoggingMiddleware()
@mock.patch.object(request_logging.middleware, "request_logger")
class LogSettingsHttp4xxAsErrorTestCase(BaseLogTestCase):
def setUp(self):
from django.urls import set_urlconf
set_urlconf("test_urls")
self.factory = RequestFactory()
self.request = self.factory.get("/not-a-valid-url")
response = mock.MagicMock()
response.status_code = 404
response.get.return_value = "application/json"
headers = {"test_headers": "test_headers"}
if IS_DJANGO_VERSION_GTE_3_2_0:
response.headers = headers
else:
response._headers = headers
self.response_404 = response
def test_logging_default_http_4xx_error(self, mock_log):
middleware = LoggingMiddleware()
middleware.process_response(self.request, self.response_404)
self.assertEqual(DEFAULT_HTTP_4XX_LOG_LEVEL, logging.ERROR)
self._assert_logged_with_level(mock_log, DEFAULT_HTTP_4XX_LOG_LEVEL)
def test_logging_http_4xx_level(self, mock_log):
for level in (logging.INFO, logging.WARNING, logging.ERROR):
mock_log.reset_mock()
with override_settings(REQUEST_LOGGING_HTTP_4XX_LOG_LEVEL=level):
middleware = LoggingMiddleware()
middleware.process_response(self.request, self.response_404)
self._assert_logged_with_level(mock_log, level)
@override_settings(REQUEST_LOGGING_HTTP_4XX_LOG_LEVEL=None)
def test_invalid_log_level(self, mock_log):
with self.assertRaises(ValueError):
LoggingMiddleware()
@mock.patch.object(request_logging.middleware, "request_logger")
class LogSettingsColorizeTestCase(BaseLogSettingsTestCase):
def test_default_colorize(self, mock_log):
middleware = LoggingMiddleware()
middleware.process_request(self.request, None, self.request.body)
self.assertEqual(DEFAULT_COLORIZE, self._is_log_colorized(mock_log))
@override_settings(REQUEST_LOGGING_ENABLE_COLORIZE=False)
def test_disable_colorize(self, mock_log):
middleware = LoggingMiddleware()
middleware.process_request(self.request, None, self.request.body)
self.assertFalse(self._is_log_colorized(mock_log))
@override_settings(REQUEST_LOGGING_ENABLE_COLORIZE="Not a boolean")
def test_invalid_colorize(self, mock_log):
with self.assertRaises(ValueError):
LoggingMiddleware()
@override_settings(REQUEST_LOGGING_DISABLE_COLORIZE=False)
def test_legacy_settings(self, mock_log):
middleware = LoggingMiddleware()
middleware.process_request(self.request, None, self.request.body)
self.assertFalse(self._is_log_colorized(mock_log))
@override_settings(REQUEST_LOGGING_DISABLE_COLORIZE=False, REQUEST_LOGGING_ENABLE_COLORIZE=True)
def test_legacy_settings_taking_precedence(self, mock_log):
middleware = LoggingMiddleware()
middleware.process_request(self.request, None, self.request.body)
self.assertFalse(self._is_log_colorized(mock_log))
def _is_log_colorized(self, mock_log):
reset_code = "\x1b[0m"
calls = mock_log.log.call_args_list
logs = " ".join(call[0][1] for call in calls)
return reset_code in logs
@mock.patch.object(request_logging.middleware, "request_logger")
class LogSettingsMaxLengthTestCase(BaseLogTestCase):
def setUp(self):
from django.urls import set_urlconf
set_urlconf("test_urls")
self.factory = RequestFactory()
def get_response(request):
response = mock.MagicMock()
response.status_code = 200
response.get.return_value = "application/json"
headers = {"test_headers": "test_headers"}
if IS_DJANGO_VERSION_GTE_3_2_0:
response.headers = headers
else:
response._headers = headers
return response
self.get_response = get_response
@override_settings(REQUEST_LOGGING_ENABLE_COLORIZE=False)
def test_default_max_body_length(self, mock_log):
body = DEFAULT_MAX_BODY_LENGTH * "0" + "1"
request = self.factory.post("/somewhere", data={"file": body})
middleware = LoggingMiddleware(self.get_response)
middleware.__call__(request)
request_body_str = request.body if isinstance(request.body, str) else request.body.decode()
self._assert_logged(mock_log, re.sub(r"\r?\n", "", request_body_str[:DEFAULT_MAX_BODY_LENGTH]))
self._assert_not_logged(mock_log, body)
@override_settings(REQUEST_LOGGING_MAX_BODY_LENGTH=150, REQUEST_LOGGING_ENABLE_COLORIZE=False)
def test_customized_max_body_length(self, mock_log):
body = 150 * "0" + "1"
request = self.factory.post("/somewhere", data={"file": body})
middleware = LoggingMiddleware(self.get_response)
middleware.__call__(request)
request_body_str = request.body if isinstance(request.body, str) else request.body.decode()
self._assert_logged(mock_log, re.sub(r"\r?\n", "", request_body_str[:150]))
self._assert_not_logged(mock_log, body)
@override_settings(REQUEST_LOGGING_MAX_BODY_LENGTH="Not an int")
def test_invalid_max_body_length(self, mock_log):
with self.assertRaises(ValueError):
LoggingMiddleware()
@mock.patch.object(request_logging.middleware, "request_logger")
class DecoratorTestCase(BaseLogTestCase):
def setUp(self):
from django.urls import set_urlconf
set_urlconf("test_urls")
self.factory = RequestFactory()
self.middleware = LoggingMiddleware()
def test_no_logging_decorator_class_view(self, mock_log):
body = u"some super secret body"
request = self.factory.post("/test_class", data={"file": body})
self.middleware.process_request(request, None, request.body)
self._assert_not_logged(mock_log, body)
self._assert_logged(mock_log, NO_LOGGING_MSG)
def test_no_logging_decorator_func_view(self, mock_log):
body = u"some super secret body"
request = self.factory.post("/test_func", data={"file": body})
self.middleware.process_request(request, None, request.body)
self._assert_not_logged(mock_log, body)
self._assert_logged(mock_log, NO_LOGGING_MSG)
def test_no_logging_decorator_custom_msg(self, mock_log):
body = u"some super secret body"
request = self.factory.post("/test_msg", data={"file": body})
self.middleware.process_request(request, None, request.body)
self._assert_not_logged(mock_log, body)
self._assert_not_logged(mock_log, NO_LOGGING_MSG)
self._assert_logged(mock_log, "Custom message")
def test_no_logging_decorator_silent(self, mock_log):
body = u"some super secret body"
request = self.factory.post("/dont_log_silent", data={"file": body})
self.middleware.process_request(request, None, request.body)
self._assert_not_logged(mock_log, body)
self._assert_not_logged(mock_log, NO_LOGGING_MSG)
self._assert_not_logged(mock_log, "not logged because")
def test_no_logging_empty_response_body(self, mock_log):
body = u"our work of art"
request = self.factory.post("/dont_log_empty_response_body", data={"file": body})
self.middleware.process_request(request, None, request.body)
self._assert_not_logged(mock_log, body)
self._assert_not_logged(mock_log, NO_LOGGING_MSG)
self._assert_logged(mock_log, "Empty response body")
def test_no_logging_alternate_urlconf(self, mock_log):
body = u"some super secret body"
request = self.factory.post("/test_route", data={"file": body})
request.urlconf = "test_urls_alternate"
self.middleware.process_request(request, None, request.body)
self._assert_not_logged(mock_log, body)
self._assert_logged(mock_log, NO_LOGGING_MSG)
def test_still_logs_verb(self, mock_log):
body = u"our work of art"
request = self.factory.post("/dont_log_empty_response_body", data={"file": body})
self.middleware.process_request(request, None, request.body)
self._assert_logged(mock_log, "POST")
def test_still_logs_path(self, mock_log):
body = u"our work of art"
uri = "/dont_log_empty_response_body"
request = self.factory.post(uri, data={"file": body})
self.middleware.process_request(request, None, request.body)
self._assert_logged(mock_log, uri)
@mock.patch.object(request_logging.middleware, "request_logger")
class DRFTestCase(BaseLogTestCase):
def setUp(self):
from django.urls import set_urlconf
set_urlconf("test_urls")
self.factory = RequestFactory()
self.middleware = LoggingMiddleware()
def test_no_request_logging_is_honored(self, mock_log):
uri = "/widgets"
request = self.factory.get(uri)
self.middleware.process_request(request, None, request.body)
self._assert_logged(mock_log, "DRF explicit annotation")
def test_no_response_logging_is_honored(self, mock_log):
uri = "/widgets"
request = self.factory.get(uri)
mock_response = HttpResponse('{"example":"response"}', content_type="application/json", status=422)
self.middleware.process_response(request, response=mock_response)
self._assert_not_logged(mock_log, '"example":"response"')
self._assert_logged(mock_log, "/widgets")
def test_non_existent_drf_route_logs(self, mock_log):
uri = "/widgets/1234"
request = self.factory.patch(uri, data={"almost": "had you"})
self.middleware.process_request(request, None, request.body)
self._assert_not_logged(mock_log, "almost")
self._assert_not_logged(mock_log, "had you")
@mock.patch.object(request_logging.middleware, "request_logger")
class LogRequestAtDifferentLevelsTestCase(BaseLogTestCase):
def setUp(self):
from django.urls import set_urlconf
set_urlconf("test_urls")
self.factory = RequestFactory()
self.request_200 = self.factory.get("/fine-thank-you")
self.response_200 = mock.MagicMock()
self.response_200.status_code = 200
self.response_200.get.return_value = "application/json"
if IS_DJANGO_VERSION_GTE_3_2_0:
self.response_200.headers = {"test_headers": "test_headers"}
else:
self.response_200._headers = {"test_headers": "test_headers"}
self.request_404 = self.factory.get("/not-a-valid-url")
self.response_404 = mock.MagicMock()
self.response_404.status_code = 404
self.response_404.get.return_value = "application/json"
if IS_DJANGO_VERSION_GTE_3_2_0:
self.response_404.headers = {"test_headers": "test_headers"}
else:
self.response_404._headers = {"test_headers": "test_headers"}
self.request_500 = self.factory.get("/bug")
self.response_500 = mock.MagicMock()
self.response_500.status_code = 500
self.response_500.get.return_value = "application/json"
if IS_DJANGO_VERSION_GTE_3_2_0:
self.response_500.headers = {"test_headers": "test_headers"}
else:
self.response_500._headers = {"test_headers": "test_headers"}
def test_log_request_200(self, mock_log):
mock_log.reset_mock()
middleware = LoggingMiddleware()
middleware.process_request(self.request_200, self.response_200, self.request_200.body)
self._assert_logged_with_level(mock_log, DEFAULT_LOG_LEVEL)
def test_log_request_404_as_4xx(self, mock_log):
for level in (logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR):
mock_log.reset_mock()
with override_settings(REQUEST_LOGGING_HTTP_4XX_LOG_LEVEL=level):
middleware = LoggingMiddleware()
middleware.process_request(self.request_404, self.response_404, self.request_404.body)
self._assert_logged_with_level(mock_log, level)
def test_log_request_500_as_error(self, mock_log):
mock_log.reset_mock()
middleware = LoggingMiddleware()
middleware.process_request(self.request_500, self.response_500, self.request_500.body)
self._assert_logged_with_level(mock_log, logging.ERROR)
if __name__ == "__main__":
unittest.main()