Move custom audit log logic into the User class
This commit is contained in:
parent
0d6f7aa3cc
commit
2776926dea
@ -54,8 +54,7 @@ class AuditableMixin(object):
|
||||
|
||||
@staticmethod
|
||||
def audit_update(mapper, connection, target):
|
||||
changes = AuditableMixin.get_changes(target)
|
||||
if changes and not "last_login" in changes:
|
||||
if AuditableMixin.get_changes(target):
|
||||
target.create_audit_event(connection, target, ACTION_UPDATE)
|
||||
|
||||
def get_changes(self):
|
||||
|
@ -7,7 +7,11 @@ from atst.models import Base, ApplicationRole, types, mixins
|
||||
from atst.models.permissions import Permissions
|
||||
from atst.models.portfolio_invitation import PortfolioInvitation
|
||||
from atst.models.application_invitation import ApplicationInvitation
|
||||
from atst.models.mixins.auditable import record_permission_sets_updates
|
||||
from atst.models.mixins.auditable import (
|
||||
AuditableMixin,
|
||||
ACTION_UPDATE,
|
||||
record_permission_sets_updates,
|
||||
)
|
||||
|
||||
|
||||
users_permission_sets = Table(
|
||||
@ -121,5 +125,11 @@ class User(
|
||||
if c.name not in ["id"]
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def audit_update(mapper, connection, target):
|
||||
changes = AuditableMixin.get_changes(target)
|
||||
if changes and not "last_login" in changes:
|
||||
target.create_audit_event(connection, target, ACTION_UPDATE)
|
||||
|
||||
|
||||
listen(User.permission_sets, "bulk_replace", record_permission_sets_updates, raw=True)
|
||||
|
@ -51,9 +51,3 @@ def test_logging_audit_event_on_update(mock_logger):
|
||||
assert event_log["action"] == "update"
|
||||
|
||||
assert "update" in mock_logger.extras[1]["tags"]
|
||||
|
||||
|
||||
def test_does_not_log_user_update_when_updating_last_login(mock_logger):
|
||||
user = UserFactory.create()
|
||||
Users.update_last_login(user)
|
||||
assert "Audit Event update" not in mock_logger.messages
|
||||
|
@ -1,6 +1,7 @@
|
||||
import pytest
|
||||
from sqlalchemy.exc import InternalError
|
||||
|
||||
from atst.domain.users import Users
|
||||
from atst.models.user import User
|
||||
|
||||
from tests.factories import UserFactory, ApplicationFactory, ApplicationRoleFactory
|
||||
@ -37,3 +38,9 @@ def test_deleted_application_roles_are_ignored(session):
|
||||
session.commit()
|
||||
|
||||
assert len(user.application_roles) == 0
|
||||
|
||||
|
||||
def test_does_not_log_user_update_when_updating_last_login(mock_logger):
|
||||
user = UserFactory.create()
|
||||
Users.update_last_login(user)
|
||||
assert "Audit Event update" not in mock_logger.messages
|
||||
|
Loading…
x
Reference in New Issue
Block a user