mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-05 19:03:30 -05:00
Refactor existing tests to expect csv stream with helper method
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
import csv
|
||||
import pytest
|
||||
import uuid
|
||||
from datetime import datetime, timedelta, date, timezone
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from flask.testing import FlaskClient
|
||||
from flask import url_for
|
||||
from flask_login import login_user
|
||||
from io import StringIO
|
||||
|
||||
|
||||
class TestClient(FlaskClient):
|
||||
@@ -295,6 +298,24 @@ def single_notification_json(
|
||||
return data
|
||||
|
||||
|
||||
def csv_notifications(notifications_json):
|
||||
csvfile = StringIO()
|
||||
csvwriter = csv.writer(csvfile)
|
||||
from app import format_datetime_24h, format_notification_status
|
||||
csvwriter.writerow(['Row number', 'Recipient', 'Template', 'Type', 'Job', 'Status', 'Time'])
|
||||
for x in notifications_json:
|
||||
csvwriter.writerow([
|
||||
int(x['job_row_number']) + 2 if 'job_row_number' in x and x['job_row_number'] else '',
|
||||
x['to'],
|
||||
x['template']['name'],
|
||||
x['template']['template_type'],
|
||||
x['job']['original_file_name'] if x['job'] else '',
|
||||
format_notification_status(x['status'], x['template']['template_type']),
|
||||
format_datetime_24h(x['created_at'])
|
||||
])
|
||||
return csvfile.getvalue()
|
||||
|
||||
|
||||
def validate_route_permission(mocker,
|
||||
app_,
|
||||
method,
|
||||
|
||||
@@ -6,7 +6,8 @@ import pytest
|
||||
from flask import url_for
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
from app.utils import generate_notifications_csv
|
||||
from app import utils
|
||||
from tests import csv_notifications
|
||||
from app.main.views.jobs import get_time_left, get_status_filters
|
||||
from tests import notification_json
|
||||
from freezegun import freeze_time
|
||||
@@ -368,9 +369,29 @@ def test_can_show_notifications(
|
||||
message_type='email',
|
||||
download='csv'
|
||||
))
|
||||
csv_content = generate_notifications_csv(
|
||||
mock_get_notifications(service_one['id'])['notifications']
|
||||
|
||||
notifications_json = mock_get_notifications(service_one['id'])['notifications']
|
||||
notifications_as_csv = csv_notifications(notifications_json)
|
||||
|
||||
mock_notifications_as_csv = mocker.patch('app.utils.generate_notifications_csv',
|
||||
return_value=notifications_as_csv)
|
||||
|
||||
csv_content = utils.generate_notifications_csv(
|
||||
limit_days=7,
|
||||
page=expected_page_argument,
|
||||
service_id=service_one['id'],
|
||||
status=expected_api_call,
|
||||
template_type=[message_type]
|
||||
)
|
||||
|
||||
mock_notifications_as_csv.assert_called_with(
|
||||
limit_days=7,
|
||||
page=expected_page_argument,
|
||||
service_id=service_one['id'],
|
||||
status=expected_api_call,
|
||||
template_type=[message_type]
|
||||
)
|
||||
|
||||
assert csv_response.status_code == 200
|
||||
assert csv_response.get_data(as_text=True) == csv_content
|
||||
assert 'text/csv' in csv_response.headers['Content-Type']
|
||||
@@ -424,6 +445,7 @@ def test_should_show_notifications_for_a_service_with_next_previous(
|
||||
@freeze_time("2016-01-01 11:09:00.061258")
|
||||
def test_should_download_notifications_for_a_job(app_,
|
||||
api_user_active,
|
||||
mocker,
|
||||
mock_login,
|
||||
mock_get_service,
|
||||
mock_get_job,
|
||||
@@ -439,9 +461,22 @@ def test_should_download_notifications_for_a_job(app_,
|
||||
service_id=fake_uuid,
|
||||
job_id=fake_uuid,
|
||||
))
|
||||
csv_content = generate_notifications_csv(
|
||||
mock_get_notifications(fake_uuid, job_id=fake_uuid)['notifications']
|
||||
|
||||
notifications_json = mock_get_notifications(fake_uuid, job_id=fake_uuid)['notifications']
|
||||
notifications_as_csv = csv_notifications(notifications_json)
|
||||
|
||||
mock_notifications_as_csv = mocker.patch('app.utils.generate_notifications_csv',
|
||||
return_value=notifications_as_csv)
|
||||
csv_content = utils.generate_notifications_csv(
|
||||
service_id=fake_uuid,
|
||||
job_id=fake_uuid
|
||||
)
|
||||
|
||||
mock_notifications_as_csv.assert_called_with(
|
||||
service_id=fake_uuid,
|
||||
job_id=fake_uuid
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.get_data(as_text=True) == csv_content
|
||||
assert 'text/csv' in response.headers['Content-Type']
|
||||
|
||||
Reference in New Issue
Block a user