2020-06-11 00:54:34 +02:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import urllib
|
|
|
|
|
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
|
2021-02-14 00:03:40 +01:00
|
|
|
from zerver.lib.actions import (
|
|
|
|
change_user_is_active,
|
|
|
|
do_change_user_delivery_email,
|
|
|
|
do_send_realm_reactivation_email,
|
|
|
|
)
|
2019-03-15 18:51:39 +01:00
|
|
|
from zerver.lib.email_notifications import enqueue_welcome_emails
|
2017-10-25 01:58:05 +02:00
|
|
|
from zerver.lib.response import json_success
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import Realm, get_realm, get_realm_stream, get_user_by_delivery_email
|
|
|
|
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
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def email_page(request: HttpRequest) -> HttpResponse:
|
2021-02-12 08:20:45 +01:00
|
|
|
if request.method == "POST":
|
2017-10-25 01:58:05 +02:00
|
|
|
set_forward_address(request.POST["forward_address"])
|
|
|
|
return json_success()
|
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:
|
2017-09-24 00:44:29 +02:00
|
|
|
try:
|
|
|
|
os.remove(settings.EMAIL_CONTENT_LOG_PATH)
|
2017-10-05 19:34:12 +02:00
|
|
|
except FileNotFoundError: # nocoverage
|
2017-09-24 00:44:29 +02:00
|
|
|
pass
|
|
|
|
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-11-14 12:50:52 +01:00
|
|
|
if not settings.TEST_SUITE: # nocoverage
|
|
|
|
# It's really convenient to automatically inline the email CSS
|
|
|
|
# here, since that saves a step when testing out changes to
|
|
|
|
# the email CSS. But we don't run this inside the test suite,
|
|
|
|
# because by role, the tests shouldn't be doing a provision-like thing.
|
2020-04-06 02:40:19 +02:00
|
|
|
subprocess.check_call(["./scripts/setup/inline_email_css.py"])
|
2018-11-14 12:50:52 +01:00
|
|
|
|
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"
|
|
|
|
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)
|
2021-02-12 08:20:45 +01:00
|
|
|
host_kwargs = {"HTTP_HOST": realm.host}
|
2017-09-24 00:44:29 +02:00
|
|
|
|
2018-12-20 21:05:26 +01:00
|
|
|
# Password reset emails
|
|
|
|
# active account in realm
|
2021-02-12 08:20:45 +01:00
|
|
|
result = client.post("/accounts/password/reset/", {"email": registered_email}, **host_kwargs)
|
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)
|
2021-02-12 08:20:45 +01:00
|
|
|
result = client.post("/accounts/password/reset/", {"email": registered_email}, **host_kwargs)
|
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-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(
|
2021-02-12 08:20:45 +01:00
|
|
|
"/accounts/password/reset/", {"email": unregistered_email_1}, **host_kwargs
|
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
|
2021-02-12 08:20:45 +01:00
|
|
|
result = client.post("/accounts/home/", {"email": unregistered_email_1}, **host_kwargs)
|
2017-10-04 21:38:03 +02:00
|
|
|
assert result.status_code == 302
|
2017-09-24 00:44:29 +02:00
|
|
|
|
|
|
|
# Find account email
|
2021-02-12 08:20:45 +01:00
|
|
|
result = client.post("/accounts/find/", {"emails": registered_email}, **host_kwargs)
|
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",
|
|
|
|
{"invitee_emails": unregistered_email_2, "stream_ids": orjson.dumps([stream.id]).decode()},
|
|
|
|
**host_kwargs,
|
|
|
|
)
|
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(
|
2021-02-12 08:20:45 +01:00
|
|
|
"/json/settings", urllib.parse.urlencode({"email": "hamlets-new@zulip.com"}), **host_kwargs
|
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
|
|
|
|
2018-11-14 12:46:56 +01:00
|
|
|
# Follow up day1 day2 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
|
|
|
|
|
|
|
# Follow up day1 day2 emails for admin user
|
2018-12-07 00:05:57 +01: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)
|