2014-01-31 21:08:40 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-12-01 08:54:21 +01:00
|
|
|
import datetime
|
2014-07-03 18:18:00 +02:00
|
|
|
from django.conf import settings
|
2017-03-31 08:41:14 +02:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from django.contrib.sites.models import Site
|
2016-06-04 20:14:05 +02:00
|
|
|
from django.http import HttpResponse
|
2017-09-16 19:51:26 +02:00
|
|
|
from django.test import TestCase, override_settings
|
2017-04-15 04:03:56 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2014-01-31 21:08:40 +01:00
|
|
|
|
2017-03-08 12:38:56 +01:00
|
|
|
from mock import patch, MagicMock
|
2016-12-13 10:59:54 +01:00
|
|
|
from zerver.lib.test_helpers import MockLDAP
|
2016-10-31 12:18:13 +01:00
|
|
|
|
2017-08-10 22:34:17 +02:00
|
|
|
from confirmation.models import Confirmation, create_confirmation_link, MultiuseInvite, \
|
2017-11-01 21:07:39 +01:00
|
|
|
generate_key, confirmation_url, get_object_from_key, ConfirmationKeyException
|
2017-10-21 03:15:12 +02:00
|
|
|
from confirmation import settings as confirmation_settings
|
2014-01-31 21:08:40 +01:00
|
|
|
|
2017-03-08 12:38:56 +01:00
|
|
|
from zerver.forms import HomepageForm, WRONG_SUBDOMAIN_ERROR
|
2017-11-16 22:12:31 +01:00
|
|
|
from zerver.lib.actions import do_change_password
|
2017-10-27 00:27:59 +02:00
|
|
|
from zerver.views.auth import login_or_register_remote_user, \
|
|
|
|
redirect_and_log_into_subdomain
|
2016-10-12 05:13:32 +02:00
|
|
|
from zerver.views.invite import get_invitee_emails_set
|
2017-05-01 01:42:17 +02:00
|
|
|
from zerver.views.registration import confirmation_key, \
|
2017-10-27 00:27:59 +02:00
|
|
|
send_registration_completion_email
|
2017-04-20 08:25:15 +02:00
|
|
|
|
2014-01-31 21:08:40 +01:00
|
|
|
from zerver.models import (
|
2017-10-28 20:26:11 +02:00
|
|
|
get_realm, get_prereg_user_by_email, get_user, get_stream_recipient,
|
2017-05-08 20:25:03 +02:00
|
|
|
PreregistrationUser, Realm, RealmDomain, Recipient, Message,
|
2017-07-02 21:10:41 +02:00
|
|
|
ScheduledEmail, UserProfile, UserMessage,
|
|
|
|
Stream, Subscription, flush_per_request_caches
|
2014-01-31 21:08:40 +01:00
|
|
|
)
|
|
|
|
from zerver.lib.actions import (
|
|
|
|
set_default_streams,
|
2017-02-27 00:29:33 +01:00
|
|
|
do_change_is_admin,
|
2017-04-24 12:19:54 +02:00
|
|
|
get_stream,
|
|
|
|
do_create_realm,
|
2017-11-16 23:00:04 +01:00
|
|
|
do_create_default_stream_group,
|
|
|
|
do_add_default_stream,
|
2014-01-31 21:08:40 +01:00
|
|
|
)
|
2017-07-11 06:13:23 +02:00
|
|
|
from zerver.lib.send_email import send_email, send_future_email, FromAddress
|
2016-09-13 22:49:03 +02:00
|
|
|
from zerver.lib.initial_password import initial_password
|
2017-03-21 18:08:40 +01:00
|
|
|
from zerver.lib.actions import (
|
|
|
|
do_deactivate_realm,
|
2017-08-25 08:14:55 +02:00
|
|
|
do_deactivate_user,
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property,
|
|
|
|
add_new_user_history,
|
|
|
|
)
|
2017-04-27 22:58:53 +02:00
|
|
|
from zerver.lib.mobile_auth_otp import xor_hex_strings, ascii_to_hex, \
|
|
|
|
otp_encrypt_api_key, is_valid_otp, hex_to_ascii, otp_decrypt_api_key
|
2017-05-02 02:07:01 +02:00
|
|
|
from zerver.lib.notifications import enqueue_welcome_emails, \
|
2017-05-04 03:11:47 +02:00
|
|
|
one_click_unsubscribe_link
|
2017-10-19 07:42:03 +02:00
|
|
|
from zerver.lib.subdomains import is_root_domain_available
|
2017-02-12 21:21:31 +01:00
|
|
|
from zerver.lib.test_helpers import find_pattern_in_email, find_key_by_email, queries_captured, \
|
2017-10-27 02:45:38 +02:00
|
|
|
HostRequestMock, load_subdomain_token
|
2016-11-10 19:30:09 +01:00
|
|
|
from zerver.lib.test_classes import (
|
|
|
|
ZulipTestCase,
|
|
|
|
)
|
2014-01-31 21:08:40 +01:00
|
|
|
from zerver.lib.test_runner import slow
|
2017-03-08 11:43:35 +01:00
|
|
|
from zerver.lib.sessions import get_session_dict_user
|
2016-12-01 08:54:21 +01:00
|
|
|
from zerver.context_processors import common_context
|
2014-01-31 21:08:40 +01:00
|
|
|
|
2017-05-02 07:17:15 +02:00
|
|
|
from collections import defaultdict
|
2014-01-31 21:08:40 +01:00
|
|
|
import re
|
2017-08-17 18:28:21 +02:00
|
|
|
import smtplib
|
2014-01-31 21:08:40 +01:00
|
|
|
import ujson
|
|
|
|
|
2017-07-10 06:10:34 +02:00
|
|
|
from typing import Any, Dict, List, Optional, Set, Text
|
2017-02-12 21:21:31 +01:00
|
|
|
|
2017-11-05 05:30:31 +01:00
|
|
|
import urllib
|
2016-12-07 10:23:36 +01:00
|
|
|
import os
|
2014-01-31 21:08:40 +01:00
|
|
|
|
2017-04-20 08:30:50 +02:00
|
|
|
class RedirectAndLogIntoSubdomainTestCase(ZulipTestCase):
|
|
|
|
def test_cookie_data(self):
|
|
|
|
# type: () -> None
|
|
|
|
realm = Realm.objects.all().first()
|
|
|
|
name = 'Hamlet'
|
2017-05-25 01:40:26 +02:00
|
|
|
email = self.example_email("hamlet")
|
2017-04-20 08:30:50 +02:00
|
|
|
response = redirect_and_log_into_subdomain(realm, name, email)
|
2017-10-27 02:45:38 +02:00
|
|
|
data = load_subdomain_token(response)
|
2017-04-20 08:30:50 +02:00
|
|
|
self.assertDictEqual(data, {'name': name, 'email': email,
|
|
|
|
'subdomain': realm.subdomain,
|
|
|
|
'is_signup': False})
|
|
|
|
|
|
|
|
response = redirect_and_log_into_subdomain(realm, name, email,
|
|
|
|
is_signup=True)
|
2017-10-27 02:45:38 +02:00
|
|
|
data = load_subdomain_token(response)
|
2017-04-20 08:30:50 +02:00
|
|
|
self.assertDictEqual(data, {'name': name, 'email': email,
|
|
|
|
'subdomain': realm.subdomain,
|
|
|
|
'is_signup': True})
|
|
|
|
|
2017-08-25 06:32:57 +02:00
|
|
|
class DeactivationNoticeTestCase(ZulipTestCase):
|
|
|
|
def test_redirection_for_deactivated_realm(self):
|
|
|
|
# type: () -> None
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
realm.deactivated = True
|
|
|
|
realm.save(update_fields=["deactivated"])
|
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
for url in ('/register/', '/login/'):
|
|
|
|
result = self.client_get(url)
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertIn('deactivated', result.url)
|
2017-08-25 06:32:57 +02:00
|
|
|
|
|
|
|
def test_redirection_for_active_realm(self):
|
|
|
|
# type: () -> None
|
2017-10-02 22:43:43 +02:00
|
|
|
for url in ('/register/', '/login/'):
|
|
|
|
result = self.client_get(url)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
2017-08-25 06:32:57 +02:00
|
|
|
|
|
|
|
def test_deactivation_notice_when_realm_is_active(self):
|
|
|
|
# type: () -> None
|
|
|
|
result = self.client_get('/accounts/deactivated/')
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertIn('login', result.url)
|
|
|
|
|
|
|
|
def test_deactivation_notice_when_deactivated(self):
|
|
|
|
# type: () -> None
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
realm.deactivated = True
|
|
|
|
realm.save(update_fields=["deactivated"])
|
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
result = self.client_get('/accounts/deactivated/')
|
|
|
|
self.assertIn("Zulip Dev, has been deactivated.", result.content.decode())
|
2017-08-25 06:32:57 +02:00
|
|
|
|
2016-11-11 05:33:30 +01:00
|
|
|
class AddNewUserHistoryTest(ZulipTestCase):
|
|
|
|
def test_add_new_user_history_race(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""Sends a message during user creation"""
|
|
|
|
# Create a user who hasn't had historical messages added
|
2016-12-08 01:43:15 +01:00
|
|
|
stream_dict = {
|
|
|
|
"Denmark": {"description": "A Scandinavian country", "invite_only": False},
|
|
|
|
"Verona": {"description": "A city in Italy", "invite_only": False}
|
|
|
|
} # type: Dict[Text, Dict[Text, Any]]
|
2017-05-24 02:42:31 +02:00
|
|
|
realm = get_realm('zulip')
|
|
|
|
set_default_streams(realm, stream_dict)
|
2016-11-11 05:33:30 +01:00
|
|
|
with patch("zerver.lib.actions.add_new_user_history"):
|
2017-05-24 02:42:31 +02:00
|
|
|
self.register(self.nonreg_email('test'), "test")
|
|
|
|
user_profile = self.nonreg_user('test')
|
2016-11-11 05:33:30 +01:00
|
|
|
|
|
|
|
subs = Subscription.objects.select_related("recipient").filter(
|
|
|
|
user_profile=user_profile, recipient__type=Recipient.STREAM)
|
|
|
|
streams = Stream.objects.filter(id__in=[sub.recipient.type_id for sub in subs])
|
2017-10-28 18:10:38 +02:00
|
|
|
self.send_stream_message(self.example_email('hamlet'), streams[0].name, "test")
|
2016-11-11 05:33:30 +01:00
|
|
|
add_new_user_history(user_profile, streams)
|
|
|
|
|
2016-09-13 22:49:03 +02:00
|
|
|
class PasswordResetTest(ZulipTestCase):
|
|
|
|
"""
|
|
|
|
Log in, reset password, log out, log in with new password.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_password_reset(self):
|
|
|
|
# type: () -> None
|
2017-05-25 01:40:26 +02:00
|
|
|
email = self.example_email("hamlet")
|
2016-09-13 22:49:03 +02:00
|
|
|
old_password = initial_password(email)
|
|
|
|
|
|
|
|
self.login(email)
|
|
|
|
|
2016-12-01 08:54:21 +01:00
|
|
|
# test password reset template
|
|
|
|
result = self.client_get('/accounts/password/reset/')
|
2017-04-20 21:02:56 +02:00
|
|
|
self.assert_in_response('Reset your password', result)
|
2016-12-01 08:54:21 +01:00
|
|
|
|
2016-09-13 22:49:03 +02:00
|
|
|
# start the password reset process by supplying an email address
|
|
|
|
result = self.client_post('/accounts/password/reset/', {'email': email})
|
|
|
|
|
|
|
|
# check the redirect link telling you to check mail for password reset link
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2016-09-13 22:49:03 +02:00
|
|
|
self.assertTrue(result["Location"].endswith(
|
2017-01-24 07:06:13 +01:00
|
|
|
"/accounts/password/reset/done/"))
|
2016-09-13 22:49:03 +02:00
|
|
|
result = self.client_get(result["Location"])
|
|
|
|
|
|
|
|
self.assert_in_response("Check your email to finish the process.", result)
|
|
|
|
|
2017-07-05 21:29:27 +02:00
|
|
|
# Check that the password reset email is from a noreply address.
|
|
|
|
from django.core.mail import outbox
|
2017-07-11 20:25:50 +02:00
|
|
|
from_email = outbox[0].from_email
|
|
|
|
self.assertIn("Zulip Account Security", from_email)
|
|
|
|
self.assertIn(FromAddress.NOREPLY, from_email)
|
2017-07-05 21:29:27 +02:00
|
|
|
|
2016-09-23 04:23:48 +02:00
|
|
|
# Visit the password reset link.
|
2017-10-30 22:56:14 +01:00
|
|
|
password_reset_url = self.get_confirmation_url_from_outbox(
|
|
|
|
email, url_pattern=settings.EXTERNAL_HOST + "(\S+)")
|
2016-09-13 22:49:03 +02:00
|
|
|
result = self.client_get(password_reset_url)
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(result.status_code, 200)
|
2016-09-13 22:49:03 +02:00
|
|
|
|
|
|
|
# Reset your password
|
|
|
|
result = self.client_post(password_reset_url,
|
|
|
|
{'new_password1': 'new_password',
|
|
|
|
'new_password2': 'new_password'})
|
|
|
|
|
|
|
|
# password reset succeeded
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2016-09-13 22:49:03 +02:00
|
|
|
self.assertTrue(result["Location"].endswith("/password/done/"))
|
|
|
|
|
|
|
|
# log back in with new password
|
|
|
|
self.login(email, password='new_password')
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2016-09-13 22:49:03 +02:00
|
|
|
self.assertEqual(get_session_dict_user(self.client.session), user_profile.id)
|
|
|
|
|
|
|
|
# make sure old password no longer works
|
|
|
|
self.login(email, password=old_password, fails=True)
|
|
|
|
|
2017-04-24 12:19:54 +02:00
|
|
|
def test_invalid_subdomain(self):
|
|
|
|
# type: () -> None
|
2017-05-25 01:40:26 +02:00
|
|
|
email = self.example_email("hamlet")
|
2017-04-24 12:19:54 +02:00
|
|
|
string_id = 'hamlet'
|
|
|
|
name = 'Hamlet'
|
|
|
|
do_create_realm(
|
|
|
|
string_id,
|
|
|
|
name,
|
|
|
|
restricted_to_domain=False,
|
|
|
|
invite_required=False
|
|
|
|
)
|
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
with patch('zerver.forms.get_subdomain', return_value=string_id):
|
|
|
|
# start the password reset process by supplying an email address
|
|
|
|
result = self.client_post(
|
|
|
|
'/accounts/password/reset/', {'email': email})
|
2017-04-24 12:19:54 +02:00
|
|
|
|
|
|
|
# check the redirect link telling you to check mail for password reset link
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertTrue(result["Location"].endswith(
|
|
|
|
"/accounts/password/reset/done/"))
|
|
|
|
result = self.client_get(result["Location"])
|
|
|
|
|
|
|
|
self.assert_in_response("Check your email to finish the process.", result)
|
|
|
|
|
|
|
|
from django.core.mail import outbox
|
|
|
|
self.assertEqual(len(outbox), 1)
|
|
|
|
message = outbox.pop()
|
2017-07-05 21:29:27 +02:00
|
|
|
self.assertIn(FromAddress.NOREPLY, message.from_email)
|
2017-04-24 12:19:54 +02:00
|
|
|
self.assertIn("hamlet@zulip.com does not\nhave an active account in http://",
|
|
|
|
message.body)
|
|
|
|
|
|
|
|
def test_correct_subdomain(self):
|
|
|
|
# type: () -> None
|
2017-05-25 01:40:26 +02:00
|
|
|
email = self.example_email("hamlet")
|
2017-04-24 12:19:54 +02:00
|
|
|
string_id = 'zulip'
|
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
with patch('zerver.forms.get_subdomain', return_value=string_id):
|
|
|
|
# start the password reset process by supplying an email address
|
|
|
|
result = self.client_post(
|
|
|
|
'/accounts/password/reset/', {'email': email})
|
2017-04-24 12:19:54 +02:00
|
|
|
|
|
|
|
# check the redirect link telling you to check mail for password reset link
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertTrue(result["Location"].endswith(
|
|
|
|
"/accounts/password/reset/done/"))
|
|
|
|
result = self.client_get(result["Location"])
|
|
|
|
|
|
|
|
self.assert_in_response("Check your email to finish the process.", result)
|
|
|
|
|
|
|
|
from django.core.mail import outbox
|
|
|
|
self.assertEqual(len(outbox), 1)
|
|
|
|
message = outbox.pop()
|
2017-07-11 20:25:50 +02:00
|
|
|
self.assertIn("Zulip Account Security", message.from_email)
|
2017-07-05 21:29:27 +02:00
|
|
|
self.assertIn(FromAddress.NOREPLY, message.from_email)
|
2017-08-29 04:15:02 +02:00
|
|
|
self.assertIn("Psst. Word on the street is that you",
|
2017-04-24 12:19:54 +02:00
|
|
|
message.body)
|
|
|
|
|
2017-10-24 20:44:01 +02:00
|
|
|
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipLDAPAuthBackend',
|
|
|
|
'zproject.backends.ZulipDummyBackend'))
|
|
|
|
def test_ldap_auth_only(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""If the email auth backend is not enabled, password reset should do nothing"""
|
|
|
|
email = self.example_email("hamlet")
|
|
|
|
result = self.client_post('/accounts/password/reset/', {'email': email})
|
|
|
|
|
|
|
|
# check the redirect link telling you to check mail for password reset link
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertTrue(result["Location"].endswith(
|
|
|
|
"/accounts/password/reset/done/"))
|
|
|
|
result = self.client_get(result["Location"])
|
|
|
|
|
|
|
|
self.assert_in_response("Check your email to finish the process.", result)
|
|
|
|
|
|
|
|
from django.core.mail import outbox
|
|
|
|
self.assertEqual(len(outbox), 0)
|
|
|
|
|
2016-11-14 19:13:59 +01:00
|
|
|
def test_redirect_endpoints(self):
|
|
|
|
# type: () -> None
|
|
|
|
'''
|
|
|
|
These tests are mostly designed to give us 100% URL coverage
|
|
|
|
in our URL coverage reports. Our mechanism for finding URL
|
|
|
|
coverage doesn't handle redirects, so we just have a few quick
|
|
|
|
tests here.
|
|
|
|
'''
|
|
|
|
result = self.client_get('/accounts/password/reset/done/')
|
2016-11-19 21:54:00 +01:00
|
|
|
self.assert_in_success_response(["Check your email"], result)
|
2016-11-14 19:13:59 +01:00
|
|
|
|
|
|
|
result = self.client_get('/accounts/password/done/')
|
2016-11-19 21:54:00 +01:00
|
|
|
self.assert_in_success_response(["We've reset your password!"], result)
|
2016-11-14 19:13:59 +01:00
|
|
|
|
|
|
|
result = self.client_get('/accounts/send_confirm/alice@example.com')
|
2016-11-19 21:54:00 +01:00
|
|
|
self.assert_in_success_response(["Still no email?"], result)
|
2016-11-14 19:13:59 +01:00
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class LoginTest(ZulipTestCase):
|
2014-01-31 21:08:40 +01:00
|
|
|
"""
|
|
|
|
Logging in, registration, and logging out.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_login(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2015-08-19 20:53:55 +02:00
|
|
|
self.assertEqual(get_session_dict_user(self.client.session), user_profile.id)
|
2014-01-31 21:08:40 +01:00
|
|
|
|
2017-11-18 02:23:03 +01:00
|
|
|
def test_login_deactivated_user(self):
|
|
|
|
# type: () -> None
|
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
do_deactivate_user(user_profile)
|
|
|
|
result = self.login_with_return(self.example_email("hamlet"), "xxx")
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
self.assert_in_response("your account has been deactivated.", result)
|
|
|
|
self.assertIsNone(get_session_dict_user(self.client.session))
|
|
|
|
|
2014-01-31 21:08:40 +01:00
|
|
|
def test_login_bad_password(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"), password="wrongpassword", fails=True)
|
2015-08-19 20:53:55 +02:00
|
|
|
self.assertIsNone(get_session_dict_user(self.client.session))
|
2014-01-31 21:08:40 +01:00
|
|
|
|
|
|
|
def test_login_nonexist_user(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2016-06-27 20:36:04 +02:00
|
|
|
result = self.login_with_return("xxx@zulip.com", "xxx")
|
2017-11-18 02:23:03 +01:00
|
|
|
self.assertEqual(result.status_code, 200)
|
2016-07-12 15:41:45 +02:00
|
|
|
self.assert_in_response("Please enter a correct email and password", result)
|
2017-11-18 02:23:03 +01:00
|
|
|
self.assertIsNone(get_session_dict_user(self.client.session))
|
|
|
|
|
|
|
|
def test_login_wrong_subdomain(self):
|
|
|
|
# type: () -> None
|
|
|
|
with patch("logging.warning") as mock_warning:
|
|
|
|
result = self.login_with_return(self.mit_email("sipbtest"), "xxx")
|
|
|
|
mock_warning.assert_called_once()
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
self.assert_in_response("Your Zulip account is not a member of the "
|
|
|
|
"organization associated with this subdomain.", result)
|
|
|
|
self.assertIsNone(get_session_dict_user(self.client.session))
|
|
|
|
|
|
|
|
def test_login_invalid_subdomain(self):
|
|
|
|
# type: () -> None
|
|
|
|
result = self.login_with_return(self.example_email("hamlet"), "xxx",
|
|
|
|
subdomain="invalid")
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
self.assert_in_response("There is no Zulip organization hosted at this subdomain.", result)
|
|
|
|
self.assertIsNone(get_session_dict_user(self.client.session))
|
2014-01-31 21:08:40 +01:00
|
|
|
|
|
|
|
def test_register(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm("zulip")
|
2016-12-08 01:43:15 +01:00
|
|
|
stream_dict = {"stream_"+str(i): {"description": "stream_%s_description" % i, "invite_only": False}
|
2016-12-11 14:30:45 +01:00
|
|
|
for i in range(40)} # type: Dict[Text, Dict[Text, Any]]
|
2016-12-08 01:43:15 +01:00
|
|
|
for stream_name in stream_dict.keys():
|
2016-10-21 23:08:52 +02:00
|
|
|
self.make_stream(stream_name, realm=realm)
|
2014-01-31 21:08:40 +01:00
|
|
|
|
2016-12-08 01:43:15 +01:00
|
|
|
set_default_streams(realm, stream_dict)
|
2017-03-31 08:41:14 +02:00
|
|
|
# Clear all the caches.
|
|
|
|
flush_per_request_caches()
|
|
|
|
ContentType.objects.clear_cache()
|
|
|
|
Site.objects.clear_cache()
|
|
|
|
|
2014-01-31 21:08:40 +01:00
|
|
|
with queries_captured() as queries:
|
2017-05-24 02:42:31 +02:00
|
|
|
self.register(self.nonreg_email('test'), "test")
|
2014-01-31 21:08:40 +01:00
|
|
|
# Ensure the number of queries we make is not O(streams)
|
2017-10-20 21:34:05 +02:00
|
|
|
self.assert_length(queries, 65)
|
2017-05-24 02:42:31 +02:00
|
|
|
user_profile = self.nonreg_user('test')
|
2015-08-19 20:53:55 +02:00
|
|
|
self.assertEqual(get_session_dict_user(self.client.session), user_profile.id)
|
2016-11-08 16:45:48 +01:00
|
|
|
self.assertFalse(user_profile.enable_stream_desktop_notifications)
|
2014-01-31 21:08:40 +01:00
|
|
|
|
|
|
|
def test_register_deactivated(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 21:08:40 +01:00
|
|
|
"""
|
|
|
|
If you try to register for a deactivated realm, you get a clear error
|
|
|
|
page.
|
|
|
|
"""
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm("zulip")
|
2014-01-31 21:08:40 +01:00
|
|
|
realm.deactivated = True
|
|
|
|
realm.save(update_fields=["deactivated"])
|
|
|
|
|
2017-10-02 08:41:00 +02:00
|
|
|
result = self.client_post('/accounts/home/', {'email': self.nonreg_email('test')},
|
|
|
|
subdomain="zulip")
|
2017-08-25 08:16:36 +02:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2017-10-02 08:41:00 +02:00
|
|
|
self.assertEqual('/accounts/deactivated/', result.url)
|
2014-01-31 21:08:40 +01:00
|
|
|
|
|
|
|
with self.assertRaises(UserProfile.DoesNotExist):
|
2017-05-24 02:42:31 +02:00
|
|
|
self.nonreg_user('test')
|
2014-01-31 21:08:40 +01:00
|
|
|
|
2017-10-02 08:41:00 +02:00
|
|
|
def test_register_deactivated_partway_through(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
If you try to register for a deactivated realm, you get a clear error
|
|
|
|
page.
|
|
|
|
"""
|
|
|
|
email = self.nonreg_email('test')
|
|
|
|
result = self.client_post('/accounts/home/', {'email': email},
|
|
|
|
subdomain="zulip")
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
print(result.url)
|
|
|
|
self.assertNotIn('deactivated', result.url)
|
|
|
|
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
realm.deactivated = True
|
|
|
|
realm.save(update_fields=["deactivated"])
|
|
|
|
|
|
|
|
result = self.submit_reg_form_for_user(email, "abcd1234", subdomain="zulip")
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertEqual('/accounts/deactivated/', result.url)
|
|
|
|
|
|
|
|
with self.assertRaises(UserProfile.DoesNotExist):
|
|
|
|
self.nonreg_user('test')
|
|
|
|
|
2017-11-18 02:23:03 +01:00
|
|
|
def test_login_deactivated_realm(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 21:08:40 +01:00
|
|
|
"""
|
|
|
|
If you try to log in to a deactivated realm, you get a clear error page.
|
|
|
|
"""
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm("zulip")
|
2014-01-31 21:08:40 +01:00
|
|
|
realm.deactivated = True
|
|
|
|
realm.save(update_fields=["deactivated"])
|
|
|
|
|
2017-10-02 08:41:00 +02:00
|
|
|
result = self.login_with_return(self.example_email("hamlet"), subdomain="zulip")
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertEqual('/accounts/deactivated/', result.url)
|
2014-01-31 21:08:40 +01:00
|
|
|
|
|
|
|
def test_logout(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2017-04-18 03:23:32 +02:00
|
|
|
# We use the logout API, not self.logout, to make sure we test
|
|
|
|
# the actual logout code path.
|
2016-07-28 00:30:22 +02:00
|
|
|
self.client_post('/accounts/logout/')
|
2015-08-19 20:53:55 +02:00
|
|
|
self.assertIsNone(get_session_dict_user(self.client.session))
|
2014-01-31 21:08:40 +01:00
|
|
|
|
|
|
|
def test_non_ascii_login(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 21:08:40 +01:00
|
|
|
"""
|
|
|
|
You can log in even if your password contain non-ASCII characters.
|
|
|
|
"""
|
2017-05-24 02:42:31 +02:00
|
|
|
email = self.nonreg_email('test')
|
2015-11-01 17:15:05 +01:00
|
|
|
password = u"hümbüǵ"
|
2014-01-31 21:08:40 +01:00
|
|
|
|
|
|
|
# Registering succeeds.
|
2017-05-24 02:42:31 +02:00
|
|
|
self.register(email, password)
|
|
|
|
user_profile = self.nonreg_user('test')
|
2015-08-19 20:53:55 +02:00
|
|
|
self.assertEqual(get_session_dict_user(self.client.session), user_profile.id)
|
2017-04-18 03:23:32 +02:00
|
|
|
self.logout()
|
2015-08-19 20:53:55 +02:00
|
|
|
self.assertIsNone(get_session_dict_user(self.client.session))
|
2014-01-31 21:08:40 +01:00
|
|
|
|
|
|
|
# Logging in succeeds.
|
2017-04-18 03:23:32 +02:00
|
|
|
self.logout()
|
2014-01-31 21:08:40 +01:00
|
|
|
self.login(email, password)
|
2015-08-19 20:53:55 +02:00
|
|
|
self.assertEqual(get_session_dict_user(self.client.session), user_profile.id)
|
2014-01-31 21:08:40 +01:00
|
|
|
|
2017-01-28 20:28:17 +01:00
|
|
|
def test_login_page_redirects_logged_in_user(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""You will be redirected to the app's main page if you land on the
|
|
|
|
login page when already logged in.
|
|
|
|
"""
|
2017-05-25 01:50:35 +02:00
|
|
|
self.login(self.example_email("cordelia"))
|
2017-01-28 20:28:17 +01:00
|
|
|
response = self.client_get("/login/")
|
2017-10-06 08:36:23 +02:00
|
|
|
self.assertEqual(response["Location"], "http://zulip.testserver")
|
2017-01-28 20:28:17 +01:00
|
|
|
|
2017-10-21 03:14:21 +02:00
|
|
|
class InviteUserBase(ZulipTestCase):
|
2017-07-11 20:25:50 +02:00
|
|
|
def check_sent_emails(self, correct_recipients, custom_body=None, custom_from_name=None):
|
|
|
|
# type: (List[Text], Optional[str], Optional[str]) -> None
|
2014-01-31 21:08:40 +01:00
|
|
|
from django.core.mail import outbox
|
|
|
|
self.assertEqual(len(outbox), len(correct_recipients))
|
|
|
|
email_recipients = [email.recipients()[0] for email in outbox]
|
2016-07-10 20:43:58 +02:00
|
|
|
self.assertEqual(sorted(email_recipients), sorted(correct_recipients))
|
2017-02-12 21:21:31 +01:00
|
|
|
if len(outbox) == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
if custom_body is None:
|
|
|
|
self.assertNotIn("Message from", outbox[0].body)
|
|
|
|
else:
|
|
|
|
self.assertIn("Message from ", outbox[0].body)
|
|
|
|
self.assertIn(custom_body, outbox[0].body)
|
2014-01-31 21:08:40 +01:00
|
|
|
|
2017-07-11 20:25:50 +02:00
|
|
|
if custom_from_name is not None:
|
|
|
|
self.assertIn(custom_from_name, outbox[0].from_email)
|
|
|
|
|
2017-07-05 21:29:27 +02:00
|
|
|
self.assertIn(FromAddress.NOREPLY, outbox[0].from_email)
|
|
|
|
|
2017-10-15 18:34:47 +02:00
|
|
|
def invite(self, users, streams, body='', invite_as_admin="false"):
|
|
|
|
# type: (Text, List[Text], str, str) -> HttpResponse
|
2017-10-21 03:14:21 +02:00
|
|
|
"""
|
|
|
|
Invites the specified users to Zulip with the specified streams.
|
|
|
|
|
|
|
|
users should be a string containing the users to invite, comma or
|
|
|
|
newline separated.
|
|
|
|
|
|
|
|
streams should be a list of strings.
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self.client_post("/json/invites",
|
|
|
|
{"invitee_emails": users,
|
|
|
|
"stream": streams,
|
2017-10-15 18:34:47 +02:00
|
|
|
"invite_as_admin": invite_as_admin,
|
2017-10-21 03:14:21 +02:00
|
|
|
"custom_body": body})
|
|
|
|
|
2017-10-21 03:15:12 +02:00
|
|
|
class InviteUserTest(InviteUserBase):
|
2014-01-31 21:08:40 +01:00
|
|
|
def test_successful_invite_user(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 21:08:40 +01:00
|
|
|
"""
|
2017-07-31 20:55:57 +02:00
|
|
|
A call to /json/invites with valid parameters causes an invitation
|
2014-01-31 21:08:40 +01:00
|
|
|
email to be sent.
|
|
|
|
"""
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2014-01-31 21:08:40 +01:00
|
|
|
invitee = "alice-test@zulip.com"
|
|
|
|
self.assert_json_success(self.invite(invitee, ["Denmark"]))
|
|
|
|
self.assertTrue(find_key_by_email(invitee))
|
2017-07-11 20:25:50 +02:00
|
|
|
self.check_sent_emails([invitee], custom_from_name="Hamlet")
|
2014-01-31 21:08:40 +01:00
|
|
|
|
2017-10-15 18:34:47 +02:00
|
|
|
def test_successful_invite_user_as_admin_from_admin_account(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
Test that a new user invited to a stream receives some initial
|
|
|
|
history but only from public streams.
|
|
|
|
"""
|
|
|
|
self.login(self.example_email('iago'))
|
|
|
|
invitee = self.nonreg_email('alice')
|
|
|
|
self.assert_json_success(self.invite(invitee, ["Denmark"], invite_as_admin="true"))
|
|
|
|
self.assertTrue(find_key_by_email(invitee))
|
|
|
|
|
|
|
|
self.submit_reg_form_for_user(invitee, "password")
|
|
|
|
invitee_profile = self.nonreg_user('alice')
|
|
|
|
self.assertTrue(invitee_profile.is_realm_admin)
|
|
|
|
|
|
|
|
def test_invite_user_as_admin_from_normal_account(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
Test that a new user invited to a stream receives some initial
|
|
|
|
history but only from public streams.
|
|
|
|
"""
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
invitee = self.nonreg_email('alice')
|
|
|
|
response = self.invite(invitee, ["Denmark"], invite_as_admin="true")
|
|
|
|
self.assert_json_error(response, "Must be a realm administrator")
|
|
|
|
|
2017-02-12 21:21:31 +01:00
|
|
|
def test_successful_invite_user_with_custom_body(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
2017-07-31 20:55:57 +02:00
|
|
|
A call to /json/invites with valid parameters causes an invitation
|
2017-02-12 21:21:31 +01:00
|
|
|
email to be sent.
|
|
|
|
"""
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2017-02-12 21:21:31 +01:00
|
|
|
invitee = "alice-test@zulip.com"
|
|
|
|
body = "Custom Text."
|
|
|
|
self.assert_json_success(self.invite(invitee, ["Denmark"], body))
|
|
|
|
self.assertTrue(find_pattern_in_email(invitee, body))
|
2017-07-11 20:25:50 +02:00
|
|
|
self.check_sent_emails([invitee], custom_body=body, custom_from_name="Hamlet")
|
2017-02-12 21:21:31 +01:00
|
|
|
|
2016-07-29 18:16:54 +02:00
|
|
|
def test_successful_invite_user_with_name(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
2017-07-31 20:55:57 +02:00
|
|
|
A call to /json/invites with valid parameters causes an invitation
|
2016-07-29 18:16:54 +02:00
|
|
|
email to be sent.
|
|
|
|
"""
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2016-07-29 18:16:54 +02:00
|
|
|
email = "alice-test@zulip.com"
|
|
|
|
invitee = "Alice Test <{}>".format(email)
|
|
|
|
self.assert_json_success(self.invite(invitee, ["Denmark"]))
|
|
|
|
self.assertTrue(find_key_by_email(email))
|
2017-07-11 20:25:50 +02:00
|
|
|
self.check_sent_emails([email], custom_from_name="Hamlet")
|
2016-07-29 18:16:54 +02:00
|
|
|
|
|
|
|
def test_successful_invite_user_with_name_and_normal_one(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
2017-07-31 20:55:57 +02:00
|
|
|
A call to /json/invites with valid parameters causes an invitation
|
2016-07-29 18:16:54 +02:00
|
|
|
email to be sent.
|
|
|
|
"""
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2016-07-29 18:16:54 +02:00
|
|
|
email = "alice-test@zulip.com"
|
|
|
|
email2 = "bob-test@zulip.com"
|
|
|
|
invitee = "Alice Test <{}>, {}".format(email, email2)
|
|
|
|
self.assert_json_success(self.invite(invitee, ["Denmark"]))
|
|
|
|
self.assertTrue(find_key_by_email(email))
|
|
|
|
self.assertTrue(find_key_by_email(email2))
|
2017-07-11 20:25:50 +02:00
|
|
|
self.check_sent_emails([email, email2], custom_from_name="Hamlet")
|
2016-07-29 18:16:54 +02:00
|
|
|
|
2017-05-18 02:45:20 +02:00
|
|
|
def test_require_realm_admin(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
The invite_by_admins_only realm setting works properly.
|
|
|
|
"""
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
realm.invite_by_admins_only = True
|
|
|
|
realm.save()
|
|
|
|
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
email = "alice-test@zulip.com"
|
|
|
|
email2 = "bob-test@zulip.com"
|
|
|
|
invitee = "Alice Test <{}>, {}".format(email, email2)
|
|
|
|
self.assert_json_error(self.invite(invitee, ["Denmark"]),
|
|
|
|
"Must be a realm administrator")
|
|
|
|
|
|
|
|
# Now verify an administrator can do it
|
|
|
|
self.login("iago@zulip.com")
|
|
|
|
self.assert_json_success(self.invite(invitee, ["Denmark"]))
|
|
|
|
self.assertTrue(find_key_by_email(email))
|
|
|
|
self.assertTrue(find_key_by_email(email2))
|
|
|
|
self.check_sent_emails([email, email2])
|
|
|
|
|
2017-02-27 00:29:33 +01:00
|
|
|
def test_successful_invite_user_with_notifications_stream(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
2017-07-31 20:55:57 +02:00
|
|
|
A call to /json/invites with valid parameters unconditionally
|
2017-02-27 00:29:33 +01:00
|
|
|
subscribes the invitee to the notifications stream if it exists and is
|
|
|
|
public.
|
|
|
|
"""
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
notifications_stream = get_stream('Verona', realm)
|
2017-09-17 19:53:38 +02:00
|
|
|
realm.notifications_stream_id = notifications_stream.id
|
2017-02-27 00:29:33 +01:00
|
|
|
realm.save()
|
|
|
|
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2017-02-27 00:29:33 +01:00
|
|
|
invitee = 'alice-test@zulip.com'
|
|
|
|
self.assert_json_success(self.invite(invitee, ['Denmark']))
|
|
|
|
self.assertTrue(find_key_by_email(invitee))
|
|
|
|
self.check_sent_emails([invitee])
|
|
|
|
|
|
|
|
prereg_user = get_prereg_user_by_email(invitee)
|
2017-09-17 19:53:38 +02:00
|
|
|
stream_ids = [stream.id for stream in prereg_user.streams.all()]
|
|
|
|
self.assertTrue(notifications_stream.id in stream_ids)
|
2017-02-27 00:29:33 +01:00
|
|
|
|
2015-10-26 16:49:22 +01:00
|
|
|
def test_invite_user_signup_initial_history(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2015-10-26 16:49:22 +01:00
|
|
|
"""
|
|
|
|
Test that a new user invited to a stream receives some initial
|
|
|
|
history but only from public streams.
|
|
|
|
"""
|
2017-05-24 02:42:31 +02:00
|
|
|
self.login(self.example_email('hamlet'))
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2015-10-26 16:49:22 +01:00
|
|
|
private_stream_name = "Secret"
|
2016-10-21 23:08:52 +02:00
|
|
|
self.make_stream(private_stream_name, invite_only=True)
|
2017-08-25 06:01:29 +02:00
|
|
|
self.subscribe(user_profile, private_stream_name)
|
2017-10-28 18:10:38 +02:00
|
|
|
public_msg_id = self.send_stream_message(
|
|
|
|
self.example_email("hamlet"),
|
|
|
|
"Denmark",
|
|
|
|
topic_name="Public topic",
|
|
|
|
content="Public message",
|
|
|
|
)
|
|
|
|
secret_msg_id = self.send_stream_message(
|
|
|
|
self.example_email("hamlet"),
|
|
|
|
private_stream_name,
|
|
|
|
topic_name="Secret topic",
|
|
|
|
content="Secret message",
|
|
|
|
)
|
2017-05-24 02:42:31 +02:00
|
|
|
invitee = self.nonreg_email('alice')
|
2015-10-26 16:49:22 +01:00
|
|
|
self.assert_json_success(self.invite(invitee, [private_stream_name, "Denmark"]))
|
|
|
|
self.assertTrue(find_key_by_email(invitee))
|
|
|
|
|
2017-05-24 02:42:31 +02:00
|
|
|
self.submit_reg_form_for_user(invitee, "password")
|
|
|
|
invitee_profile = self.nonreg_user('alice')
|
2015-10-26 16:49:22 +01:00
|
|
|
invitee_msg_ids = [um.message_id for um in
|
|
|
|
UserMessage.objects.filter(user_profile=invitee_profile)]
|
|
|
|
self.assertTrue(public_msg_id in invitee_msg_ids)
|
|
|
|
self.assertFalse(secret_msg_id in invitee_msg_ids)
|
2017-10-15 18:34:47 +02:00
|
|
|
self.assertFalse(invitee_profile.is_realm_admin)
|
2017-09-25 22:53:07 +02:00
|
|
|
# Test that exactly 2 new Zulip messages were sent, both notifications.
|
|
|
|
last_3_messages = list(reversed(list(Message.objects.all().order_by("-id")[0:3])))
|
|
|
|
first_msg = last_3_messages[0]
|
|
|
|
self.assertEqual(first_msg.id, secret_msg_id)
|
|
|
|
|
|
|
|
# The first, from notification-bot to the user who invited the new user.
|
|
|
|
second_msg = last_3_messages[1]
|
|
|
|
self.assertEqual(second_msg.sender.email, "notification-bot@zulip.com")
|
|
|
|
self.assertTrue(second_msg.content.startswith("alice_zulip.com <`alice@zulip.com`> accepted your"))
|
|
|
|
|
|
|
|
# The second, from welcome-bot to the user who was invited.
|
|
|
|
third_msg = last_3_messages[2]
|
|
|
|
self.assertEqual(third_msg.sender.email, "welcome-bot@zulip.com")
|
|
|
|
self.assertTrue(third_msg.content.startswith("Hello, and welcome to Zulip!"))
|
|
|
|
|
2014-01-31 21:08:40 +01:00
|
|
|
def test_multi_user_invite(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 21:08:40 +01:00
|
|
|
"""
|
|
|
|
Invites multiple users with a variety of delimiters.
|
|
|
|
"""
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2014-01-31 21:08:40 +01:00
|
|
|
# Intentionally use a weird string.
|
|
|
|
self.assert_json_success(self.invite(
|
2016-12-02 08:15:16 +01:00
|
|
|
"""bob-test@zulip.com, carol-test@zulip.com,
|
|
|
|
dave-test@zulip.com
|
2014-01-31 21:08:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
earl-test@zulip.com""", ["Denmark"]))
|
|
|
|
for user in ("bob", "carol", "dave", "earl"):
|
2015-12-01 17:11:16 +01:00
|
|
|
self.assertTrue(find_key_by_email("%s-test@zulip.com" % (user,)))
|
2014-01-31 21:08:40 +01:00
|
|
|
self.check_sent_emails(["bob-test@zulip.com", "carol-test@zulip.com",
|
|
|
|
"dave-test@zulip.com", "earl-test@zulip.com"])
|
|
|
|
|
|
|
|
def test_missing_or_invalid_params(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 21:08:40 +01:00
|
|
|
"""
|
|
|
|
Tests inviting with various missing or invalid parameters.
|
|
|
|
"""
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2014-01-31 21:08:40 +01:00
|
|
|
self.assert_json_error(
|
2017-07-31 20:55:57 +02:00
|
|
|
self.client_post("/json/invites",
|
|
|
|
{"invitee_emails": "foo@zulip.com",
|
|
|
|
"custom_body": ''}),
|
2014-01-31 21:08:40 +01:00
|
|
|
"You must specify at least one stream for invitees to join.")
|
|
|
|
|
|
|
|
for address in ("noatsign.com", "outsideyourdomain@example.net"):
|
|
|
|
self.assert_json_error(
|
|
|
|
self.invite(address, ["Denmark"]),
|
|
|
|
"Some emails did not validate, so we didn't send any invitations.")
|
|
|
|
self.check_sent_emails([])
|
|
|
|
|
2017-02-27 00:29:33 +01:00
|
|
|
self.assert_json_error(
|
|
|
|
self.invite("", ["Denmark"]),
|
|
|
|
"You must specify at least one email address.")
|
|
|
|
self.check_sent_emails([])
|
|
|
|
|
2014-01-31 21:08:40 +01:00
|
|
|
def test_invalid_stream(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 21:08:40 +01:00
|
|
|
"""
|
|
|
|
Tests inviting to a non-existent stream.
|
|
|
|
"""
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2014-01-31 21:08:40 +01:00
|
|
|
self.assert_json_error(self.invite("iago-test@zulip.com", ["NotARealStream"]),
|
2016-12-03 00:04:17 +01:00
|
|
|
"Stream does not exist: NotARealStream. No invites were sent.")
|
2014-01-31 21:08:40 +01:00
|
|
|
self.check_sent_emails([])
|
|
|
|
|
|
|
|
def test_invite_existing_user(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 21:08:40 +01:00
|
|
|
"""
|
|
|
|
If you invite an address already using Zulip, no invitation is sent.
|
|
|
|
"""
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2014-01-31 21:08:40 +01:00
|
|
|
self.assert_json_error(
|
2017-07-31 20:55:57 +02:00
|
|
|
self.client_post("/json/invites",
|
2017-05-25 01:40:26 +02:00
|
|
|
{"invitee_emails": self.example_email("hamlet"),
|
2017-02-12 21:21:31 +01:00
|
|
|
"stream": ["Denmark"],
|
|
|
|
"custom_body": ''}),
|
2014-01-31 21:08:40 +01:00
|
|
|
"We weren't able to invite anyone.")
|
|
|
|
self.assertRaises(PreregistrationUser.DoesNotExist,
|
|
|
|
lambda: PreregistrationUser.objects.get(
|
2017-05-25 01:40:26 +02:00
|
|
|
email=self.example_email("hamlet")))
|
2014-01-31 21:08:40 +01:00
|
|
|
self.check_sent_emails([])
|
|
|
|
|
|
|
|
def test_invite_some_existing_some_new(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 21:08:40 +01:00
|
|
|
"""
|
|
|
|
If you invite a mix of already existing and new users, invitations are
|
|
|
|
only sent to the new users.
|
|
|
|
"""
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
|
|
|
existing = [self.example_email("hamlet"), u"othello@zulip.com"]
|
2017-05-25 00:17:02 +02:00
|
|
|
new = [u"foo-test@zulip.com", u"bar-test@zulip.com"]
|
2014-01-31 21:08:40 +01:00
|
|
|
|
2017-07-31 20:55:57 +02:00
|
|
|
result = self.client_post("/json/invites",
|
2014-01-31 21:08:40 +01:00
|
|
|
{"invitee_emails": "\n".join(existing + new),
|
2017-02-12 21:21:31 +01:00
|
|
|
"stream": ["Denmark"],
|
|
|
|
"custom_body": ''})
|
2014-01-31 21:08:40 +01:00
|
|
|
self.assert_json_error(result,
|
|
|
|
"Some of those addresses are already using Zulip, \
|
|
|
|
so we didn't send them an invitation. We did send invitations to everyone else!")
|
|
|
|
|
|
|
|
# We only created accounts for the new users.
|
|
|
|
for email in existing:
|
|
|
|
self.assertRaises(PreregistrationUser.DoesNotExist,
|
|
|
|
lambda: PreregistrationUser.objects.get(
|
2017-01-24 06:02:39 +01:00
|
|
|
email=email))
|
2014-01-31 21:08:40 +01:00
|
|
|
for email in new:
|
|
|
|
self.assertTrue(PreregistrationUser.objects.get(email=email))
|
|
|
|
|
|
|
|
# We only sent emails to the new users.
|
|
|
|
self.check_sent_emails(new)
|
|
|
|
|
2016-10-04 01:15:24 +02:00
|
|
|
prereg_user = get_prereg_user_by_email('foo-test@zulip.com')
|
|
|
|
self.assertEqual(prereg_user.email, 'foo-test@zulip.com')
|
|
|
|
|
2014-01-31 21:08:40 +01:00
|
|
|
def test_invite_outside_domain_in_closed_realm(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 21:08:40 +01:00
|
|
|
"""
|
|
|
|
In a realm with `restricted_to_domain = True`, you can't invite people
|
|
|
|
with a different domain from that of the realm or your e-mail address.
|
|
|
|
"""
|
2017-01-04 05:30:48 +01:00
|
|
|
zulip_realm = get_realm("zulip")
|
2014-01-31 21:08:40 +01:00
|
|
|
zulip_realm.restricted_to_domain = True
|
|
|
|
zulip_realm.save()
|
|
|
|
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2014-01-31 21:08:40 +01:00
|
|
|
external_address = "foo@example.com"
|
|
|
|
|
|
|
|
self.assert_json_error(
|
|
|
|
self.invite(external_address, ["Denmark"]),
|
|
|
|
"Some emails did not validate, so we didn't send any invitations.")
|
|
|
|
|
|
|
|
def test_invite_outside_domain_in_open_realm(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 21:08:40 +01:00
|
|
|
"""
|
|
|
|
In a realm with `restricted_to_domain = False`, you can invite people
|
|
|
|
with a different domain from that of the realm or your e-mail address.
|
|
|
|
"""
|
2017-01-04 05:30:48 +01:00
|
|
|
zulip_realm = get_realm("zulip")
|
2014-01-31 21:08:40 +01:00
|
|
|
zulip_realm.restricted_to_domain = False
|
|
|
|
zulip_realm.save()
|
|
|
|
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2014-01-31 21:08:40 +01:00
|
|
|
external_address = "foo@example.com"
|
|
|
|
|
|
|
|
self.assert_json_success(self.invite(external_address, ["Denmark"]))
|
|
|
|
self.check_sent_emails([external_address])
|
|
|
|
|
2017-03-18 20:36:40 +01:00
|
|
|
def test_invite_outside_domain_before_closing(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
If you invite someone with a different domain from that of the realm
|
|
|
|
when `restricted_to_domain = False`, but `restricted_to_domain` later
|
|
|
|
changes to true, the invitation should succeed but the invitee's signup
|
|
|
|
attempt should fail.
|
|
|
|
"""
|
|
|
|
zulip_realm = get_realm("zulip")
|
|
|
|
zulip_realm.restricted_to_domain = False
|
|
|
|
zulip_realm.save()
|
|
|
|
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2017-03-18 20:36:40 +01:00
|
|
|
external_address = "foo@example.com"
|
|
|
|
|
|
|
|
self.assert_json_success(self.invite(external_address, ["Denmark"]))
|
|
|
|
self.check_sent_emails([external_address])
|
|
|
|
|
|
|
|
zulip_realm.restricted_to_domain = True
|
|
|
|
zulip_realm.save()
|
|
|
|
|
|
|
|
result = self.submit_reg_form_for_user("foo@example.com", "password")
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
self.assert_in_response("only allows users with e-mail", result)
|
|
|
|
|
2014-01-31 21:08:40 +01:00
|
|
|
def test_invite_with_non_ascii_streams(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 21:08:40 +01:00
|
|
|
"""
|
|
|
|
Inviting someone to streams with non-ASCII characters succeeds.
|
|
|
|
"""
|
2017-05-25 01:40:26 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2014-01-31 21:08:40 +01:00
|
|
|
invitee = "alice-test@zulip.com"
|
|
|
|
|
2015-11-01 17:15:05 +01:00
|
|
|
stream_name = u"hümbüǵ"
|
2014-01-31 21:08:40 +01:00
|
|
|
|
|
|
|
# Make sure we're subscribed before inviting someone.
|
2017-08-25 06:01:29 +02:00
|
|
|
self.subscribe(self.example_user("hamlet"), stream_name)
|
2014-01-31 21:08:40 +01:00
|
|
|
|
|
|
|
self.assert_json_success(self.invite(invitee, [stream_name]))
|
|
|
|
|
2016-12-01 08:54:21 +01:00
|
|
|
def test_invitation_reminder_email(self):
|
|
|
|
# type: () -> None
|
|
|
|
from django.core.mail import outbox
|
2017-05-24 02:42:31 +02:00
|
|
|
|
|
|
|
# All users belong to zulip realm
|
|
|
|
referrer_user = 'hamlet'
|
|
|
|
current_user_email = self.example_email(referrer_user)
|
2016-12-01 08:54:21 +01:00
|
|
|
self.login(current_user_email)
|
2017-09-21 15:11:20 +02:00
|
|
|
invitee_email = self.nonreg_email('alice')
|
|
|
|
self.assert_json_success(self.invite(invitee_email, ["Denmark"]))
|
|
|
|
self.assertTrue(find_key_by_email(invitee_email))
|
|
|
|
self.check_sent_emails([invitee_email])
|
2016-12-01 08:54:21 +01:00
|
|
|
|
2017-09-21 15:11:20 +02:00
|
|
|
data = {"email": invitee_email, "referrer_email": current_user_email}
|
2016-12-01 08:54:21 +01:00
|
|
|
invitee = get_prereg_user_by_email(data["email"])
|
2017-05-24 02:42:31 +02:00
|
|
|
referrer = self.example_user(referrer_user)
|
2017-07-08 04:38:13 +02:00
|
|
|
link = create_confirmation_link(invitee, referrer.realm.host, Confirmation.INVITATION)
|
2016-12-01 08:54:21 +01:00
|
|
|
context = common_context(referrer)
|
|
|
|
context.update({
|
|
|
|
'activate_url': link,
|
2017-05-04 06:34:30 +02:00
|
|
|
'referrer_name': referrer.full_name,
|
|
|
|
'referrer_email': referrer.email,
|
|
|
|
'referrer_realm_name': referrer.realm.name,
|
2016-12-01 08:54:21 +01:00
|
|
|
})
|
|
|
|
with self.settings(EMAIL_BACKEND='django.core.mail.backends.console.EmailBackend'):
|
2017-05-03 18:20:16 +02:00
|
|
|
send_future_email(
|
2017-07-11 05:34:32 +02:00
|
|
|
"zerver/emails/invitation_reminder", to_email=data["email"],
|
2017-07-05 21:29:27 +02:00
|
|
|
from_address=FromAddress.NOREPLY, context=context)
|
2017-07-02 21:10:41 +02:00
|
|
|
email_jobs_to_deliver = ScheduledEmail.objects.filter(
|
2017-04-15 04:03:56 +02:00
|
|
|
scheduled_timestamp__lte=timezone_now())
|
2016-12-01 08:54:21 +01:00
|
|
|
self.assertEqual(len(email_jobs_to_deliver), 1)
|
|
|
|
email_count = len(outbox)
|
|
|
|
for job in email_jobs_to_deliver:
|
2017-07-12 01:05:59 +02:00
|
|
|
send_email(**ujson.loads(job.data))
|
2016-12-01 08:54:21 +01:00
|
|
|
self.assertEqual(len(outbox), email_count + 1)
|
2017-07-05 21:29:27 +02:00
|
|
|
self.assertIn(FromAddress.NOREPLY, outbox[-1].from_email)
|
2016-12-01 08:54:21 +01:00
|
|
|
|
2017-09-21 15:11:20 +02:00
|
|
|
# Now verify that signing up clears invite_reminder emails
|
|
|
|
email_jobs_to_deliver = ScheduledEmail.objects.filter(
|
2017-10-19 04:41:28 +02:00
|
|
|
scheduled_timestamp__lte=timezone_now(), type=ScheduledEmail.INVITATION_REMINDER)
|
2017-09-21 15:11:20 +02:00
|
|
|
self.assertEqual(len(email_jobs_to_deliver), 1)
|
|
|
|
|
|
|
|
self.register(invitee_email, "test")
|
|
|
|
email_jobs_to_deliver = ScheduledEmail.objects.filter(
|
2017-10-19 04:41:28 +02:00
|
|
|
scheduled_timestamp__lte=timezone_now(), type=ScheduledEmail.INVITATION_REMINDER)
|
2017-09-21 15:11:20 +02:00
|
|
|
self.assertEqual(len(email_jobs_to_deliver), 0)
|
|
|
|
|
2017-11-01 21:07:39 +01:00
|
|
|
# make sure users can't take a valid confirmation key from another
|
|
|
|
# pathway and use it with the invitation url route
|
|
|
|
# Mainly a test of get_object_from_key, rather than of the invitation pathway
|
|
|
|
def test_confirmation_key_of_wrong_type(self):
|
|
|
|
# type: () -> None
|
|
|
|
user = self.example_user('hamlet')
|
|
|
|
registration_key = create_confirmation_link(user, 'host', Confirmation.USER_REGISTRATION).split('/')[-1]
|
2017-11-01 21:16:23 +01:00
|
|
|
with self.assertRaises(ConfirmationKeyException) as cm:
|
2017-11-01 21:07:39 +01:00
|
|
|
get_object_from_key(registration_key, Confirmation.INVITATION)
|
2017-11-01 21:16:23 +01:00
|
|
|
self.assertEqual(cm.exception.error_type, ConfirmationKeyException.DOES_NOT_EXIST)
|
2017-11-01 21:07:39 +01:00
|
|
|
|
2017-10-21 03:15:12 +02:00
|
|
|
class InvitationsTestCase(InviteUserBase):
|
|
|
|
def test_successful_get_open_invitations(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
A GET call to /json/invites returns all unexpired invitations.
|
|
|
|
"""
|
|
|
|
|
|
|
|
days_to_activate = getattr(settings, 'ACCOUNT_ACTIVATION_DAYS', "Wrong")
|
|
|
|
active_value = getattr(confirmation_settings, 'STATUS_ACTIVE', "Wrong")
|
|
|
|
self.assertNotEqual(days_to_activate, "Wrong")
|
|
|
|
self.assertNotEqual(active_value, "Wrong")
|
|
|
|
|
|
|
|
self.login(self.example_email("iago"))
|
|
|
|
user_profile = self.example_user("iago")
|
|
|
|
|
|
|
|
prereg_user_one = PreregistrationUser(email="TestOne@zulip.com", referred_by=user_profile)
|
|
|
|
prereg_user_one.save()
|
|
|
|
expired_datetime = timezone_now() - datetime.timedelta(days=(days_to_activate+1))
|
|
|
|
prereg_user_two = PreregistrationUser(email="TestTwo@zulip.com", referred_by=user_profile)
|
|
|
|
prereg_user_two.save()
|
|
|
|
PreregistrationUser.objects.filter(id=prereg_user_two.id).update(invited_at=expired_datetime)
|
|
|
|
prereg_user_three = PreregistrationUser(email="TestThree@zulip.com",
|
|
|
|
referred_by=user_profile, status=active_value)
|
|
|
|
prereg_user_three.save()
|
|
|
|
|
|
|
|
result = self.client_get("/json/invites")
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
self.assert_in_success_response(["TestOne@zulip.com"], result)
|
|
|
|
self.assert_not_in_success_response(["TestTwo@zulip.com", "TestThree@zulip.com"], result)
|
|
|
|
|
|
|
|
def test_successful_delete_invitation(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
A DELETE call to /json/invites/<ID> should delete the invite and
|
|
|
|
any scheduled invitation reminder emails.
|
|
|
|
"""
|
|
|
|
self.login(self.example_email("iago"))
|
|
|
|
|
|
|
|
invitee = "DeleteMe@zulip.com"
|
|
|
|
self.assert_json_success(self.invite(invitee, ['Denmark']))
|
|
|
|
prereg_user = PreregistrationUser.objects.get(email=invitee)
|
|
|
|
|
|
|
|
# Verify that the scheduled email exists.
|
|
|
|
ScheduledEmail.objects.get(address__iexact=invitee,
|
|
|
|
type=ScheduledEmail.INVITATION_REMINDER)
|
|
|
|
|
|
|
|
result = self.client_delete('/json/invites/' + str(prereg_user.id))
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
error_result = self.client_delete('/json/invites/' + str(prereg_user.id))
|
|
|
|
self.assert_json_error(error_result, "Invalid invitation ID.")
|
|
|
|
|
|
|
|
self.assertRaises(ScheduledEmail.DoesNotExist,
|
|
|
|
lambda: ScheduledEmail.objects.get(address__iexact=invitee,
|
|
|
|
type=ScheduledEmail.INVITATION_REMINDER))
|
|
|
|
|
|
|
|
def test_successful_resend_invitation(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
A POST call to /json/invites/<ID>/resend should send an invitation reminder email
|
|
|
|
and delete any scheduled invitation reminder email.
|
|
|
|
"""
|
|
|
|
self.login(self.example_email("iago"))
|
|
|
|
invitee = "ResendMe@zulip.com"
|
|
|
|
|
|
|
|
self.assert_json_success(self.invite(invitee, ['Denmark']))
|
|
|
|
prereg_user = PreregistrationUser.objects.get(email=invitee)
|
|
|
|
|
|
|
|
# Verify and then clear from the outbox the original invite email
|
|
|
|
self.check_sent_emails([invitee], custom_from_name="Zulip")
|
|
|
|
from django.core.mail import outbox
|
|
|
|
outbox.pop()
|
|
|
|
|
|
|
|
# Verify that the scheduled email exists.
|
|
|
|
ScheduledEmail.objects.get(address__iexact=invitee,
|
|
|
|
type=ScheduledEmail.INVITATION_REMINDER)
|
|
|
|
result = self.client_post('/json/invites/' + str(prereg_user.id) + '/resend')
|
|
|
|
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
error_result = self.client_post('/json/invites/' + str(9999) + '/resend')
|
|
|
|
self.assert_json_error(error_result, "Invalid invitation ID.")
|
|
|
|
|
|
|
|
self.check_sent_emails([invitee], custom_from_name="Zulip")
|
|
|
|
|
|
|
|
self.assertRaises(ScheduledEmail.DoesNotExist,
|
|
|
|
lambda: ScheduledEmail.objects.get(address__iexact=invitee,
|
|
|
|
type=ScheduledEmail.INVITATION_REMINDER))
|
|
|
|
|
2016-07-29 18:16:54 +02:00
|
|
|
class InviteeEmailsParserTests(TestCase):
|
|
|
|
def setUp(self):
|
2016-09-10 20:34:33 +02:00
|
|
|
# type: () -> None
|
2016-07-29 18:16:54 +02:00
|
|
|
self.email1 = "email1@zulip.com"
|
|
|
|
self.email2 = "email2@zulip.com"
|
|
|
|
self.email3 = "email3@zulip.com"
|
|
|
|
|
|
|
|
def test_if_emails_separated_by_commas_are_parsed_and_striped_correctly(self):
|
2016-09-10 20:34:33 +02:00
|
|
|
# type: () -> None
|
2016-07-29 18:16:54 +02:00
|
|
|
emails_raw = "{} ,{}, {}".format(self.email1, self.email2, self.email3)
|
|
|
|
expected_set = {self.email1, self.email2, self.email3}
|
|
|
|
self.assertEqual(get_invitee_emails_set(emails_raw), expected_set)
|
|
|
|
|
|
|
|
def test_if_emails_separated_by_newlines_are_parsed_and_striped_correctly(self):
|
2016-09-10 20:34:33 +02:00
|
|
|
# type: () -> None
|
2016-07-29 18:16:54 +02:00
|
|
|
emails_raw = "{}\n {}\n {} ".format(self.email1, self.email2, self.email3)
|
|
|
|
expected_set = {self.email1, self.email2, self.email3}
|
|
|
|
self.assertEqual(get_invitee_emails_set(emails_raw), expected_set)
|
|
|
|
|
|
|
|
def test_if_emails_from_email_client_separated_by_newlines_are_parsed_correctly(self):
|
2016-09-10 20:34:33 +02:00
|
|
|
# type: () -> None
|
2016-07-29 18:16:54 +02:00
|
|
|
emails_raw = "Email One <{}>\nEmailTwo<{}>\nEmail Three<{}>".format(self.email1, self.email2, self.email3)
|
|
|
|
expected_set = {self.email1, self.email2, self.email3}
|
|
|
|
self.assertEqual(get_invitee_emails_set(emails_raw), expected_set)
|
|
|
|
|
|
|
|
def test_if_emails_in_mixed_style_are_parsed_correctly(self):
|
2016-09-10 20:34:33 +02:00
|
|
|
# type: () -> None
|
2016-07-29 18:16:54 +02:00
|
|
|
emails_raw = "Email One <{}>,EmailTwo<{}>\n{}".format(self.email1, self.email2, self.email3)
|
|
|
|
expected_set = {self.email1, self.email2, self.email3}
|
|
|
|
self.assertEqual(get_invitee_emails_set(emails_raw), expected_set)
|
|
|
|
|
2017-08-10 22:34:17 +02:00
|
|
|
class MultiuseInviteTest(ZulipTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.realm = get_realm('zulip')
|
|
|
|
self.realm.invite_required = True
|
|
|
|
self.realm.save()
|
|
|
|
|
|
|
|
def generate_multiuse_invite_link(self, streams=None, date_sent=None):
|
|
|
|
# type: (List[Stream], Optional[datetime.datetime]) -> Text
|
|
|
|
invite = MultiuseInvite(realm=self.realm, referred_by=self.example_user("iago"))
|
|
|
|
invite.save()
|
|
|
|
|
|
|
|
if streams is not None:
|
|
|
|
invite.streams = streams
|
|
|
|
invite.save()
|
|
|
|
|
|
|
|
if date_sent is None:
|
|
|
|
date_sent = timezone_now()
|
|
|
|
key = generate_key()
|
|
|
|
Confirmation.objects.create(content_object=invite, date_sent=date_sent,
|
|
|
|
confirmation_key=key, type=Confirmation.MULTIUSE_INVITE)
|
|
|
|
|
|
|
|
return confirmation_url(key, self.realm.host, Confirmation.MULTIUSE_INVITE)
|
|
|
|
|
|
|
|
def check_user_able_to_register(self, email, invite_link):
|
|
|
|
# type: (Text, Text) -> None
|
|
|
|
password = "password"
|
|
|
|
|
|
|
|
result = self.client_post(invite_link, {'email': email})
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertTrue(result["Location"].endswith(
|
|
|
|
"/accounts/send_confirm/%s" % (email,)))
|
|
|
|
result = self.client_get(result["Location"])
|
|
|
|
self.assert_in_response("Check your email so we can get started.", result)
|
|
|
|
|
|
|
|
confirmation_url = self.get_confirmation_url_from_outbox(email)
|
|
|
|
result = self.client_get(confirmation_url)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
result = self.submit_reg_form_for_user(email, password)
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
|
|
|
|
from django.core.mail import outbox
|
|
|
|
outbox.pop()
|
|
|
|
|
|
|
|
def test_valid_multiuse_link(self):
|
|
|
|
# type: () -> None
|
|
|
|
email1 = self.nonreg_email("test")
|
|
|
|
email2 = self.nonreg_email("test1")
|
|
|
|
email3 = self.nonreg_email("alice")
|
|
|
|
|
|
|
|
date_sent = timezone_now() - datetime.timedelta(days=settings.INVITATION_LINK_VALIDITY_DAYS - 1)
|
|
|
|
invite_link = self.generate_multiuse_invite_link(date_sent=date_sent)
|
|
|
|
|
|
|
|
self.check_user_able_to_register(email1, invite_link)
|
|
|
|
self.check_user_able_to_register(email2, invite_link)
|
|
|
|
self.check_user_able_to_register(email3, invite_link)
|
|
|
|
|
|
|
|
def test_expired_multiuse_link(self):
|
|
|
|
# type: () -> None
|
|
|
|
email = self.nonreg_email('newuser')
|
|
|
|
date_sent = timezone_now() - datetime.timedelta(days=settings.INVITATION_LINK_VALIDITY_DAYS)
|
|
|
|
invite_link = self.generate_multiuse_invite_link(date_sent=date_sent)
|
|
|
|
result = self.client_post(invite_link, {'email': email})
|
|
|
|
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
self.assert_in_response("Whoops. The confirmation link has expired.", result)
|
|
|
|
|
|
|
|
def test_invalid_multiuse_link(self):
|
|
|
|
# type: () -> None
|
|
|
|
email = self.nonreg_email('newuser')
|
|
|
|
invite_link = "/join/invalid_key/"
|
|
|
|
result = self.client_post(invite_link, {'email': email})
|
|
|
|
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
self.assert_in_response("Whoops. The confirmation link is malformed.", result)
|
|
|
|
|
|
|
|
def test_invalid_multiuse_link_in_open_realm(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.realm.invite_required = False
|
|
|
|
self.realm.save()
|
|
|
|
|
|
|
|
email = self.nonreg_email('newuser')
|
|
|
|
invite_link = "/join/invalid_key/"
|
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
with patch('zerver.views.registration.get_realm_from_request', return_value=self.realm):
|
|
|
|
with patch('zerver.views.registration.get_realm', return_value=self.realm):
|
|
|
|
self.check_user_able_to_register(email, invite_link)
|
2017-08-10 22:34:17 +02:00
|
|
|
|
|
|
|
def test_multiuse_link_with_specified_streams(self):
|
|
|
|
# type: () -> None
|
|
|
|
name1 = "newuser"
|
|
|
|
name2 = "bob"
|
|
|
|
email1 = self.nonreg_email(name1)
|
|
|
|
email2 = self.nonreg_email(name2)
|
|
|
|
|
|
|
|
stream_names = ["Rome", "Scotland", "Venice"]
|
|
|
|
streams = [get_stream(stream_name, self.realm) for stream_name in stream_names]
|
|
|
|
invite_link = self.generate_multiuse_invite_link(streams=streams)
|
|
|
|
self.check_user_able_to_register(email1, invite_link)
|
|
|
|
self.check_user_subscribed_only_to_streams(name1, streams)
|
|
|
|
|
|
|
|
stream_names = ["Rome", "Verona"]
|
|
|
|
streams = [get_stream(stream_name, self.realm) for stream_name in stream_names]
|
|
|
|
invite_link = self.generate_multiuse_invite_link(streams=streams)
|
|
|
|
self.check_user_able_to_register(email2, invite_link)
|
|
|
|
self.check_user_subscribed_only_to_streams(name2, streams)
|
2016-07-29 18:16:54 +02:00
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class EmailUnsubscribeTests(ZulipTestCase):
|
2016-12-01 08:54:21 +01:00
|
|
|
def test_error_unsubscribe(self):
|
|
|
|
# type: () -> None
|
2017-02-26 21:48:38 +01:00
|
|
|
|
2017-11-07 20:29:37 +01:00
|
|
|
# An invalid unsubscribe token "test123" produces an error.
|
2016-12-01 08:54:21 +01:00
|
|
|
result = self.client_get('/accounts/unsubscribe/missed_messages/test123')
|
2017-11-07 20:29:37 +01:00
|
|
|
self.assert_in_response('Unknown email unsubscribe request', result)
|
2016-12-01 08:54:21 +01:00
|
|
|
|
2017-02-26 21:48:38 +01:00
|
|
|
# An unknown message type "fake" produces an error.
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-02-26 21:48:38 +01:00
|
|
|
unsubscribe_link = one_click_unsubscribe_link(user_profile, "fake")
|
|
|
|
result = self.client_get(urllib.parse.urlparse(unsubscribe_link).path)
|
|
|
|
self.assert_in_response('Unknown email unsubscribe request', result)
|
|
|
|
|
2014-01-31 21:08:40 +01:00
|
|
|
def test_missedmessage_unsubscribe(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 21:08:40 +01:00
|
|
|
"""
|
|
|
|
We provide one-click unsubscribe links in missed message
|
|
|
|
e-mails that you can click even when logged out to update your
|
|
|
|
email notification settings.
|
|
|
|
"""
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2014-01-31 21:08:40 +01:00
|
|
|
user_profile.enable_offline_email_notifications = True
|
|
|
|
user_profile.save()
|
|
|
|
|
|
|
|
unsubscribe_link = one_click_unsubscribe_link(user_profile,
|
|
|
|
"missed_messages")
|
2016-07-28 00:38:45 +02:00
|
|
|
result = self.client_get(urllib.parse.urlparse(unsubscribe_link).path)
|
2014-01-31 21:08:40 +01:00
|
|
|
|
|
|
|
self.assertEqual(result.status_code, 200)
|
2017-08-15 21:52:23 +02:00
|
|
|
|
|
|
|
user_profile.refresh_from_db()
|
2014-01-31 21:08:40 +01:00
|
|
|
self.assertFalse(user_profile.enable_offline_email_notifications)
|
|
|
|
|
|
|
|
def test_welcome_unsubscribe(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 21:08:40 +01:00
|
|
|
"""
|
|
|
|
We provide one-click unsubscribe links in welcome e-mails that you can
|
|
|
|
click even when logged out to stop receiving them.
|
|
|
|
"""
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2014-01-31 21:08:40 +01:00
|
|
|
# Simulate a new user signing up, which enqueues 2 welcome e-mails.
|
2017-09-22 04:29:01 +02:00
|
|
|
enqueue_welcome_emails(user_profile)
|
2017-07-12 02:26:10 +02:00
|
|
|
self.assertEqual(2, ScheduledEmail.objects.filter(user=user_profile).count())
|
2014-01-31 21:08:40 +01:00
|
|
|
|
|
|
|
# Simulate unsubscribing from the welcome e-mails.
|
|
|
|
unsubscribe_link = one_click_unsubscribe_link(user_profile, "welcome")
|
2016-07-28 00:38:45 +02:00
|
|
|
result = self.client_get(urllib.parse.urlparse(unsubscribe_link).path)
|
2014-01-31 21:08:40 +01:00
|
|
|
|
|
|
|
# The welcome email jobs are no longer scheduled.
|
|
|
|
self.assertEqual(result.status_code, 200)
|
2017-07-12 02:26:10 +02:00
|
|
|
self.assertEqual(0, ScheduledEmail.objects.filter(user=user_profile).count())
|
2014-01-31 21:08:40 +01:00
|
|
|
|
|
|
|
def test_digest_unsubscribe(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 21:08:40 +01:00
|
|
|
"""
|
|
|
|
We provide one-click unsubscribe links in digest e-mails that you can
|
|
|
|
click even when logged out to stop receiving them.
|
|
|
|
|
|
|
|
Unsubscribing from these emails also dequeues any digest email jobs that
|
|
|
|
have been queued.
|
|
|
|
"""
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2014-01-31 21:08:40 +01:00
|
|
|
self.assertTrue(user_profile.enable_digest_emails)
|
|
|
|
|
|
|
|
# Enqueue a fake digest email.
|
2017-05-04 06:42:23 +02:00
|
|
|
context = {'name': '', 'realm_uri': '', 'unread_pms': [], 'hot_conversations': [],
|
|
|
|
'new_users': [], 'new_streams': {'plain': []}, 'unsubscribe_link': ''}
|
2017-07-11 05:34:32 +02:00
|
|
|
send_future_email('zerver/emails/digest', to_user_id=user_profile.id, context=context)
|
2017-05-02 02:07:01 +02:00
|
|
|
|
2017-07-12 02:26:10 +02:00
|
|
|
self.assertEqual(1, ScheduledEmail.objects.filter(user=user_profile).count())
|
2014-01-31 21:08:40 +01:00
|
|
|
|
|
|
|
# Simulate unsubscribing from digest e-mails.
|
|
|
|
unsubscribe_link = one_click_unsubscribe_link(user_profile, "digest")
|
2016-07-28 00:38:45 +02:00
|
|
|
result = self.client_get(urllib.parse.urlparse(unsubscribe_link).path)
|
2014-01-31 21:08:40 +01:00
|
|
|
|
|
|
|
# The setting is toggled off, and scheduled jobs have been removed.
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
# Circumvent user_profile caching.
|
2017-08-15 21:52:23 +02:00
|
|
|
|
|
|
|
user_profile.refresh_from_db()
|
2014-01-31 21:08:40 +01:00
|
|
|
self.assertFalse(user_profile.enable_digest_emails)
|
2017-07-12 02:26:10 +02:00
|
|
|
self.assertEqual(0, ScheduledEmail.objects.filter(user=user_profile).count())
|
2014-01-31 21:08:40 +01:00
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class RealmCreationTest(ZulipTestCase):
|
2016-06-03 01:02:58 +02:00
|
|
|
def test_create_realm(self):
|
|
|
|
# type: () -> None
|
|
|
|
password = "test"
|
2016-10-31 23:28:20 +01:00
|
|
|
string_id = "zuliptest"
|
2016-06-03 01:02:58 +02:00
|
|
|
email = "user1@test.com"
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm('test')
|
2016-06-03 01:02:58 +02:00
|
|
|
|
|
|
|
# Make sure the realm does not exist
|
2016-11-07 21:49:36 +01:00
|
|
|
self.assertIsNone(realm)
|
2016-06-03 01:02:58 +02:00
|
|
|
|
|
|
|
with self.settings(OPEN_REALM_CREATION=True):
|
|
|
|
# Create new realm with the email
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post('/create_realm/', {'email': email})
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2016-06-03 01:02:58 +02:00
|
|
|
self.assertTrue(result["Location"].endswith(
|
2017-01-24 07:06:13 +01:00
|
|
|
"/accounts/send_confirm/%s" % (email,)))
|
2016-07-28 00:38:45 +02:00
|
|
|
result = self.client_get(result["Location"])
|
2016-07-12 15:41:45 +02:00
|
|
|
self.assert_in_response("Check your email so we can get started.", result)
|
2016-06-03 01:02:58 +02:00
|
|
|
|
|
|
|
# Visit the confirmation link.
|
2016-09-23 04:23:48 +02:00
|
|
|
confirmation_url = self.get_confirmation_url_from_outbox(email)
|
2016-07-28 00:38:45 +02:00
|
|
|
result = self.client_get(confirmation_url)
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(result.status_code, 200)
|
2016-06-03 01:02:58 +02:00
|
|
|
|
2017-01-04 08:53:56 +01:00
|
|
|
result = self.submit_reg_form_for_user(email, password, realm_subdomain=string_id)
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2017-10-27 02:45:38 +02:00
|
|
|
self.assertTrue(result["Location"].startswith('http://zuliptest.testserver/accounts/login/subdomain/'))
|
2016-06-03 01:02:58 +02:00
|
|
|
|
|
|
|
# Make sure the realm is created
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm(string_id)
|
2016-06-03 01:02:58 +02:00
|
|
|
self.assertIsNotNone(realm)
|
2016-10-28 07:21:53 +02:00
|
|
|
self.assertEqual(realm.string_id, string_id)
|
2017-05-24 02:42:31 +02:00
|
|
|
self.assertEqual(get_user(email, realm).realm, realm)
|
2016-06-03 01:02:58 +02:00
|
|
|
|
2016-09-16 19:05:14 +02:00
|
|
|
# Check defaults
|
2017-06-26 23:58:50 +02:00
|
|
|
self.assertEqual(realm.org_type, Realm.CORPORATE)
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(realm.restricted_to_domain, False)
|
|
|
|
self.assertEqual(realm.invite_required, True)
|
2016-09-16 19:05:14 +02:00
|
|
|
|
2017-05-08 20:25:03 +02:00
|
|
|
# Check welcome messages
|
2017-07-15 05:06:58 +02:00
|
|
|
for stream_name, text, message_count in [
|
|
|
|
('announce', 'This is', 1),
|
|
|
|
('core team', 'This is', 1),
|
|
|
|
('general', 'Welcome to', 1),
|
|
|
|
('new members', 'stream is', 1),
|
|
|
|
('zulip', 'Here is', 3)]:
|
2017-05-08 20:25:03 +02:00
|
|
|
stream = get_stream(stream_name, realm)
|
2017-10-28 20:26:11 +02:00
|
|
|
recipient = get_stream_recipient(stream.id)
|
2017-07-15 05:06:58 +02:00
|
|
|
messages = Message.objects.filter(recipient=recipient).order_by('pub_date')
|
|
|
|
self.assertEqual(len(messages), message_count)
|
|
|
|
self.assertIn(text, messages[0].content)
|
2017-05-08 20:25:03 +02:00
|
|
|
|
2017-03-11 20:12:01 +01:00
|
|
|
def test_create_realm_existing_email(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
Trying to create a realm with an existing email should just redirect to
|
|
|
|
a login page.
|
|
|
|
"""
|
|
|
|
with self.settings(OPEN_REALM_CREATION=True):
|
2017-05-25 01:40:26 +02:00
|
|
|
email = self.example_email("hamlet")
|
2017-03-11 20:12:01 +01:00
|
|
|
result = self.client_post('/create_realm/', {'email': email})
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertIn('login', result['Location'])
|
|
|
|
|
|
|
|
def test_create_realm_no_creation_key(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
Trying to create a realm without a creation_key should fail when
|
|
|
|
OPEN_REALM_CREATION is false.
|
|
|
|
"""
|
|
|
|
email = "user1@test.com"
|
|
|
|
realm = get_realm('test')
|
|
|
|
|
|
|
|
# Make sure the realm does not exist
|
|
|
|
self.assertIsNone(realm)
|
|
|
|
|
|
|
|
with self.settings(OPEN_REALM_CREATION=False):
|
|
|
|
# Create new realm with the email, but no creation key.
|
|
|
|
result = self.client_post('/create_realm/', {'email': email})
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
self.assert_in_response('New organization creation disabled.', result)
|
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
@override_settings(OPEN_REALM_CREATION=True)
|
2016-07-19 14:35:08 +02:00
|
|
|
def test_create_realm_with_subdomain(self):
|
|
|
|
# type: () -> None
|
|
|
|
password = "test"
|
2016-10-31 23:28:20 +01:00
|
|
|
string_id = "zuliptest"
|
2016-07-19 14:35:08 +02:00
|
|
|
email = "user1@test.com"
|
|
|
|
realm_name = "Test"
|
|
|
|
|
|
|
|
# Make sure the realm does not exist
|
2017-01-04 05:30:48 +01:00
|
|
|
self.assertIsNone(get_realm('test'))
|
2016-11-04 01:33:46 +01:00
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
# Create new realm with the email
|
|
|
|
result = self.client_post('/create_realm/', {'email': email})
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertTrue(result["Location"].endswith(
|
|
|
|
"/accounts/send_confirm/%s" % (email,)))
|
|
|
|
result = self.client_get(result["Location"])
|
|
|
|
self.assert_in_response("Check your email so we can get started.", result)
|
2016-07-19 14:35:08 +02:00
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
# Visit the confirmation link.
|
|
|
|
confirmation_url = self.get_confirmation_url_from_outbox(email)
|
|
|
|
result = self.client_get(confirmation_url)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
2016-07-19 14:35:08 +02:00
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
result = self.submit_reg_form_for_user(email, password,
|
|
|
|
realm_subdomain = string_id,
|
|
|
|
realm_name=realm_name,
|
|
|
|
# Pass HTTP_HOST for the target subdomain
|
|
|
|
HTTP_HOST=string_id + ".testserver")
|
|
|
|
self.assertEqual(result.status_code, 302)
|
2016-07-19 14:35:08 +02:00
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
# Make sure the realm is created
|
|
|
|
realm = get_realm(string_id)
|
|
|
|
self.assertIsNotNone(realm)
|
|
|
|
self.assertEqual(realm.string_id, string_id)
|
|
|
|
self.assertEqual(get_user(email, realm).realm, realm)
|
2016-10-13 20:09:32 +02:00
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
self.assertEqual(realm.name, realm_name)
|
|
|
|
self.assertEqual(realm.subdomain, string_id)
|
2016-11-04 01:33:46 +01:00
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
@override_settings(OPEN_REALM_CREATION=True)
|
2016-11-05 03:26:30 +01:00
|
|
|
def test_mailinator_signup(self):
|
|
|
|
# type: () -> None
|
2017-10-02 22:43:43 +02:00
|
|
|
result = self.client_post('/create_realm/', {'email': "hi@mailinator.com"})
|
|
|
|
self.assert_in_response('Please use your real email address.', result)
|
2016-11-05 03:26:30 +01:00
|
|
|
|
2017-10-02 22:43:43 +02:00
|
|
|
@override_settings(OPEN_REALM_CREATION=True)
|
2016-10-31 23:28:20 +01:00
|
|
|
def test_subdomain_restrictions(self):
|
|
|
|
# type: () -> None
|
|
|
|
password = "test"
|
|
|
|
email = "user1@test.com"
|
|
|
|
realm_name = "Test"
|
|
|
|
|
2017-10-02 22:48:13 +02:00
|
|
|
result = self.client_post('/create_realm/', {'email': email})
|
|
|
|
self.client_get(result["Location"])
|
|
|
|
confirmation_url = self.get_confirmation_url_from_outbox(email)
|
|
|
|
self.client_get(confirmation_url)
|
|
|
|
|
2017-10-03 00:31:35 +02:00
|
|
|
errors = {'id': "length 3 or greater",
|
2017-10-02 22:48:13 +02:00
|
|
|
'-id': "cannot start or end with a",
|
|
|
|
'string-ID': "lowercase letters",
|
|
|
|
'string_id': "lowercase letters",
|
|
|
|
'stream': "unavailable",
|
|
|
|
'streams': "unavailable",
|
|
|
|
'about': "unavailable",
|
|
|
|
'abouts': "unavailable",
|
|
|
|
'zephyr': "unavailable"}
|
|
|
|
for string_id, error_msg in errors.items():
|
2017-01-04 08:53:56 +01:00
|
|
|
result = self.submit_reg_form_for_user(email, password,
|
2017-10-02 22:48:13 +02:00
|
|
|
realm_subdomain = string_id,
|
2016-10-31 23:28:20 +01:00
|
|
|
realm_name = realm_name)
|
2017-10-02 22:48:13 +02:00
|
|
|
self.assert_in_response(error_msg, result)
|
|
|
|
|
|
|
|
# test valid subdomain
|
|
|
|
result = self.submit_reg_form_for_user(email, password,
|
|
|
|
realm_subdomain = 'a-0',
|
|
|
|
realm_name = realm_name)
|
|
|
|
self.assertEqual(result.status_code, 302)
|
2017-10-27 02:45:38 +02:00
|
|
|
self.assertTrue(result.url.startswith('http://a-0.testserver/accounts/login/subdomain/'))
|
2017-10-19 08:30:40 +02:00
|
|
|
|
|
|
|
@override_settings(OPEN_REALM_CREATION=True)
|
|
|
|
def test_subdomain_restrictions_root_domain(self):
|
|
|
|
# type: () -> None
|
|
|
|
password = "test"
|
|
|
|
email = "user1@test.com"
|
|
|
|
realm_name = "Test"
|
|
|
|
|
|
|
|
result = self.client_post('/create_realm/', {'email': email})
|
|
|
|
self.client_get(result["Location"])
|
|
|
|
confirmation_url = self.get_confirmation_url_from_outbox(email)
|
|
|
|
self.client_get(confirmation_url)
|
|
|
|
|
|
|
|
# test root domain will fail with ROOT_DOMAIN_LANDING_PAGE
|
|
|
|
with self.settings(ROOT_DOMAIN_LANDING_PAGE=True):
|
|
|
|
result = self.submit_reg_form_for_user(email, password,
|
|
|
|
realm_subdomain = '',
|
|
|
|
realm_name = realm_name)
|
|
|
|
self.assert_in_response('unavailable', result)
|
|
|
|
|
|
|
|
# test valid use of root domain
|
|
|
|
result = self.submit_reg_form_for_user(email, password,
|
|
|
|
realm_subdomain = '',
|
|
|
|
realm_name = realm_name)
|
|
|
|
self.assertEqual(result.status_code, 302)
|
2017-10-27 02:45:38 +02:00
|
|
|
self.assertTrue(result.url.startswith('http://testserver/accounts/login/subdomain/'))
|
2017-10-19 08:30:40 +02:00
|
|
|
|
|
|
|
@override_settings(OPEN_REALM_CREATION=True)
|
|
|
|
def test_subdomain_restrictions_root_domain_option(self):
|
|
|
|
# type: () -> None
|
|
|
|
password = "test"
|
|
|
|
email = "user1@test.com"
|
|
|
|
realm_name = "Test"
|
|
|
|
|
|
|
|
result = self.client_post('/create_realm/', {'email': email})
|
|
|
|
self.client_get(result["Location"])
|
|
|
|
confirmation_url = self.get_confirmation_url_from_outbox(email)
|
|
|
|
self.client_get(confirmation_url)
|
|
|
|
|
|
|
|
# test root domain will fail with ROOT_DOMAIN_LANDING_PAGE
|
|
|
|
with self.settings(ROOT_DOMAIN_LANDING_PAGE=True):
|
|
|
|
result = self.submit_reg_form_for_user(email, password,
|
|
|
|
realm_subdomain = 'abcdef',
|
|
|
|
realm_in_root_domain = 'true',
|
|
|
|
realm_name = realm_name)
|
|
|
|
self.assert_in_response('unavailable', result)
|
|
|
|
|
|
|
|
# test valid use of root domain
|
|
|
|
result = self.submit_reg_form_for_user(email, password,
|
|
|
|
realm_subdomain = 'abcdef',
|
|
|
|
realm_in_root_domain = 'true',
|
|
|
|
realm_name = realm_name)
|
|
|
|
self.assertEqual(result.status_code, 302)
|
2017-10-27 02:45:38 +02:00
|
|
|
self.assertTrue(result.url.startswith('http://testserver/accounts/login/subdomain/'))
|
2016-10-31 23:28:20 +01:00
|
|
|
|
2017-10-19 07:42:03 +02:00
|
|
|
def test_is_root_domain_available(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.assertTrue(is_root_domain_available())
|
|
|
|
with self.settings(ROOT_DOMAIN_LANDING_PAGE=True):
|
|
|
|
self.assertFalse(is_root_domain_available())
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
realm.string_id = Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
|
|
|
|
realm.save()
|
|
|
|
self.assertFalse(is_root_domain_available())
|
|
|
|
|
2016-11-04 01:23:43 +01:00
|
|
|
class UserSignUpTest(ZulipTestCase):
|
|
|
|
|
2017-08-17 18:28:21 +02:00
|
|
|
def _assert_redirected_to(self, result, url):
|
|
|
|
# type: (HttpResponse, Text) -> None
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertEqual(result['LOCATION'], url)
|
|
|
|
|
|
|
|
def test_bad_email_configuration_for_accounts_home(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
Make sure we redirect for SMTP errors.
|
|
|
|
"""
|
|
|
|
email = self.nonreg_email('newguy')
|
|
|
|
|
|
|
|
smtp_mock = patch(
|
|
|
|
'zerver.views.registration.send_registration_completion_email',
|
|
|
|
side_effect=smtplib.SMTPException('uh oh')
|
|
|
|
)
|
|
|
|
|
|
|
|
error_mock = patch('logging.error')
|
|
|
|
|
|
|
|
with smtp_mock, error_mock as err:
|
|
|
|
result = self.client_post('/accounts/home/', {'email': email})
|
|
|
|
|
|
|
|
self._assert_redirected_to(result, '/config-error/smtp')
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
err.call_args_list[0][0][0],
|
|
|
|
'Error in accounts_home: uh oh'
|
|
|
|
)
|
|
|
|
|
2017-08-17 19:59:17 +02:00
|
|
|
def test_bad_email_configuration_for_create_realm(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
Make sure we redirect for SMTP errors.
|
|
|
|
"""
|
|
|
|
email = self.nonreg_email('newguy')
|
|
|
|
|
|
|
|
smtp_mock = patch(
|
|
|
|
'zerver.views.registration.send_registration_completion_email',
|
|
|
|
side_effect=smtplib.SMTPException('uh oh')
|
|
|
|
)
|
|
|
|
|
|
|
|
error_mock = patch('logging.error')
|
|
|
|
|
|
|
|
with smtp_mock, error_mock as err:
|
|
|
|
result = self.client_post('/create_realm/', {'email': email})
|
|
|
|
|
|
|
|
self._assert_redirected_to(result, '/config-error/smtp')
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
err.call_args_list[0][0][0],
|
|
|
|
'Error in create_realm: uh oh'
|
|
|
|
)
|
|
|
|
|
2017-05-04 15:20:25 +02:00
|
|
|
def test_user_default_language_and_timezone(self):
|
2016-11-04 01:23:43 +01:00
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
Check if the default language of new user is the default language
|
|
|
|
of the realm.
|
|
|
|
"""
|
2017-05-24 02:42:31 +02:00
|
|
|
email = self.nonreg_email('newguy')
|
2016-11-04 01:23:43 +01:00
|
|
|
password = "newpassword"
|
2017-05-04 15:20:25 +02:00
|
|
|
timezone = "US/Mountain"
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm('zulip')
|
2017-03-24 02:37:12 +01:00
|
|
|
do_set_realm_property(realm, 'default_language', u"de")
|
2016-11-04 01:23:43 +01:00
|
|
|
|
|
|
|
result = self.client_post('/accounts/home/', {'email': email})
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2016-11-04 01:23:43 +01:00
|
|
|
self.assertTrue(result["Location"].endswith(
|
2017-01-24 07:06:13 +01:00
|
|
|
"/accounts/send_confirm/%s" % (email,)))
|
2016-11-04 01:23:43 +01:00
|
|
|
result = self.client_get(result["Location"])
|
|
|
|
self.assert_in_response("Check your email so we can get started.", result)
|
|
|
|
|
|
|
|
# Visit the confirmation link.
|
|
|
|
confirmation_url = self.get_confirmation_url_from_outbox(email)
|
|
|
|
result = self.client_get(confirmation_url)
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(result.status_code, 200)
|
2016-11-04 01:23:43 +01:00
|
|
|
|
|
|
|
# Pick a password and agree to the ToS.
|
2017-05-04 15:20:25 +02:00
|
|
|
result = self.submit_reg_form_for_user(email, password, timezone=timezone)
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2016-11-04 01:23:43 +01:00
|
|
|
|
2017-05-24 02:42:31 +02:00
|
|
|
user_profile = self.nonreg_user('newguy')
|
2016-11-04 01:23:43 +01:00
|
|
|
self.assertEqual(user_profile.default_language, realm.default_language)
|
2017-05-04 15:20:25 +02:00
|
|
|
self.assertEqual(user_profile.timezone, timezone)
|
2016-11-04 01:23:43 +01:00
|
|
|
from django.core.mail import outbox
|
|
|
|
outbox.pop()
|
|
|
|
|
2017-03-11 20:12:01 +01:00
|
|
|
def test_signup_already_active(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
Check if signing up with an active email redirects to a login page.
|
|
|
|
"""
|
2017-05-25 01:40:26 +02:00
|
|
|
email = self.example_email("hamlet")
|
2017-03-11 20:12:01 +01:00
|
|
|
result = self.client_post('/accounts/home/', {'email': email})
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertIn('login', result['Location'])
|
2017-08-28 08:13:15 +02:00
|
|
|
result = self.client_get(result.url)
|
|
|
|
self.assert_in_response("You've already registered", result)
|
2017-03-11 20:12:01 +01:00
|
|
|
|
2017-02-08 05:04:14 +01:00
|
|
|
def test_signup_invalid_name(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
2017-03-11 20:10:15 +01:00
|
|
|
Check if an invalid name during signup is handled properly.
|
2017-02-08 05:04:14 +01:00
|
|
|
"""
|
|
|
|
email = "newguy@zulip.com"
|
|
|
|
password = "newpassword"
|
|
|
|
|
|
|
|
result = self.client_post('/accounts/home/', {'email': email})
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertTrue(result["Location"].endswith(
|
|
|
|
"/accounts/send_confirm/%s" % (email,)))
|
|
|
|
result = self.client_get(result["Location"])
|
|
|
|
self.assert_in_response("Check your email so we can get started.", result)
|
|
|
|
|
|
|
|
# Visit the confirmation link.
|
|
|
|
confirmation_url = self.get_confirmation_url_from_outbox(email)
|
|
|
|
result = self.client_get(confirmation_url)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
# Pick a password and agree to the ToS.
|
|
|
|
result = self.submit_reg_form_for_user(email, password, full_name="<invalid>")
|
2017-03-18 22:48:44 +01:00
|
|
|
self.assert_in_success_response(["Invalid characters in name!"], result)
|
2017-02-08 05:04:14 +01:00
|
|
|
|
2017-08-09 22:09:38 +02:00
|
|
|
# Verify that the user is asked for name and password
|
|
|
|
self.assert_in_success_response(['id_password', 'id_full_name'], result)
|
|
|
|
|
2017-03-19 01:48:12 +01:00
|
|
|
def test_signup_without_password(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
Check if signing up without a password works properly when
|
|
|
|
password_auth_enabled is False.
|
|
|
|
"""
|
|
|
|
|
2017-05-24 02:42:31 +02:00
|
|
|
email = self.nonreg_email('newuser')
|
2017-03-19 01:48:12 +01:00
|
|
|
|
|
|
|
result = self.client_post('/accounts/home/', {'email': email})
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertTrue(result["Location"].endswith(
|
|
|
|
"/accounts/send_confirm/%s" % (email,)))
|
|
|
|
result = self.client_get(result["Location"])
|
|
|
|
self.assert_in_response("Check your email so we can get started.", result)
|
|
|
|
|
|
|
|
# Visit the confirmation link.
|
|
|
|
confirmation_url = self.get_confirmation_url_from_outbox(email)
|
|
|
|
result = self.client_get(confirmation_url)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
with patch('zerver.views.registration.password_auth_enabled', return_value=False):
|
|
|
|
result = self.client_post(
|
|
|
|
'/accounts/register/',
|
|
|
|
{'full_name': 'New User',
|
|
|
|
'key': find_key_by_email(email),
|
|
|
|
'terms': True})
|
|
|
|
|
|
|
|
# User should now be logged in.
|
|
|
|
self.assertEqual(result.status_code, 302)
|
2017-05-24 02:42:31 +02:00
|
|
|
user_profile = self.nonreg_user('newuser')
|
2017-03-19 01:48:12 +01:00
|
|
|
self.assertEqual(get_session_dict_user(self.client.session), user_profile.id)
|
|
|
|
|
2017-03-18 20:36:40 +01:00
|
|
|
def test_signup_without_full_name(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
Check if signing up without a full name redirects to a registration
|
|
|
|
form.
|
|
|
|
"""
|
|
|
|
email = "newguy@zulip.com"
|
|
|
|
password = "newpassword"
|
|
|
|
|
|
|
|
result = self.client_post('/accounts/home/', {'email': email})
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertTrue(result["Location"].endswith(
|
|
|
|
"/accounts/send_confirm/%s" % (email,)))
|
|
|
|
result = self.client_get(result["Location"])
|
|
|
|
self.assert_in_response("Check your email so we can get started.", result)
|
|
|
|
|
|
|
|
# Visit the confirmation link.
|
|
|
|
confirmation_url = self.get_confirmation_url_from_outbox(email)
|
|
|
|
result = self.client_get(confirmation_url)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
result = self.client_post(
|
|
|
|
'/accounts/register/',
|
|
|
|
{'password': password,
|
|
|
|
'key': find_key_by_email(email),
|
|
|
|
'terms': True,
|
|
|
|
'from_confirmation': '1'})
|
|
|
|
self.assert_in_success_response(["You're almost there."], result)
|
|
|
|
|
2017-08-09 22:09:38 +02:00
|
|
|
# Verify that the user is asked for name and password
|
|
|
|
self.assert_in_success_response(['id_password', 'id_full_name'], result)
|
|
|
|
|
2017-04-20 08:25:15 +02:00
|
|
|
def test_signup_with_full_name(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
Check if signing up without a full name redirects to a registration
|
|
|
|
form.
|
|
|
|
"""
|
|
|
|
email = "newguy@zulip.com"
|
|
|
|
password = "newpassword"
|
|
|
|
|
|
|
|
result = self.client_post('/accounts/home/', {'email': email})
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertTrue(result["Location"].endswith(
|
|
|
|
"/accounts/send_confirm/%s" % (email,)))
|
|
|
|
result = self.client_get(result["Location"])
|
|
|
|
self.assert_in_response("Check your email so we can get started.", result)
|
|
|
|
|
|
|
|
# Visit the confirmation link.
|
|
|
|
confirmation_url = self.get_confirmation_url_from_outbox(email)
|
|
|
|
result = self.client_get(confirmation_url)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
result = self.client_post(
|
|
|
|
'/accounts/register/',
|
|
|
|
{'password': password,
|
|
|
|
'key': find_key_by_email(email),
|
|
|
|
'terms': True,
|
|
|
|
'full_name': "New Guy",
|
|
|
|
'from_confirmation': '1'})
|
|
|
|
self.assert_in_success_response(["You're almost there."], result)
|
|
|
|
|
2017-11-16 23:00:04 +01:00
|
|
|
def test_signup_with_default_stream_group(self) -> None:
|
|
|
|
# Check if user is subscribed to the streams of default
|
|
|
|
# stream group as well as default streams.
|
|
|
|
email = self.nonreg_email('newguy')
|
|
|
|
password = "newpassword"
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
|
|
|
|
result = self.client_post('/accounts/home/', {'email': email})
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
result = self.client_get(result["Location"])
|
|
|
|
|
|
|
|
confirmation_url = self.get_confirmation_url_from_outbox(email)
|
|
|
|
result = self.client_get(confirmation_url)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
default_streams = []
|
|
|
|
for stream_name in ["venice", "verona"]:
|
|
|
|
stream = get_stream(stream_name, realm)
|
|
|
|
do_add_default_stream(stream)
|
|
|
|
default_streams.append(stream)
|
|
|
|
|
|
|
|
group1_streams = []
|
|
|
|
for stream_name in ["scotland", "denmark"]:
|
|
|
|
stream = get_stream(stream_name, realm)
|
|
|
|
group1_streams.append(stream)
|
|
|
|
do_create_default_stream_group(realm, "group 1", "group 1 description", group1_streams)
|
|
|
|
|
|
|
|
result = self.submit_reg_form_for_user(email, password, default_stream_groups=["group 1"])
|
|
|
|
self.check_user_subscribed_only_to_streams("newguy", default_streams + group1_streams)
|
|
|
|
|
|
|
|
def test_signup_with_multiple_default_stream_groups(self) -> None:
|
|
|
|
# Check if user is subscribed to the streams of default
|
|
|
|
# stream groups as well as default streams.
|
|
|
|
email = self.nonreg_email('newguy')
|
|
|
|
password = "newpassword"
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
|
|
|
|
result = self.client_post('/accounts/home/', {'email': email})
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
result = self.client_get(result["Location"])
|
|
|
|
|
|
|
|
confirmation_url = self.get_confirmation_url_from_outbox(email)
|
|
|
|
result = self.client_get(confirmation_url)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
default_streams = []
|
|
|
|
for stream_name in ["venice", "verona"]:
|
|
|
|
stream = get_stream(stream_name, realm)
|
|
|
|
do_add_default_stream(stream)
|
|
|
|
default_streams.append(stream)
|
|
|
|
|
|
|
|
group1_streams = []
|
|
|
|
for stream_name in ["scotland", "denmark"]:
|
|
|
|
stream = get_stream(stream_name, realm)
|
|
|
|
group1_streams.append(stream)
|
|
|
|
do_create_default_stream_group(realm, "group 1", "group 1 description", group1_streams)
|
|
|
|
|
|
|
|
group2_streams = []
|
|
|
|
for stream_name in ["scotland", "rome"]:
|
|
|
|
stream = get_stream(stream_name, realm)
|
|
|
|
group2_streams.append(stream)
|
|
|
|
do_create_default_stream_group(realm, "group 2", "group 2 description", group2_streams)
|
|
|
|
|
2017-11-17 07:00:53 +01:00
|
|
|
result = self.submit_reg_form_for_user(email, password,
|
|
|
|
default_stream_groups=["group 1", "group 2"])
|
|
|
|
self.check_user_subscribed_only_to_streams(
|
|
|
|
"newguy", list(set(default_streams + group1_streams + group2_streams)))
|
2017-11-16 23:00:04 +01:00
|
|
|
|
2017-03-19 01:48:12 +01:00
|
|
|
def test_signup_invalid_subdomain(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
Check if attempting to authenticate to the wrong subdomain logs an
|
|
|
|
error and redirects.
|
|
|
|
"""
|
|
|
|
email = "newuser@zulip.com"
|
|
|
|
password = "newpassword"
|
|
|
|
|
|
|
|
result = self.client_post('/accounts/home/', {'email': email})
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertTrue(result["Location"].endswith(
|
|
|
|
"/accounts/send_confirm/%s" % (email,)))
|
|
|
|
result = self.client_get(result["Location"])
|
|
|
|
self.assert_in_response("Check your email so we can get started.", result)
|
|
|
|
|
|
|
|
# Visit the confirmation link.
|
|
|
|
confirmation_url = self.get_confirmation_url_from_outbox(email)
|
|
|
|
result = self.client_get(confirmation_url)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
def invalid_subdomain(**kwargs):
|
|
|
|
# type: (**Any) -> Any
|
|
|
|
return_data = kwargs.get('return_data', {})
|
|
|
|
return_data['invalid_subdomain'] = True
|
|
|
|
|
|
|
|
with patch('zerver.views.registration.authenticate', side_effect=invalid_subdomain):
|
|
|
|
with patch('logging.error') as mock_error:
|
|
|
|
result = self.client_post(
|
|
|
|
'/accounts/register/',
|
|
|
|
{'password': password,
|
|
|
|
'full_name': 'New User',
|
|
|
|
'key': find_key_by_email(email),
|
|
|
|
'terms': True})
|
|
|
|
mock_error.assert_called_once()
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
|
2016-11-09 00:47:27 +01:00
|
|
|
def test_failed_signup_due_to_restricted_domain(self):
|
|
|
|
# type: () -> None
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm('zulip')
|
2017-01-11 23:06:33 +01:00
|
|
|
realm.invite_required = False
|
|
|
|
realm.save()
|
2017-10-02 22:43:43 +02:00
|
|
|
|
|
|
|
request = HostRequestMock(host = realm.host)
|
|
|
|
request.session = {} # type: ignore
|
|
|
|
email = 'user@acme.com'
|
|
|
|
form = HomepageForm({'email': email}, realm=realm)
|
|
|
|
self.assertIn("Your email address, {}, is not in one of the domains".format(email),
|
|
|
|
form.errors['email'][0])
|
2016-11-09 00:47:27 +01:00
|
|
|
|
|
|
|
def test_failed_signup_due_to_invite_required(self):
|
|
|
|
# type: () -> None
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm('zulip')
|
2016-11-09 00:47:27 +01:00
|
|
|
realm.invite_required = True
|
|
|
|
realm.save()
|
|
|
|
request = HostRequestMock(host = realm.host)
|
2017-07-11 21:41:31 +02:00
|
|
|
request.session = {} # type: ignore
|
2017-04-14 11:01:24 +02:00
|
|
|
email = 'user@zulip.com'
|
|
|
|
form = HomepageForm({'email': email}, realm=realm)
|
|
|
|
self.assertIn("Please request an invite for {} from".format(email),
|
|
|
|
form.errors['email'][0])
|
2016-11-09 00:47:27 +01:00
|
|
|
|
|
|
|
def test_failed_signup_due_to_nonexistent_realm(self):
|
|
|
|
# type: () -> None
|
2017-10-02 22:43:43 +02:00
|
|
|
request = HostRequestMock(host = 'acme.' + settings.EXTERNAL_HOST)
|
|
|
|
request.session = {} # type: ignore
|
|
|
|
email = 'user@acme.com'
|
|
|
|
form = HomepageForm({'email': email}, realm=None)
|
|
|
|
self.assertIn("organization you are trying to join using {} does "
|
|
|
|
"not exist".format(email), form.errors['email'][0])
|
2016-11-09 00:47:27 +01:00
|
|
|
|
2017-10-23 21:12:59 +02:00
|
|
|
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipLDAPAuthBackend',
|
|
|
|
'zproject.backends.ZulipDummyBackend'))
|
2017-09-28 09:41:19 +02:00
|
|
|
def test_ldap_registration_from_confirmation(self):
|
2016-11-02 11:10:21 +01:00
|
|
|
# type: () -> None
|
|
|
|
password = "testing"
|
|
|
|
email = "newuser@zulip.com"
|
|
|
|
subdomain = "zulip"
|
|
|
|
ldap_user_attr_map = {'full_name': 'fn', 'short_name': 'sn'}
|
|
|
|
|
|
|
|
ldap_patcher = patch('django_auth_ldap.config.ldap.initialize')
|
|
|
|
mock_initialize = ldap_patcher.start()
|
|
|
|
mock_ldap = MockLDAP()
|
|
|
|
mock_initialize.return_value = mock_ldap
|
|
|
|
|
|
|
|
mock_ldap.directory = {
|
|
|
|
'uid=newuser,ou=users,dc=zulip,dc=com': {
|
|
|
|
'userPassword': 'testing',
|
2017-09-28 09:41:19 +02:00
|
|
|
'fn': ['New LDAP fullname']
|
2016-11-02 11:10:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 21:46:03 +01:00
|
|
|
with patch('zerver.views.registration.get_subdomain', return_value=subdomain):
|
2016-11-02 11:10:21 +01:00
|
|
|
result = self.client_post('/register/', {'email': email})
|
|
|
|
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2016-11-02 11:10:21 +01:00
|
|
|
self.assertTrue(result["Location"].endswith(
|
2017-01-24 07:06:13 +01:00
|
|
|
"/accounts/send_confirm/%s" % (email,)))
|
2016-11-02 11:10:21 +01:00
|
|
|
result = self.client_get(result["Location"])
|
|
|
|
self.assert_in_response("Check your email so we can get started.", result)
|
|
|
|
# Visit the confirmation link.
|
|
|
|
from django.core.mail import outbox
|
|
|
|
for message in reversed(outbox):
|
|
|
|
if email in message.to:
|
|
|
|
confirmation_link_pattern = re.compile(settings.EXTERNAL_HOST + "(\S+)>")
|
|
|
|
confirmation_url = confirmation_link_pattern.search(
|
|
|
|
message.body).groups()[0]
|
|
|
|
break
|
|
|
|
else:
|
2017-03-05 08:01:50 +01:00
|
|
|
raise AssertionError("Couldn't find a confirmation email.")
|
2016-11-02 11:10:21 +01:00
|
|
|
|
|
|
|
with self.settings(
|
|
|
|
POPULATE_PROFILE_VIA_LDAP=True,
|
|
|
|
LDAP_APPEND_DOMAIN='zulip.com',
|
|
|
|
AUTH_LDAP_BIND_PASSWORD='',
|
|
|
|
AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map,
|
|
|
|
AUTH_LDAP_USER_DN_TEMPLATE='uid=%(user)s,ou=users,dc=zulip,dc=com'):
|
|
|
|
result = self.client_get(confirmation_url)
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(result.status_code, 200)
|
2017-03-19 01:48:12 +01:00
|
|
|
|
2017-09-28 09:41:19 +02:00
|
|
|
# Full name should be set from LDAP
|
|
|
|
result = self.submit_reg_form_for_user(email,
|
|
|
|
password,
|
|
|
|
full_name="Ignore",
|
|
|
|
from_confirmation="1",
|
|
|
|
# Pass HTTP_HOST for the target subdomain
|
|
|
|
HTTP_HOST=subdomain + ".testserver")
|
|
|
|
|
2017-03-19 01:48:12 +01:00
|
|
|
self.assert_in_success_response(["You're almost there.",
|
2017-09-28 09:41:19 +02:00
|
|
|
"New LDAP fullname",
|
2017-03-19 01:48:12 +01:00
|
|
|
"newuser@zulip.com"],
|
|
|
|
result)
|
|
|
|
|
2017-08-09 22:09:38 +02:00
|
|
|
# Verify that the user is asked for name
|
|
|
|
self.assert_in_success_response(['id_full_name'], result)
|
|
|
|
# TODO: Ideally, we wouldn't ask for a password if LDAP is
|
|
|
|
# enabled, in which case this assert should be invertedq.
|
|
|
|
self.assert_in_success_response(['id_password'], result)
|
|
|
|
|
2016-11-02 11:10:21 +01:00
|
|
|
# Test the TypeError exception handler
|
|
|
|
mock_ldap.directory = {
|
|
|
|
'uid=newuser,ou=users,dc=zulip,dc=com': {
|
|
|
|
'userPassword': 'testing',
|
|
|
|
'fn': None # This will raise TypeError
|
|
|
|
}
|
|
|
|
}
|
2017-06-15 19:24:46 +02:00
|
|
|
result = self.submit_reg_form_for_user(email,
|
|
|
|
password,
|
|
|
|
from_confirmation='1',
|
|
|
|
# Pass HTTP_HOST for the target subdomain
|
|
|
|
HTTP_HOST=subdomain + ".testserver")
|
|
|
|
self.assert_in_success_response(["You're almost there.",
|
|
|
|
"newuser@zulip.com"],
|
|
|
|
result)
|
|
|
|
|
2017-10-23 21:12:59 +02:00
|
|
|
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipLDAPAuthBackend',
|
|
|
|
'zproject.backends.ZulipDummyBackend'))
|
2017-09-29 08:43:18 +02:00
|
|
|
def test_ldap_registration_end_to_end(self):
|
|
|
|
# type: () -> None
|
|
|
|
password = "testing"
|
|
|
|
email = "newuser@zulip.com"
|
|
|
|
subdomain = "zulip"
|
|
|
|
|
|
|
|
ldap_user_attr_map = {'full_name': 'fn', 'short_name': 'sn'}
|
|
|
|
|
|
|
|
ldap_patcher = patch('django_auth_ldap.config.ldap.initialize')
|
|
|
|
mock_initialize = ldap_patcher.start()
|
|
|
|
mock_ldap = MockLDAP()
|
|
|
|
mock_initialize.return_value = mock_ldap
|
|
|
|
|
|
|
|
full_name = 'New LDAP fullname'
|
|
|
|
mock_ldap.directory = {
|
|
|
|
'uid=newuser,ou=users,dc=zulip,dc=com': {
|
|
|
|
'userPassword': 'testing',
|
2017-10-24 19:38:31 +02:00
|
|
|
'fn': [full_name],
|
|
|
|
'sn': ['shortname'],
|
2017-09-29 08:43:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
with patch('zerver.views.registration.get_subdomain', return_value=subdomain):
|
|
|
|
result = self.client_post('/register/', {'email': email})
|
|
|
|
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertTrue(result["Location"].endswith(
|
|
|
|
"/accounts/send_confirm/%s" % (email,)))
|
|
|
|
result = self.client_get(result["Location"])
|
|
|
|
self.assert_in_response("Check your email so we can get started.", result)
|
|
|
|
|
|
|
|
with self.settings(
|
|
|
|
POPULATE_PROFILE_VIA_LDAP=True,
|
|
|
|
LDAP_APPEND_DOMAIN='zulip.com',
|
|
|
|
AUTH_LDAP_BIND_PASSWORD='',
|
|
|
|
AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map,
|
|
|
|
AUTH_LDAP_USER_DN_TEMPLATE='uid=%(user)s,ou=users,dc=zulip,dc=com'):
|
|
|
|
|
|
|
|
# Click confirmation link
|
|
|
|
result = self.submit_reg_form_for_user(email,
|
|
|
|
password,
|
|
|
|
full_name="Ignore",
|
|
|
|
from_confirmation="1",
|
|
|
|
# Pass HTTP_HOST for the target subdomain
|
|
|
|
HTTP_HOST=subdomain + ".testserver")
|
|
|
|
|
|
|
|
# Full name should be set from LDAP
|
|
|
|
self.assert_in_success_response(["You're almost there.",
|
|
|
|
full_name,
|
|
|
|
"newuser@zulip.com"],
|
|
|
|
result)
|
|
|
|
|
2017-10-24 19:38:31 +02:00
|
|
|
# Submit the final form with the wrong password.
|
|
|
|
result = self.submit_reg_form_for_user(email,
|
|
|
|
'wrongpassword',
|
|
|
|
full_name=full_name,
|
|
|
|
# Pass HTTP_HOST for the target subdomain
|
|
|
|
HTTP_HOST=subdomain + ".testserver")
|
|
|
|
# Didn't create an account
|
|
|
|
with self.assertRaises(UserProfile.DoesNotExist):
|
|
|
|
user_profile = UserProfile.objects.get(email=email)
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertEqual(result.url, "/accounts/login/?email=newuser%40zulip.com")
|
|
|
|
|
|
|
|
# Submit the final form with the wrong password.
|
2017-10-23 21:12:59 +02:00
|
|
|
result = self.submit_reg_form_for_user(email,
|
|
|
|
password,
|
|
|
|
full_name=full_name,
|
|
|
|
# Pass HTTP_HOST for the target subdomain
|
|
|
|
HTTP_HOST=subdomain + ".testserver")
|
|
|
|
user_profile = UserProfile.objects.get(email=email)
|
|
|
|
# Name comes from form which was set by LDAP.
|
|
|
|
self.assertEqual(user_profile.full_name, full_name)
|
2017-09-29 08:43:18 +02:00
|
|
|
|
2017-10-23 20:42:37 +02:00
|
|
|
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipLDAPAuthBackend',
|
|
|
|
'zproject.backends.ZulipDummyBackend'))
|
|
|
|
def test_ldap_auto_registration_on_login(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""The most common way for LDAP authentication to be used is with a
|
|
|
|
server that doesn't have a terms-of-service required, in which
|
|
|
|
case we offer a complete single-sign-on experience (where the
|
|
|
|
user just enters their LDAP username and password, and their
|
|
|
|
account is created if it doesn't already exist).
|
|
|
|
|
|
|
|
This test verifies that flow.
|
|
|
|
"""
|
|
|
|
password = "testing"
|
|
|
|
email = "newuser@zulip.com"
|
|
|
|
subdomain = "zulip"
|
|
|
|
|
|
|
|
ldap_user_attr_map = {'full_name': 'fn', 'short_name': 'sn'}
|
|
|
|
|
|
|
|
ldap_patcher = patch('django_auth_ldap.config.ldap.initialize')
|
|
|
|
mock_initialize = ldap_patcher.start()
|
|
|
|
mock_ldap = MockLDAP()
|
|
|
|
mock_initialize.return_value = mock_ldap
|
|
|
|
|
|
|
|
full_name = 'New LDAP fullname'
|
|
|
|
mock_ldap.directory = {
|
|
|
|
'uid=newuser,ou=users,dc=zulip,dc=com': {
|
|
|
|
'userPassword': 'testing',
|
|
|
|
'fn': [full_name],
|
|
|
|
'sn': ['shortname'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
with self.settings(
|
|
|
|
POPULATE_PROFILE_VIA_LDAP=True,
|
|
|
|
LDAP_APPEND_DOMAIN='zulip.com',
|
|
|
|
AUTH_LDAP_BIND_PASSWORD='',
|
|
|
|
AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map,
|
|
|
|
AUTH_LDAP_USER_DN_TEMPLATE='uid=%(user)s,ou=users,dc=zulip,dc=com'):
|
|
|
|
|
|
|
|
self.login_with_return(email, password,
|
|
|
|
HTTP_HOST=subdomain + ".testserver")
|
|
|
|
|
|
|
|
user_profile = UserProfile.objects.get(email=email)
|
|
|
|
# Name comes from form which was set by LDAP.
|
|
|
|
self.assertEqual(user_profile.full_name, full_name)
|
|
|
|
|
2017-10-23 21:12:59 +02:00
|
|
|
@override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipLDAPAuthBackend',
|
|
|
|
'zproject.backends.ZulipDummyBackend'))
|
2017-09-29 08:43:18 +02:00
|
|
|
def test_ldap_registration_when_names_changes_are_disabled(self):
|
|
|
|
# type: () -> None
|
|
|
|
password = "testing"
|
|
|
|
email = "newuser@zulip.com"
|
|
|
|
subdomain = "zulip"
|
|
|
|
|
|
|
|
ldap_user_attr_map = {'full_name': 'fn', 'short_name': 'sn'}
|
|
|
|
|
|
|
|
ldap_patcher = patch('django_auth_ldap.config.ldap.initialize')
|
|
|
|
mock_initialize = ldap_patcher.start()
|
|
|
|
mock_ldap = MockLDAP()
|
|
|
|
mock_initialize.return_value = mock_ldap
|
|
|
|
|
|
|
|
mock_ldap.directory = {
|
|
|
|
'uid=newuser,ou=users,dc=zulip,dc=com': {
|
|
|
|
'userPassword': 'testing',
|
2017-10-24 19:38:31 +02:00
|
|
|
'fn': ['New LDAP fullname'],
|
|
|
|
'sn': ['New LDAP shortname'],
|
2017-09-29 08:43:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
with patch('zerver.views.registration.get_subdomain', return_value=subdomain):
|
|
|
|
result = self.client_post('/register/', {'email': email})
|
|
|
|
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertTrue(result["Location"].endswith(
|
|
|
|
"/accounts/send_confirm/%s" % (email,)))
|
|
|
|
result = self.client_get(result["Location"])
|
|
|
|
self.assert_in_response("Check your email so we can get started.", result)
|
|
|
|
|
|
|
|
with self.settings(
|
|
|
|
POPULATE_PROFILE_VIA_LDAP=True,
|
|
|
|
LDAP_APPEND_DOMAIN='zulip.com',
|
|
|
|
AUTH_LDAP_BIND_PASSWORD='',
|
|
|
|
AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map,
|
|
|
|
AUTH_LDAP_USER_DN_TEMPLATE='uid=%(user)s,ou=users,dc=zulip,dc=com'):
|
|
|
|
|
|
|
|
# Click confirmation link. This will 'authenticated_full_name'
|
|
|
|
# session variable which will be used to set the fullname of
|
|
|
|
# the user.
|
|
|
|
result = self.submit_reg_form_for_user(email,
|
|
|
|
password,
|
|
|
|
full_name="Ignore",
|
|
|
|
from_confirmation="1",
|
|
|
|
# Pass HTTP_HOST for the target subdomain
|
|
|
|
HTTP_HOST=subdomain + ".testserver")
|
|
|
|
|
2017-10-24 19:38:31 +02:00
|
|
|
with patch('zerver.views.registration.name_changes_disabled', return_value=True):
|
|
|
|
result = self.submit_reg_form_for_user(email,
|
|
|
|
password,
|
|
|
|
# Pass HTTP_HOST for the target subdomain
|
|
|
|
HTTP_HOST=subdomain + ".testserver")
|
2017-09-29 08:43:18 +02:00
|
|
|
user_profile = UserProfile.objects.get(email=email)
|
|
|
|
# Name comes from LDAP session.
|
|
|
|
self.assertEqual(user_profile.full_name, 'New LDAP fullname')
|
|
|
|
|
|
|
|
def test_registration_when_name_changes_are_disabled(self):
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
Test `name_changes_disabled` when we are not running under LDAP.
|
|
|
|
"""
|
|
|
|
password = "testing"
|
|
|
|
email = "newuser@zulip.com"
|
|
|
|
subdomain = "zulip"
|
|
|
|
|
|
|
|
with patch('zerver.views.registration.get_subdomain', return_value=subdomain):
|
|
|
|
result = self.client_post('/register/', {'email': email})
|
|
|
|
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertTrue(result["Location"].endswith(
|
|
|
|
"/accounts/send_confirm/%s" % (email,)))
|
|
|
|
result = self.client_get(result["Location"])
|
|
|
|
self.assert_in_response("Check your email so we can get started.", result)
|
|
|
|
|
|
|
|
with patch('zerver.views.registration.name_changes_disabled', return_value=True):
|
|
|
|
result = self.submit_reg_form_for_user(email,
|
|
|
|
password,
|
|
|
|
full_name="New Name",
|
|
|
|
# Pass HTTP_HOST for the target subdomain
|
|
|
|
HTTP_HOST=subdomain + ".testserver")
|
|
|
|
user_profile = UserProfile.objects.get(email=email)
|
|
|
|
# 'New Name' comes from POST data; not from LDAP session.
|
|
|
|
self.assertEqual(user_profile.full_name, 'New Name')
|
|
|
|
|
2017-06-15 19:24:46 +02:00
|
|
|
def test_realm_creation_through_ldap(self):
|
|
|
|
# type: () -> None
|
|
|
|
password = "testing"
|
|
|
|
email = "newuser@zulip.com"
|
|
|
|
subdomain = "zulip"
|
|
|
|
realm_name = "Zulip"
|
|
|
|
ldap_user_attr_map = {'full_name': 'fn', 'short_name': 'sn'}
|
|
|
|
|
|
|
|
ldap_patcher = patch('django_auth_ldap.config.ldap.initialize')
|
|
|
|
mock_initialize = ldap_patcher.start()
|
|
|
|
mock_ldap = MockLDAP()
|
|
|
|
mock_initialize.return_value = mock_ldap
|
|
|
|
|
|
|
|
mock_ldap.directory = {
|
|
|
|
'uid=newuser,ou=users,dc=zulip,dc=com': {
|
|
|
|
'userPassword': 'testing',
|
|
|
|
'fn': ['New User Name']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
with patch('zerver.views.registration.get_subdomain', return_value=subdomain):
|
|
|
|
result = self.client_post('/register/', {'email': email})
|
|
|
|
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertTrue(result["Location"].endswith(
|
|
|
|
"/accounts/send_confirm/%s" % (email,)))
|
|
|
|
result = self.client_get(result["Location"])
|
|
|
|
self.assert_in_response("Check your email so we can get started.", result)
|
|
|
|
# Visit the confirmation link.
|
|
|
|
from django.core.mail import outbox
|
|
|
|
for message in reversed(outbox):
|
|
|
|
if email in message.to:
|
|
|
|
confirmation_link_pattern = re.compile(settings.EXTERNAL_HOST + "(\S+)>")
|
|
|
|
confirmation_url = confirmation_link_pattern.search(
|
|
|
|
message.body).groups()[0]
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise AssertionError("Couldn't find a confirmation email.")
|
|
|
|
|
|
|
|
with self.settings(
|
|
|
|
POPULATE_PROFILE_VIA_LDAP=True,
|
|
|
|
LDAP_APPEND_DOMAIN='zulip.com',
|
|
|
|
AUTH_LDAP_BIND_PASSWORD='',
|
|
|
|
AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map,
|
|
|
|
AUTHENTICATION_BACKENDS=('zproject.backends.ZulipLDAPAuthBackend',),
|
|
|
|
AUTH_LDAP_USER_DN_TEMPLATE='uid=%(user)s,ou=users,dc=zulip,dc=com',
|
|
|
|
TERMS_OF_SERVICE=False,
|
|
|
|
):
|
|
|
|
result = self.client_get(confirmation_url)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
key = find_key_by_email(email),
|
|
|
|
confirmation = Confirmation.objects.get(confirmation_key=key[0])
|
|
|
|
prereg_user = confirmation.content_object
|
|
|
|
prereg_user.realm_creation = True
|
|
|
|
prereg_user.save()
|
|
|
|
|
2017-01-04 08:53:56 +01:00
|
|
|
result = self.submit_reg_form_for_user(email,
|
2016-11-02 11:10:21 +01:00
|
|
|
password,
|
|
|
|
realm_name=realm_name,
|
|
|
|
realm_subdomain=subdomain,
|
|
|
|
from_confirmation='1',
|
|
|
|
# Pass HTTP_HOST for the target subdomain
|
|
|
|
HTTP_HOST=subdomain + ".testserver")
|
2016-11-19 21:54:00 +01:00
|
|
|
self.assert_in_success_response(["You're almost there.",
|
|
|
|
"newuser@zulip.com"],
|
|
|
|
result)
|
2016-11-02 11:10:21 +01:00
|
|
|
|
|
|
|
mock_ldap.reset()
|
|
|
|
mock_initialize.stop()
|
|
|
|
|
2016-11-03 21:24:54 +01:00
|
|
|
@patch('DNS.dnslookup', return_value=[['sipbtest:*:20922:101:Fred Sipb,,,:/mit/sipbtest:/bin/athena/tcsh']])
|
|
|
|
def test_registration_of_mirror_dummy_user(self, ignored):
|
|
|
|
# type: (Any) -> None
|
2016-11-02 17:31:11 +01:00
|
|
|
password = "test"
|
2017-09-16 19:51:26 +02:00
|
|
|
subdomain = "zephyr"
|
2017-05-23 01:27:31 +02:00
|
|
|
user_profile = self.mit_user("sipbtest")
|
|
|
|
email = user_profile.email
|
2016-11-02 17:31:11 +01:00
|
|
|
user_profile.is_mirror_dummy = True
|
|
|
|
user_profile.is_active = False
|
|
|
|
user_profile.save()
|
|
|
|
|
2017-09-16 19:51:26 +02:00
|
|
|
result = self.client_post('/register/', {'email': email}, subdomain="zephyr")
|
2016-11-02 17:31:11 +01:00
|
|
|
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2016-11-02 17:31:11 +01:00
|
|
|
self.assertTrue(result["Location"].endswith(
|
2017-01-24 07:06:13 +01:00
|
|
|
"/accounts/send_confirm/%s" % (email,)))
|
2017-09-16 19:51:26 +02:00
|
|
|
result = self.client_get(result["Location"], subdomain="zephyr")
|
2016-11-02 17:31:11 +01:00
|
|
|
self.assert_in_response("Check your email so we can get started.", result)
|
|
|
|
# Visit the confirmation link.
|
|
|
|
from django.core.mail import outbox
|
|
|
|
for message in reversed(outbox):
|
|
|
|
if email in message.to:
|
|
|
|
confirmation_link_pattern = re.compile(settings.EXTERNAL_HOST + "(\S+)>")
|
|
|
|
confirmation_url = confirmation_link_pattern.search(
|
|
|
|
message.body).groups()[0]
|
|
|
|
break
|
|
|
|
else:
|
2017-03-05 08:01:50 +01:00
|
|
|
raise AssertionError("Couldn't find a confirmation email.")
|
2016-11-02 17:31:11 +01:00
|
|
|
|
2017-09-16 19:51:26 +02:00
|
|
|
result = self.client_get(confirmation_url, subdomain="zephyr")
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(result.status_code, 200)
|
2017-03-18 20:36:40 +01:00
|
|
|
|
|
|
|
# If the mirror dummy user is already active, attempting to submit the
|
|
|
|
# registration form should just redirect to a login page.
|
|
|
|
user_profile.is_active = True
|
|
|
|
user_profile.save()
|
|
|
|
result = self.submit_reg_form_for_user(email,
|
|
|
|
password,
|
|
|
|
from_confirmation='1',
|
|
|
|
# Pass HTTP_HOST for the target subdomain
|
|
|
|
HTTP_HOST=subdomain + ".testserver")
|
2017-09-16 19:51:26 +02:00
|
|
|
|
2017-03-18 20:36:40 +01:00
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertIn('login', result['Location'])
|
|
|
|
user_profile.is_active = False
|
|
|
|
user_profile.save()
|
|
|
|
|
2017-01-04 08:53:56 +01:00
|
|
|
result = self.submit_reg_form_for_user(email,
|
2016-11-02 17:31:11 +01:00
|
|
|
password,
|
|
|
|
from_confirmation='1',
|
|
|
|
# Pass HTTP_HOST for the target subdomain
|
|
|
|
HTTP_HOST=subdomain + ".testserver")
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(result.status_code, 200)
|
2017-01-04 08:53:56 +01:00
|
|
|
result = self.submit_reg_form_for_user(email,
|
2016-11-02 17:31:11 +01:00
|
|
|
password,
|
|
|
|
# Pass HTTP_HOST for the target subdomain
|
|
|
|
HTTP_HOST=subdomain + ".testserver")
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2016-11-02 17:31:11 +01:00
|
|
|
self.assertEqual(get_session_dict_user(self.client.session), user_profile.id)
|
|
|
|
|
2017-03-18 20:36:40 +01:00
|
|
|
def test_registration_of_active_mirror_dummy_user(self):
|
|
|
|
# type: (Any) -> None
|
|
|
|
"""
|
|
|
|
Trying to activate an already-active mirror dummy user should just
|
|
|
|
redirect to a login page.
|
|
|
|
"""
|
2017-05-23 01:27:31 +02:00
|
|
|
user_profile = self.mit_user("sipbtest")
|
|
|
|
email = user_profile.email
|
2017-03-18 20:36:40 +01:00
|
|
|
user_profile.is_mirror_dummy = True
|
|
|
|
user_profile.is_active = True
|
|
|
|
user_profile.save()
|
|
|
|
|
|
|
|
result = self.client_post('/register/', {'email': email})
|
|
|
|
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
self.assertIn('login', result['Location'])
|
|
|
|
|
2016-10-13 20:09:32 +02:00
|
|
|
class DeactivateUserTest(ZulipTestCase):
|
|
|
|
|
|
|
|
def test_deactivate_user(self):
|
|
|
|
# type: () -> None
|
2017-05-25 01:40:26 +02:00
|
|
|
email = self.example_email("hamlet")
|
2016-10-13 20:09:32 +02:00
|
|
|
self.login(email)
|
2017-05-07 17:21:26 +02:00
|
|
|
user = self.example_user('hamlet')
|
2016-10-13 20:09:32 +02:00
|
|
|
self.assertTrue(user.is_active)
|
|
|
|
result = self.client_delete('/json/users/me')
|
|
|
|
self.assert_json_success(result)
|
2017-05-07 17:21:26 +02:00
|
|
|
user = self.example_user('hamlet')
|
2016-10-13 20:09:32 +02:00
|
|
|
self.assertFalse(user.is_active)
|
|
|
|
self.login(email, fails=True)
|
|
|
|
|
|
|
|
def test_do_not_deactivate_final_admin(self):
|
|
|
|
# type: () -> None
|
2017-05-25 01:44:04 +02:00
|
|
|
email = self.example_email("iago")
|
2016-10-13 20:09:32 +02:00
|
|
|
self.login(email)
|
2017-05-07 17:21:26 +02:00
|
|
|
user = self.example_user('iago')
|
2016-10-13 20:09:32 +02:00
|
|
|
self.assertTrue(user.is_active)
|
2016-12-05 06:40:00 +01:00
|
|
|
result = self.client_delete('/json/users/me')
|
|
|
|
self.assert_json_error(result, "Cannot deactivate the only organization administrator")
|
2017-05-07 17:21:26 +02:00
|
|
|
user = self.example_user('iago')
|
2016-10-13 20:09:32 +02:00
|
|
|
self.assertTrue(user.is_active)
|
|
|
|
self.assertTrue(user.is_realm_admin)
|
2017-05-25 01:40:26 +02:00
|
|
|
email = self.example_email("hamlet")
|
2017-05-07 17:21:26 +02:00
|
|
|
user_2 = self.example_user('hamlet')
|
2016-10-13 20:09:32 +02:00
|
|
|
do_change_is_admin(user_2, True)
|
|
|
|
self.assertTrue(user_2.is_realm_admin)
|
|
|
|
result = self.client_delete('/json/users/me')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
do_change_is_admin(user, True)
|
2017-03-08 12:38:56 +01:00
|
|
|
|
|
|
|
class TestLoginPage(ZulipTestCase):
|
|
|
|
def test_login_page_wrong_subdomain_error(self):
|
|
|
|
# type: () -> None
|
|
|
|
result = self.client_get("/login/?subdomain=1")
|
|
|
|
self.assertIn(WRONG_SUBDOMAIN_ERROR, result.content.decode('utf8'))
|
|
|
|
|
|
|
|
@patch('django.http.HttpRequest.get_host')
|
|
|
|
def test_login_page_redirects_for_root_alias(self, mock_get_host):
|
|
|
|
# type: (MagicMock) -> None
|
|
|
|
mock_get_host.return_value = 'www.testserver'
|
2017-08-25 05:18:28 +02:00
|
|
|
with self.settings(ROOT_DOMAIN_LANDING_PAGE=True):
|
2017-03-08 12:38:56 +01:00
|
|
|
result = self.client_get("/en/login/")
|
|
|
|
self.assertEqual(result.status_code, 302)
|
2017-08-28 23:36:57 +02:00
|
|
|
self.assertEqual(result.url, '/accounts/find/')
|
2017-03-08 12:38:56 +01:00
|
|
|
|
|
|
|
@patch('django.http.HttpRequest.get_host')
|
|
|
|
def test_login_page_redirects_for_root_domain(self, mock_get_host):
|
|
|
|
# type: (MagicMock) -> None
|
|
|
|
mock_get_host.return_value = 'testserver'
|
2017-08-25 05:18:28 +02:00
|
|
|
with self.settings(ROOT_DOMAIN_LANDING_PAGE=True):
|
2017-03-08 12:38:56 +01:00
|
|
|
result = self.client_get("/en/login/")
|
|
|
|
self.assertEqual(result.status_code, 302)
|
2017-08-28 23:36:57 +02:00
|
|
|
self.assertEqual(result.url, '/accounts/find/')
|
2017-03-08 12:38:56 +01:00
|
|
|
|
|
|
|
mock_get_host.return_value = 'www.testserver.com'
|
2017-08-25 05:18:28 +02:00
|
|
|
with self.settings(ROOT_DOMAIN_LANDING_PAGE=True,
|
2017-03-08 12:38:56 +01:00
|
|
|
EXTERNAL_HOST='www.testserver.com',
|
|
|
|
ROOT_SUBDOMAIN_ALIASES=['test']):
|
|
|
|
result = self.client_get("/en/login/")
|
|
|
|
self.assertEqual(result.status_code, 302)
|
2017-08-28 23:36:57 +02:00
|
|
|
self.assertEqual(result.url, '/accounts/find/')
|
2017-03-08 12:38:56 +01:00
|
|
|
|
|
|
|
@patch('django.http.HttpRequest.get_host')
|
|
|
|
def test_login_page_works_without_subdomains(self, mock_get_host):
|
|
|
|
# type: (MagicMock) -> None
|
|
|
|
mock_get_host.return_value = 'www.testserver'
|
|
|
|
with self.settings(ROOT_SUBDOMAIN_ALIASES=['www']):
|
|
|
|
result = self.client_get("/en/login/")
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
mock_get_host.return_value = 'testserver'
|
|
|
|
with self.settings(ROOT_SUBDOMAIN_ALIASES=['www']):
|
|
|
|
result = self.client_get("/en/login/")
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
class TestFindMyTeam(ZulipTestCase):
|
|
|
|
def test_template(self):
|
|
|
|
# type: () -> None
|
2017-08-28 23:36:57 +02:00
|
|
|
result = self.client_get('/accounts/find/')
|
2017-08-28 23:27:16 +02:00
|
|
|
self.assertIn("Find your Zulip accounts", result.content.decode('utf8'))
|
2017-03-08 12:38:56 +01:00
|
|
|
|
|
|
|
def test_result(self):
|
|
|
|
# type: () -> None
|
2017-08-28 23:36:57 +02:00
|
|
|
result = self.client_post('/accounts/find/',
|
2017-08-25 08:10:12 +02:00
|
|
|
dict(emails="iago@zulip.com,cordelia@zulip.com"))
|
|
|
|
self.assertEqual(result.status_code, 302)
|
2017-08-28 23:36:57 +02:00
|
|
|
self.assertEqual(result.url, "/accounts/find/?emails=iago%40zulip.com%2Ccordelia%40zulip.com")
|
2017-08-25 08:10:12 +02:00
|
|
|
result = self.client_get(result.url)
|
2017-03-08 12:38:56 +01:00
|
|
|
content = result.content.decode('utf8')
|
|
|
|
self.assertIn("Emails sent! You will only receive emails", content)
|
2017-05-25 01:44:04 +02:00
|
|
|
self.assertIn(self.example_email("iago"), content)
|
2017-05-25 01:50:35 +02:00
|
|
|
self.assertIn(self.example_email("cordelia"), content)
|
2017-08-25 08:10:12 +02:00
|
|
|
from django.core.mail import outbox
|
|
|
|
self.assertEqual(len(outbox), 2)
|
2017-03-08 12:38:56 +01:00
|
|
|
|
2017-03-11 20:12:01 +01:00
|
|
|
def test_find_team_ignore_invalid_email(self):
|
|
|
|
# type: () -> None
|
2017-08-28 23:36:57 +02:00
|
|
|
result = self.client_post('/accounts/find/',
|
2017-08-25 08:10:12 +02:00
|
|
|
dict(emails="iago@zulip.com,invalid_email@zulip.com"))
|
|
|
|
self.assertEqual(result.status_code, 302)
|
2017-08-28 23:36:57 +02:00
|
|
|
self.assertEqual(result.url, "/accounts/find/?emails=iago%40zulip.com%2Cinvalid_email%40zulip.com")
|
2017-08-25 08:10:12 +02:00
|
|
|
result = self.client_get(result.url)
|
2017-03-11 20:12:01 +01:00
|
|
|
content = result.content.decode('utf8')
|
|
|
|
self.assertIn("Emails sent! You will only receive emails", content)
|
2017-05-25 01:44:04 +02:00
|
|
|
self.assertIn(self.example_email("iago"), content)
|
2017-08-25 08:10:12 +02:00
|
|
|
self.assertIn("invalid_email@", content)
|
|
|
|
from django.core.mail import outbox
|
|
|
|
self.assertEqual(len(outbox), 1)
|
|
|
|
|
|
|
|
def test_find_team_reject_invalid_email(self):
|
|
|
|
# type: () -> None
|
2017-08-28 23:36:57 +02:00
|
|
|
result = self.client_post('/accounts/find/',
|
2017-08-25 08:10:12 +02:00
|
|
|
dict(emails="invalid_string"))
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
self.assertIn(b"Enter a valid email", result.content)
|
|
|
|
from django.core.mail import outbox
|
|
|
|
self.assertEqual(len(outbox), 0)
|
2017-03-11 20:12:01 +01:00
|
|
|
|
2017-08-25 08:30:33 +02:00
|
|
|
# Just for coverage on perhaps-unnecessary validation code.
|
2017-08-28 23:36:57 +02:00
|
|
|
result = self.client_get('/accounts/find/?emails=invalid')
|
2017-08-25 08:30:33 +02:00
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2017-03-08 12:38:56 +01:00
|
|
|
def test_find_team_zero_emails(self):
|
|
|
|
# type: () -> None
|
|
|
|
data = {'emails': ''}
|
2017-08-28 23:36:57 +02:00
|
|
|
result = self.client_post('/accounts/find/', data)
|
2017-03-08 12:38:56 +01:00
|
|
|
self.assertIn('This field is required', result.content.decode('utf8'))
|
|
|
|
self.assertEqual(result.status_code, 200)
|
2017-08-25 08:10:12 +02:00
|
|
|
from django.core.mail import outbox
|
|
|
|
self.assertEqual(len(outbox), 0)
|
2017-03-08 12:38:56 +01:00
|
|
|
|
|
|
|
def test_find_team_one_email(self):
|
|
|
|
# type: () -> None
|
2017-05-25 01:40:26 +02:00
|
|
|
data = {'emails': self.example_email("hamlet")}
|
2017-08-28 23:36:57 +02:00
|
|
|
result = self.client_post('/accounts/find/', data)
|
2017-03-08 12:38:56 +01:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2017-08-28 23:36:57 +02:00
|
|
|
self.assertEqual(result.url, '/accounts/find/?emails=hamlet%40zulip.com')
|
2017-08-25 08:10:12 +02:00
|
|
|
from django.core.mail import outbox
|
|
|
|
self.assertEqual(len(outbox), 1)
|
2017-03-08 12:38:56 +01:00
|
|
|
|
2017-08-25 08:14:55 +02:00
|
|
|
def test_find_team_deactivated_user(self):
|
|
|
|
# type: () -> None
|
|
|
|
do_deactivate_user(self.example_user("hamlet"))
|
|
|
|
data = {'emails': self.example_email("hamlet")}
|
2017-08-28 23:36:57 +02:00
|
|
|
result = self.client_post('/accounts/find/', data)
|
2017-08-25 08:14:55 +02:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2017-08-28 23:36:57 +02:00
|
|
|
self.assertEqual(result.url, '/accounts/find/?emails=hamlet%40zulip.com')
|
2017-08-25 08:14:55 +02:00
|
|
|
from django.core.mail import outbox
|
|
|
|
self.assertEqual(len(outbox), 0)
|
|
|
|
|
|
|
|
def test_find_team_deactivated_realm(self):
|
|
|
|
# type: () -> None
|
|
|
|
do_deactivate_realm(get_realm("zulip"))
|
|
|
|
data = {'emails': self.example_email("hamlet")}
|
2017-08-28 23:36:57 +02:00
|
|
|
result = self.client_post('/accounts/find/', data)
|
2017-08-25 08:14:55 +02:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2017-08-28 23:36:57 +02:00
|
|
|
self.assertEqual(result.url, '/accounts/find/?emails=hamlet%40zulip.com')
|
2017-08-25 08:14:55 +02:00
|
|
|
from django.core.mail import outbox
|
|
|
|
self.assertEqual(len(outbox), 0)
|
|
|
|
|
|
|
|
def test_find_team_bot_email(self):
|
|
|
|
# type: () -> None
|
|
|
|
data = {'emails': self.example_email("webhook_bot")}
|
2017-08-28 23:36:57 +02:00
|
|
|
result = self.client_post('/accounts/find/', data)
|
2017-08-25 08:14:55 +02:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2017-08-28 23:36:57 +02:00
|
|
|
self.assertEqual(result.url, '/accounts/find/?emails=webhook-bot%40zulip.com')
|
2017-08-25 08:14:55 +02:00
|
|
|
from django.core.mail import outbox
|
|
|
|
self.assertEqual(len(outbox), 0)
|
|
|
|
|
2017-03-08 12:38:56 +01:00
|
|
|
def test_find_team_more_than_ten_emails(self):
|
|
|
|
# type: () -> None
|
|
|
|
data = {'emails': ','.join(['hamlet-{}@zulip.com'.format(i) for i in range(11)])}
|
2017-08-28 23:36:57 +02:00
|
|
|
result = self.client_post('/accounts/find/', data)
|
2017-03-08 12:38:56 +01:00
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
self.assertIn("Please enter at most 10", result.content.decode('utf8'))
|
2017-08-25 08:10:12 +02:00
|
|
|
from django.core.mail import outbox
|
|
|
|
self.assertEqual(len(outbox), 0)
|
2017-03-11 20:12:01 +01:00
|
|
|
|
|
|
|
class ConfirmationKeyTest(ZulipTestCase):
|
|
|
|
def test_confirmation_key(self):
|
|
|
|
# type: () -> None
|
|
|
|
request = MagicMock()
|
|
|
|
request.session = {
|
|
|
|
'confirmation_key': {'confirmation_key': 'xyzzy'}
|
|
|
|
}
|
|
|
|
result = confirmation_key(request)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assert_in_response('xyzzy', result)
|
2017-04-27 22:58:53 +02:00
|
|
|
|
|
|
|
class MobileAuthOTPTest(ZulipTestCase):
|
|
|
|
def test_xor_hex_strings(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.assertEqual(xor_hex_strings('1237c81ab', '18989fd12'), '0aaf57cb9')
|
|
|
|
with self.assertRaises(AssertionError):
|
|
|
|
xor_hex_strings('1', '31')
|
|
|
|
|
|
|
|
def test_is_valid_otp(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.assertEqual(is_valid_otp('1234'), False)
|
|
|
|
self.assertEqual(is_valid_otp('1234abcd' * 8), True)
|
|
|
|
self.assertEqual(is_valid_otp('1234abcZ' * 8), False)
|
|
|
|
|
|
|
|
def test_ascii_to_hex(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.assertEqual(ascii_to_hex('ZcdR1234'), '5a63645231323334')
|
|
|
|
self.assertEqual(hex_to_ascii('5a63645231323334'), 'ZcdR1234')
|
|
|
|
|
|
|
|
def test_otp_encrypt_api_key(self):
|
|
|
|
# type: () -> None
|
2017-05-07 17:21:26 +02:00
|
|
|
hamlet = self.example_user('hamlet')
|
2017-04-27 22:58:53 +02:00
|
|
|
hamlet.api_key = '12ac' * 8
|
|
|
|
otp = '7be38894' * 8
|
|
|
|
result = otp_encrypt_api_key(hamlet, otp)
|
|
|
|
self.assertEqual(result, '4ad1e9f7' * 8)
|
|
|
|
|
|
|
|
decryped = otp_decrypt_api_key(result, otp)
|
|
|
|
self.assertEqual(decryped, hamlet.api_key)
|
2017-04-20 08:25:15 +02:00
|
|
|
|
|
|
|
class LoginOrAskForRegistrationTestCase(ZulipTestCase):
|
|
|
|
def test_confirm(self):
|
|
|
|
# type: () -> None
|
2017-08-26 01:37:58 +02:00
|
|
|
request = HostRequestMock()
|
2017-04-20 08:25:15 +02:00
|
|
|
email = 'new@zulip.com'
|
2017-07-06 08:27:57 +02:00
|
|
|
user_profile = None # type: Optional[UserProfile]
|
2017-04-20 08:25:15 +02:00
|
|
|
full_name = 'New User'
|
|
|
|
invalid_subdomain = False
|
2017-05-13 22:05:06 +02:00
|
|
|
result = login_or_register_remote_user(
|
2017-04-20 08:25:15 +02:00
|
|
|
request,
|
|
|
|
email,
|
|
|
|
user_profile,
|
|
|
|
full_name=full_name,
|
|
|
|
invalid_subdomain=invalid_subdomain)
|
2017-07-17 17:31:05 +02:00
|
|
|
self.assert_in_response('No account found for',
|
2017-05-13 22:05:06 +02:00
|
|
|
result)
|
2017-07-17 17:31:05 +02:00
|
|
|
self.assert_in_response('new@zulip.com. Would you like to register instead?',
|
2017-05-13 22:05:06 +02:00
|
|
|
result)
|
2017-04-20 08:25:15 +02:00
|
|
|
|
|
|
|
def test_invalid_subdomain(self):
|
|
|
|
# type: () -> None
|
2017-08-26 01:37:58 +02:00
|
|
|
request = HostRequestMock()
|
2017-04-20 08:25:15 +02:00
|
|
|
email = 'new@zulip.com'
|
2017-07-06 08:27:57 +02:00
|
|
|
user_profile = None # type: Optional[UserProfile]
|
2017-04-20 08:25:15 +02:00
|
|
|
full_name = 'New User'
|
|
|
|
invalid_subdomain = True
|
|
|
|
response = login_or_register_remote_user(
|
|
|
|
request,
|
|
|
|
email,
|
|
|
|
user_profile,
|
|
|
|
full_name=full_name,
|
|
|
|
invalid_subdomain=invalid_subdomain)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertIn('/accounts/login/?subdomain=1', response.url)
|
|
|
|
|
|
|
|
def test_invalid_email(self):
|
|
|
|
# type: () -> None
|
2017-08-26 01:37:58 +02:00
|
|
|
request = HostRequestMock()
|
2017-07-06 08:30:10 +02:00
|
|
|
email = None # type: Optional[Text]
|
2017-07-06 08:27:57 +02:00
|
|
|
user_profile = None # type: Optional[UserProfile]
|
2017-04-20 08:25:15 +02:00
|
|
|
full_name = 'New User'
|
|
|
|
invalid_subdomain = False
|
|
|
|
response = login_or_register_remote_user(
|
|
|
|
request,
|
|
|
|
email,
|
|
|
|
user_profile,
|
|
|
|
full_name=full_name,
|
|
|
|
invalid_subdomain=invalid_subdomain)
|
|
|
|
self.assert_in_response('Please click the following button if '
|
|
|
|
'you wish to register', response)
|
|
|
|
|
|
|
|
def test_login_under_subdomains(self):
|
|
|
|
# type: () -> None
|
2017-08-26 01:37:58 +02:00
|
|
|
request = HostRequestMock()
|
2017-04-20 08:25:15 +02:00
|
|
|
setattr(request, 'session', self.client.session)
|
2017-05-24 02:42:31 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-04-20 08:25:15 +02:00
|
|
|
user_profile.backend = 'zproject.backends.GitHubAuthBackend'
|
|
|
|
full_name = 'Hamlet'
|
|
|
|
invalid_subdomain = False
|
2017-10-02 22:43:43 +02:00
|
|
|
|
|
|
|
response = login_or_register_remote_user(
|
|
|
|
request,
|
|
|
|
user_profile.email,
|
|
|
|
user_profile,
|
|
|
|
full_name=full_name,
|
|
|
|
invalid_subdomain=invalid_subdomain)
|
|
|
|
user_id = get_session_dict_user(getattr(request, 'session'))
|
|
|
|
self.assertEqual(user_id, user_profile.id)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertIn('http://zulip.testserver', response.url)
|