2017-01-20 12:27:38 +01:00
|
|
|
import datetime
|
2021-04-05 18:41:59 +02:00
|
|
|
from unittest import mock
|
2017-01-20 12:27:38 +01:00
|
|
|
|
2021-01-26 04:20:36 +01:00
|
|
|
from django.conf import settings
|
2017-01-20 12:27:38 +01:00
|
|
|
from django.core import mail
|
|
|
|
from django.utils.timezone import now
|
|
|
|
|
2021-04-05 18:41:59 +02:00
|
|
|
from confirmation.models import (
|
|
|
|
Confirmation,
|
|
|
|
confirmation_url,
|
|
|
|
create_confirmation_link,
|
|
|
|
generate_key,
|
|
|
|
)
|
2021-11-04 00:18:32 +01:00
|
|
|
from zerver.lib.actions import (
|
|
|
|
do_deactivate_realm,
|
|
|
|
do_deactivate_user,
|
|
|
|
do_set_realm_property,
|
|
|
|
do_start_email_change_process,
|
|
|
|
)
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
from zerver.models import (
|
|
|
|
EmailChangeStatus,
|
|
|
|
Realm,
|
|
|
|
UserProfile,
|
|
|
|
get_realm,
|
|
|
|
get_user,
|
|
|
|
get_user_by_delivery_email,
|
|
|
|
get_user_profile_by_id,
|
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:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("hamlet")
|
2017-01-20 12:27:38 +01:00
|
|
|
key = generate_key()
|
2020-06-14 01:36:12 +02:00
|
|
|
url = confirmation_url(key, None, Confirmation.EMAIL_CHANGE)
|
2017-01-20 12:27:38 +01:00
|
|
|
response = self.client_get(url)
|
2021-02-12 08:19:30 +01: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:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("hamlet")
|
|
|
|
key = "invalid_key"
|
2020-06-14 01:36:12 +02:00
|
|
|
url = confirmation_url(key, None, 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:
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("hamlet")
|
2017-05-07 21:25:59 +02:00
|
|
|
old_email = user_profile.email
|
2021-02-12 08:20:45 +01:00
|
|
|
new_email = "hamlet-new@zulip.com"
|
|
|
|
self.login("hamlet")
|
2021-02-12 08:19:30 +01:00
|
|
|
obj = EmailChangeStatus.objects.create(
|
|
|
|
new_email=new_email,
|
|
|
|
old_email=old_email,
|
|
|
|
user_profile=user_profile,
|
|
|
|
realm=user_profile.realm,
|
|
|
|
)
|
2017-01-20 12:27:38 +01:00
|
|
|
date_sent = now() - datetime.timedelta(days=2)
|
2021-04-05 18:41:59 +02:00
|
|
|
with mock.patch("confirmation.models.timezone_now", return_value=date_sent):
|
|
|
|
url = create_confirmation_link(obj, Confirmation.EMAIL_CHANGE)
|
|
|
|
|
2017-01-20 12:27:38 +01:00
|
|
|
response = self.client_get(url)
|
2021-02-12 08:19:30 +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:
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("hamlet")
|
2020-03-12 13:51:54 +01:00
|
|
|
do_set_realm_property(
|
|
|
|
user_profile.realm,
|
2021-02-12 08:20:45 +01:00
|
|
|
"email_address_visibility",
|
2020-03-12 13:51:54 +01:00
|
|
|
Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE,
|
2021-03-01 11:33:24 +01:00
|
|
|
acting_user=None,
|
2020-03-12 13:51:54 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
old_email = user_profile.delivery_email
|
2021-02-12 08:20:45 +01:00
|
|
|
new_email = "hamlet-new@zulip.com"
|
|
|
|
new_realm = get_realm("zulip")
|
|
|
|
self.login("hamlet")
|
2021-02-12 08:19:30 +01:00
|
|
|
obj = EmailChangeStatus.objects.create(
|
|
|
|
new_email=new_email,
|
|
|
|
old_email=old_email,
|
|
|
|
user_profile=user_profile,
|
|
|
|
realm=user_profile.realm,
|
|
|
|
)
|
2021-04-05 18:41:59 +02:00
|
|
|
url = create_confirmation_link(obj, 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)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assert_in_success_response(
|
|
|
|
["This confirms that the email address for your Zulip"], response
|
|
|
|
)
|
2018-08-02 08:47:13 +02:00
|
|
|
user_profile = get_user_by_delivery_email(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)
|
|
|
|
|
2021-11-04 00:18:32 +01:00
|
|
|
def test_change_email_deactivated_user_realm(self) -> None:
|
|
|
|
data = {"email": "hamlet-new@zulip.com"}
|
|
|
|
user_profile = self.example_user("hamlet")
|
|
|
|
self.login_user(user_profile)
|
|
|
|
url = "/json/settings"
|
|
|
|
self.assert_length(mail.outbox, 0)
|
|
|
|
result = self.client_patch(url, data)
|
|
|
|
self.assert_length(mail.outbox, 1)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
email_message = mail.outbox[0]
|
|
|
|
self.assertEqual(
|
|
|
|
email_message.subject,
|
|
|
|
"Verify your new email address",
|
|
|
|
)
|
|
|
|
body = email_message.body
|
|
|
|
self.assertIn("We received a request to change the email", body)
|
|
|
|
|
|
|
|
activation_url = [s for s in body.split("\n") if s][2]
|
|
|
|
|
|
|
|
do_deactivate_user(user_profile, acting_user=None)
|
|
|
|
response = self.client_get(activation_url)
|
|
|
|
self.assertEqual(response.status_code, 401)
|
|
|
|
|
|
|
|
do_deactivate_realm(user_profile.realm, acting_user=None)
|
|
|
|
|
|
|
|
response = self.client_get(activation_url)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertTrue(response["Location"].endswith("/accounts/deactivated/"))
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_start_email_change_process(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("hamlet")
|
|
|
|
do_start_email_change_process(user_profile, "hamlet-new@zulip.com")
|
2017-01-20 12:27:38 +01:00
|
|
|
self.assertEqual(EmailChangeStatus.objects.count(), 1)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_end_to_end_flow(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
data = {"email": "hamlet-new@zulip.com"}
|
|
|
|
self.login("hamlet")
|
|
|
|
url = "/json/settings"
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(mail.outbox, 0)
|
2017-07-31 20:44:52 +02:00
|
|
|
result = self.client_patch(url, data)
|
2021-07-15 18:31:34 +02:00
|
|
|
self.assert_json_success(result)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(mail.outbox, 1)
|
2017-01-20 12:27:38 +01:00
|
|
|
email_message = mail.outbox[0]
|
|
|
|
self.assertEqual(
|
|
|
|
email_message.subject,
|
2021-02-12 08:20:45 +01:00
|
|
|
"Verify your new email address",
|
2017-01-20 12:27:38 +01:00
|
|
|
)
|
|
|
|
body = email_message.body
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertIn("We received a request to change the email", body)
|
2021-01-26 04:20:36 +01:00
|
|
|
self.assertEqual(self.email_envelope_from(email_message), settings.NOREPLY_EMAIL_ADDRESS)
|
2020-06-05 23:26:35 +02:00
|
|
|
self.assertRegex(
|
2021-01-26 04:20:36 +01:00
|
|
|
self.email_display_from(email_message),
|
2020-06-05 23:26:35 +02:00
|
|
|
fr"^Zulip Account Security <{self.TOKENIZED_NOREPLY_REGEX}>\Z",
|
|
|
|
)
|
2017-01-20 12:27:38 +01:00
|
|
|
|
2020-06-14 13:32:38 +02:00
|
|
|
self.assertEqual(email_message.extra_headers["List-Id"], "Zulip Dev <zulip.testserver>")
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
activation_url = [s for s in body.split("\n") if s][2]
|
2017-01-20 12:27:38 +01:00
|
|
|
response = self.client_get(activation_url)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assert_in_success_response(["This confirms that the email address"], 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"})
|
2021-07-15 18:31:34 +02:00
|
|
|
self.assert_json_success(result)
|
2017-08-05 19:42:59 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_unauthorized_email_change(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
data = {"email": "hamlet-new@zulip.com"}
|
|
|
|
user_profile = self.example_user("hamlet")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2021-03-01 11:33:24 +01:00
|
|
|
do_set_realm_property(
|
|
|
|
user_profile.realm,
|
|
|
|
"email_changes_disabled",
|
|
|
|
True,
|
|
|
|
acting_user=None,
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
url = "/json/settings"
|
2017-07-31 20:44:52 +02:00
|
|
|
result = self.client_patch(url, data)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(mail.outbox, 0)
|
2017-03-04 06:39:45 +01:00
|
|
|
self.assertEqual(result.status_code, 400)
|
2021-02-12 08:19:30 +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.
|
2021-02-12 08:20:45 +01:00
|
|
|
data = {"email": "iago-new@zulip.com"}
|
|
|
|
self.login("iago")
|
|
|
|
url = "/json/settings"
|
2018-02-02 16:54:26 +01:00
|
|
|
result = self.client_patch(url, data)
|
2021-07-15 18:31:34 +02:00
|
|
|
self.assert_json_success(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:
|
2021-02-12 08:20:45 +01:00
|
|
|
data = {"email": "cordelia@zulip.com"}
|
|
|
|
user_profile = self.example_user("hamlet")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2017-09-26 20:15:37 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
url = "/json/settings"
|
2017-09-26 20:15:37 +02:00
|
|
|
result = self.client_patch(url, data)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(mail.outbox, 0)
|
2017-09-26 20:15:37 +02:00
|
|
|
self.assertEqual(result.status_code, 400)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assert_in_response("Already has an account", result)
|
2017-09-26 20:15:37 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_unauthorized_email_change_from_email_confirmation_link(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
data = {"email": "hamlet-new@zulip.com"}
|
|
|
|
user_profile = self.example_user("hamlet")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2021-02-12 08:20:45 +01:00
|
|
|
url = "/json/settings"
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(mail.outbox, 0)
|
2017-07-31 20:44:52 +02:00
|
|
|
result = self.client_patch(url, data)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(mail.outbox, 1)
|
2021-07-15 18:31:34 +02:00
|
|
|
self.assert_json_success(result)
|
2017-03-04 06:39:45 +01:00
|
|
|
email_message = mail.outbox[0]
|
|
|
|
self.assertEqual(
|
|
|
|
email_message.subject,
|
2021-02-12 08:20:45 +01:00
|
|
|
"Verify your new email address",
|
2017-03-04 06:39:45 +01:00
|
|
|
)
|
|
|
|
body = email_message.body
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertIn("We received a request to change the email", body)
|
2017-03-04 06:39:45 +01:00
|
|
|
|
2021-03-01 11:33:24 +01:00
|
|
|
do_set_realm_property(
|
|
|
|
user_profile.realm,
|
|
|
|
"email_changes_disabled",
|
|
|
|
True,
|
|
|
|
acting_user=None,
|
|
|
|
)
|
2017-03-04 06:39:45 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
activation_url = [s for s in body.split("\n") if s][2]
|
2017-03-04 06:39:45 +01:00
|
|
|
response = self.client_get(activation_url)
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 400)
|
2021-02-12 08:19:30 +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:
|
2021-02-12 08:20:45 +01:00
|
|
|
data = {"email": "hamlet-new"}
|
|
|
|
self.login("hamlet")
|
|
|
|
url = "/json/settings"
|
2017-07-31 20:44:52 +02:00
|
|
|
result = self.client_patch(url, data)
|
2021-02-12 08:20:45 +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:
|
2021-02-12 08:20:45 +01:00
|
|
|
data = {"email": self.example_email("hamlet")}
|
|
|
|
self.login("hamlet")
|
|
|
|
url = "/json/settings"
|
2017-07-31 20:44:52 +02:00
|
|
|
result = self.client_patch(url, data)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual("success", result.json()["result"])
|
|
|
|
self.assertEqual("", result.json()["msg"])
|
2018-12-06 23:17:46 +01:00
|
|
|
|
|
|
|
def test_change_delivery_email_end_to_end_with_admins_visibility(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("hamlet")
|
2021-02-12 08:19:30 +01:00
|
|
|
do_set_realm_property(
|
2021-03-01 11:33:24 +01:00
|
|
|
user_profile.realm,
|
|
|
|
"email_address_visibility",
|
|
|
|
Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS,
|
|
|
|
acting_user=None,
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-12-06 23:17:46 +01:00
|
|
|
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2020-03-12 14:17:25 +01:00
|
|
|
old_email = user_profile.delivery_email
|
2021-02-12 08:20:45 +01:00
|
|
|
new_email = "hamlet-new@zulip.com"
|
2021-02-12 08:19:30 +01:00
|
|
|
obj = EmailChangeStatus.objects.create(
|
|
|
|
new_email=new_email,
|
|
|
|
old_email=old_email,
|
|
|
|
user_profile=user_profile,
|
|
|
|
realm=user_profile.realm,
|
|
|
|
)
|
2021-04-05 18:41:59 +02:00
|
|
|
url = create_confirmation_link(obj, Confirmation.EMAIL_CHANGE)
|
2018-12-06 23:17:46 +01:00
|
|
|
response = self.client_get(url)
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assert_in_success_response(
|
|
|
|
["This confirms that the email address for your Zulip"], response
|
|
|
|
)
|
2018-12-06 23:17:46 +01:00
|
|
|
user_profile = get_user_profile_by_id(user_profile.id)
|
|
|
|
self.assertEqual(user_profile.delivery_email, new_email)
|
2020-06-09 00:25:09 +02:00
|
|
|
self.assertEqual(user_profile.email, f"user{user_profile.id}@zulip.testserver")
|
2018-12-06 23:17:46 +01:00
|
|
|
obj.refresh_from_db()
|
|
|
|
self.assertEqual(obj.status, 1)
|
|
|
|
with self.assertRaises(UserProfile.DoesNotExist):
|
|
|
|
get_user(old_email, user_profile.realm)
|
|
|
|
with self.assertRaises(UserProfile.DoesNotExist):
|
|
|
|
get_user_by_delivery_email(old_email, user_profile.realm)
|
|
|
|
self.assertEqual(get_user_by_delivery_email(new_email, user_profile.realm), user_profile)
|