Skip to content

Commit

Permalink
fix: admins not receiving new user notification emails
Browse files Browse the repository at this point in the history
  • Loading branch information
kennedykori committed Oct 18, 2021
1 parent 6999213 commit 997c118
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
It's currently awaiting review and approval.
</mj-text>
<mj-text color="#525252">
Please log in to the administration site and review the request.
Please log in to the administration site and review the request or alternatively use the following link: {{user_approval_url}}.
</mj-text>
<mj-text color="#525252">
With kind regards,
Expand Down
15 changes: 11 additions & 4 deletions fahari/users/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.db import transaction
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.http import HttpRequest
from django.template.loader import get_template

LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -45,7 +46,7 @@ def email_confirmed_hander(request, email_address, **kwargs):
if user.is_approved or user.approval_notified:
return False # do nothing

send_admin_awaiting_approval_email(user)
send_admin_awaiting_approval_email(user, request)
send_user_awaiting_approval_email(user)
return True
except User.DoesNotExist as e:
Expand Down Expand Up @@ -105,14 +106,20 @@ def account_confirmed_handler(sender, instance, created, **kwargs):
return True


def send_admin_awaiting_approval_email(user):
context = {"user": user, "support_email": settings.SERVER_EMAIL}
def send_admin_awaiting_approval_email(user, request: HttpRequest) -> None:
context = {
"user": user,
"support_email": settings.SERVER_EMAIL,
"user_approval_url": request.build_absolute_uri(
"/admin/users/user/%s/change/" % str(user.pk)
),
}
message = get_template("emails/account_pending_approval_admin.html").render(context)
mail = EmailMessage(
subject="New Fahari System Account Pending Approval",
body=message,
from_email=settings.SERVER_EMAIL,
to=[user.email],
to=[admin.email for admin in User.objects.filter(is_staff=True)],
reply_to=[settings.SERVER_EMAIL],
)
mail.content_subtype = "html"
Expand Down
12 changes: 7 additions & 5 deletions fahari/users/tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,18 @@ def test_send_user_awaiting_approval_email(mailoutbox):
]


def test_send_admin_awaiting_approval_email(mailoutbox):
def test_send_admin_awaiting_approval_email(mailoutbox, rf):
admins = baker.make(
User, 3, approval_notified=True, email=fake.email(), is_approved=True, is_staff=True
)
user = baker.make(User, email=fake.email(), is_approved=True, approval_notified=False)
send_admin_awaiting_approval_email(user) # no error
request = rf.get("/")
send_admin_awaiting_approval_email(user, request) # no error
assert len(mailoutbox) >= 1
m = mailoutbox[len(mailoutbox) - 1]
assert m.subject == "New Fahari System Account Pending Approval"
assert m.from_email == settings.SERVER_EMAIL
assert list(m.to) == [
user.email,
]
assert set(m.to) == set(admin.email for admin in admins)


def test_account_confirmed_handler_newly_created():
Expand Down

0 comments on commit 997c118

Please sign in to comment.