From 7737413cec31590eb8a878df40754f4daaceb471 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Thu, 12 Nov 2020 14:17:33 +0000 Subject: [PATCH] digest tests: Improve gather_new_streams test. We don't need to mock the dates here. We also explicitly clear out all streams first, and then we explicitly test with both the stream being current and the stream being old. --- zerver/tests/test_digest.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/zerver/tests/test_digest.py b/zerver/tests/test_digest.py index 8fa9447605..ce11c8fa66 100644 --- a/zerver/tests/test_digest.py +++ b/zerver/tests/test_digest.py @@ -25,6 +25,7 @@ from zerver.models import ( Message, Realm, RealmAuditLog, + Stream, UserActivity, UserProfile, flush_per_request_caches, @@ -400,16 +401,27 @@ class TestDigestEmailMessages(ZulipTestCase): user = arg[0][0] self.assertNotEqual(user.id, bot.id) - @mock.patch('zerver.lib.digest.timezone_now') @override_settings(SEND_DIGEST_EMAILS=True) - def test_new_stream_link(self, mock_django_timezone: mock.MagicMock) -> None: - cutoff = datetime.datetime(year=2017, month=11, day=1, tzinfo=datetime.timezone.utc) - mock_django_timezone.return_value = datetime.datetime(year=2017, month=11, day=5, tzinfo=datetime.timezone.utc) + def test_new_stream_link(self) -> None: + Stream.objects.all().delete() + cutoff = timezone_now() - datetime.timedelta(days=5) cordelia = self.example_user('cordelia') - stream_id = create_stream_if_needed(cordelia.realm, 'New stream')[0].id - new_stream = gather_new_streams(cordelia, cutoff)[1] - expected_html = f"New stream" - self.assertIn(expected_html, new_stream['html']) + stream = create_stream_if_needed(cordelia.realm, 'New stream')[0] + stream.date_created = timezone_now() + stream.save() + + stream_count, stream_info = gather_new_streams(cordelia, cutoff) + self.assertEqual(stream_count, 1) + expected_html = f"New stream" + self.assertEqual(stream_info['html'][0], expected_html) + + # Make the stream appear to be older. + stream.date_created = timezone_now() - datetime.timedelta(days=7) + stream.save() + + stream_count, stream_info = gather_new_streams(cordelia, cutoff) + self.assertEqual(stream_count, 0) + self.assertEqual(stream_info['html'], []) def simulate_stream_conversation(self, stream: str, senders: List[str]) -> List[int]: client = 'website' # this makes `sent_by_human` return True