def test_receive_digest_email_messages( self, mock_send_future_email: mock.MagicMock, mock_enough_traffic: mock.MagicMock) -> None: # build dummy messages for missed messages email reply # have Hamlet send Othello a PM. Othello will reply via email # Hamlet will receive the message. email = self.example_email('hamlet') self.login(email) result = self.client_post( "/json/messages", { "type": "private", "content": "test_receive_missed_message_email_messages", "client": "test suite", "to": self.example_email('othello') }) self.assert_json_success(result) user_profile = self.example_user('othello') cutoff = time.mktime( datetime.datetime(year=2016, month=1, day=1).timetuple()) handle_digest_email(user_profile.id, cutoff) self.assertEqual(mock_send_future_email.call_count, 1) self.assertEqual(mock_send_future_email.call_args[1]['to_user_id'], user_profile.id)
def test_receive_digest_email_messages( self, mock_send_future_email: mock.MagicMock, mock_enough_traffic: mock.MagicMock) -> None: # build dummy messages for missed messages email reply # have Hamlet send Othello a PM. Othello will reply via email # Hamlet will receive the message. hamlet = self.example_user('hamlet') self.login(hamlet.email) result = self.client_post( "/json/messages", { "type": "private", "content": "test_receive_missed_message_email_messages", "client": "test suite", "to": self.example_email('othello') }) self.assert_json_success(result) user_profile = self.example_user('othello') cutoff = time.mktime( datetime.datetime(year=2016, month=1, day=1).timetuple()) handle_digest_email(user_profile.id, cutoff) self.assertEqual(mock_send_future_email.call_count, 1) kwargs = mock_send_future_email.call_args[1] self.assertEqual(kwargs['to_user_ids'], [user_profile.id]) html = kwargs['context']['unread_pms'][0]['header']['html'] expected_url = "'http://zulip.testserver/#narrow/pm-with/{id}-hamlet'".format( id=hamlet.id) self.assertIn(expected_url, html)
def test_receive_digest_email_messages(self, mock_send_future_email: mock.MagicMock, mock_enough_traffic: mock.MagicMock) -> None: # build dummy messages for missed messages email reply # have Hamlet send Othello a PM. Othello will reply via email # Hamlet will receive the message. hamlet = self.example_user('hamlet') self.login(hamlet.email) result = self.client_post("/json/messages", {"type": "private", "content": "test_receive_missed_message_email_messages", "client": "test suite", "to": self.example_email('othello')}) self.assert_json_success(result) user_profile = self.example_user('othello') cutoff = time.mktime(datetime.datetime(year=2016, month=1, day=1).timetuple()) handle_digest_email(user_profile.id, cutoff) self.assertEqual(mock_send_future_email.call_count, 1) kwargs = mock_send_future_email.call_args[1] self.assertEqual(kwargs['to_user_ids'], [user_profile.id]) html = kwargs['context']['unread_pms'][0]['header']['html'] expected_url = "'http://zulip.testserver/#narrow/pm-with/{id}-hamlet'".format(id=hamlet.id) self.assertIn(expected_url, html)
def test_soft_deactivated_user_multiple_stream_senders( self, mock_send_future_email: mock.MagicMock, mock_enough_traffic: mock.MagicMock) -> None: one_day_ago = timezone_now() - datetime.timedelta(days=1) Message.objects.all().update(pub_date=one_day_ago) othello = self.example_user('othello') for stream in ['Verona', 'Scotland', 'Denmark']: self.subscribe(othello, stream) RealmAuditLog.objects.all().delete() othello.long_term_idle = True othello.save(update_fields=['long_term_idle']) # Send messages to a stream and unsubscribe - subscribe from that stream senders = ['hamlet', 'cordelia', 'iago', 'prospero', 'ZOE'] self.simulate_stream_conversation('Verona', senders) self.unsubscribe(othello, 'Verona') self.subscribe(othello, 'Verona') # Send messages to other streams self.simulate_stream_conversation('Scotland', senders) self.simulate_stream_conversation('Denmark', senders) one_hour_ago = timezone_now() - datetime.timedelta(seconds=3600) cutoff = time.mktime(one_hour_ago.timetuple()) flush_per_request_caches() # When this test is run in isolation, one additional query is run which # is equivalent to # ContentType.objects.get(app_label='zerver', model='userprofile') # This code is run when we call `confirmation.models.create_confirmation_link`. # To trigger this, we call the one_click_unsubscribe_link function below. one_click_unsubscribe_link(othello, 'digest') with queries_captured() as queries: handle_digest_email(othello.id, cutoff) self.assert_length(queries, 9) self.assertEqual(mock_send_future_email.call_count, 1) kwargs = mock_send_future_email.call_args[1] self.assertEqual(kwargs['to_user_ids'], [othello.id]) hot_conversations = kwargs['context']['hot_conversations'] self.assertEqual(2, len(hot_conversations), [othello.id]) hot_convo = hot_conversations[0] expected_participants = { self.example_user(sender).full_name for sender in senders } self.assertEqual(set(hot_convo['participants']), expected_participants) self.assertEqual(hot_convo['count'], 5 - 2) # 5 messages, but 2 shown teaser_messages = hot_convo['first_few_messages'][0]['senders'] self.assertIn('some content', teaser_messages[0]['content'][0]['plain']) self.assertIn(teaser_messages[0]['sender'], expected_participants)
def test_multiple_stream_senders( self, mock_send_future_email: mock.MagicMock, mock_enough_traffic: mock.MagicMock) -> None: client = 'website' # this makes `sent_by_human` return True othello = self.example_user('othello') self.subscribe(othello, 'Verona') one_day_ago = timezone_now() - datetime.timedelta(days=1) Message.objects.all().update(pub_date=one_day_ago) one_sec_ago = timezone_now() - datetime.timedelta(seconds=1) cutoff = time.mktime(one_sec_ago.timetuple()) senders = ['hamlet', 'cordelia', 'iago', 'prospero', 'ZOE'] for sender_name in senders: email = self.example_email(sender_name) self.login(email) content = 'some content for ' + email payload = dict( type='stream', client=client, to='Verona', topic='lunch', content=content, ) result = self.client_post("/json/messages", payload) self.assert_json_success(result) flush_per_request_caches() with queries_captured() as queries: handle_digest_email(othello.id, cutoff) self.assertTrue(21 <= len(queries) <= 22) self.assertEqual(mock_send_future_email.call_count, 1) kwargs = mock_send_future_email.call_args[1] self.assertEqual(kwargs['to_user_ids'], [othello.id]) hot_convo = kwargs['context']['hot_conversations'][0] expected_participants = { self.example_user(sender).full_name for sender in senders } self.assertEqual(set(hot_convo['participants']), expected_participants) self.assertEqual(hot_convo['count'], 5 - 2) # 5 messages, but 2 shown teaser_messages = hot_convo['first_few_messages'][0]['senders'] self.assertIn('some content', teaser_messages[0]['content'][0]['plain']) self.assertIn(teaser_messages[0]['sender'], expected_participants)
def test_multiple_stream_senders(self, mock_send_future_email: mock.MagicMock, mock_enough_traffic: mock.MagicMock) -> None: client = 'website' # this makes `sent_by_human` return True othello = self.example_user('othello') self.subscribe(othello, 'Verona') one_day_ago = timezone_now() - datetime.timedelta(days=1) Message.objects.all().update(pub_date=one_day_ago) one_sec_ago = timezone_now() - datetime.timedelta(seconds=1) cutoff = time.mktime(one_sec_ago.timetuple()) senders = ['hamlet', 'cordelia', 'iago', 'prospero', 'ZOE'] for sender_name in senders: email = self.example_email(sender_name) self.login(email) content = 'some content for ' + email payload = dict( type='stream', client=client, to='Verona', topic='lunch', content=content, ) result = self.client_post("/json/messages", payload) self.assert_json_success(result) flush_per_request_caches() with queries_captured() as queries: handle_digest_email(othello.id, cutoff) self.assertTrue(24 <= len(queries) <= 25) self.assertEqual(mock_send_future_email.call_count, 1) kwargs = mock_send_future_email.call_args[1] self.assertEqual(kwargs['to_user_ids'], [othello.id]) hot_convo = kwargs['context']['hot_conversations'][0] expected_participants = { self.example_user(sender).full_name for sender in senders } self.assertEqual(set(hot_convo['participants']), expected_participants) self.assertEqual(hot_convo['count'], 5 - 2) # 5 messages, but 2 shown teaser_messages = hot_convo['first_few_messages'][0]['senders'] self.assertIn('some content', teaser_messages[0]['content'][0]['plain']) self.assertIn(teaser_messages[0]['sender'], expected_participants)
def digest_page(request: HttpRequest) -> HttpResponse: user_profile_id = request.user.id cutoff = time.mktime((timezone_now() - timedelta(days=DIGEST_CUTOFF)).timetuple()) context = handle_digest_email(user_profile_id, cutoff, render_to_web=True) if context: context.update({'physical_address': settings.PHYSICAL_ADDRESS}) return render(request, 'zerver/digest_base.html', context=context)
def test_guest_user_multiple_stream_sender( self, mock_send_future_email: mock.MagicMock, mock_enough_traffic: mock.MagicMock) -> None: othello = self.example_user('othello') hamlet = self.example_user('hamlet') cordelia = self.example_user('cordelia') polonius = self.example_user('polonius') create_stream_if_needed(cordelia.realm, 'web_public_stream', is_web_public=True) self.subscribe(othello, 'web_public_stream') self.subscribe(hamlet, 'web_public_stream') self.subscribe(cordelia, 'web_public_stream') self.subscribe(polonius, 'web_public_stream') one_day_ago = timezone_now() - datetime.timedelta(days=1) Message.objects.all().update(date_sent=one_day_ago) one_hour_ago = timezone_now() - datetime.timedelta(seconds=3600) cutoff = time.mktime(one_hour_ago.timetuple()) senders = ['hamlet', 'cordelia', 'othello', 'desdemona'] self.simulate_stream_conversation('web_public_stream', senders) flush_per_request_caches() # When this test is run in isolation, one additional query is run which # is equivalent to # ContentType.objects.get(app_label='zerver', model='userprofile') # This code is run when we call `confirmation.models.create_confirmation_link`. # To trigger this, we call the one_click_unsubscribe_link function below. one_click_unsubscribe_link(polonius, 'digest') with queries_captured() as queries: handle_digest_email(polonius.id, cutoff) self.assert_length(queries, 7) self.assertEqual(mock_send_future_email.call_count, 1) kwargs = mock_send_future_email.call_args[1] self.assertEqual(kwargs['to_user_ids'], [polonius.id]) new_stream_names = kwargs['context']['new_streams']['plain'] self.assertTrue('web_public_stream' in new_stream_names)
def test_receive_digest_email_messages(self, mock_send_future_email: mock.MagicMock, mock_enough_traffic: mock.MagicMock) -> None: # build dummy messages for missed messages email reply # have Hamlet send Othello a PM. Othello will reply via email # Hamlet will receive the message. email = self.example_email('hamlet') self.login(email) result = self.client_post("/json/messages", {"type": "private", "content": "test_receive_missed_message_email_messages", "client": "test suite", "to": self.example_email('othello')}) self.assert_json_success(result) user_profile = self.example_user('othello') cutoff = time.mktime(datetime.datetime(year=2016, month=1, day=1).timetuple()) handle_digest_email(user_profile.id, cutoff) self.assertEqual(mock_send_future_email.call_count, 1) self.assertEqual(mock_send_future_email.call_args[1]['to_user_id'], user_profile.id)
def test_huddle_urls(self, mock_send_future_email: mock.MagicMock, mock_enough_traffic: mock.MagicMock) -> None: email = self.example_email('hamlet') self.login(email) huddle_emails = [ self.example_email('cordelia'), self.example_email('othello'), ] payload = dict( type='private', content='huddle message', client='test suite', to=','.join(huddle_emails), ) result = self.client_post("/json/messages", payload) self.assert_json_success(result) user_profile = self.example_user('othello') cutoff = time.mktime( datetime.datetime(year=2016, month=1, day=1).timetuple()) handle_digest_email(user_profile.id, cutoff) self.assertEqual(mock_send_future_email.call_count, 1) kwargs = mock_send_future_email.call_args[1] self.assertEqual(kwargs['to_user_ids'], [user_profile.id]) html = kwargs['context']['unread_pms'][0]['header']['html'] other_user_ids = sorted([ self.example_user('cordelia').id, self.example_user('hamlet').id, ]) slug = ','.join(str(user_id) for user_id in other_user_ids) + '-group' expected_url = "'http://zulip.testserver/#narrow/pm-with/" + slug + "'" self.assertIn(expected_url, html)
def test_huddle_urls(self, mock_send_future_email: mock.MagicMock, mock_enough_traffic: mock.MagicMock) -> None: email = self.example_email('hamlet') self.login(email) huddle_emails = [ self.example_email('cordelia'), self.example_email('othello'), ] payload = dict( type='private', content='huddle message', client='test suite', to=','.join(huddle_emails), ) result = self.client_post("/json/messages", payload) self.assert_json_success(result) user_profile = self.example_user('othello') cutoff = time.mktime(datetime.datetime(year=2016, month=1, day=1).timetuple()) handle_digest_email(user_profile.id, cutoff) self.assertEqual(mock_send_future_email.call_count, 1) kwargs = mock_send_future_email.call_args[1] self.assertEqual(kwargs['to_user_ids'], [user_profile.id]) html = kwargs['context']['unread_pms'][0]['header']['html'] other_user_ids = sorted([ self.example_user('cordelia').id, self.example_user('hamlet').id, ]) slug = ','.join(str(user_id) for user_id in other_user_ids) + '-group' expected_url = "'http://zulip.testserver/#narrow/pm-with/" + slug + "'" self.assertIn(expected_url, html)
def consume(self, event): # type: (Mapping[str, Any]) -> None logging.info("Received digest event: %s" % (event, )) handle_digest_email(event["user_profile_id"], event["cutoff"])
def consume(self, event): logging.info("Received digest event: %s" % (event,)) handle_digest_email(event["user_profile_id"], event["cutoff"])
def consume(self, event): # type: (Mapping[str, Any]) -> None logging.info("Received digest event: %s" % (event,)) handle_digest_email(event["user_profile_id"], event["cutoff"])
def consume(self, event): logging.info("Received digest event: %s" % (event, )) handle_digest_email(event["user_profile_id"], event["cutoff"])
def digest_page(request: HttpRequest) -> HttpResponse: user_profile_id = request.user.id cutoff = time.mktime((timezone_now() - timedelta(days=DIGEST_CUTOFF)).timetuple()) context = handle_digest_email(user_profile_id, cutoff, render_to_web=True) return render(request, 'zerver/emails/compiled/digest.html', context=context)