Skip to content

Commit

Permalink
feat: emit log in / out tracking logs (#33219)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian2012 authored Sep 19, 2023
1 parent 9b44065 commit 4a9aed4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
18 changes: 18 additions & 0 deletions common/djangoapps/student/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from django_countries.fields import CountryField
from edx_django_utils import monitoring
from edx_django_utils.cache import RequestCache
from eventtracking import tracker
from model_utils.models import TimeStampedModel
from opaque_keys.edx.django.models import CourseKeyField, LearningContextKeyField
from pytz import UTC, timezone
Expand All @@ -69,6 +70,9 @@

IS_MARKETABLE = 'is_marketable'

USER_LOGGED_IN_EVENT_NAME = 'edx.user.login'
USER_LOGGED_OUT_EVENT_NAME = 'edx.user.logout'


class AnonymousUserId(models.Model):
"""
Expand Down Expand Up @@ -1230,6 +1234,13 @@ def create_comments_service_user(user): # lint-amnesty, pylint: disable=missing
@receiver(user_logged_in)
def log_successful_login(sender, request, user, **kwargs): # lint-amnesty, pylint: disable=unused-argument
"""Handler to log when logins have occurred successfully."""
tracker.emit(
USER_LOGGED_IN_EVENT_NAME,
{
'user_id': getattr(user, 'id', None) if user else None,
'event_type': "login",
}
)
if settings.FEATURES['SQUELCH_PII_IN_LOGS']:
AUDIT_LOG.info(f"Login success - user.id: {user.id}")
else:
Expand All @@ -1240,6 +1251,13 @@ def log_successful_login(sender, request, user, **kwargs): # lint-amnesty, pyli
def log_successful_logout(sender, request, user, **kwargs): # lint-amnesty, pylint: disable=unused-argument
"""Handler to log when logouts have occurred successfully."""
if hasattr(request, 'user'):
tracker.emit(
USER_LOGGED_OUT_EVENT_NAME,
{
'user_id': getattr(user, 'id', None) if user else None,
'event_type': "logout",
}
)
if settings.FEATURES['SQUELCH_PII_IN_LOGS']:
AUDIT_LOG.info(f'Logout - user.id: {request.user.id}') # pylint: disable=logging-format-interpolation
else:
Expand Down
55 changes: 55 additions & 0 deletions common/djangoapps/student/tests/test_tracking_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Test to verify tracking logs are emitted correctly"""
from unittest.mock import patch, Mock

from django.test import TestCase

from common.djangoapps.student.models.user import (
USER_LOGGED_IN_EVENT_NAME,
USER_LOGGED_OUT_EVENT_NAME,
log_successful_login,
log_successful_logout,
)
from common.djangoapps.student.tests.factories import UserFactory


class TestTrackingLog(TestCase):
"""
Tests for tracking log
"""

def setUp(self):
self.user = UserFactory()

@patch("common.djangoapps.student.models.user.tracker")
def test_log_successful_login(self, patched_tracker):
"""
Test log_successful_login
"""
log_successful_login(
sender="dummy_sender", request="dummy_request", user=self.user
)

patched_tracker.emit.assert_called_once_with(
USER_LOGGED_IN_EVENT_NAME,
{
"user_id": self.user.id,
"event_type": "login",
},
)

@patch("common.djangoapps.student.models.user.tracker")
def test_log_successful_logout(self, patched_tracker):
"""
Test log_successful_logout
"""
log_successful_logout(
sender="dummy_sender", request=Mock(user=self.user), user=self.user
)

patched_tracker.emit.assert_called_once_with(
USER_LOGGED_OUT_EVENT_NAME,
{
"user_id": self.user.id,
"event_type": "logout",
},
)

0 comments on commit 4a9aed4

Please sign in to comment.