Commit 1dd04d32 authored by Vitalis Salis's avatar Vitalis Salis

astakos: On email change email the current address

parent 222882d8
......@@ -25,6 +25,8 @@ Astakos
projects, and project memberships. Reactivation unsuspends them.
* Add command `user-check`. It supports suspending projects for previously
deactivated users.
* Send an informative email to the user's current email address when they
request to change their email.
Cyclades
--------
......
{% extends "im/email.txt" %}
{% block content %}
Dear {{ ec.user.realname }}
You recently requested to modify the e-mail that is used for your {{ BRANDING_SERVICE_NAME }} account from {{ ec.user.email }} to {{ ec.new_email_address }}.
In case you are not aware of the e-mail change request please contact {{ support }} as soon as possible.
{% endblock %}
{% block note %}{% endblock%}
......@@ -808,7 +808,7 @@ class UserActionsTests(TestCase):
self.assertTrue(user.email_change_is_pending())
# link was sent
self.assertEqual(len(get_mailbox('kpap@synnefo.org')), 0)
self.assertEqual(len(get_mailbox('kpap@synnefo.org')), 1)
self.assertEqual(len(get_mailbox('kpap@gmail.com')), 1)
# proper email change
......@@ -816,7 +816,7 @@ class UserActionsTests(TestCase):
r = self.client.post(ui_url('email_change'), data, follow=True)
self.assertRedirects(r, ui_url('profile'))
self.assertContains(r, messages.EMAIL_CHANGE_REGISTERED)
self.assertEqual(len(get_mailbox('kpap@synnefo.org')), 0)
self.assertEqual(len(get_mailbox('kpap@synnefo.org')), 2)
self.assertEqual(len(get_mailbox('kpap@yahoo.com')), 1)
def test_change_email(self):
......
......@@ -24,7 +24,8 @@ from django.core.exceptions import ValidationError
from synnefo_branding.utils import render_to_string
from astakos.im import settings as astakos_settings
from astakos.im.models import AstakosUser, EmailChange
from astakos.im.user_utils import send_plain, change_user_email
from astakos.im.user_utils import send_plain, change_user_email, \
send_change_email_to_old
from astakos.im.auth import make_local_user
import astakos.im.messages as astakos_messages
from astakos.im.tests.common import get_local_user
......@@ -91,7 +92,10 @@ class TestUserUtils(TestCase):
verify_sent_email(email_dict, mail.outbox[3])
@patch('astakos.im.user_utils.send_change_email_to_new')
def test_change_user_email(self, send_change_email_to_new_mock):
@patch('astakos.im.user_utils.send_change_email_to_old')
def test_change_user_email(self,
send_change_email_to_old_mock,
send_change_email_to_new_mock):
"""
The `change_user_email` method should check if the email
provided is valid. If it is invalid it should raise a
......@@ -110,21 +114,64 @@ class TestUserUtils(TestCase):
# valid `new_email`, default `email_to_new_template_name`
new_email = 'something@somethingelse.com'
default_to_new_template = 'registration/email_change_email_new_email.txt'
default_to_old_template = 'registration/email_change_email_old_email.txt'
change_user_email(user, new_email)
email_change = EmailChange.objects.get(new_email_address=new_email)
send_change_email_to_new_mock.assert_called_once_with(email_change, default_to_new_template)
send_change_email_to_old_mock.assert_called_once_with(email_change, default_to_old_template)
self.assertTrue(user.email_change_is_pending())
self.assertEqual(user.emailchanges.count(), 1)
# valid mail, different `email_to_new_template_name`
to_new_template = 'mytemplate/template.txt'
change_user_email(user, new_email, email_to_new_template_name=to_new_template)
to_old_template = 'mysecondtemplate/template.txt'
change_user_email(
user,
new_email,
email_to_new_template_name=to_new_template,
email_to_old_template_name=to_old_template
)
email_change = EmailChange.objects.get(new_email_address=new_email)
send_change_email_to_new_mock.assert_called_with(email_change, to_new_template)
send_change_email_to_old_mock.assert_called_with(email_change, to_old_template)
# the previous email change was deleted
self.assertEqual(user.emailchanges.count(), 1)
self.assertEqual(email_change, user.emailchanges.all()[0])
@patch('astakos.im.user_utils.get_connection')
@patch('astakos.im.user_utils.send_mail')
@patch('astakos.im.user_utils.render_to_string')
def test_send_change_email_to_old(self,
render_to_string_mock, send_mail_mock, get_connection_mock):
old_email = 'something@something.com'
new_email = 'somethingnew@something.com'
default_template = 'registration/email_change_email_old_email.txt'
template = 'mytemplate/template.txt'
message = 'imamessage'
connection = 0
render_to_string_mock.return_value = message
get_connection_mock.return_value = connection
user = get_local_user(old_email)
email_change = EmailChange.objects.create(
user=user,
new_email_address=new_email
)
send_change_email_to_old(email_change)
render_to_string_mock.assert_called_once_with(default_template, {'ec': email_change})
send_mail_mock.assert_called_once_with(
_(astakos_messages.EMAIL_CHANGE_EMAIL_SUBJECT),
message,
astakos_settings.SERVER_EMAIL,
[email_change.user.email],
connection=connection
)
send_change_email_to_old(email_change, template)
render_to_string_mock.assert_called_with(template, {'ec': email_change})
......@@ -206,7 +206,8 @@ def send_feedback(msg, data, user, email_template_name='im/feedback_mail.txt'):
def change_user_email(user, new_email,
email_to_new_template_name='registration/email_change_email_new_email.txt'):
email_to_new_template_name='registration/email_change_email_new_email.txt',
email_to_old_template_name='registration/email_change_email_old_email.txt'):
validate_email(new_email)
......@@ -225,6 +226,7 @@ def change_user_email(user, new_email,
)
send_change_email_to_new(email_change, email_to_new_template_name)
send_change_email_to_old(email_change, email_to_old_template_name)
return email_change
......@@ -243,3 +245,12 @@ def send_change_email_to_new(ec, email_template_name=(
[ec.new_email_address], connection=get_connection())
msg = 'Sent change email for %s'
logger.log(settings.LOGGING_LEVEL, msg, ec.user.log_display)
def send_change_email_to_old(email_change,
email_template_name='registration/email_change_email_old_email.txt'):
message = render_to_string(email_template_name, {'ec': email_change})
from_email = settings.SERVER_EMAIL
send_mail(_(astakos_messages.EMAIL_CHANGE_EMAIL_SUBJECT), message,
from_email, [email_change.user.email], connection=get_connection())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment