Skip to content

Commit

Permalink
fixup!: loginas
Browse files Browse the repository at this point in the history
  • Loading branch information
rgraber committed Aug 9, 2024
1 parent 0eab8a3 commit 2f069bc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
18 changes: 1 addition & 17 deletions kobo/apps/audit_log/tests/api/v2/test_api_audit_log.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
import contextlib

from django.contrib.auth import get_user_model
from django.contrib.auth.signals import user_logged_in
from django.utils.timezone import now
from rest_framework import status
from rest_framework.reverse import reverse

from kobo.apps.audit_log.models import AuditAction, AuditLog
from kobo.apps.audit_log.signals import create_access_log
from kobo.apps.audit_log.tests.test_signals import skip_login_access_log
from kpi.tests.base_test_case import BaseTestCase
from kpi.urls.router_api_v2 import URL_NAMESPACE as ROUTER_URL_NAMESPACE


@contextlib.contextmanager
def skip_login_access_log():
"""
Context manager for skipping the creation of an access log on login
Disconnects the method that creates access logs from the user_logged_in signal within the contextmanager block.
Useful when you want full control over the audit logs produced in a test.
"""
user_logged_in.disconnect(create_access_log)
yield
user_logged_in.connect(create_access_log)


class ApiAuditLogTestCase(BaseTestCase):

fixtures = ['test_data']
Expand Down
30 changes: 29 additions & 1 deletion kobo/apps/audit_log/tests/test_signals.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import contextlib
from unittest.mock import patch

from allauth.account.models import EmailAddress
from django.contrib.auth import get_user_model
from django.contrib.auth.signals import user_logged_in
from django.urls import resolve, reverse
from trench.utils import get_mfa_model

from kobo.apps.accounts.mfa.forms import MfaTokenForm
from kobo.apps.audit_log.models import AuditAction, AuditLog
from kobo.apps.audit_log.signals import create_access_log
from kpi.tests.base_test_case import BaseTestCase


@contextlib.contextmanager
def skip_login_access_log():
"""
Context manager for skipping the creation of an access log on login
Disconnects the method that creates access logs from the user_logged_in signal within the contextmanager block.
Useful when you want full control over the audit logs produced in a test.
"""
user_logged_in.disconnect(create_access_log)
yield
user_logged_in.connect(create_access_log)


class AuditLogSignalsTestCase(BaseTestCase):
"""
Class for testing that logins produce AuditLogs.
Expand Down Expand Up @@ -102,3 +117,16 @@ def test_mfa_login(self):
audit_log = AuditLog.objects.first()
self.assertEqual(audit_log.user.id, AuditLogSignalsTestCase.user.id)
self.assertEqual(audit_log.action, AuditAction.AUTH)

def test_loginas(self):
AuditLogSignalsTestCase.user.is_superuser = True
AuditLogSignalsTestCase.user.save()
new_user = get_user_model().objects.create_user('user2', '[email protected]', 'pass2')
new_user.save()
with skip_login_access_log():
self.client.login(username='user', password='pass')
self.client.post(reverse('loginas-user-login', args=[new_user.id]))
self.assertEqual(AuditLog.objects.count(), 1)
audit_log = AuditLog.objects.first()
self.assertEqual(audit_log.user.id, new_user.id)
self.assertEqual(audit_log.action, AuditAction.AUTH)

0 comments on commit 2f069bc

Please sign in to comment.