From 639b30122876962caf0092e27e2b5566d843936f Mon Sep 17 00:00:00 2001 From: Uppara Nithiya Shree Date: Tue, 28 Mar 2017 19:17:17 +0530 Subject: [PATCH] Added code for send mail after password change and password reset --- cadasta/accounts/forms.py | 16 +++++++++++ cadasta/accounts/tests/test_forms.py | 28 +++++++++++++++++++ .../email/password_changed_message.txt | 5 ++++ .../email/password_changed_subject.txt | 4 +++ 4 files changed, 53 insertions(+) create mode 100644 cadasta/templates/accounts/email/password_changed_message.txt create mode 100644 cadasta/templates/accounts/email/password_changed_subject.txt diff --git a/cadasta/accounts/forms.py b/cadasta/accounts/forms.py index af20606ca..d9732123f 100644 --- a/cadasta/accounts/forms.py +++ b/cadasta/accounts/forms.py @@ -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 @@ -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, diff --git a/cadasta/accounts/tests/test_forms.py b/cadasta/accounts/tests/test_forms.py index 8e3f5bb0e..686ecf70c 100644 --- a/cadasta/accounts/tests/test_forms.py +++ b/cadasta/accounts/tests/test_forms.py @@ -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') @@ -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') diff --git a/cadasta/templates/accounts/email/password_changed_message.txt b/cadasta/templates/accounts/email/password_changed_message.txt new file mode 100644 index 000000000..ced88c948 --- /dev/null +++ b/cadasta/templates/accounts/email/password_changed_message.txt @@ -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 security@cadasta.org if you have not changed your password. + +Thank you for using Cadasta Platform! {% endblocktrans %} diff --git a/cadasta/templates/accounts/email/password_changed_subject.txt b/cadasta/templates/accounts/email/password_changed_subject.txt new file mode 100644 index 000000000..567643655 --- /dev/null +++ b/cadasta/templates/accounts/email/password_changed_subject.txt @@ -0,0 +1,4 @@ +{% load i18n %} +{% autoescape off %} +{% blocktrans %}Your password at Cadasta was changed{% endblocktrans %} +{% endautoescape %} \ No newline at end of file