Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
raftmsohani committed Nov 5, 2024
1 parent 74730d0 commit 3e2c6e2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
7 changes: 4 additions & 3 deletions tdrs-backend/tdpservice/email/helpers/admin_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def email_admin_deactivated_user(user):
from tdpservice.users.models import User
from tdpservice.email.email_enums import EmailType
from tdpservice.email.email import automated_email, log
from tdpservice.email.tasks import get_ofa_admin_user_emails, get_system_owner_email
from tdpservice.email.tasks import get_ofa_admin_user_emails

recipient_emails = get_ofa_admin_user_emails()
logger_context = {
Expand Down Expand Up @@ -56,7 +56,8 @@ def email_system_owner_system_admin_role_change(user, action):
'action': action,
}

log(f"Preparing email to System Owner for System Admin role change for user {user.username}", logger_context=logger_context)
log(f"Preparing email to System Owner for System Admin role change for user {user.username}",
logger_context=logger_context)

automated_email(
email_path=template_path,
Expand All @@ -65,4 +66,4 @@ def email_system_owner_system_admin_role_change(user, action):
email_context=context,
text_message=text_message,
logger_context=logger_context
)
)
2 changes: 1 addition & 1 deletion tdrs-backend/tdpservice/email/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def get_system_owner_email():
try:
user_email = User.objects.filter(groups__name='System Owner').values_list('email', flat=True).distinct()
except User.DoesNotExist:
user_email=[None]
user_email = [None]
return user_email

def get_num_access_requests():
Expand Down
3 changes: 2 additions & 1 deletion tdrs-backend/tdpservice/users/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ class UsersConfig(AppConfig):
verbose_name = "Users"

def ready(self):
import tdpservice.users.signals
"""Import signals."""
import tdpservice.users.signals # noqa
11 changes: 7 additions & 4 deletions tdrs-backend/tdpservice/users/signals.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Signals for the users app."""
from django.db.models.signals import m2m_changed
from django.dispatch import receiver
from tdpservice.users.models import User
Expand All @@ -9,11 +10,13 @@

@receiver(m2m_changed, sender=User.groups.through)
def user_group_changed(sender, instance, action, pk_set, **kwargs):
if pk_set:
"""Send an email to the System Owner when a user is assigned or removed from the System Admin role."""
if pk_set:
ADMIN_GROUP_PK = Group.objects.get(name="OFA System Admin").pk
ACTIONS = {
'PRE_REMOVE' : 'pre_remove',
'PRE_ADD' : 'pre_add',
'PRE_REMOVE': 'pre_remove',
'PRE_ADD': 'pre_add',
'PRE_CLEAR': 'pre_clear'
}
group_change_list = [pk for pk in pk_set]
if ADMIN_GROUP_PK in group_change_list and action == ACTIONS['PRE_ADD']:
Expand All @@ -22,6 +25,6 @@ def user_group_changed(sender, instance, action, pk_set, **kwargs):
elif ADMIN_GROUP_PK in group_change_list and action == ACTIONS['PRE_REMOVE']:
# EMAIL ADMIN GROUP REMOVED from OFA ADMIN
email_system_owner_system_admin_role_change(instance, "removed")
elif pk_set is None and action == 'pre_clear':
elif pk_set is None and action == ACTIONS['PRE_CLEAR']:
# EMAIL ADMIN GROUP REMOVED from OFA ADMIN
email_system_owner_system_admin_role_change(instance, "removed")
37 changes: 37 additions & 0 deletions tdrs-backend/tdpservice/users/test/test_signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Test signals."""
import pytest
from unittest.mock import patch
from tdpservice.users.models import User
from tdpservice.users.test.factories import AdminUserFactory
from django.contrib.auth.models import Group
import logging
import django


logger = logging.getLogger(__name__)


@pytest.mark.django_db
def test_my_signal_receiver(mocker):
"""Test my_signal_receiver."""
with patch("django.db.models.signals.m2m_changed.send") as mock_receiver:
instance = AdminUserFactory.create()
instance.groups.add(Group.objects.get(name="OFA System Admin"))

mock_receiver.assert_called_with(
sender=User.groups.through,
instance=instance,
action="post_add",
pk_set={Group.objects.get(name="OFA System Admin").pk},
reverse=False,
using="default",
model=django.contrib.auth.models.Group,
)
mock_receiver.call_count = 2 # pre_save and post_save

with patch(
"tdpservice.users.signals.email_system_owner_system_admin_role_change"
) as mock_email_system_owner_system_admin_role_change:
instance = AdminUserFactory.create()
instance.groups.add(Group.objects.get(name="OFA System Admin"))
mock_email_system_owner_system_admin_role_change.assert_called_once()

0 comments on commit 3e2c6e2

Please sign in to comment.