Mock out g
in logging utils test.
This gives us better test isolation. Previously, we were manually setting `g.current_user` with a factory instance and not cleaning it up properly, which could break later tests.
This commit is contained in:
parent
a56d9c9ca8
commit
d82fd46a3c
@ -2,6 +2,7 @@ from io import StringIO
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
from unittest.mock import Mock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@ -62,13 +63,16 @@ def test_json_formatter_for_exceptions(logger, log_stream_content):
|
|||||||
assert log.get("details")
|
assert log.get("details")
|
||||||
|
|
||||||
|
|
||||||
def test_request_context_filter(logger, log_stream_content, request_ctx):
|
def test_request_context_filter(logger, log_stream_content, request_ctx, monkeypatch):
|
||||||
user = UserFactory.create()
|
request_uuid = str(uuid4())
|
||||||
uuid = str(uuid4())
|
user_uuid = str(uuid4())
|
||||||
|
|
||||||
request_ctx.g.current_user = user
|
user = Mock(spec=["id"])
|
||||||
request_ctx.request.environ["HTTP_X_REQUEST_ID"] = uuid
|
user.id = user_uuid
|
||||||
|
|
||||||
|
monkeypatch.setattr("atst.utils.logging.g", Mock(current_user=user))
|
||||||
|
request_ctx.request.environ["HTTP_X_REQUEST_ID"] = request_uuid
|
||||||
logger.info("this user is doing something")
|
logger.info("this user is doing something")
|
||||||
log = json.loads(log_stream_content())
|
log = json.loads(log_stream_content())
|
||||||
assert log["user_id"] == str(user.id)
|
assert log["user_id"] == str(user_uuid)
|
||||||
assert log["request_id"] == uuid
|
assert log["request_id"] == request_uuid
|
||||||
|
Loading…
x
Reference in New Issue
Block a user