add tests for notification status page

This commit is contained in:
Leo Hemsted
2017-06-16 14:57:48 +01:00
parent 09dc85e5bc
commit 20bb34849d
4 changed files with 119 additions and 5 deletions

View File

@@ -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,

View File

@@ -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]

View File

@@ -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'
)

View File

@@ -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