mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-06-04 21:40:23 -04:00
Right now we have separate pages for email and text message templates. In the future we will also have a separate page for letter templates. This commit changes Notify to only have one page for all templates. What is the problem? --- The left-hand navigation is getting quite crowded, at 8 items for a service that can send letters. Research suggests that the number of objects an average human can hold in working memory is 7 ± 2 [1]. So we’re at the limit of how many items the navigation should have. In the future we will need to search/sort/filter templates by attributes other than type, for example: - show me the ‘confirmation’ templates - show me the most recently used templates - show me all templates containing the placeholder `((ref_no))` These are hypothetical for now, but these needs (or others) may become real in the future. At this point pre-filtering the list of templates by type would restrict what searches a user could do. So by making this change now we’re in a better position to iterate the design in the future. What’s the change? --- This commit replaces the ‘Email templates’, ‘Text message templates’ and ‘Letter templates’ pages with one page called ‘Templates’. This new templates page shows all the templates for the service, sorted by most recently created first (as before). To add a new template there is a new page with a form asking you what kind of template you want to create. This is necessary because in the past we knew what kind of template you wanted to create based on the kind you were looking at. What’s the impact of this change on new users? --- This change alters the onboarding process slightly. We still want to take people through the empty templates page from the call-to-action on the dashboard because it helps them understand that to send a message using Notify you need a template. But because we don’t have separate pages for emails/text messages we will have to send users through the extra step of choosing what kind of template to create. This is a bit clunkier on first use but: - it still gets the point across - it takes them through the actual flow they will be using to create new templates in the future (ie they’re learning how to use Notify, not just being taken through a special onboarding route) I’m not too worried about this change in terms of the experience for new users. Furthermore, by making it now we get to validate whether it’s causing any problems in the lab research booked for next week. What’s the impact of this change on current services? --- Looking at the top 15 services by number of templates[2], most are using either text messages or emails. So this change would not have a significant impact on these services because the page will not get any longer. In other words we wouldn’t be making it worse for them. Those services who do use both are not using as many templates. The worst-case scenario is SSCS, who have 16 templates, evenly split between email and text messages. So they would go from having 8 templates per page to 16, which is still less than half the number that HMPO or Digital Marketplace are managing. References --- 1. https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two 2. Template usage by service Service name | Template count | Template types ---------------------------------------|----------------|--------------- Her Majesty's Passport Office | 40 | sms Digital Marketplace | 40 | email GovWifi-Staging | 19 | sms GovWifi | 18 | sms Digital Apprenticeship Service | 16 | email SSCS | 16 | both Crown Commercial Service MI Collection | 15 | email Help with Prison Visits | 12 | both Digital Future | 12 | email Export Licensing Service | 11 | email Civil Money Claims | 9 | both DVLA Drivers Medical Service | 9 | sms GOV.UK Notify | 8 | both Manage your benefit overpayments | 8 | both Tax Renewals | 8 | both
614 lines
20 KiB
Python
614 lines
20 KiB
Python
from datetime import datetime
|
|
import copy
|
|
from flask import url_for
|
|
|
|
import pytest
|
|
from bs4 import BeautifulSoup
|
|
from freezegun import freeze_time
|
|
|
|
from app.main.views.dashboard import (
|
|
get_dashboard_totals,
|
|
format_monthly_stats_to_list,
|
|
get_free_paid_breakdown_for_billable_units,
|
|
aggregate_status_types,
|
|
)
|
|
|
|
from tests import validate_route_permission
|
|
from tests.conftest import SERVICE_ONE_ID
|
|
from tests.app.test_utils import normalize_spaces
|
|
|
|
stub_template_stats = [
|
|
{
|
|
'template_type': 'sms',
|
|
'template_name': 'one',
|
|
'template_id': 'id-1',
|
|
'count': 100
|
|
},
|
|
{
|
|
'template_type': 'email',
|
|
'template_name': 'two',
|
|
'template_id': 'id-2',
|
|
'count': 200
|
|
}
|
|
]
|
|
|
|
|
|
def test_get_started(
|
|
logged_in_client,
|
|
mocker,
|
|
api_user_active,
|
|
mock_get_service,
|
|
mock_get_service_templates_when_no_templates_exist,
|
|
mock_get_user,
|
|
mock_get_user_by_email,
|
|
mock_login,
|
|
mock_get_jobs,
|
|
mock_has_permissions,
|
|
mock_get_detailed_service,
|
|
mock_get_usage,
|
|
):
|
|
mock_template_stats = mocker.patch('app.template_statistics_client.get_template_statistics_for_service',
|
|
return_value=copy.deepcopy(stub_template_stats))
|
|
|
|
response = logged_in_client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
|
|
|
|
# mock_get_service_templates_when_no_templates_exist.assert_called_once_with(SERVICE_ONE_ID)
|
|
assert response.status_code == 200
|
|
assert 'Get started' in response.get_data(as_text=True)
|
|
|
|
|
|
def test_get_started_is_hidden_once_templates_exist(
|
|
logged_in_client,
|
|
mocker,
|
|
api_user_active,
|
|
mock_get_service,
|
|
mock_get_service_templates,
|
|
mock_get_user,
|
|
mock_get_user_by_email,
|
|
mock_login,
|
|
mock_get_jobs,
|
|
mock_has_permissions,
|
|
mock_get_detailed_service,
|
|
mock_get_usage,
|
|
):
|
|
mock_template_stats = mocker.patch('app.template_statistics_client.get_template_statistics_for_service',
|
|
return_value=copy.deepcopy(stub_template_stats))
|
|
response = logged_in_client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
|
|
|
|
# mock_get_service_templates.assert_called_once_with(SERVICE_ONE_ID)
|
|
assert response.status_code == 200
|
|
assert 'Get started' not in response.get_data(as_text=True)
|
|
|
|
|
|
def test_should_show_recent_templates_on_dashboard(
|
|
logged_in_client,
|
|
mocker,
|
|
api_user_active,
|
|
mock_get_service,
|
|
mock_get_service_templates,
|
|
mock_get_user,
|
|
mock_get_user_by_email,
|
|
mock_login,
|
|
mock_get_jobs,
|
|
mock_has_permissions,
|
|
mock_get_detailed_service,
|
|
mock_get_usage,
|
|
):
|
|
mock_template_stats = mocker.patch('app.template_statistics_client.get_template_statistics_for_service',
|
|
return_value=copy.deepcopy(stub_template_stats))
|
|
|
|
response = logged_in_client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
|
|
|
|
assert response.status_code == 200
|
|
response.get_data(as_text=True)
|
|
mock_template_stats.assert_called_once_with(SERVICE_ONE_ID, limit_days=7)
|
|
|
|
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
|
headers = [header.text.strip() for header in page.find_all('h2') + page.find_all('h1')]
|
|
assert 'In the last 7 days' in headers
|
|
|
|
table_rows = page.find_all('tbody')[1].find_all('tr')
|
|
|
|
assert len(table_rows) == 2
|
|
|
|
assert 'two' in table_rows[0].find_all('th')[0].text
|
|
assert 'Email template' in table_rows[0].find_all('th')[0].text
|
|
assert '200' in table_rows[0].find_all('td')[0].text
|
|
|
|
assert 'one' in table_rows[1].find_all('th')[0].text
|
|
assert 'Text message template' in table_rows[1].find_all('th')[0].text
|
|
assert '100' in table_rows[1].find_all('td')[0].text
|
|
|
|
|
|
def test_should_show_all_templates_on_template_statistics_page(
|
|
logged_in_client,
|
|
mocker,
|
|
api_user_active,
|
|
mock_get_service,
|
|
mock_get_service_templates,
|
|
mock_get_user,
|
|
mock_get_user_by_email,
|
|
mock_login,
|
|
mock_get_jobs,
|
|
mock_has_permissions,
|
|
):
|
|
mock_template_stats = mocker.patch('app.template_statistics_client.get_template_statistics_for_service',
|
|
return_value=copy.deepcopy(stub_template_stats))
|
|
|
|
response = logged_in_client.get(url_for('main.template_history', service_id=SERVICE_ONE_ID))
|
|
|
|
assert response.status_code == 200
|
|
response.get_data(as_text=True)
|
|
mock_template_stats.assert_called_once_with(SERVICE_ONE_ID)
|
|
|
|
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
|
table_rows = page.find_all('tbody')[0].find_all('tr')
|
|
|
|
assert len(table_rows) == 2
|
|
|
|
assert 'two' in table_rows[0].find_all('th')[0].text
|
|
assert 'Email template' in table_rows[0].find_all('th')[0].text
|
|
assert '200' in table_rows[0].find_all('td')[0].text
|
|
|
|
assert 'one' in table_rows[1].find_all('th')[0].text
|
|
assert 'Text message template' in table_rows[1].find_all('th')[0].text
|
|
assert '100' in table_rows[1].find_all('td')[0].text
|
|
|
|
|
|
@freeze_time("2016-01-01 11:09:00.061258")
|
|
def test_should_show_upcoming_jobs_on_dashboard(
|
|
logged_in_client,
|
|
mocker,
|
|
api_user_active,
|
|
mock_get_service,
|
|
mock_get_service_templates,
|
|
mock_get_user,
|
|
mock_get_user_by_email,
|
|
mock_login,
|
|
mock_get_template_statistics,
|
|
mock_get_detailed_service,
|
|
mock_get_jobs,
|
|
mock_has_permissions,
|
|
mock_get_usage,
|
|
):
|
|
response = logged_in_client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
|
|
|
|
first_call = mock_get_jobs.call_args_list[0]
|
|
assert first_call[0] == (SERVICE_ONE_ID,)
|
|
assert first_call[1]['statuses'] == ['scheduled']
|
|
|
|
assert response.status_code == 200
|
|
|
|
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
|
|
|
table_rows = page.find_all('tbody')[0].find_all('tr')
|
|
assert len(table_rows) == 2
|
|
|
|
assert 'send_me_later.csv' in table_rows[0].find_all('th')[0].text
|
|
assert 'Sending today at 11:09am' in table_rows[0].find_all('th')[0].text
|
|
assert table_rows[0].find_all('td')[0].text.strip() == '1'
|
|
assert 'even_later.csv' in table_rows[1].find_all('th')[0].text
|
|
assert 'Sending today at 11:09pm' in table_rows[1].find_all('th')[0].text
|
|
assert table_rows[1].find_all('td')[0].text.strip() == '1'
|
|
|
|
|
|
@freeze_time("2016-01-01 11:09:00.061258")
|
|
def test_should_show_recent_jobs_on_dashboard(
|
|
logged_in_client,
|
|
mocker,
|
|
api_user_active,
|
|
mock_get_service,
|
|
mock_get_service_templates,
|
|
mock_get_user,
|
|
mock_get_user_by_email,
|
|
mock_login,
|
|
mock_get_template_statistics,
|
|
mock_get_detailed_service,
|
|
mock_get_jobs,
|
|
mock_has_permissions,
|
|
mock_get_usage,
|
|
):
|
|
response = logged_in_client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
|
|
|
|
second_call = mock_get_jobs.call_args_list[1]
|
|
assert second_call[0] == (SERVICE_ONE_ID,)
|
|
assert second_call[1]['limit_days'] == 7
|
|
assert 'scheduled' not in second_call[1]['statuses']
|
|
|
|
assert response.status_code == 200
|
|
|
|
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
|
table_rows = page.find_all('tbody')[2].find_all('tr')
|
|
|
|
assert len(table_rows) == 4
|
|
|
|
for index, filename in enumerate((
|
|
"export 1/1/2016.xls",
|
|
"all email addresses.xlsx",
|
|
"applicants.ods",
|
|
"thisisatest.csv",
|
|
)):
|
|
assert filename in table_rows[index].find_all('th')[0].text
|
|
assert 'Sent 1 January at 11:09' in table_rows[index].find_all('th')[0].text
|
|
for column_index, count in enumerate((1, 0, 0)):
|
|
assert table_rows[index].find_all('td')[column_index].text.strip() == str(count)
|
|
|
|
|
|
@freeze_time("2012-03-31 12:12:12")
|
|
def test_usage_page(
|
|
logged_in_client,
|
|
mock_get_usage,
|
|
mock_get_billable_units,
|
|
):
|
|
response = logged_in_client.get(url_for('main.usage', service_id=SERVICE_ONE_ID))
|
|
|
|
assert response.status_code == 200
|
|
|
|
mock_get_billable_units.assert_called_once_with(SERVICE_ONE_ID, 2011)
|
|
mock_get_usage.assert_called_once_with(SERVICE_ONE_ID, 2011)
|
|
|
|
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
|
|
|
cols = page.find_all('div', {'class': 'column-half'})
|
|
nav = page.find('ul', {'class': 'pill', 'role': 'tablist'})
|
|
nav_links = nav.find_all('a')
|
|
|
|
assert normalize_spaces(nav_links[0].text) == '2010 to 2011 financial year'
|
|
assert normalize_spaces(nav.find('li', {'aria-selected': 'true'}).text) == '2011 to 2012 financial year'
|
|
assert normalize_spaces(nav_links[1].text) == '2012 to 2013 financial year'
|
|
|
|
assert '123' in cols[0].text
|
|
assert 'Emails' in cols[0].text
|
|
|
|
assert '456,123' in cols[1].text
|
|
assert 'Text messages' in cols[1].text
|
|
|
|
table = page.find('table').text.strip()
|
|
|
|
assert 'April' in table
|
|
assert 'March' in table
|
|
assert '123 free text messages' in table
|
|
assert '£3,403.06' in table
|
|
assert '249,877 free text messages' in table
|
|
assert '206,246 text messages at 1.65p' in table
|
|
|
|
|
|
def test_usage_page_with_year_argument(
|
|
logged_in_client,
|
|
mock_get_usage,
|
|
mock_get_billable_units
|
|
):
|
|
assert logged_in_client.get(url_for('main.usage', service_id=SERVICE_ONE_ID, year=2000)).status_code == 200
|
|
mock_get_billable_units.assert_called_once_with(SERVICE_ONE_ID, 2000)
|
|
mock_get_usage.assert_called_once_with(SERVICE_ONE_ID, 2000)
|
|
|
|
|
|
def test_usage_page_for_invalid_year(
|
|
logged_in_client,
|
|
):
|
|
assert logged_in_client.get(url_for('main.usage', service_id=SERVICE_ONE_ID, year='abcd')).status_code == 404
|
|
|
|
|
|
def _test_dashboard_menu(mocker, app_, usr, service, permissions):
|
|
with app_.test_request_context():
|
|
with app_.test_client() as client:
|
|
usr._permissions[str(service['id'])] = permissions
|
|
mocker.patch('app.user_api_client.check_verify_code', return_value=(True, ''))
|
|
mocker.patch('app.service_api_client.get_services', return_value={'data': [service]})
|
|
mocker.patch('app.user_api_client.get_user', return_value=usr)
|
|
mocker.patch('app.user_api_client.get_user_by_email', return_value=usr)
|
|
mocker.patch('app.service_api_client.get_service', return_value={'data': service})
|
|
client.login(usr)
|
|
return client.get(url_for('main.service_dashboard', service_id=service['id']))
|
|
|
|
|
|
def test_menu_send_messages(
|
|
mocker,
|
|
app_,
|
|
api_user_active,
|
|
service_one,
|
|
mock_get_service_templates,
|
|
mock_get_jobs,
|
|
mock_get_template_statistics,
|
|
mock_get_detailed_service,
|
|
mock_get_usage,
|
|
):
|
|
with app_.test_request_context():
|
|
resp = _test_dashboard_menu(
|
|
mocker,
|
|
app_,
|
|
api_user_active,
|
|
service_one,
|
|
['view_activity', 'send_texts', 'send_emails', 'send_letters'])
|
|
page = resp.get_data(as_text=True)
|
|
assert url_for(
|
|
'main.choose_template',
|
|
service_id=service_one['id'],
|
|
) in page
|
|
assert url_for('main.manage_users', service_id=service_one['id']) in page
|
|
|
|
assert url_for('main.service_settings', service_id=service_one['id']) not in page
|
|
assert url_for('main.api_keys', service_id=service_one['id']) not in page
|
|
assert url_for('main.view_providers') not in page
|
|
|
|
|
|
def test_menu_manage_service(
|
|
mocker,
|
|
app_,
|
|
api_user_active,
|
|
service_one,
|
|
mock_get_service_templates,
|
|
mock_get_jobs,
|
|
mock_get_template_statistics,
|
|
mock_get_detailed_service,
|
|
mock_get_usage,
|
|
):
|
|
with app_.test_request_context():
|
|
resp = _test_dashboard_menu(
|
|
mocker,
|
|
app_,
|
|
api_user_active,
|
|
service_one,
|
|
['view_activity', 'manage_users', 'manage_templates', 'manage_settings'])
|
|
page = resp.get_data(as_text=True)
|
|
assert url_for(
|
|
'main.choose_template',
|
|
service_id=service_one['id'],
|
|
) in page
|
|
assert url_for('main.manage_users', service_id=service_one['id']) in page
|
|
assert url_for('main.service_settings', service_id=service_one['id']) in page
|
|
|
|
assert url_for('main.api_keys', service_id=service_one['id']) not in page
|
|
|
|
|
|
def test_menu_manage_api_keys(
|
|
mocker,
|
|
app_,
|
|
api_user_active,
|
|
service_one,
|
|
mock_get_service_templates,
|
|
mock_get_jobs,
|
|
mock_get_template_statistics,
|
|
mock_get_detailed_service,
|
|
mock_get_usage,
|
|
):
|
|
with app_.test_request_context():
|
|
resp = _test_dashboard_menu(
|
|
mocker,
|
|
app_,
|
|
api_user_active,
|
|
service_one,
|
|
['view_activity', 'manage_api_keys'])
|
|
page = resp.get_data(as_text=True)
|
|
assert url_for(
|
|
'main.choose_template',
|
|
service_id=service_one['id'],
|
|
) in page
|
|
assert url_for('main.manage_users', service_id=service_one['id']) in page
|
|
assert url_for('main.service_settings', service_id=service_one['id']) not in page
|
|
|
|
assert url_for('main.api_integration', service_id=service_one['id']) in page
|
|
|
|
|
|
def test_menu_all_services_for_platform_admin_user(
|
|
mocker,
|
|
app_,
|
|
platform_admin_user,
|
|
service_one,
|
|
mock_get_service_templates,
|
|
mock_get_jobs,
|
|
mock_get_template_statistics,
|
|
mock_get_detailed_service,
|
|
mock_get_usage,
|
|
):
|
|
with app_.test_request_context():
|
|
resp = _test_dashboard_menu(
|
|
mocker,
|
|
app_,
|
|
platform_admin_user,
|
|
service_one,
|
|
[])
|
|
page = resp.get_data(as_text=True)
|
|
assert url_for('main.choose_template', service_id=service_one['id']) in page
|
|
assert url_for('main.manage_users', service_id=service_one['id']) in page
|
|
assert url_for('main.service_settings', service_id=service_one['id']) in page
|
|
assert url_for('main.view_notifications', service_id=service_one['id'], message_type='email') in page
|
|
assert url_for('main.view_notifications', service_id=service_one['id'], message_type='sms') in page
|
|
assert url_for('main.api_keys', service_id=service_one['id']) not in page
|
|
|
|
|
|
def test_route_for_service_permissions(
|
|
mocker,
|
|
app_,
|
|
api_user_active,
|
|
service_one,
|
|
mock_get_service,
|
|
mock_get_user,
|
|
mock_get_service_templates,
|
|
mock_get_jobs,
|
|
mock_get_template_statistics,
|
|
mock_get_detailed_service,
|
|
mock_get_usage,
|
|
):
|
|
with app_.test_request_context():
|
|
validate_route_permission(
|
|
mocker,
|
|
app_,
|
|
"GET",
|
|
200,
|
|
url_for('main.service_dashboard', service_id=service_one['id']),
|
|
['view_activity'],
|
|
api_user_active,
|
|
service_one)
|
|
|
|
|
|
def test_aggregate_template_stats():
|
|
from app.main.views.dashboard import aggregate_usage
|
|
expected = aggregate_usage(copy.deepcopy(stub_template_stats))
|
|
|
|
assert len(expected) == 2
|
|
assert expected[0]['template_name'] == 'two'
|
|
assert expected[0]['count'] == 200
|
|
assert expected[0]['template_id'] == 'id-2'
|
|
assert expected[0]['template_type'] == 'email'
|
|
assert expected[1]['template_name'] == 'one'
|
|
assert expected[1]['count'] == 100
|
|
assert expected[1]['template_id'] == 'id-1'
|
|
assert expected[1]['template_type'] == 'sms'
|
|
|
|
|
|
def test_service_dashboard_updates_gets_dashboard_totals(
|
|
mocker,
|
|
logged_in_client,
|
|
active_user_with_permissions,
|
|
service_one,
|
|
mock_get_user,
|
|
mock_get_service_templates,
|
|
mock_get_template_statistics,
|
|
mock_get_detailed_service,
|
|
mock_get_jobs,
|
|
mock_get_usage,
|
|
):
|
|
dashboard_totals = mocker.patch('app.main.views.dashboard.get_dashboard_totals', return_value={
|
|
'email': {'requested': 123, 'delivered': 0, 'failed': 0},
|
|
'sms': {'requested': 456, 'delivered': 0, 'failed': 0}
|
|
})
|
|
|
|
response = logged_in_client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
|
|
|
|
assert response.status_code == 200
|
|
|
|
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
|
numbers = [number.text.strip() for number in page.find_all('div', class_='big-number-number')]
|
|
assert '123' in numbers
|
|
assert '456' in numbers
|
|
|
|
table_rows = page.find_all('tbody')[0].find_all('tr')
|
|
|
|
|
|
def test_get_dashboard_totals_adds_percentages():
|
|
stats = {
|
|
'sms': {
|
|
'requested': 3,
|
|
'delivered': 0,
|
|
'failed': 2
|
|
},
|
|
'email': {
|
|
'requested': 0,
|
|
'delivered': 0,
|
|
'failed': 0
|
|
}
|
|
}
|
|
assert get_dashboard_totals(stats)['sms']['failed_percentage'] == '66.7'
|
|
assert get_dashboard_totals(stats)['email']['failed_percentage'] == '0'
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
'failures,expected', [
|
|
(2, False),
|
|
(3, False),
|
|
(4, True)
|
|
]
|
|
)
|
|
def test_get_dashboard_totals_adds_warning(failures, expected):
|
|
stats = {
|
|
'sms': {
|
|
'requested': 100,
|
|
'delivered': 0,
|
|
'failed': failures
|
|
}
|
|
}
|
|
assert get_dashboard_totals(stats)['sms']['show_warning'] == expected
|
|
|
|
|
|
def test_format_monthly_stats_empty_case():
|
|
assert format_monthly_stats_to_list({}) == []
|
|
|
|
|
|
def test_format_monthly_stats_labels_month():
|
|
resp = format_monthly_stats_to_list({'2016-07': {}})
|
|
assert resp[0]['name'] == 'July'
|
|
|
|
|
|
def test_format_monthly_stats_has_stats_with_failure_rate():
|
|
resp = format_monthly_stats_to_list({
|
|
'2016-07': {'sms': _stats(3, 1, 2)}
|
|
})
|
|
assert resp[0]['sms_counts'] == {
|
|
'failed': 2,
|
|
'failed_percentage': '66.7',
|
|
'requested': 3,
|
|
'show_warning': True,
|
|
}
|
|
|
|
|
|
def test_format_monthly_stats_works_for_email_letter():
|
|
resp = format_monthly_stats_to_list({
|
|
'2016-07': {
|
|
'sms': {},
|
|
'email': {},
|
|
'letter': {},
|
|
}
|
|
})
|
|
assert isinstance(resp[0]['sms_counts'], dict)
|
|
assert isinstance(resp[0]['email_counts'], dict)
|
|
assert isinstance(resp[0]['letter_counts'], dict)
|
|
|
|
|
|
def _stats(requested, delivered, failed):
|
|
return {'requested': requested, 'delivered': delivered, 'failed': failed}
|
|
|
|
|
|
@pytest.mark.parametrize('dict_in, expected_failed, expected_requested', [
|
|
(
|
|
{},
|
|
0,
|
|
0
|
|
),
|
|
(
|
|
{'temporary-failure': 1, 'permanent-failure': 1, 'technical-failure': 1},
|
|
3,
|
|
3,
|
|
),
|
|
(
|
|
{'created': 1, 'pending': 1, 'sending': 1, 'delivered': 1},
|
|
0,
|
|
4,
|
|
),
|
|
])
|
|
def test_aggregate_status_types(dict_in, expected_failed, expected_requested):
|
|
sms_counts = aggregate_status_types({'sms': dict_in})['sms_counts']
|
|
assert sms_counts['failed'] == expected_failed
|
|
assert sms_counts['requested'] == expected_requested
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
'now, expected_number_of_months', [
|
|
(freeze_time("2017-12-31 11:09:00.061258"), 12),
|
|
(freeze_time("2017-01-01 11:09:00.061258"), 10)
|
|
]
|
|
)
|
|
def test_get_free_paid_breakdown_for_billable_units(now, expected_number_of_months):
|
|
with now:
|
|
assert list(get_free_paid_breakdown_for_billable_units(
|
|
2016, {
|
|
'April': 100000,
|
|
'May': 100000,
|
|
'June': 100000,
|
|
'February': 1234
|
|
}
|
|
)) == [
|
|
{'name': 'April', 'free': 100000, 'paid': 0},
|
|
{'name': 'May', 'free': 100000, 'paid': 0},
|
|
{'name': 'June', 'free': 50000, 'paid': 50000},
|
|
{'name': 'July', 'free': 0, 'paid': 0},
|
|
{'name': 'August', 'free': 0, 'paid': 0},
|
|
{'name': 'September', 'free': 0, 'paid': 0},
|
|
{'name': 'October', 'free': 0, 'paid': 0},
|
|
{'name': 'November', 'free': 0, 'paid': 0},
|
|
{'name': 'December', 'free': 0, 'paid': 0},
|
|
{'name': 'January', 'free': 0, 'paid': 0},
|
|
{'name': 'February', 'free': 0, 'paid': 1234},
|
|
{'name': 'March', 'free': 0, 'paid': 0}
|
|
][:expected_number_of_months]
|