2017-01-20 12:27:38 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
from typing import Any
|
2018-06-11 11:55:25 +02:00
|
|
|
from email.utils import parseaddr, formataddr
|
|
|
|
import re
|
2017-01-20 12:27:38 +01:00
|
|
|
|
|
|
|
import django
|
|
|
|
import mock
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core import mail
|
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.urls import reverse
|
|
|
|
from django.utils.timezone import now
|
|
|
|
|
2017-07-08 04:38:13 +02:00
|
|
|
from confirmation.models import Confirmation, generate_key, confirmation_url
|
2017-03-21 18:08:40 +01:00
|
|
|
from zerver.lib.actions import do_start_email_change_process, do_set_realm_property
|
2017-01-20 12:27:38 +01:00
|
|
|
from zerver.lib.test_classes import (
|
|
|
|
ZulipTestCase,
|
|
|
|
)
|
2017-07-05 21:29:27 +02:00
|
|
|
from zerver.lib.send_email import FromAddress
|
2017-05-23 20:57:59 +02:00
|
|
|
from zerver.models import get_user, EmailChangeStatus, Realm, get_realm
|
2017-01-20 12:27:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
class EmailChangeTestCase(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_confirm_email_change_with_non_existent_key(self) -> None:
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2017-01-20 12:27:38 +01:00
|
|
|
key = generate_key()
|
2017-07-08 04:38:13 +02:00
|
|
|
url = confirmation_url(key, 'testserver', Confirmation.EMAIL_CHANGE)
|
2017-01-20 12:27:38 +01:00
|
|
|
response = self.client_get(url)
|
2017-07-22 00:27:45 +02:00
|
|
|
self.assert_in_success_response(["Whoops. We couldn't find your confirmation link in the system."], response)
|
2017-01-20 12:27:38 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_confirm_email_change_with_invalid_key(self) -> None:
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2017-01-20 12:27:38 +01:00
|
|
|
key = 'invalid key'
|
2017-07-08 04:38:13 +02:00
|
|
|
url = confirmation_url(key, 'testserver', Confirmation.EMAIL_CHANGE)
|
2017-01-20 12:27:38 +01:00
|
|
|
response = self.client_get(url)
|
2017-07-22 00:27:45 +02:00
|
|
|
self.assert_in_success_response(["Whoops. The confirmation link is malformed."], response)
|
2017-01-20 12:27:38 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_confirm_email_change_when_time_exceeded(self) -> None:
|
2017-05-07 21:25:59 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
old_email = user_profile.email
|
2017-01-20 12:27:38 +01:00
|
|
|
new_email = 'hamlet-new@zulip.com'
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2017-01-20 12:27:38 +01:00
|
|
|
obj = EmailChangeStatus.objects.create(new_email=new_email,
|
|
|
|
old_email=old_email,
|
|
|
|
user_profile=user_profile,
|
|
|
|
realm=user_profile.realm)
|
|
|
|
key = generate_key()
|
|
|
|
date_sent = now() - datetime.timedelta(days=2)
|
2017-07-08 04:38:13 +02:00
|
|
|
Confirmation.objects.create(content_object=obj,
|
|
|
|
date_sent=date_sent,
|
2017-07-08 06:25:05 +02:00
|
|
|
confirmation_key=key,
|
|
|
|
type=Confirmation.EMAIL_CHANGE)
|
2017-07-08 04:38:13 +02:00
|
|
|
url = confirmation_url(key, user_profile.realm.host, Confirmation.EMAIL_CHANGE)
|
2017-01-20 12:27:38 +01:00
|
|
|
response = self.client_get(url)
|
2017-11-08 23:02:50 +01:00
|
|
|
self.assert_in_success_response(["The confirmation link has expired or been deactivated."], response)
|
2017-01-20 12:27:38 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_confirm_email_change(self) -> None:
|
2017-05-07 21:25:59 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
old_email = user_profile.email
|
2017-01-20 12:27:38 +01:00
|
|
|
new_email = 'hamlet-new@zulip.com'
|
2017-05-23 20:57:59 +02:00
|
|
|
new_realm = get_realm('zulip')
|
|
|
|
self.login(self.example_email('hamlet'))
|
2017-01-20 12:27:38 +01:00
|
|
|
obj = EmailChangeStatus.objects.create(new_email=new_email,
|
|
|
|
old_email=old_email,
|
|
|
|
user_profile=user_profile,
|
|
|
|
realm=user_profile.realm)
|
|
|
|
key = generate_key()
|
2017-07-08 04:38:13 +02:00
|
|
|
Confirmation.objects.create(content_object=obj,
|
|
|
|
date_sent=now(),
|
2017-07-08 06:25:05 +02:00
|
|
|
confirmation_key=key,
|
|
|
|
type=Confirmation.EMAIL_CHANGE)
|
2017-07-08 04:38:13 +02:00
|
|
|
url = confirmation_url(key, user_profile.realm.host, Confirmation.EMAIL_CHANGE)
|
2017-01-20 12:27:38 +01:00
|
|
|
response = self.client_get(url)
|
2017-03-04 06:39:45 +01:00
|
|
|
|
2017-01-20 12:27:38 +01:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2017-03-18 22:48:44 +01:00
|
|
|
self.assert_in_success_response(["This confirms that the email address for your Zulip"],
|
2017-03-05 02:18:42 +01:00
|
|
|
response)
|
2017-05-23 20:57:59 +02:00
|
|
|
user_profile = get_user(new_email, new_realm)
|
2017-01-20 12:27:38 +01:00
|
|
|
self.assertTrue(bool(user_profile))
|
|
|
|
obj.refresh_from_db()
|
|
|
|
self.assertEqual(obj.status, 1)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_start_email_change_process(self) -> None:
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-01-20 12:27:38 +01:00
|
|
|
do_start_email_change_process(user_profile, 'hamlet-new@zulip.com')
|
|
|
|
self.assertEqual(EmailChangeStatus.objects.count(), 1)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_end_to_end_flow(self) -> None:
|
2017-01-20 12:27:38 +01:00
|
|
|
data = {'email': 'hamlet-new@zulip.com'}
|
2017-05-25 01:40:26 +02:00
|
|
|
email = self.example_email("hamlet")
|
2017-01-20 12:27:38 +01:00
|
|
|
self.login(email)
|
2017-07-31 20:44:52 +02:00
|
|
|
url = '/json/settings'
|
2017-01-20 12:27:38 +01:00
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
2017-07-31 20:44:52 +02:00
|
|
|
result = self.client_patch(url, data)
|
2017-01-20 12:27:38 +01:00
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
2017-04-28 03:09:57 +02:00
|
|
|
self.assert_in_success_response(['Check your email for a confirmation link.'], result)
|
2017-01-20 12:27:38 +01:00
|
|
|
email_message = mail.outbox[0]
|
|
|
|
self.assertEqual(
|
|
|
|
email_message.subject,
|
2017-07-03 20:45:52 +02:00
|
|
|
'Verify your new email address'
|
2017-01-20 12:27:38 +01:00
|
|
|
)
|
|
|
|
body = email_message.body
|
2017-07-11 20:25:50 +02:00
|
|
|
from_email = email_message.from_email
|
2017-01-20 12:27:38 +01:00
|
|
|
self.assertIn('We received a request to change the email', body)
|
2017-07-11 20:25:50 +02:00
|
|
|
self.assertIn('Zulip Account Security', from_email)
|
2018-06-11 11:55:25 +02:00
|
|
|
tokenized_no_reply_email = parseaddr(email_message.from_email)[1]
|
|
|
|
self.assertTrue(re.search(self.TOKENIZED_NOREPLY_REGEX, tokenized_no_reply_email))
|
2017-01-20 12:27:38 +01:00
|
|
|
|
|
|
|
activation_url = [s for s in body.split('\n') if s][4]
|
|
|
|
response = self.client_get(activation_url)
|
|
|
|
|
2017-03-18 22:48:44 +01:00
|
|
|
self.assert_in_success_response(["This confirms that the email address"],
|
2017-03-05 02:18:42 +01:00
|
|
|
response)
|
2017-01-20 12:27:38 +01:00
|
|
|
|
2017-08-05 19:42:59 +02:00
|
|
|
# Now confirm trying to change your email back doesn't throw an immediate error
|
|
|
|
result = self.client_patch(url, {"email": "hamlet@zulip.com"})
|
|
|
|
self.assert_in_success_response(['Check your email for a confirmation link.'], result)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_unauthorized_email_change(self) -> None:
|
2017-03-04 06:39:45 +01:00
|
|
|
data = {'email': 'hamlet-new@zulip.com'}
|
2017-05-07 19:39:30 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
email = user_profile.email
|
2017-03-04 06:39:45 +01:00
|
|
|
self.login(email)
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property(user_profile.realm, 'email_changes_disabled', True)
|
2017-07-31 20:44:52 +02:00
|
|
|
url = '/json/settings'
|
|
|
|
result = self.client_patch(url, data)
|
2017-03-04 06:39:45 +01:00
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
|
|
|
self.assertEqual(result.status_code, 400)
|
2017-03-05 02:18:42 +01:00
|
|
|
self.assert_in_response("Email address changes are disabled in this organization.",
|
|
|
|
result)
|
2018-02-02 16:54:26 +01:00
|
|
|
# Realm admins can change their email address even setting is disabled.
|
|
|
|
data = {'email': 'iago-new@zulip.com'}
|
|
|
|
self.login(self.example_email("iago"))
|
|
|
|
url = '/json/settings'
|
|
|
|
result = self.client_patch(url, data)
|
|
|
|
self.assert_in_success_response(['Check your email for a confirmation link.'], result)
|
2017-03-04 06:39:45 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_email_change_already_taken(self) -> None:
|
2017-09-26 20:15:37 +02:00
|
|
|
data = {'email': 'cordelia@zulip.com'}
|
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
email = user_profile.email
|
|
|
|
self.login(email)
|
|
|
|
|
|
|
|
url = '/json/settings'
|
|
|
|
result = self.client_patch(url, data)
|
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
|
|
|
self.assertEqual(result.status_code, 400)
|
|
|
|
self.assert_in_response("Already has an account",
|
|
|
|
result)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_unauthorized_email_change_from_email_confirmation_link(self) -> None:
|
2017-03-04 06:39:45 +01:00
|
|
|
data = {'email': 'hamlet-new@zulip.com'}
|
2017-05-07 19:39:30 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
email = user_profile.email
|
2017-03-04 06:39:45 +01:00
|
|
|
self.login(email)
|
2017-07-31 20:44:52 +02:00
|
|
|
url = '/json/settings'
|
2017-03-04 06:39:45 +01:00
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
2017-07-31 20:44:52 +02:00
|
|
|
result = self.client_patch(url, data)
|
2017-03-04 06:39:45 +01:00
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
2017-04-28 03:09:57 +02:00
|
|
|
self.assert_in_success_response(['Check your email for a confirmation link.'], result)
|
2017-03-04 06:39:45 +01:00
|
|
|
email_message = mail.outbox[0]
|
|
|
|
self.assertEqual(
|
|
|
|
email_message.subject,
|
2017-07-03 20:45:52 +02:00
|
|
|
'Verify your new email address'
|
2017-03-04 06:39:45 +01:00
|
|
|
)
|
|
|
|
body = email_message.body
|
|
|
|
self.assertIn('We received a request to change the email', body)
|
|
|
|
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property(user_profile.realm, 'email_changes_disabled', True)
|
2017-03-04 06:39:45 +01:00
|
|
|
|
|
|
|
activation_url = [s for s in body.split('\n') if s][4]
|
|
|
|
response = self.client_get(activation_url)
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 400)
|
2017-03-05 02:18:42 +01:00
|
|
|
self.assert_in_response("Email address changes are disabled in this organization.",
|
|
|
|
response)
|
2017-03-04 06:39:45 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_post_invalid_email(self) -> None:
|
2017-01-20 12:27:38 +01:00
|
|
|
data = {'email': 'hamlet-new'}
|
2017-05-25 01:40:26 +02:00
|
|
|
email = self.example_email("hamlet")
|
2017-01-20 12:27:38 +01:00
|
|
|
self.login(email)
|
2017-07-31 20:44:52 +02:00
|
|
|
url = '/json/settings'
|
|
|
|
result = self.client_patch(url, data)
|
2017-03-05 02:18:42 +01:00
|
|
|
self.assert_in_response('Invalid address', result)
|
2017-01-20 12:27:38 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_post_same_email(self) -> None:
|
2017-05-25 01:40:26 +02:00
|
|
|
data = {'email': self.example_email("hamlet")}
|
|
|
|
email = self.example_email("hamlet")
|
2017-01-20 12:27:38 +01:00
|
|
|
self.login(email)
|
2017-07-31 20:44:52 +02:00
|
|
|
url = '/json/settings'
|
|
|
|
result = self.client_patch(url, data)
|
2017-01-20 12:27:38 +01:00
|
|
|
self.assertEqual('success', result.json()['result'])
|
|
|
|
self.assertEqual('', result.json()['msg'])
|