2019-12-31 12:33:07 +00:00
|
|
|
"""audit event tests"""
|
|
|
|
|
2020-01-02 12:20:41 +00:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2019-12-31 12:33:07 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
from guardian.shortcuts import get_anonymous_user
|
|
|
|
|
2020-09-21 18:30:30 +00:00
|
|
|
from passbook.audit.models import Event
|
2020-05-16 16:07:00 +00:00
|
|
|
from passbook.policies.dummy.models import DummyPolicy
|
2019-12-31 12:33:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestAuditEvent(TestCase):
|
|
|
|
"""Test Audit Event"""
|
|
|
|
|
|
|
|
def test_new_with_model(self):
|
|
|
|
"""Create a new Event passing a model as kwarg"""
|
2020-09-21 18:16:14 +00:00
|
|
|
event = Event.new("unittest", test={"model": get_anonymous_user()})
|
2020-01-02 12:00:16 +00:00
|
|
|
event.save() # We save to ensure nothing is un-saveable
|
|
|
|
model_content_type = ContentType.objects.get_for_model(get_anonymous_user())
|
|
|
|
self.assertEqual(
|
|
|
|
event.context.get("test").get("model").get("app"),
|
|
|
|
model_content_type.app_label,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_new_with_uuid_model(self):
|
|
|
|
"""Create a new Event passing a model (with UUID PK) as kwarg"""
|
2020-05-16 16:07:00 +00:00
|
|
|
temp_model = DummyPolicy.objects.create(name="test", result=True)
|
2020-09-21 18:16:14 +00:00
|
|
|
event = Event.new("unittest", model=temp_model)
|
2020-01-02 12:00:16 +00:00
|
|
|
event.save() # We save to ensure nothing is un-saveable
|
|
|
|
model_content_type = ContentType.objects.get_for_model(temp_model)
|
|
|
|
self.assertEqual(
|
|
|
|
event.context.get("model").get("app"), model_content_type.app_label
|
|
|
|
)
|
|
|
|
self.assertEqual(event.context.get("model").get("pk"), temp_model.pk.hex)
|