2018-05-27 00:17:08 +02:00
|
|
|
import datetime
|
2022-06-28 00:43:57 +02:00
|
|
|
import sys
|
2021-05-28 15:57:08 +02:00
|
|
|
from typing import Sequence
|
2020-06-11 00:54:34 +02:00
|
|
|
from unittest import mock
|
|
|
|
|
2017-01-30 23:19:38 +01:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core import mail
|
2018-08-24 07:28:51 +02:00
|
|
|
from django.test import override_settings
|
|
|
|
|
2021-05-28 15:57:08 +02:00
|
|
|
from corporate.lib.stripe import get_latest_seat_count
|
2022-04-14 23:53:15 +02:00
|
|
|
from zerver.actions.create_user import notify_new_user
|
2022-04-14 23:49:26 +02:00
|
|
|
from zerver.actions.user_settings import do_change_user_setting
|
2017-06-15 20:54:14 +02:00
|
|
|
from zerver.lib.initial_password import initial_password
|
2021-04-29 17:22:48 +02:00
|
|
|
from zerver.lib.streams import create_stream_if_needed
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2021-04-29 17:22:48 +02:00
|
|
|
from zerver.models import Message, Realm, Recipient, Stream, UserProfile, get_realm
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.signals import JUST_CREATED_THRESHOLD, get_device_browser, get_device_os
|
|
|
|
|
2022-06-28 00:43:57 +02:00
|
|
|
if sys.version_info < (3, 9): # nocoverage
|
|
|
|
from backports import zoneinfo
|
|
|
|
else: # nocoverage
|
|
|
|
import zoneinfo
|
|
|
|
|
2017-01-30 23:19:38 +01:00
|
|
|
|
|
|
|
class SendLoginEmailTest(ZulipTestCase):
|
|
|
|
"""
|
|
|
|
Uses django's user_logged_in signal to send emails on new login.
|
|
|
|
|
|
|
|
The receiver handler for this signal is always registered in production,
|
|
|
|
development and testing, but emails are only sent based on SEND_LOGIN_EMAILS setting.
|
|
|
|
|
|
|
|
SEND_LOGIN_EMAILS is set to true in default settings.
|
|
|
|
It is turned off during testing.
|
|
|
|
"""
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_send_login_emails_if_send_login_email_setting_is_true(self) -> None:
|
2017-01-30 23:19:38 +01:00
|
|
|
with self.settings(SEND_LOGIN_EMAILS=True):
|
|
|
|
self.assertTrue(settings.SEND_LOGIN_EMAILS)
|
2017-06-15 20:54:14 +02:00
|
|
|
# we don't use the self.login method since we spoof the user-agent
|
2020-10-27 01:04:07 +01:00
|
|
|
mock_time = datetime.datetime(year=2018, month=1, day=1, tzinfo=datetime.timezone.utc)
|
2018-08-10 00:58:44 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
|
|
|
user.timezone = "US/Pacific"
|
2018-08-13 23:00:51 +02:00
|
|
|
user.twenty_four_hour_time = False
|
2018-08-10 00:58:44 +02:00
|
|
|
user.date_joined = mock_time - datetime.timedelta(seconds=JUST_CREATED_THRESHOLD + 1)
|
2018-05-27 00:17:08 +02:00
|
|
|
user.save()
|
2020-03-12 14:17:25 +01:00
|
|
|
password = initial_password(user.delivery_email)
|
|
|
|
login_info = dict(
|
|
|
|
username=user.delivery_email,
|
|
|
|
password=password,
|
|
|
|
)
|
2021-02-12 08:19:30 +01:00
|
|
|
firefox_windows = (
|
|
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"
|
|
|
|
)
|
2022-06-28 00:43:57 +02:00
|
|
|
user_tz = zoneinfo.ZoneInfo(user.timezone)
|
2020-10-27 01:04:07 +01:00
|
|
|
mock_time = datetime.datetime(year=2018, month=1, day=1, tzinfo=datetime.timezone.utc)
|
2021-02-12 08:20:45 +01:00
|
|
|
reference_time = mock_time.astimezone(user_tz).strftime("%A, %B %d, %Y at %I:%M%p %Z")
|
|
|
|
with mock.patch("zerver.signals.timezone_now", return_value=mock_time):
|
2021-02-12 08:19:30 +01:00
|
|
|
self.client_post(
|
|
|
|
"/accounts/login/", info=login_info, HTTP_USER_AGENT=firefox_windows
|
|
|
|
)
|
2017-01-30 23:19:38 +01:00
|
|
|
|
|
|
|
# email is sent and correct subject
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(mail.outbox, 1)
|
2021-02-12 08:20:45 +01:00
|
|
|
subject = "New login from Firefox on Windows"
|
2017-06-15 20:54:14 +02:00
|
|
|
self.assertEqual(mail.outbox[0].subject, subject)
|
2018-05-27 00:17:08 +02:00
|
|
|
# local time is correct and in email's body
|
|
|
|
self.assertIn(reference_time, mail.outbox[0].body)
|
2017-01-30 23:19:38 +01:00
|
|
|
|
2018-08-13 23:00:51 +02:00
|
|
|
# Try again with the 24h time format setting enabled for this user
|
|
|
|
self.logout() # We just logged in, we'd be redirected without this
|
|
|
|
user.twenty_four_hour_time = True
|
|
|
|
user.save()
|
2021-02-12 08:20:45 +01:00
|
|
|
with mock.patch("zerver.signals.timezone_now", return_value=mock_time):
|
2021-02-12 08:19:30 +01:00
|
|
|
self.client_post(
|
|
|
|
"/accounts/login/", info=login_info, HTTP_USER_AGENT=firefox_windows
|
|
|
|
)
|
2018-08-13 23:00:51 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
reference_time = mock_time.astimezone(user_tz).strftime("%A, %B %d, %Y at %H:%M %Z")
|
2018-08-13 23:00:51 +02:00
|
|
|
self.assertIn(reference_time, mail.outbox[1].body)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_dont_send_login_emails_if_send_login_emails_is_false(self) -> None:
|
2017-01-30 23:19:38 +01:00
|
|
|
self.assertFalse(settings.SEND_LOGIN_EMAILS)
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2017-01-30 23:19:38 +01:00
|
|
|
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(mail.outbox, 0)
|
2017-01-30 23:19:38 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_dont_send_login_emails_for_new_user_registration_logins(self) -> None:
|
2017-01-30 23:19:38 +01:00
|
|
|
with self.settings(SEND_LOGIN_EMAILS=True):
|
|
|
|
self.register("test@zulip.com", "test")
|
|
|
|
|
2017-08-23 01:14:45 +02:00
|
|
|
# Verify that there's just 1 email for new user registration.
|
|
|
|
self.assertEqual(mail.outbox[0].subject, "Activate your Zulip account")
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(mail.outbox, 1)
|
2017-01-30 23:19:38 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def test_without_path_info_dont_send_login_emails_for_new_user_registration_logins(
|
|
|
|
self,
|
|
|
|
) -> None:
|
2017-01-30 23:19:38 +01:00
|
|
|
with self.settings(SEND_LOGIN_EMAILS=True):
|
2021-02-12 08:20:45 +01:00
|
|
|
self.client_post("/accounts/home/", {"email": "orange@zulip.com"})
|
|
|
|
self.submit_reg_form_for_user("orange@zulip.com", "orange", PATH_INFO="")
|
2017-01-30 23:19:38 +01:00
|
|
|
|
|
|
|
for email in mail.outbox:
|
2021-02-12 08:20:45 +01:00
|
|
|
subject = "New login from an unknown browser on an unknown operating system"
|
2017-06-15 20:54:14 +02:00
|
|
|
self.assertNotEqual(email.subject, subject)
|
2017-01-30 23:19:38 +01:00
|
|
|
|
2018-08-24 07:28:51 +02:00
|
|
|
@override_settings(SEND_LOGIN_EMAILS=True)
|
|
|
|
def test_enable_login_emails_user_setting(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
2020-10-27 01:04:07 +01:00
|
|
|
mock_time = datetime.datetime(year=2018, month=1, day=1, tzinfo=datetime.timezone.utc)
|
2018-08-24 07:28:51 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
user.timezone = "US/Pacific"
|
2018-08-24 07:28:51 +02:00
|
|
|
user.date_joined = mock_time - datetime.timedelta(seconds=JUST_CREATED_THRESHOLD + 1)
|
|
|
|
user.save()
|
|
|
|
|
2021-09-08 15:36:08 +02:00
|
|
|
do_change_user_setting(user, "enable_login_emails", False, acting_user=None)
|
2018-08-24 07:28:51 +02:00
|
|
|
self.assertFalse(user.enable_login_emails)
|
2021-02-12 08:20:45 +01:00
|
|
|
with mock.patch("zerver.signals.timezone_now", return_value=mock_time):
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(mail.outbox, 0)
|
2018-08-24 07:28:51 +02:00
|
|
|
|
2021-09-08 15:36:08 +02:00
|
|
|
do_change_user_setting(user, "enable_login_emails", True, acting_user=None)
|
2018-08-24 07:28:51 +02:00
|
|
|
self.assertTrue(user.enable_login_emails)
|
2021-02-12 08:20:45 +01:00
|
|
|
with mock.patch("zerver.signals.timezone_now", return_value=mock_time):
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(mail.outbox, 1)
|
2018-08-24 07:28:51 +02:00
|
|
|
|
|
|
|
|
2017-01-30 23:19:38 +01:00
|
|
|
class TestBrowserAndOsUserAgentStrings(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def setUp(self) -> None:
|
2019-10-19 20:47:00 +02:00
|
|
|
super().setUp()
|
2017-01-30 23:19:38 +01:00
|
|
|
self.user_agents = [
|
2021-02-12 08:19:30 +01:00
|
|
|
(
|
2023-01-03 02:16:53 +01:00
|
|
|
(
|
|
|
|
"mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)"
|
|
|
|
" Chrome/54.0.2840.59 Safari/537.36"
|
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
"Chrome",
|
|
|
|
"Linux",
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
(
|
2023-01-03 02:16:53 +01:00
|
|
|
(
|
|
|
|
"mozilla/5.0 (windows nt 6.1; win64; x64) "
|
|
|
|
" applewebkit/537.36 (khtml, like gecko)"
|
|
|
|
" chrome/56.0.2924.87 safari/537.36"
|
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
"Chrome",
|
|
|
|
"Windows",
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
(
|
2021-02-12 08:20:45 +01:00
|
|
|
"mozilla/5.0 (windows nt 6.1; wow64; rv:51.0) gecko/20100101 firefox/51.0",
|
|
|
|
"Firefox",
|
|
|
|
"Windows",
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
(
|
2021-02-12 08:20:45 +01:00
|
|
|
"mozilla/5.0 (windows nt 6.1; wow64; trident/7.0; rv:11.0) like gecko",
|
|
|
|
"Internet Explorer",
|
|
|
|
"Windows",
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
(
|
2021-02-12 08:20:45 +01:00
|
|
|
"Mozilla/5.0 (Android; Mobile; rv:27.0) Gecko/27.0 Firefox/27.0",
|
|
|
|
"Firefox",
|
|
|
|
"Android",
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
(
|
2023-01-03 02:16:53 +01:00
|
|
|
(
|
|
|
|
"Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X)"
|
|
|
|
" AppleWebKit/602.1.50 (KHTML, like Gecko)"
|
|
|
|
" CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1"
|
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
"Chrome",
|
|
|
|
"iOS",
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
(
|
2023-01-03 02:16:53 +01:00
|
|
|
(
|
|
|
|
"Mozilla/5.0 (iPad; CPU OS 6_1_3 like Mac OS X)"
|
|
|
|
" AppleWebKit/536.26 (KHTML, like Gecko)"
|
|
|
|
" Version/6.0 Mobile/10B329 Safari/8536.25"
|
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
"Safari",
|
|
|
|
"iOS",
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
(
|
2023-01-03 02:16:53 +01:00
|
|
|
(
|
|
|
|
"Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_4 like Mac OS X)"
|
|
|
|
" AppleWebKit/536.26 (KHTML, like Gecko) Mobile/10B350"
|
|
|
|
),
|
2021-02-12 08:19:30 +01:00
|
|
|
None,
|
2021-02-12 08:20:45 +01:00
|
|
|
"iOS",
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
(
|
2023-01-03 02:16:53 +01:00
|
|
|
(
|
|
|
|
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6)"
|
|
|
|
" AppleWebKit/537.36 (KHTML, like Gecko)"
|
|
|
|
" Chrome/56.0.2924.87 Safari/537.36"
|
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
"Chrome",
|
|
|
|
"macOS",
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
(
|
2023-01-03 02:16:53 +01:00
|
|
|
(
|
|
|
|
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6)"
|
|
|
|
" AppleWebKit/602.3.12 (KHTML, like Gecko)"
|
|
|
|
" Version/10.0.2 Safari/602.3.12"
|
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
"Safari",
|
|
|
|
"macOS",
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
("ZulipAndroid/1.0", "Zulip", "Android"),
|
|
|
|
("ZulipMobile/1.0.12 (Android 7.1.1)", "Zulip", "Android"),
|
|
|
|
("ZulipMobile/0.7.1.1 (iOS 10.3.1)", "Zulip", "iOS"),
|
2021-02-12 08:19:30 +01:00
|
|
|
(
|
2023-01-03 02:16:53 +01:00
|
|
|
(
|
|
|
|
"ZulipElectron/1.1.0-beta Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
|
|
|
|
" AppleWebKit/537.36 (KHTML, like Gecko) Zulip/1.1.0-beta"
|
|
|
|
" Chrome/56.0.2924.87 Electron/1.6.8 Safari/537.36"
|
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
"Zulip",
|
|
|
|
"Windows",
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
(
|
2023-01-03 02:16:53 +01:00
|
|
|
(
|
|
|
|
"Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.7 (KHTML, like Gecko)"
|
|
|
|
" Ubuntu/11.10 Chromium/16.0.912.77 Chrome/16.0.912.77 Safari/535.7"
|
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
"Chromium",
|
|
|
|
"Linux",
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
(
|
2023-01-03 02:16:53 +01:00
|
|
|
(
|
|
|
|
"Mozilla/5.0 (Windows NT 6.1; WOW64)"
|
|
|
|
" AppleWebKit/537.36 (KHTML, like Gecko)"
|
|
|
|
" Chrome/28.0.1500.52 Safari/537.36 OPR/15.0.1147.100"
|
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
"Opera",
|
|
|
|
"Windows",
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
(
|
2023-01-03 02:16:53 +01:00
|
|
|
(
|
|
|
|
"Mozilla/5.0 (Windows NT 10.0; <64-bit tags>)"
|
|
|
|
" AppleWebKit/<WebKit Rev> (KHTML, like Gecko)"
|
|
|
|
" Chrome/<Chrome Rev> Safari/<WebKit Rev>"
|
|
|
|
" Edge/<EdgeHTML Rev>.<Windows Build>"
|
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
"Edge",
|
|
|
|
"Windows",
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
(
|
2023-01-03 02:16:53 +01:00
|
|
|
(
|
|
|
|
"Mozilla/5.0 (X11; CrOS x86_64 10895.56.0) AppleWebKit/537.36"
|
|
|
|
" (KHTML, like Gecko) Chrome/69.0.3497.95 Safari/537.36"
|
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
"Chrome",
|
|
|
|
"ChromeOS",
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
("", None, None),
|
2017-01-30 23:19:38 +01:00
|
|
|
]
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_get_browser_on_new_login(self) -> None:
|
2017-01-30 23:19:38 +01:00
|
|
|
for user_agent in self.user_agents:
|
|
|
|
device_browser = get_device_browser(user_agent[0])
|
|
|
|
self.assertEqual(device_browser, user_agent[1])
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_get_os_on_new_login(self) -> None:
|
2017-01-30 23:19:38 +01:00
|
|
|
for user_agent in self.user_agents:
|
|
|
|
device_os = get_device_os(user_agent[0])
|
|
|
|
self.assertEqual(device_os, user_agent[2])
|
2017-05-24 01:20:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestNotifyNewUser(ZulipTestCase):
|
2021-04-29 17:22:48 +02:00
|
|
|
def get_message_count(self) -> int:
|
|
|
|
return Message.objects.all().count()
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_notify_realm_of_new_user(self) -> None:
|
2021-04-29 17:22:48 +02:00
|
|
|
realm = get_realm("zulip")
|
2021-02-12 08:20:45 +01:00
|
|
|
new_user = self.example_user("cordelia")
|
2021-04-29 17:22:48 +02:00
|
|
|
admin_realm = get_realm("zulipinternal")
|
|
|
|
admin_realm_signups_stream, created = create_stream_if_needed(admin_realm, "signups")
|
|
|
|
message_count = self.get_message_count()
|
|
|
|
|
2017-05-24 23:13:10 +02:00
|
|
|
notify_new_user(new_user)
|
2021-04-29 17:22:48 +02:00
|
|
|
self.assertEqual(self.get_message_count(), message_count + 2)
|
|
|
|
message = self.get_second_to_last_message()
|
|
|
|
self.assertEqual(message.recipient.type, Recipient.STREAM)
|
|
|
|
actual_stream = Stream.objects.get(id=message.recipient.type_id)
|
|
|
|
self.assertEqual(actual_stream.name, Realm.INITIAL_PRIVATE_STREAM_NAME)
|
|
|
|
self.assertIn(
|
|
|
|
f"@_**Cordelia, Lear's daughter|{new_user.id}** just signed up for Zulip.",
|
|
|
|
message.content,
|
|
|
|
)
|
|
|
|
message = self.get_last_message()
|
|
|
|
self.assertEqual(message.recipient.type, Recipient.STREAM)
|
|
|
|
actual_stream = Stream.objects.get(id=message.recipient.type_id)
|
|
|
|
self.assertEqual(actual_stream.name, "signups")
|
|
|
|
self.assertIn(
|
|
|
|
f"Cordelia, Lear's daughter <`{new_user.email}`> just signed up for Zulip. (total:",
|
|
|
|
message.content,
|
|
|
|
)
|
2017-05-24 23:13:10 +02:00
|
|
|
|
2021-04-29 17:22:48 +02:00
|
|
|
admin_realm_signups_stream.delete()
|
|
|
|
notify_new_user(new_user)
|
|
|
|
self.assertEqual(self.get_message_count(), message_count + 3)
|
2017-05-24 23:13:10 +02:00
|
|
|
message = self.get_last_message()
|
|
|
|
self.assertEqual(message.recipient.type, Recipient.STREAM)
|
|
|
|
actual_stream = Stream.objects.get(id=message.recipient.type_id)
|
2017-10-04 02:07:44 +02:00
|
|
|
self.assertEqual(actual_stream.name, Realm.INITIAL_PRIVATE_STREAM_NAME)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertIn(
|
2021-04-11 16:26:54 +02:00
|
|
|
f"@_**Cordelia, Lear's daughter|{new_user.id}** just signed up for Zulip.",
|
|
|
|
message.content,
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-04-29 17:22:48 +02:00
|
|
|
realm.signup_notifications_stream = None
|
|
|
|
realm.save(update_fields=["signup_notifications_stream"])
|
|
|
|
new_user.refresh_from_db()
|
|
|
|
notify_new_user(new_user)
|
|
|
|
self.assertEqual(self.get_message_count(), message_count + 3)
|
2021-05-28 15:57:08 +02:00
|
|
|
|
|
|
|
def test_notify_realm_of_new_user_in_manual_license_management(self) -> None:
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
|
|
|
|
user_count = get_latest_seat_count(realm)
|
|
|
|
self.subscribe_realm_to_monthly_plan_on_manual_license_management(
|
|
|
|
realm, user_count + 5, user_count + 5
|
|
|
|
)
|
|
|
|
|
2022-02-12 02:29:48 +01:00
|
|
|
user_no = 0
|
|
|
|
|
2021-05-28 15:57:08 +02:00
|
|
|
def create_new_user_and_verify_strings_in_notification_message(
|
|
|
|
strings_present: Sequence[str] = [], strings_absent: Sequence[str] = []
|
|
|
|
) -> None:
|
2022-02-12 02:29:48 +01:00
|
|
|
nonlocal user_no
|
|
|
|
user_no += 1
|
2021-05-28 15:57:08 +02:00
|
|
|
new_user = UserProfile.objects.create(
|
|
|
|
realm=realm,
|
|
|
|
full_name=f"new user {user_no}",
|
|
|
|
email=f"user-{user_no}-email@zulip.com",
|
|
|
|
delivery_email=f"user-{user_no}-delivery-email@zulip.com",
|
|
|
|
)
|
|
|
|
notify_new_user(new_user)
|
|
|
|
|
|
|
|
message = self.get_last_message()
|
|
|
|
actual_stream = Stream.objects.get(id=message.recipient.type_id)
|
2021-04-29 17:22:48 +02:00
|
|
|
self.assertEqual(actual_stream, realm.signup_notifications_stream)
|
2021-05-28 15:57:08 +02:00
|
|
|
self.assertIn(
|
|
|
|
f"@_**new user {user_no}|{new_user.id}** just signed up for Zulip.",
|
|
|
|
message.content,
|
|
|
|
)
|
|
|
|
for string_present in strings_present:
|
|
|
|
self.assertIn(
|
|
|
|
string_present,
|
|
|
|
message.content,
|
|
|
|
)
|
|
|
|
for string_absent in strings_absent:
|
|
|
|
self.assertNotIn(string_absent, message.content)
|
|
|
|
|
|
|
|
create_new_user_and_verify_strings_in_notification_message(
|
|
|
|
strings_absent=["Your organization has"]
|
|
|
|
)
|
|
|
|
create_new_user_and_verify_strings_in_notification_message(
|
|
|
|
strings_present=[
|
|
|
|
"Your organization has only three Zulip licenses remaining",
|
|
|
|
"to allow more than three users to",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
create_new_user_and_verify_strings_in_notification_message(
|
|
|
|
strings_present=[
|
|
|
|
"Your organization has only two Zulip licenses remaining",
|
|
|
|
"to allow more than two users to",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
create_new_user_and_verify_strings_in_notification_message(
|
|
|
|
strings_present=[
|
|
|
|
"Your organization has only one Zulip license remaining",
|
|
|
|
"to allow more than one user to",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
create_new_user_and_verify_strings_in_notification_message(
|
|
|
|
strings_present=[
|
|
|
|
"Your organization has no Zulip licenses remaining",
|
|
|
|
"to allow new users to",
|
|
|
|
]
|
|
|
|
)
|