Add formatted notification status to CSV

This commit makes the CSV download use the same language for failure
reasons as the frontend.

It also adds a test around this stuff, which was patchily tested before.
This commit is contained in:
Chris Hill-Scott
2016-07-11 10:49:01 +01:00
parent f9a9048579
commit 50c20ce680
5 changed files with 92 additions and 24 deletions

View File

@@ -89,7 +89,7 @@ def get_errors_for_csv(recipients, template_type):
def generate_notifications_csv(json_list):
from app import format_datetime
from app import format_datetime, format_notification_status
content = StringIO()
retval = None
with content as csvfile:
@@ -102,7 +102,7 @@ def generate_notifications_csv(json_list):
x['template']['name'],
x['template']['template_type'],
x['job']['original_file_name'] if x['job'] else '',
x['status'],
format_notification_status(x['status'], x['template']['template_type']),
format_datetime(x['created_at'])])
retval = content.getvalue()
return retval

View File

@@ -149,13 +149,15 @@ def job_json(service_id,
original_file_name="thisisatest.csv",
notification_count=1,
notifications_sent=1,
status=''):
status=None):
if job_id is None:
job_id = str(generate_uuid())
if template_id is None:
template_id = str(generate_uuid())
if created_at is None:
created_at = str(datetime.utcnow().time())
if status is None:
status = 'Delivered'
data = {
'id': job_id,
'service': service_id,
@@ -174,16 +176,19 @@ def job_json(service_id,
return data
def notification_json(service_id,
job=None,
template=None,
to='07123456789',
status='delivered',
sent_at=None,
job_row_number=None,
created_at=None,
updated_at=None,
with_links=False):
def notification_json(
service_id,
job=None,
template=None,
to='07123456789',
status=None,
sent_at=None,
job_row_number=None,
created_at=None,
updated_at=None,
with_links=False,
rows=5
):
if template is None:
template = template_json(service_id, str(generate_uuid()))
if sent_at is None:
@@ -192,6 +197,8 @@ def notification_json(service_id,
created_at = str(datetime.utcnow().time())
if updated_at is None:
updated_at = str((datetime.utcnow() + timedelta(minutes=1)).time())
if status is None:
status = 'delivered'
links = {}
if with_links:
links = {
@@ -213,7 +220,7 @@ def notification_json(service_id,
'updated_at': updated_at,
'job_row_number': job_row_number,
'template_version': template['version']
} for i in range(5)],
} for i in range(rows)],
'total': 5,
'page_size': 50,
'links': links

View File

@@ -152,7 +152,7 @@ def test_should_show_updates_for_one_job_as_json(
assert 'Recipient' in content['notifications']
assert '07123456789' in content['notifications']
assert 'Status' in content['notifications']
assert job_json['status'] in content['status']
print(content['notifications'])
assert 'Delivered' in content['notifications']
assert '11:10' in content['notifications']
assert 'Uploaded by Test User on 1 January at 11:09' in content['status']

View File

@@ -1,4 +1,8 @@
from app.utils import email_safe
import pytest
from io import StringIO
from app.utils import email_safe, generate_notifications_csv
from csv import DictReader
from freezegun import freeze_time
def test_email_safe_return_dot_separated_email_domain():
@@ -6,3 +10,41 @@ def test_email_safe_return_dot_separated_email_domain():
expected = 'some.service.withstuff.b123'
actual = email_safe(test_name)
assert actual == expected
@pytest.mark.parametrize(
"status, template_type, expected_status",
[
('sending', None, 'Sending'),
('delivered', None, 'Delivered'),
('failed', None, 'Failed'),
('technical-failure', None, 'Technical failure'),
('temporary-failure', 'email', 'Inbox not accepting messages right now'),
('permanent-failure', 'email', 'Email address doesnt exist'),
('temporary-failure', 'sms', 'Phone not accepting messages right now'),
('permanent-failure', 'sms', 'Phone number doesnt exist')
]
)
@freeze_time("2016-01-01 11:09:00.061258")
def test_generate_csv_from_notifications(
app_,
service_one,
active_user_with_permissions,
mock_get_notifications,
status,
template_type,
expected_status
):
with app_.test_request_context():
csv_content = generate_notifications_csv(
mock_get_notifications(
service_one['id'],
rows=1,
set_template_type=template_type,
set_status=status
)['notifications']
)
for row in DictReader(StringIO(csv_content)):
assert row['Time'] == 'Friday 01 January 2016 at 11:09'
assert row['Status'] == expected_status

View File

@@ -870,17 +870,36 @@ def mock_get_jobs(mocker, api_user_active):
@pytest.fixture(scope='function')
def mock_get_notifications(mocker, api_user_active):
def _get_notifications(service_id,
job_id=None,
page=1,
page_size=50,
template_type=None,
status=None,
limit_days=None):
def _get_notifications(
service_id,
job_id=None,
page=1,
page_size=50,
template_type=None,
status=None,
limit_days=None,
rows=5,
set_template_type=None,
set_status=None
):
job = None
if job_id is not None:
job = job_json(service_id, api_user_active, job_id=job_id)
return notification_json(service_id, job=job)
if set_template_type:
return notification_json(
service_id,
template={'template_type': set_template_type, 'name': 'name', 'id': 'id', 'version': 1},
rows=rows,
status=set_status,
job=job
)
else:
return notification_json(
service_id,
rows=rows,
status=set_status,
job=job
)
return mocker.patch(
'app.notification_api_client.get_notifications_for_service',