From 67d11e8d40fde8b8a7decb38ad996623c0305102 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 2 Apr 2021 13:49:55 +0300 Subject: [PATCH] Fix the month boundary calculation in monthly reports Fixes: #497 --- CHANGELOG.md | 1 + hc/accounts/models.py | 6 +++++- hc/accounts/tests/test_profile_model.py | 15 +++++++++++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6745edf..24497aa3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ All notable changes to this project will be documented in this file. - Fix sendalerts to clear Profile.next_nag_date if all checks up - Fix the pause action to clear Profile.next_nag_date if all checks up - Fix the "Email Reports" screen to clear Profile.next_nag_date if all checks up +- Fix the month boundary calculation in monthly reports (#497) ## v1.19.0 - 2021-02-03 diff --git a/hc/accounts/models.py b/hc/accounts/models.py index f87d2290..1d40c896 100644 --- a/hc/accounts/models.py +++ b/hc/accounts/models.py @@ -195,6 +195,10 @@ class Profile(models.Model): "List-Unsubscribe-Post": "List-Unsubscribe=One-Click", } + boundaries = month_boundaries(months=3) + # throw away the current month, keep two previous months + boundaries.pop() + ctx = { "checks": checks, "sort": self.sort, @@ -204,7 +208,7 @@ class Profile(models.Model): "nag": nag, "nag_period": self.nag_period.total_seconds(), "num_down": num_down, - "month_boundaries": month_boundaries(), + "month_boundaries": boundaries, } emails.report(self.user.email, ctx, headers) diff --git a/hc/accounts/tests/test_profile_model.py b/hc/accounts/tests/test_profile_model.py index f4e16014..8b0aee8a 100644 --- a/hc/accounts/tests/test_profile_model.py +++ b/hc/accounts/tests/test_profile_model.py @@ -1,12 +1,17 @@ -from datetime import timedelta as td +from datetime import datetime, timedelta as td +from unittest.mock import Mock, patch from django.core import mail -from django.utils.timezone import now +from django.utils.timezone import now, utc from hc.test import BaseTestCase from hc.api.models import Check +CURRENT_TIME = datetime(2020, 1, 15, tzinfo=utc) +MOCK_NOW = Mock(return_value=CURRENT_TIME) + class ProfileModelTestCase(BaseTestCase): + @patch("hc.lib.date.timezone.now", MOCK_NOW) def test_it_sends_report(self): check = Check(project=self.project, name="Test Check") check.last_ping = now() @@ -22,6 +27,12 @@ class ProfileModelTestCase(BaseTestCase): self.assertEqual(message.subject, "Monthly Report") self.assertIn("Test Check", message.body) + html, _ = message.alternatives[0] + self.assertNotIn("Jan. 2020", html) + self.assertIn("Dec. 2019", html) + self.assertIn("Nov. 2019", html) + self.assertNotIn("Oct. 2019", html) + def test_it_skips_report_if_no_pings(self): check = Check(project=self.project, name="Test Check") check.save()