diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index 347f522df..2d59d822f 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -204,9 +204,11 @@ def get_dashboard_totals(statistics): def calculate_usage(usage): # TODO: Don't hardcode these - get em from the API sms_free_allowance = 250000 - sms_rate = usage[0].get("rate", 0) + + sms_rate = 0 if len(usage) == 0 else usage[0].get("rate", 0) sms_sent = get_sum_billing_units(breakdown for breakdown in usage if breakdown['notification_type'] == 'sms') - emails_sent = [breakdown["billing_units"] for breakdown in usage if breakdown['notification_type'] == 'email'][0] + emails = [breakdown["billing_units"] for breakdown in usage if breakdown['notification_type'] == 'email'] + emails_sent = 0 if len(emails) == 0 else emails[0] return { 'emails_sent': emails_sent, diff --git a/tests/app/main/views/test_dashboard.py b/tests/app/main/views/test_dashboard.py index b9b8605b1..b6c8b9404 100644 --- a/tests/app/main/views/test_dashboard.py +++ b/tests/app/main/views/test_dashboard.py @@ -257,6 +257,18 @@ def test_usage_page_for_invalid_year( assert logged_in_client.get(url_for('main.usage', service_id=SERVICE_ONE_ID, year='abcd')).status_code == 404 +@freeze_time("2012-03-31 12:12:12") +def test_future_usage_page( + logged_in_client, + mock_get_future_usage, + mock_get_future_billable_units, +): + assert logged_in_client.get(url_for('main.usage', service_id=SERVICE_ONE_ID, year=2014)).status_code == 200 + + mock_get_future_billable_units.assert_called_once_with(SERVICE_ONE_ID, 2014) + mock_get_future_usage.assert_called_once_with(SERVICE_ONE_ID, 2014) + + def _test_dashboard_menu(mocker, app_, usr, service, permissions): with app_.test_request_context(): with app_.test_client() as client: diff --git a/tests/conftest.py b/tests/conftest.py index 380f1c733..75b658a3d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1423,6 +1423,33 @@ def mock_get_billable_units(mocker): 'app.service_api_client.get_billable_units', side_effect=_get_usage) +@pytest.fixture(scope='function') +def mock_get_future_usage(mocker, service_one, fake_uuid): + def _get_usage(service_id, year=None): + return [ + { + 'notification_type': 'sms', 'international': False, + 'credits': 0, 'rate_multiplier': 1, 'rate': 1.58, 'billing_units': 0 + }, + { + 'notification_type': 'email', 'international': False, + 'credits': 0, 'rate_multiplier': 1, 'rate': 0, 'billing_units': 0 + } + ] + + return mocker.patch( + 'app.service_api_client.get_service_usage', side_effect=_get_usage) + + +@pytest.fixture(scope='function') +def mock_get_future_billable_units(mocker): + def _get_usage(service_id, year): + return [] + + return mocker.patch( + 'app.service_api_client.get_billable_units', side_effect=_get_usage) + + @pytest.fixture(scope='function') def mock_events(mocker): def _create_event(event_type, event_data):