From 2f069bc2915d4437f402fa54f71870f702cc5602 Mon Sep 17 00:00:00 2001 From: rgraber Date: Fri, 9 Aug 2024 14:29:02 -0400 Subject: [PATCH] fixup!: loginas --- .../tests/api/v2/test_api_audit_log.py | 18 +---------- kobo/apps/audit_log/tests/test_signals.py | 30 ++++++++++++++++++- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/kobo/apps/audit_log/tests/api/v2/test_api_audit_log.py b/kobo/apps/audit_log/tests/api/v2/test_api_audit_log.py index 43a0ab903a..c9ac1dd0f0 100644 --- a/kobo/apps/audit_log/tests/api/v2/test_api_audit_log.py +++ b/kobo/apps/audit_log/tests/api/v2/test_api_audit_log.py @@ -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'] diff --git a/kobo/apps/audit_log/tests/test_signals.py b/kobo/apps/audit_log/tests/test_signals.py index 7fb84b237b..b5189e01f1 100644 --- a/kobo/apps/audit_log/tests/test_signals.py +++ b/kobo/apps/audit_log/tests/test_signals.py @@ -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. @@ -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', 'user2@example.com', '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)