Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code for sending mail when a user changes password or resets the password #1352

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cadasta/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
from django.contrib.auth.password_validation import validate_password
from allauth.account.utils import send_email_confirmation
from allauth.account import forms as allauth_forms
from django.core.mail import send_mail

from .models import User, now_plus_48_hours
from .validators import check_username_case_insensitive
from parsley.decorators import parsleyfy
from django.template.loader import render_to_string


@parsleyfy
Expand Down Expand Up @@ -127,6 +129,20 @@ def clean_password(self):
def save(self):
allauth_forms.get_adapter().set_password(
self.user, self.cleaned_data['password'])
msg_subject = render_to_string(
'accounts/email/password_changed_subject.txt'
)
msg_subject = msg_subject.replace(u'\n', u'')
msg_body = render_to_string(
'accounts/email/password_changed_message.txt'
)
send_mail(
msg_subject,
msg_body,
settings.DEFAULT_FROM_EMAIL,
[self.user.email],
fail_silently=False,
)


class ChangePasswordForm(ChangePasswordMixin,
Expand Down
28 changes: 28 additions & 0 deletions cadasta/accounts/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,20 @@ def test_valid_data(self):

assert user.check_password('iloveyoko79!') is True

def test_email_test(self):
user = UserFactory.create(password='beatles4Lyfe!')

data = {
'oldpassword': 'beatles4Lyfe!',
'password': 'iloveyoko79!',
}

form = forms.ChangePasswordForm(user, data)
assert form.is_valid() is True
form.save()

assert len(mail.outbox) == 1

def test_password_incorrect(self):
user = UserFactory.create(
password='beatles4Lyfe!', username='imagine71')
Expand Down Expand Up @@ -484,6 +498,20 @@ def test_valid_data(self):

assert user.check_password('iloveyoko79!') is True

def test_email_test(self):
user = UserFactory.create(password='beatles4Lyfe!')

data = {
'password': 'iloveyoko79!',
}

form = forms.ResetPasswordKeyForm(data, user=user)

assert form.is_valid() is True
form.save()

assert len(mail.outbox) == 1

def test_password_contains_username(self):
user = UserFactory.create(
password='beatles4Lyfe!', username='imagine71')
Expand Down
5 changes: 5 additions & 0 deletions cadasta/templates/accounts/email/password_changed_message.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% load i18n %}{% blocktrans %}You are receiving this email because someone changed the password for your account at Cadasta Platform.

Please contact us immediately under [email protected] if you have not changed your password.

Thank you for using Cadasta Platform! {% endblocktrans %}
4 changes: 4 additions & 0 deletions cadasta/templates/accounts/email/password_changed_subject.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% load i18n %}
{% autoescape off %}
{% blocktrans %}Your password at Cadasta was changed{% endblocktrans %}
{% endautoescape %}