Skip to content

Commit

Permalink
Add testcase for testing password change notification
Browse files Browse the repository at this point in the history
  • Loading branch information
varun kumar committed Oct 15, 2023
1 parent 70be0c0 commit 591dc9e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
13 changes: 8 additions & 5 deletions allauth/account/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,12 +681,15 @@ def get_login_stages(self):

def send_notification_mail(self, template_prefix, user, context):
from allauth.account.models import EmailAddress

if app_settings.ACCOUNT_EMAIL_NOTIFICATIONS:
context |= {
"timestamp": timezone.now(),
"ip": self.get_client_ip(self.request),
"browser_agent": self.get_browser_user_agent(self.request),
}
context.update(
{
"timestamp": timezone.now(),
"ip": self.get_client_ip(self.request),
"browser_agent": self.get_browser_user_agent(self.request),
}
)
email = EmailAddress.objects.get_primary(user)
if email:
self.send_mail(template_prefix, email.email, context)
Expand Down
25 changes: 25 additions & 0 deletions allauth/account/tests/test_reset_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,28 @@ def _create_user_and_login(self, usable_password=True):
def _password_set_or_change_redirect(self, urlname, usable_password):
self._create_user_and_login(usable_password)
return self.client.get(reverse(urlname))


def test_notification_on_password_reset(user_factory, client):
user = user_factory(
email="[email protected]",
username="john.doe",
password="password",
commit=True,
email_verified=True,
)
client.force_login(user)

client.post(
reverse("account_change_password"),
data={
"oldpassword": "password",
"password1": "change_password",
"password2": "change_password",
},
**{
"HTTP_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}
)
assert len(mail.outbox) == 1
assert "Your password has been changed" in mail.outbox[0].body
7 changes: 4 additions & 3 deletions allauth/templates/account/email/base_notification.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
{% load account %}
{% user_display user as user_display %}

{% block content %}
Hello {{ user_display }},

We wanted to let you know that there has been a recent change to your account information.

Here are the details of the change:

{% block content %}
{% endblock %}
{% block notification_message %}
{% endblock notification_message%}

Change details
Time: {{timestamp}}
Expand All @@ -20,4 +21,4 @@ Browser agent : {{browser_agent}}

If this change was not made by you, please contact us immediately at support of {{site_name}}.


{% endblock %}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "account/email/base_notification.txt" %}
{% load i18n %}

{% block content %}{% blocktrans %}Your password has been changed.{% endblocktrans %}{% endblock content %}
{% block notification_message %}{% blocktrans %}Your password has been changed.{% endblocktrans %}{% endblock notification_message %}

0 comments on commit 591dc9e

Please sign in to comment.