From 20bb34849d8599c0d8eb95f1b9b006909d007d3c Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Fri, 16 Jun 2017 14:57:48 +0100 Subject: [PATCH] add tests for notification status page --- tests/__init__.py | 8 ++- tests/app/main/views/test_notifications.py | 54 +++++++++++++++++++ .../notify_client/test_notification_client.py | 8 +++ tests/conftest.py | 54 +++++++++++++++++++ 4 files changed, 119 insertions(+), 5 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index 8560ead2d..70fbaf095 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -245,18 +245,16 @@ def notification_json( 'notifications': [{ 'id': uuid.uuid4(), 'to': to, - 'template': { - 'id': template['id'], - 'name': template['name'], - 'template_type': template['template_type'], - }, 'body': template['content'], + 'template': template, 'job': job_payload, 'sent_at': sent_at, 'status': status, 'created_at': created_at, + 'created_by': None, 'updated_at': updated_at, 'job_row_number': job_row_number, + 'service': service_id, 'template_version': template['version'] } for i in range(rows)], 'total': rows, diff --git a/tests/app/main/views/test_notifications.py b/tests/app/main/views/test_notifications.py index b4b70b776..07a2d867c 100644 --- a/tests/app/main/views/test_notifications.py +++ b/tests/app/main/views/test_notifications.py @@ -10,6 +10,8 @@ from app.utils import ( DELIVERED_STATUSES, ) +from tests.conftest import mock_get_notification + @pytest.mark.parametrize('multidict_args, expected_statuses', [ ([], REQUESTED_STATUSES), @@ -26,3 +28,55 @@ def test_status_filters(mocker, multidict_args, expected_statuses): args['status'] = get_status_arg(args) assert sorted(args['status']) == sorted(expected_statuses) + + +@freeze_time("2016-01-01 11:09:00.061258") +def test_notification_status_page_shows_details( + client_request, + mock_get_notification, + service_one, + fake_uuid, +): + page = client_request.get( + 'main.view_notification', + endpoint_kwargs={ + 'service_id': service_one['id'], + 'notification_id': fake_uuid + } + ) + + assert page.find('div', {'class': 'sms-message-wrapper'}).text.strip() == 'service one: template content' + assert ' '.join(page.find('tbody').find('tr').text.split()) == '07123456789 Delivered 1 January at 11:10am' + + mock_get_notification.assert_called_with( + service_one['id'], + fake_uuid + ) + + +@pytest.mark.parametrize('notification_status, expected_big_number_vals', [ + ('created', [1, 1, 0, 0]), + ('sending', [1, 1, 0, 0]), + ('delivered', [1, 0, 1, 0]), + ('temporary-failure', [1, 0, 0, 1]), +]) +def test_notification_status_page_shows_correct_numbers( + client_request, + mocker, + service_one, + fake_uuid, + notification_status, + expected_big_number_vals +): + mock_get_notification(mocker, fake_uuid, notification_status=notification_status) + + page = client_request.get( + 'main.view_notification', + endpoint_kwargs={ + 'service_id': service_one['id'], + 'notification_id': fake_uuid + } + ) + + big_numbers = page.find_all('div', {'class': 'big-number-number'}) + assert expected_big_number_vals == [int(num.text.strip()) for num in big_numbers] diff --git a/tests/app/notify_client/test_notification_client.py b/tests/app/notify_client/test_notification_client.py index d0e2168dc..a794c4241 100644 --- a/tests/app/notify_client/test_notification_client.py +++ b/tests/app/notify_client/test_notification_client.py @@ -33,3 +33,11 @@ def test_client_gets_notifications_for_service_and_job_by_page(mocker, arguments mock_get = mocker.patch('app.notify_client.notification_api_client.NotificationApiClient.get') NotificationApiClient().get_notifications_for_service('abcd1234', **arguments) mock_get.assert_called_once_with(**expected_call) + + +def test_get_notification(mocker): + mock_get = mocker.patch('app.notify_client.notification_api_client.NotificationApiClient.get') + NotificationApiClient().get_notification('foo', 'bar') + mock_get.assert_called_once_with( + url='/service/foo/notifications/bar' + ) diff --git a/tests/conftest.py b/tests/conftest.py index c763008e5..2b90a9e6e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,6 +4,8 @@ from unittest.mock import Mock import pytest from notifications_python_client.errors import HTTPError +from flask import url_for +from bs4 import BeautifulSoup from app import create_app from app.notify_client.models import ( @@ -1630,6 +1632,33 @@ def mock_reset_failed_login_count(mocker): return mocker.patch('app.user_api_client.reset_failed_login_count') +@pytest.fixture +def mock_get_notification(mocker, fake_uuid, notification_status='delivered'): + def _get_notification( + service_id, + notification_id, + ): + noti = notification_json( + service_id, + rows=1, + status=notification_status + )['notifications'][0] + + noti['id'] = notification_id + noti['created_by'] = { + 'id': fake_uuid, + 'name': 'Test User', + 'email_address': 'test@user.gov.uk' + } + noti['template'] = template_json(service_id, str(generate_uuid())) + return noti + + return mocker.patch( + 'app.notification_api_client.get_notification', + side_effect=_get_notification + ) + + @pytest.fixture(scope='function') def client(app_): with app_.test_request_context(), app_.test_client() as client: @@ -1671,3 +1700,28 @@ def os_environ(): os.environ = {} yield os.environ = old_env + + +@pytest.fixture +@pytest.fixture +def client_request(logged_in_client): + class ClientRequest: + + @staticmethod + def get(endpoint, endpoint_kwargs=None, expected_status=200, follow_redirects=False): + resp = logged_in_client.get( + url_for(endpoint, **(endpoint_kwargs or {})) + ) + assert resp.status_code == expected_status + return BeautifulSoup(resp.data.decode('utf-8'), 'html.parser') + + @staticmethod + def post(endpoint, endpoint_kwargs=None, data=None, expected_status=302, follow_redirects=False): + resp = logged_in_client.post( + url_for(endpoint, **(endpoint_kwargs or {})), + data + ) + assert resp.status_code == expected_status + return BeautifulSoup(resp.data.decode('utf-8'), 'html.parser') + + return ClientRequest