2020-06-11 00:54:34 +02:00
|
|
|
import os
|
2023-01-18 05:25:49 +01:00
|
|
|
from contextlib import suppress
|
2022-07-14 21:15:45 +02:00
|
|
|
from typing import Optional
|
2023-12-05 21:14:17 +01:00
|
|
|
from urllib.parse import urlencode
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2017-09-24 00:44:29 +02:00
|
|
|
from django.conf import settings
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.shortcuts import redirect, render
|
2019-08-12 05:44:35 +02:00
|
|
|
from django.views.decorators.http import require_safe
|
2017-09-24 00:44:29 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from confirmation.models import Confirmation, confirmation_url
|
2022-04-14 23:57:15 +02:00
|
|
|
from zerver.actions.realm_settings import do_send_realm_reactivation_email
|
2022-04-14 23:49:26 +02:00
|
|
|
from zerver.actions.user_settings import do_change_user_delivery_email
|
2022-04-14 23:48:28 +02:00
|
|
|
from zerver.actions.users import change_user_is_active
|
2023-06-30 13:27:25 +02:00
|
|
|
from zerver.lib.email_notifications import enqueue_welcome_emails, send_account_registered_email
|
2022-07-14 21:15:45 +02:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2017-10-25 01:58:05 +02:00
|
|
|
from zerver.lib.response import json_success
|
2023-12-15 01:16:00 +01:00
|
|
|
from zerver.models import Realm, get_realm, get_realm_stream
|
|
|
|
from zerver.models.users import get_user_by_delivery_email
|
2022-07-19 22:59:47 +02:00
|
|
|
from zerver.views.invite import INVITATION_LINK_VALIDITY_MINUTES
|
2020-06-11 00:54:34 +02:00
|
|
|
from zproject.email_backends import get_forward_address, set_forward_address
|
2018-12-22 05:41:54 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
ZULIP_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../")
|
2017-09-24 00:44:29 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-07-14 21:15:45 +02:00
|
|
|
@has_request_variables
|
|
|
|
def email_page(
|
|
|
|
request: HttpRequest, forward_address: Optional[str] = REQ(default=None)
|
|
|
|
) -> HttpResponse:
|
2021-02-12 08:20:45 +01:00
|
|
|
if request.method == "POST":
|
2022-07-14 21:15:45 +02:00
|
|
|
assert forward_address is not None
|
|
|
|
set_forward_address(forward_address)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2017-09-24 00:44:29 +02:00
|
|
|
try:
|
|
|
|
with open(settings.EMAIL_CONTENT_LOG_PATH, "r+") as f:
|
|
|
|
content = f.read()
|
|
|
|
except FileNotFoundError:
|
|
|
|
content = ""
|
2021-02-12 08:19:30 +01:00
|
|
|
return render(
|
2021-04-16 17:37:13 +02:00
|
|
|
request,
|
|
|
|
"zerver/development/email_log.html",
|
|
|
|
{"log": content, "forward_address": get_forward_address()},
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
|
2017-09-24 00:44:29 +02:00
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def clear_emails(request: HttpRequest) -> HttpResponse:
|
2023-01-18 05:25:49 +01:00
|
|
|
with suppress(FileNotFoundError):
|
2017-09-24 00:44:29 +02:00
|
|
|
os.remove(settings.EMAIL_CONTENT_LOG_PATH)
|
|
|
|
return redirect(email_page)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-08-12 05:44:35 +02:00
|
|
|
@require_safe
|
2017-11-27 09:28:57 +01:00
|
|
|
def generate_all_emails(request: HttpRequest) -> HttpResponse:
|
2018-08-08 22:19:44 +02:00
|
|
|
# We import the Django test client inside the view function,
|
|
|
|
# because it isn't needed in production elsewhere, and not
|
|
|
|
# importing it saves ~50ms of unnecessary manage.py startup time.
|
2018-11-14 12:50:52 +01:00
|
|
|
|
2018-08-08 22:19:44 +02:00
|
|
|
from django.test import Client
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-08-08 22:19:44 +02:00
|
|
|
client = Client()
|
2017-09-24 00:44:29 +02:00
|
|
|
|
|
|
|
# write fake data for all variables
|
|
|
|
registered_email = "hamlet@zulip.com"
|
|
|
|
unregistered_email_1 = "new-person@zulip.com"
|
|
|
|
unregistered_email_2 = "new-person-2@zulip.com"
|
2022-07-19 22:59:47 +02:00
|
|
|
invite_expires_in_minutes = INVITATION_LINK_VALIDITY_MINUTES
|
2017-09-24 00:44:29 +02:00
|
|
|
realm = get_realm("zulip")
|
2021-02-12 08:20:45 +01:00
|
|
|
other_realm = Realm.objects.exclude(string_id="zulip").first()
|
2018-12-20 21:05:26 +01:00
|
|
|
user = get_user_by_delivery_email(registered_email, realm)
|
2017-09-24 00:44:29 +02:00
|
|
|
|
2018-12-20 21:05:26 +01:00
|
|
|
# Password reset emails
|
|
|
|
# active account in realm
|
2022-06-14 22:34:47 +02:00
|
|
|
result = client.post(
|
|
|
|
"/accounts/password/reset/", {"email": registered_email}, HTTP_HOST=realm.host
|
|
|
|
)
|
2017-10-04 21:38:03 +02:00
|
|
|
assert result.status_code == 302
|
2018-12-20 21:05:26 +01:00
|
|
|
# deactivated user
|
2021-02-14 00:03:40 +01:00
|
|
|
change_user_is_active(user, False)
|
2022-06-14 22:34:47 +02:00
|
|
|
result = client.post(
|
|
|
|
"/accounts/password/reset/", {"email": registered_email}, HTTP_HOST=realm.host
|
|
|
|
)
|
2018-12-20 21:05:26 +01:00
|
|
|
assert result.status_code == 302
|
2021-02-14 00:03:40 +01:00
|
|
|
change_user_is_active(user, True)
|
2018-12-20 21:05:26 +01:00
|
|
|
# account on different realm
|
2021-07-24 18:16:48 +02:00
|
|
|
assert other_realm is not None
|
2021-02-12 08:19:30 +01:00
|
|
|
result = client.post(
|
2021-02-12 08:20:45 +01:00
|
|
|
"/accounts/password/reset/", {"email": registered_email}, HTTP_HOST=other_realm.host
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-12-20 21:05:26 +01:00
|
|
|
assert result.status_code == 302
|
|
|
|
# no account anywhere
|
2021-02-12 08:19:30 +01:00
|
|
|
result = client.post(
|
2022-06-14 22:34:47 +02:00
|
|
|
"/accounts/password/reset/", {"email": unregistered_email_1}, HTTP_HOST=realm.host
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-12-20 21:05:26 +01:00
|
|
|
assert result.status_code == 302
|
2017-09-24 00:44:29 +02:00
|
|
|
|
|
|
|
# Confirm account email
|
2022-06-14 22:34:47 +02:00
|
|
|
result = client.post("/accounts/home/", {"email": unregistered_email_1}, HTTP_HOST=realm.host)
|
2017-10-04 21:38:03 +02:00
|
|
|
assert result.status_code == 302
|
2017-09-24 00:44:29 +02:00
|
|
|
|
|
|
|
# Find account email
|
2022-06-14 22:34:47 +02:00
|
|
|
result = client.post("/accounts/find/", {"emails": registered_email}, HTTP_HOST=realm.host)
|
2017-10-04 21:38:03 +02:00
|
|
|
assert result.status_code == 302
|
2017-09-24 00:44:29 +02:00
|
|
|
|
|
|
|
# New login email
|
2017-11-21 21:19:20 +01:00
|
|
|
logged_in = client.login(dev_auth_username=registered_email, realm=realm)
|
2017-10-04 21:38:03 +02:00
|
|
|
assert logged_in
|
2017-09-24 00:44:29 +02:00
|
|
|
|
|
|
|
# New user invite and reminder emails
|
2019-03-18 18:59:09 +01:00
|
|
|
stream = get_realm_stream("Denmark", user.realm.id)
|
2021-02-12 08:19:30 +01:00
|
|
|
result = client.post(
|
|
|
|
"/json/invites",
|
2021-04-05 18:42:45 +02:00
|
|
|
{
|
|
|
|
"invitee_emails": unregistered_email_2,
|
2022-02-10 11:52:34 +01:00
|
|
|
"invite_expires_in_minutes": invite_expires_in_minutes,
|
2021-04-05 18:42:45 +02:00
|
|
|
"stream_ids": orjson.dumps([stream.id]).decode(),
|
|
|
|
},
|
2022-06-14 22:34:47 +02:00
|
|
|
HTTP_HOST=realm.host,
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2017-10-04 21:38:03 +02:00
|
|
|
assert result.status_code == 200
|
2017-09-24 00:44:29 +02:00
|
|
|
|
|
|
|
# Verification for new email
|
2021-02-12 08:19:30 +01:00
|
|
|
result = client.patch(
|
2022-06-24 05:18:42 +02:00
|
|
|
"/json/settings",
|
2023-12-05 21:14:17 +01:00
|
|
|
urlencode({"email": "hamlets-new@zulip.com"}),
|
2022-06-24 05:18:42 +02:00
|
|
|
content_type="application/x-www-form-urlencoded",
|
2022-06-14 22:34:47 +02:00
|
|
|
HTTP_HOST=realm.host,
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2017-10-04 21:38:03 +02:00
|
|
|
assert result.status_code == 200
|
2017-09-24 00:44:29 +02:00
|
|
|
|
|
|
|
# Email change successful
|
2021-02-12 08:20:45 +01:00
|
|
|
key = Confirmation.objects.filter(type=Confirmation.EMAIL_CHANGE).latest("id").confirmation_key
|
2020-06-14 01:36:12 +02:00
|
|
|
url = confirmation_url(key, realm, Confirmation.EMAIL_CHANGE)
|
2018-12-07 00:05:57 +01:00
|
|
|
user_profile = get_user_by_delivery_email(registered_email, realm)
|
2017-10-04 21:38:03 +02:00
|
|
|
result = client.get(url)
|
2017-11-07 20:48:32 +01:00
|
|
|
assert result.status_code == 200
|
2017-11-20 19:52:10 +01:00
|
|
|
|
|
|
|
# Reset the email value so we can run this again
|
2018-12-19 20:07:32 +01:00
|
|
|
do_change_user_delivery_email(user_profile, registered_email)
|
2017-09-24 00:44:29 +02:00
|
|
|
|
2023-09-10 17:12:59 +02:00
|
|
|
# Initial email with new account information for normal user
|
2023-06-30 13:27:25 +02:00
|
|
|
send_account_registered_email(user_profile)
|
|
|
|
|
2023-09-10 17:12:59 +02:00
|
|
|
# Onboarding emails for normal user
|
2017-09-24 00:44:29 +02:00
|
|
|
enqueue_welcome_emails(user_profile)
|
2018-11-14 12:46:56 +01:00
|
|
|
|
2023-06-30 13:27:25 +02:00
|
|
|
# Initial email with new account information for admin user
|
|
|
|
send_account_registered_email(get_user_by_delivery_email("iago@zulip.com", realm))
|
|
|
|
|
2023-09-10 17:12:59 +02:00
|
|
|
# Onboarding emails for admin user
|
2023-04-24 17:46:49 +02:00
|
|
|
enqueue_welcome_emails(get_user_by_delivery_email("iago@zulip.com", realm), realm_creation=True)
|
2018-11-14 12:46:56 +01:00
|
|
|
|
2018-12-20 21:05:26 +01:00
|
|
|
# Realm reactivation email
|
2020-12-04 11:46:51 +01:00
|
|
|
do_send_realm_reactivation_email(realm, acting_user=None)
|
2018-12-20 21:05:26 +01:00
|
|
|
|
2017-09-29 20:38:45 +02:00
|
|
|
return redirect(email_page)
|