diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index 4d5aca14d..8453ef369 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -134,11 +134,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), @@ -155,31 +161,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)), @@ -341,8 +332,15 @@ 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(), ) + # 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(), @@ -433,13 +431,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 c4326ef89..926163dbd 100644 --- a/tests/app/main/views/test_dashboard.py +++ b/tests/app/main/views/test_dashboard.py @@ -678,12 +678,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-01-01 12:00") @pytest.mark.parametrize( "extra_args", [ {}, - {"year": "2016"}, + {"year": "2017"}, ], ) def test_should_show_monthly_breakdown_of_template_usage( @@ -695,7 +695,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") @@ -703,9 +703,22 @@ 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"]) + # October is the only month with data, thus it's not in the list. assert len(page.select(".table-no-data")) == len( - ["November", "December", "January"] + [ + "January", + "February", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "November", + "December", + ] ) @@ -748,9 +761,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" ) @@ -965,18 +978,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"}) @@ -1043,6 +1056,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 "January" in monthly_breakdown assert "October" in monthly_breakdown assert "February" in monthly_breakdown assert "March" in monthly_breakdown @@ -1051,8 +1065,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( @@ -1112,7 +1126,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) @@ -1147,7 +1161,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) @@ -1378,7 +1392,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/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): diff --git a/tests/conftest.py b/tests/conftest.py index 4c2ceeea9..ab86e3052 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2330,6 +2330,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