2017-09-24 00:44:29 +02:00
|
|
|
from django.conf import settings
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.shortcuts import render, redirect
|
|
|
|
from django.views.decorators.http import require_GET
|
2017-10-25 01:58:05 +02:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2017-09-24 00:44:29 +02:00
|
|
|
|
|
|
|
from zerver.models import get_realm, get_user
|
|
|
|
from zerver.lib.notifications import enqueue_welcome_emails
|
2017-10-25 01:58:05 +02:00
|
|
|
from zerver.lib.response import json_success
|
|
|
|
from zproject.email_backends import (
|
|
|
|
get_forward_address,
|
|
|
|
set_forward_address,
|
|
|
|
)
|
2018-05-09 20:38:25 +02:00
|
|
|
import urllib
|
2017-09-24 00:44:29 +02:00
|
|
|
from confirmation.models import Confirmation, confirmation_url
|
|
|
|
|
|
|
|
import os
|
|
|
|
from typing import List, Dict, Any, Optional
|
|
|
|
import datetime
|
2018-11-14 12:50:52 +01:00
|
|
|
import subprocess
|
2017-09-24 00:44:29 +02:00
|
|
|
ZULIP_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../')
|
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def email_page(request: HttpRequest) -> HttpResponse:
|
2017-10-25 01:58:05 +02:00
|
|
|
if request.method == 'POST':
|
|
|
|
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 = ""
|
2017-10-25 01:58:05 +02:00
|
|
|
return render(request, 'zerver/email_log.html',
|
|
|
|
{'log': content,
|
|
|
|
'forward_address': get_forward_address()})
|
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)
|
|
|
|
|
|
|
|
@require_GET
|
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.
|
|
|
|
subprocess.check_call(["./tools/inline-email-css"])
|
|
|
|
|
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
|
|
|
|
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")
|
2017-10-02 18:48:45 +02:00
|
|
|
host_kwargs = {'HTTP_HOST': realm.host}
|
2017-09-24 00:44:29 +02:00
|
|
|
|
|
|
|
# Password reset email
|
2017-10-04 21:38:03 +02:00
|
|
|
result = client.post('/accounts/password/reset/', {'email': registered_email}, **host_kwargs)
|
|
|
|
assert result.status_code == 302
|
2017-09-24 00:44:29 +02:00
|
|
|
|
|
|
|
# Confirm account email
|
2017-10-04 21:38:03 +02:00
|
|
|
result = client.post('/accounts/home/', {'email': unregistered_email_1}, **host_kwargs)
|
|
|
|
assert result.status_code == 302
|
2017-09-24 00:44:29 +02:00
|
|
|
|
|
|
|
# Find account email
|
2017-10-04 21:38:03 +02:00
|
|
|
result = client.post('/accounts/find/', {'emails': registered_email}, **host_kwargs)
|
|
|
|
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
|
2017-10-04 21:38:03 +02:00
|
|
|
result = client.post("/json/invites",
|
2017-12-06 22:31:11 +01:00
|
|
|
{"invitee_emails": unregistered_email_2, "stream": ["Denmark"]},
|
2017-10-04 21:38:03 +02:00
|
|
|
**host_kwargs)
|
|
|
|
assert result.status_code == 200
|
2017-09-24 00:44:29 +02:00
|
|
|
|
|
|
|
# Verification for new email
|
2017-11-07 20:48:32 +01:00
|
|
|
result = client.patch('/json/settings',
|
|
|
|
urllib.parse.urlencode({'email': 'hamlets-new@zulip.com'}),
|
|
|
|
**host_kwargs)
|
2017-10-04 21:38:03 +02:00
|
|
|
assert result.status_code == 200
|
2017-09-24 00:44:29 +02:00
|
|
|
|
|
|
|
# Email change successful
|
|
|
|
key = Confirmation.objects.filter(type=Confirmation.EMAIL_CHANGE).latest('id').confirmation_key
|
|
|
|
url = confirmation_url(key, realm.host, Confirmation.EMAIL_CHANGE)
|
|
|
|
user_profile = get_user(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
|
2017-11-20 18:02:49 +01:00
|
|
|
user_profile.email = registered_email
|
2017-11-20 19:52:10 +01:00
|
|
|
user_profile.save(update_fields=['email'])
|
2017-09-24 00:44:29 +02:00
|
|
|
|
|
|
|
# Follow up day1 day2 emails
|
|
|
|
enqueue_welcome_emails(user_profile)
|
2017-09-29 20:38:45 +02:00
|
|
|
return redirect(email_page)
|