user settings: Add setting to make login notifications optional.

This adds a feature in the "Notification" section of "Settings" tab,
which lets user enable or disable login emails notification.

Tweaked by tabbott to simplify the test.

Fixes: #5795, progress towards #5854.
This commit is contained in:
Pragati Agrawal 2018-08-24 10:58:51 +05:30 committed by Tim Abbott
parent 1400a040fd
commit ba4e4e38ae
9 changed files with 59 additions and 1 deletions

View File

@ -102,6 +102,7 @@ function setup_settings_label() {
// other_notification_settings
enable_digest_emails: i18n.t("Send digest emails when I'm away"),
enable_login_emails: i18n.t("Send email notifications for new logins to my account"),
message_content_in_email_notifications: i18n.t("Include message content in missed message emails"),
realm_name_in_notifications: i18n.t("Include organization name in subject of missed message emails"),

View File

@ -20,6 +20,7 @@ var pm_mention_notification_settings = [
var other_notification_settings = [
"enable_digest_emails",
"enable_login_emails",
"realm_name_in_notifications",
"message_content_in_email_notifications",
];

View File

@ -102,6 +102,11 @@
"is_checked" page_params.enable_digest_emails
"label" settings_label.enable_digest_emails}}
{{partial "settings_checkbox"
"setting_name" "enable_login_emails"
"is_checked" page_params.enable_login_emails
"label" settings_label.enable_login_emails}}
<div class="input-group {{#unless page_params.enable_offline_email_notifications}}control-label-disabled{{/unless}}">
<label class="checkbox">
<input type="checkbox" name="message_content_in_email_notifications"

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.14 on 2018-08-22 09:57
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('zerver', '0187_userprofile_is_billing_admin'),
]
operations = [
migrations.AddField(
model_name='userprofile',
name='enable_login_emails',
field=models.BooleanField(default=True),
),
]

View File

@ -735,6 +735,7 @@ class UserProfile(AbstractBaseUser, PermissionsMixin):
enable_online_push_notifications = models.BooleanField(default=False) # type: bool
enable_digest_emails = models.BooleanField(default=True) # type: bool
enable_login_emails = models.BooleanField(default=True) # type: bool
realm_name_in_notifications = models.BooleanField(default=False) # type: bool
# Words that trigger a mention for this user, formatted as a json-serialized list of strings
@ -830,6 +831,7 @@ class UserProfile(AbstractBaseUser, PermissionsMixin):
notification_setting_types = dict(
enable_desktop_notifications=bool,
enable_digest_emails=bool,
enable_login_emails=bool,
enable_offline_email_notifications=bool,
enable_offline_push_notifications=bool,
enable_online_push_notifications=bool,

View File

@ -59,6 +59,8 @@ def get_device_os(user_agent: str) -> Optional[str]:
@receiver(user_logged_in, dispatch_uid="only_on_login")
def email_on_new_login(sender: Any, user: UserProfile, request: Any, **kwargs: Any) -> None:
if not user.enable_login_emails:
return
# We import here to minimize the dependencies of this module,
# since it runs as part of `manage.py` initialization
from zerver.context_processors import common_context

View File

@ -66,6 +66,7 @@ class HomeTest(ZulipTestCase):
"emojiset_choices",
"enable_desktop_notifications",
"enable_digest_emails",
"enable_login_emails",
"enable_offline_email_notifications",
"enable_offline_push_notifications",
"enable_online_push_notifications",

View File

@ -2,9 +2,11 @@ import datetime
from django.conf import settings
from django.core import mail
from django.contrib.auth.signals import user_logged_in
from django.test import override_settings
from zerver.lib.test_classes import ZulipTestCase
from zerver.signals import get_device_browser, get_device_os, JUST_CREATED_THRESHOLD
from zerver.lib.actions import notify_new_user
from zerver.lib.actions import notify_new_user, do_change_notification_settings
from zerver.models import Recipient, Stream, Realm
from zerver.lib.initial_password import initial_password
from unittest import mock
@ -84,6 +86,29 @@ class SendLoginEmailTest(ZulipTestCase):
subject = 'New login from an unknown browser on an unknown operating system'
self.assertNotEqual(email.subject, subject)
@override_settings(SEND_LOGIN_EMAILS=True)
def test_enable_login_emails_user_setting(self) -> None:
user = self.example_user('hamlet')
utc = get_timezone('utc')
mock_time = datetime.datetime(year=2018, month=1, day=1, tzinfo=utc)
user.timezone = 'US/Pacific'
user.date_joined = mock_time - datetime.timedelta(seconds=JUST_CREATED_THRESHOLD + 1)
user.save()
do_change_notification_settings(user, "enable_login_emails", False)
self.assertFalse(user.enable_login_emails)
with mock.patch('zerver.signals.timezone_now', return_value=mock_time):
self.login(user.email)
self.assertEqual(len(mail.outbox), 0)
do_change_notification_settings(user, "enable_login_emails", True)
self.assertTrue(user.enable_login_emails)
with mock.patch('zerver.signals.timezone_now', return_value=mock_time):
self.login(user.email)
self.assertEqual(len(mail.outbox), 1)
class TestBrowserAndOsUserAgentStrings(ZulipTestCase):
def setUp(self) -> None:

View File

@ -160,6 +160,7 @@ def json_change_notify_settings(
enable_offline_push_notifications: Optional[bool]=REQ(validator=check_bool, default=None),
enable_online_push_notifications: Optional[bool]=REQ(validator=check_bool, default=None),
enable_digest_emails: Optional[bool]=REQ(validator=check_bool, default=None),
enable_login_emails: Optional[bool]=REQ(validator=check_bool, default=None),
message_content_in_email_notifications: Optional[bool]=REQ(validator=check_bool, default=None),
pm_content_in_desktop_notifications: Optional[bool]=REQ(validator=check_bool, default=None),
realm_name_in_notifications: Optional[bool]=REQ(validator=check_bool, default=None)