diff --git a/app/main/views/templates.py b/app/main/views/templates.py index 6e304ea1a..4ffe6c2b0 100644 --- a/app/main/views/templates.py +++ b/app/main/views/templates.py @@ -186,8 +186,8 @@ def delete_service_template(service_id, template_id): template['template_content'] = template['content'] form = form_objects[template['template_type']](**template) - template_statistics = template_statistics_client.get_template_statistics_for_service(service_id) - last_use_message = get_last_use_message(form.name.data, template['id'], template_statistics) + template_statistics = template_statistics_client.get_template_statistics_for_service(service_id, template['id']) + last_use_message = get_last_use_message(form.name.data, template_statistics) flash('{}. Are you sure you want to delete it?'.format(last_use_message), 'delete') return render_template( 'views/edit-{}-template.html'.format(template['template_type']), @@ -223,12 +223,11 @@ def view_template_versions(service_id, template_id): ) -def get_last_use_message(template_name, template_id, template_statistics): +def get_last_use_message(template_name, template_statistics): try: most_recent_use = max( parse(template_stats['updated_at']).replace(tzinfo=None) for template_stats in template_statistics - if template_stats['template']['id'] == template_id ) except ValueError: return '{} has never been used'.format(template_name) diff --git a/app/notify_client/template_statistics_api_client.py b/app/notify_client/template_statistics_api_client.py index 2ac088f76..b0d54b22c 100644 --- a/app/notify_client/template_statistics_api_client.py +++ b/app/notify_client/template_statistics_api_client.py @@ -20,3 +20,8 @@ class TemplateStatisticsApiClient(BaseAPIClient): url='/service/{}/template-statistics'.format(service_id), params=params )['data'] + + def get_template_statistics_for_template(self, service_id, template_id): + return self.get( + url='/service/{}/template-statistics/{}'.format(service_id, template_id) + )['data'] diff --git a/tests/app/main/notify_client/test_template_statistics_client.py b/tests/app/main/notify_client/test_template_statistics_client.py index 938eb4254..b79d67d29 100644 --- a/tests/app/main/notify_client/test_template_statistics_client.py +++ b/tests/app/main/notify_client/test_template_statistics_client.py @@ -3,7 +3,7 @@ import uuid from app.notify_client.template_statistics_api_client import TemplateStatisticsApiClient -def test_template_statistics_client_calls_correct_api_endpoint(mocker, api_user_active): +def test_template_statistics_client_calls_correct_api_endpoint_for_service(mocker, api_user_active): some_service_id = uuid.uuid4() expected_url = '/service/{}/template-statistics'.format(some_service_id) @@ -15,3 +15,16 @@ def test_template_statistics_client_calls_correct_api_endpoint(mocker, api_user_ client.get_template_statistics_for_service(some_service_id) mock_get.assert_called_once_with(url=expected_url, params={}) + + +def test_template_statistics_client_calls_correct_api_endpoint_for_template(mocker, api_user_active): + some_service_id = uuid.uuid4() + some_template_id = uuid.uuid4() + expected_url = '/service/{}/template-statistics/{}'.format(some_service_id, some_template_id) + + client = TemplateStatisticsApiClient() + mock_get = mocker.patch('app.notify_client.template_statistics_api_client.TemplateStatisticsApiClient.get') + + client.get_template_statistics_for_template(some_service_id, some_template_id) + + mock_get.assert_called_once_with(url=expected_url) diff --git a/tests/app/main/views/test_templates.py b/tests/app/main/views/test_templates.py index c7b5242dc..9d33c3893 100644 --- a/tests/app/main/views/test_templates.py +++ b/tests/app/main/views/test_templates.py @@ -230,6 +230,7 @@ def test_should_show_delete_template_page(app_, mock_get_user_by_email, mock_has_permissions, mock_get_template_statistics, + mock_get_template_statistics_for_template, fake_uuid): with app_.test_request_context(): with app_.test_client() as client: @@ -297,6 +298,7 @@ def test_route_permissions(mocker, service_one, mock_get_service_template, mock_get_template_statistics, + mock_get_template_statistics_for_template, fake_uuid): routes = [ 'main.add_service_template', @@ -367,31 +369,21 @@ def test_route_invalid_permissions(mocker, service_one) -@pytest.mark.parametrize('template_statistics', [ - [{'template': {'id': 'bar'}, 'updated_at': '2000-01-01T12:00:00.000000+00:00'}], - [] -]) -def test_get_last_use_message_returns_no_template_message(template_statistics): - assert get_last_use_message('My Template', 'foo', template_statistics) == 'My Template has never been used' +def test_get_last_use_message_returns_no_template_message(): + assert get_last_use_message('My Template', []) == 'My Template has never been used' @freeze_time('2000-01-01T15:00') -def test_get_last_use_message_uses_most_recent_statistics_for_template(): +def test_get_last_use_message_uses_most_recent_statistics(): template_statistics = [ { - 'template': {'id': 'foo'}, 'updated_at': '2000-01-01T12:00:00.000000+00:00' }, { - 'template': {'id': 'foo'}, 'updated_at': '2000-01-01T09:00:00.000000+00:00' }, - { - 'template': {'id': 'bar'}, - 'updated_at': '2000-01-01T15:00:00.000000+00:00' - }, ] - assert get_last_use_message('My Template', 'foo', template_statistics) == 'My Template was last used 3 hours ago' + assert get_last_use_message('My Template', template_statistics) == 'My Template was last used 3 hours ago' @pytest.mark.parametrize('from_time, until_time, message', [ diff --git a/tests/conftest.py b/tests/conftest.py index c29354a70..de8dc8ea3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -992,6 +992,29 @@ def mock_get_template_statistics(mocker, service_one, fake_uuid): 'app.template_statistics_client.get_template_statistics_for_service', side_effect=_get_stats) +@pytest.fixture(scope='function') +def mock_get_template_statistics_for_template(mocker, service_one): + def _get_stats(service_id, template_id): + template = template_json(service_id, template_id, "Test template", "sms", "Something very interesting") + return [ + { + "usage_count": 1, + "template": { + "name": template['name'], + "template_type": template['template_type'], + "id": template['id'] + }, + "service": template['service'], + "id": str(generate_uuid()), + "day": "2016-04-04", + "updated_at": "2016-04-04T12:00:00.000000+00:00" + } + ] + + return mocker.patch( + 'app.template_statistics_client.get_template_statistics_for_service', side_effect=_get_stats) + + @pytest.fixture(scope='function') def mock_get_usage(mocker, service_one, fake_uuid): def _get_usage(service_id):