From 6999d72f76e2581c7023208f39326720cf381f14 Mon Sep 17 00:00:00 2001 From: Andrew Shumway Date: Wed, 7 Feb 2024 09:38:18 -0700 Subject: [PATCH 1/3] Fix dashboard bug/usage page --- app/main/views/dashboard.py | 46 ++++++----------- app/utils/time.py | 2 +- .../views/organizations/test_organizations.py | 10 ++-- tests/app/main/views/test_dashboard.py | 49 ++++++++++++------- tests/app/utils/test_time.py | 8 +-- 5 files changed, 55 insertions(+), 60 deletions(-) diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index a88203ae7..f5f38087a 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -123,11 +123,17 @@ def template_usage(service_id): months=months, stats=stats, most_used_template_count=max( - max( - (template["requested_count"] for template in month["templates_used"]), - default=0, - ) - for month in months + ( + max( + ( + template["requested_count"] + for template in month["templates_used"] + ), + default=0, + ) + for month in months + ), + default=0, ), years=get_tuples_of_financial_years( partial(url_for, ".template_usage", service_id=service_id), @@ -144,31 +150,16 @@ def usage(service_id): year, current_financial_year = requested_and_current_financial_year(request) free_sms_allowance = billing_api_client.get_free_sms_fragment_limit_for_year( - service_id, year + service_id ) + units = billing_api_client.get_monthly_usage_for_service(service_id, year) + yearly_usage = billing_api_client.get_annual_usage_for_service(service_id, year) more_stats = format_monthly_stats_to_list( service_api_client.get_monthly_notification_stats(service_id, year)["data"] ) - if year == current_financial_year: - # This includes Oct, Nov, Dec - # but we don't need next year's data yet - more_stats = [ - month - for month in more_stats - if month["name"] in ["October", "November", "December"] - ] - elif year == (current_financial_year + 1): - # This is all the other months - # and we need last year's data - more_stats = [ - month - for month in more_stats - if month["name"] not in ["October", "November", "December"] - ] - return render_template( "views/usage.html", months=list(get_monthly_usage_breakdown(year, units, more_stats)), @@ -330,7 +321,6 @@ def get_dashboard_partials(service_id): dashboard_totals = (get_dashboard_totals(stats),) free_sms_allowance = billing_api_client.get_free_sms_fragment_limit_for_year( current_service.id, - get_current_financial_year(), ) yearly_usage = billing_api_client.get_annual_usage_for_service( service_id, @@ -422,13 +412,7 @@ def aggregate_status_types(counts_dict): def get_months_for_financial_year(year, time_format="%B"): - return [ - month.strftime(time_format) - for month in ( - get_months_for_year(10, 13, year) + get_months_for_year(1, 10, year + 1) - ) - if month < datetime.now() - ] + return [month.strftime(time_format) for month in (get_months_for_year(1, 13, year))] def get_months_for_year(start, end, year): diff --git a/app/utils/time.py b/app/utils/time.py index 9ae24b1bd..e2f4a8e36 100644 --- a/app/utils/time.py +++ b/app/utils/time.py @@ -11,7 +11,7 @@ def get_current_financial_year(): now = datetime.now(preferred_tz) current_month = int(now.strftime("%-m")) current_year = int(now.strftime("%Y")) - return current_year if current_month > 9 else current_year - 1 + return current_year if current_month < 10 else current_year + 1 def is_less_than_days_ago(date_from_db, number_of_days): diff --git a/tests/app/main/views/organizations/test_organizations.py b/tests/app/main/views/organizations/test_organizations.py index 7679e1429..27663ad22 100644 --- a/tests/app/main/views/organizations/test_organizations.py +++ b/tests/app/main/views/organizations/test_organizations.py @@ -388,7 +388,7 @@ def test_organization_services_shows_live_services_and_usage( client_request.login(active_user_with_permissions) page = client_request.get(".organization_dashboard", org_id=ORGANISATION_ID) - mock.assert_called_once_with(ORGANISATION_ID, 2019) + mock.assert_called_once_with(ORGANISATION_ID, 2020) services = page.select("main h3") usage_rows = page.select("main .grid-col-6") @@ -459,9 +459,9 @@ def test_organization_services_shows_live_services_and_usage_with_count_of_1( @pytest.mark.parametrize( ("financial_year", "expected_selected"), [ - (2017, "2017 to 2018 fiscal year"), (2018, "2018 to 2019 fiscal year"), (2019, "2019 to 2020 fiscal year"), + (2020, "2020 to 2021 fiscal year"), ], ) def test_organization_services_filters_by_financial_year( @@ -483,9 +483,9 @@ def test_organization_services_filters_by_financial_year( ) mock.assert_called_once_with(ORGANISATION_ID, financial_year) assert normalize_spaces(page.select_one(".pill").text) == ( + "2020 to 2021 fiscal year " "2019 to 2020 fiscal year " - "2018 to 2019 fiscal year " - "2017 to 2018 fiscal year" + "2018 to 2019 fiscal year" ) assert normalize_spaces(page.select_one(".pill-item--selected").text) == ( expected_selected @@ -610,7 +610,7 @@ def test_organization_services_links_to_downloadable_report( assert link_to_report.attrs["href"] == url_for( ".download_organization_usage_report", org_id=ORGANISATION_ID, - selected_year=2021, + selected_year=2022, ) diff --git a/tests/app/main/views/test_dashboard.py b/tests/app/main/views/test_dashboard.py index 8e9ecb531..9703a2de5 100644 --- a/tests/app/main/views/test_dashboard.py +++ b/tests/app/main/views/test_dashboard.py @@ -666,12 +666,12 @@ def test_should_show_redirect_from_template_history( ) -@freeze_time("2017-01-01 12:00") # 4 months into 2016 financial year +@freeze_time("2017-1-1 12:00") # Switching to calendar year @pytest.mark.parametrize( "extra_args", [ {}, - {"year": "2016"}, + {"year": "2017"}, ], ) def test_should_show_monthly_breakdown_of_template_usage( @@ -683,7 +683,7 @@ def test_should_show_monthly_breakdown_of_template_usage( "main.template_usage", service_id=SERVICE_ONE_ID, **extra_args ) - mock_get_monthly_template_usage.assert_called_once_with(SERVICE_ONE_ID, 2016) + mock_get_monthly_template_usage.assert_called_once_with(SERVICE_ONE_ID, 2017) table_rows = page.select("tbody tr") @@ -691,9 +691,21 @@ def test_should_show_monthly_breakdown_of_template_usage( "My first template " "Text message template " "2" ) - assert len(table_rows) == len(["October"]) + assert len(table_rows) == len(["January"]) assert len(page.select(".table-no-data")) == len( - ["November", "December", "January"] + [ + "January", + "February", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "October", + "November", + ] ) @@ -736,9 +748,9 @@ def test_stats_pages_show_last_3_years( ) assert normalize_spaces(page.select_one(".pill").text) == ( + "2015 to 2016 fiscal year " "2014 to 2015 fiscal year " - "2013 to 2014 fiscal year " - "2012 to 2013 fiscal year" + "2013 to 2014 fiscal year" ) @@ -953,18 +965,18 @@ def test_usage_page( service_id=SERVICE_ONE_ID, ) - mock_get_monthly_usage_for_service.assert_called_once_with(SERVICE_ONE_ID, 2011) - mock_get_annual_usage_for_service.assert_called_once_with(SERVICE_ONE_ID, 2011) - mock_get_free_sms_fragment_limit.assert_called_with(SERVICE_ONE_ID, 2011) + mock_get_monthly_usage_for_service.assert_called_once_with(SERVICE_ONE_ID, 2012) + mock_get_annual_usage_for_service.assert_called_once_with(SERVICE_ONE_ID, 2012) + mock_get_free_sms_fragment_limit.assert_called_with(SERVICE_ONE_ID) nav = page.find("ul", {"class": "pill"}) unselected_nav_links = nav.select("a:not(.pill-item--selected)") assert ( normalize_spaces(nav.find("a", {"aria-current": "page"}).text) - == "2011 to 2012 fiscal year" + == "2012 to 2013 fiscal year" ) - assert normalize_spaces(unselected_nav_links[0].text) == "2010 to 2011 fiscal year" - assert normalize_spaces(unselected_nav_links[1].text) == "2009 to 2010 fiscal year" + assert normalize_spaces(unselected_nav_links[0].text) == "2011 to 2012 fiscal year" + assert normalize_spaces(unselected_nav_links[1].text) == "2010 to 2011 fiscal year" annual_usage = page.find_all("div", {"class": "keyline-block"}) @@ -1031,8 +1043,7 @@ def test_usage_page_monthly_breakdown( page = client_request.get("main.usage", service_id=SERVICE_ONE_ID) monthly_breakdown = normalize_spaces(page.find("table").text) - assert "October" in monthly_breakdown - assert "249,860 free text messages" in monthly_breakdown + assert "January" in monthly_breakdown assert "February" in monthly_breakdown assert "$16.40" in monthly_breakdown @@ -1048,8 +1059,8 @@ def test_usage_page_monthly_breakdown( @pytest.mark.parametrize( ("now", "expected_number_of_months"), [ - (freeze_time("2017-03-31 11:09:00.061258"), 6), - (freeze_time("2017-01-01 11:09:00.061258"), 4), + (freeze_time("2017-03-31 11:09:00.061258"), 12), + (freeze_time("2017-01-01 11:09:00.061258"), 12), ], ) def test_usage_page_monthly_breakdown_shows_months_so_far( @@ -1109,7 +1120,7 @@ def test_usage_page_with_year_argument( ) mock_get_monthly_usage_for_service.assert_called_once_with(SERVICE_ONE_ID, 2000) mock_get_annual_usage_for_service.assert_called_once_with(SERVICE_ONE_ID, 2000) - mock_get_free_sms_fragment_limit.assert_called_with(SERVICE_ONE_ID, 2000) + mock_get_free_sms_fragment_limit.assert_called_with(SERVICE_ONE_ID) mock_get_monthly_notification_stats.assert_called_with(SERVICE_ONE_ID, 2000) @@ -1144,7 +1155,7 @@ def test_future_usage_page( mock_get_annual_usage_for_service_in_future.assert_called_once_with( SERVICE_ONE_ID, 2014 ) - mock_get_free_sms_fragment_limit.assert_called_with(SERVICE_ONE_ID, 2014) + mock_get_free_sms_fragment_limit.assert_called_with(SERVICE_ONE_ID) mock_get_monthly_notification_stats.assert_called_with(SERVICE_ONE_ID, 2014) diff --git a/tests/app/utils/test_time.py b/tests/app/utils/test_time.py index 931fdb816..ae51edb97 100644 --- a/tests/app/utils/test_time.py +++ b/tests/app/utils/test_time.py @@ -20,10 +20,10 @@ def test_is_less_than_days_ago(date_from_db, expected_result): @pytest.mark.parametrize( ("datetime_string", "financial_year"), [ - ("2021-01-01T00:00:00+00:00", 2020), # Start of 2021 - ("2021-04-01T03:59:59+00:00", 2020), # One minute before midnight (BST) - ("2021-10-01T04:05:00+00:00", 2021), # Midnight (BST) - ("2021-12-12T12:12:12+01:00", 2021), # Later in the year + ("2021-01-01T00:00:00+00:00", 2021), # Start of 2021 + ("2021-04-01T03:59:59+00:00", 2021), # One minute before midnight (BST) + ("2021-10-01T04:05:00+00:00", 2022), # Midnight (BST) + ("2021-12-12T12:12:12+01:00", 2022), # Later in the year ], ) def test_get_financial_year(datetime_string, financial_year): From 5a440863afa1592acf51b0927fc364989ab0d326 Mon Sep 17 00:00:00 2001 From: Andrew Shumway Date: Wed, 7 Feb 2024 14:43:39 -0700 Subject: [PATCH 2/3] Fix trial mode allowance bug --- app/main/views/dashboard.py | 8 ++++++++ tests/app/main/views/test_dashboard.py | 2 ++ tests/conftest.py | 2 ++ 3 files changed, 12 insertions(+) diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index f5f38087a..87a013547 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -322,6 +322,14 @@ def get_dashboard_partials(service_id): free_sms_allowance = billing_api_client.get_free_sms_fragment_limit_for_year( current_service.id, ) + # These 2 calls will update the dashboard sms allowance count while in trial mode. + billing_api_client.get_monthly_usage_for_service( + service_id, get_current_financial_year() + ) + billing_api_client.create_or_update_free_sms_fragment_limit( + service_id, free_sms_fragment_limit=free_sms_allowance + ) + yearly_usage = billing_api_client.get_annual_usage_for_service( service_id, get_current_financial_year(), diff --git a/tests/app/main/views/test_dashboard.py b/tests/app/main/views/test_dashboard.py index 9703a2de5..10d686a98 100644 --- a/tests/app/main/views/test_dashboard.py +++ b/tests/app/main/views/test_dashboard.py @@ -1386,7 +1386,9 @@ def test_route_for_service_permissions( mock_has_no_jobs, mock_get_template_statistics, mock_get_service_statistics, + mock_get_monthly_usage_for_service, mock_get_annual_usage_for_service, + mock_create_or_update_free_sms_fragment_limit, mock_get_free_sms_fragment_limit, mock_get_inbound_sms_summary, ): diff --git a/tests/conftest.py b/tests/conftest.py index ec4ff8ad1..1724f0234 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2327,6 +2327,8 @@ def client_request(logged_in_client, mocker, service_one): # noqa (C901 too com "app.billing_api_client.create_or_update_free_sms_fragment_limit", autospec=True ) + mocker.patch("app.billing_api_client.get_monthly_usage_for_service", autospec=True) + class ClientRequest: @staticmethod @contextmanager From c9c8882dc9d465b2f66306a146b153218e39cfa9 Mon Sep 17 00:00:00 2001 From: Andrew Shumway Date: Wed, 27 Mar 2024 10:10:56 -0600 Subject: [PATCH 3/3] Fix test --- tests/app/main/views/test_dashboard.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/app/main/views/test_dashboard.py b/tests/app/main/views/test_dashboard.py index cd543d903..9679f2bce 100644 --- a/tests/app/main/views/test_dashboard.py +++ b/tests/app/main/views/test_dashboard.py @@ -679,7 +679,7 @@ def test_should_show_redirect_from_template_history( ) -@freeze_time("2017-1-1 12:00") # Switching to calendar year +@freeze_time("2017-01-01 12:00") @pytest.mark.parametrize( "extra_args", [ @@ -705,6 +705,7 @@ def test_should_show_monthly_breakdown_of_template_usage( ) assert len(table_rows) == len(["January"]) + # October is the only month with data, thus it's not in the list. assert len(page.select(".table-no-data")) == len( [ "January", @@ -716,8 +717,8 @@ def test_should_show_monthly_breakdown_of_template_usage( "July", "August", "September", - "October", "November", + "December", ] )