Skip to content

Commit

Permalink
test: added test for saving cid in model
Browse files Browse the repository at this point in the history
chore: `Union[None, str]` -> `Optional[str]`
  • Loading branch information
aqeelat committed Dec 21, 2022
1 parent 45f0520 commit be9e8de
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
6 changes: 3 additions & 3 deletions auditlog/cid.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from contextvars import ContextVar
from typing import Optional, Union
from typing import Optional

from django.conf import settings
from django.http import HttpRequest
Expand All @@ -20,11 +20,11 @@ def set_cid(request: Optional[HttpRequest] = None) -> None:
correlation_id.set(cid)


def _get_cid() -> Union[str, None]:
def _get_cid() -> Optional[str]:
return correlation_id.get()


def get_cid() -> Union[str, None]:
def get_cid() -> Optional[str]:
method = settings.AUDITLOG_CID_GETTER
if not method:
return _get_cid()
Expand Down
16 changes: 9 additions & 7 deletions auditlog_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def test_additional_data(self):
)


class MiddlewareTest(TestCase):
class MiddlewareTest(SimpleModelTest):
"""
Test the middleware responsible for connecting and disconnecting the signals used in automatic logging.
"""
Expand Down Expand Up @@ -483,7 +483,7 @@ def test_get_remote_addr(self):
self.middleware._get_remote_addr(request), expected_remote_addr
)

def test_cid_from_header(self):
def test_cid(self):
header = "HTTP_" + str(settings.AUDITLOG_CID_HEADER).upper().replace("-", "_")
cid = "random_CID"

Expand All @@ -507,6 +507,10 @@ def test_cid_from_header(self):
request = self.factory.get("/", **{header: cid})
self.middleware(request)

obj = self.make_object()
history = obj.history.get(action=LogEntry.Action.CREATE)

self.assertEqual(history.cid, expected_result)
self.assertEqual(get_cid(), expected_result)


Expand Down Expand Up @@ -1362,12 +1366,10 @@ def test_created_timezone(self):

def test_cid(self):
self.client.force_login(self.user)
expected_response = '<a href="/admin/auditlog/logentry/?cid=123" '
expected_response += (
'title="Click to filter by records with this correlation id">'
expected_response = (
'<a href="/admin/auditlog/logentry/?cid=123" '
'title="Click to filter by records with this correlation id">123</a>'
)
expected_response += "123"
expected_response += "</a>"

log_entry = self.obj.history.latest()
log_entry.cid = "123"
Expand Down

0 comments on commit be9e8de

Please sign in to comment.