mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-06-26 02:11:49 -04:00
The GDS Way™[1] recommends using Flake8 to lint Python projects.
This commit takes the Flake8 config from Digital Marketplace API[2] and
removes the bits we don’t need.
It changes the `max_complexity` setting to 14, which is the most complex
code we have in this repo currently (we shouldn’t be writing code _more_
complex than what we already have).
This commit also fixes the errors found by Flake8, which includes 6(!)
tests which were never getting run because they had the same names as
existing tests.
Here is a full list of the errors that were found and fixed:
```
./app/__init__.py:2:1: F401 're' imported but unused
./app/__init__.py:4:1: F401 'json' imported but unused
./app/__init__.py:8:1: F401 'dateutil' imported but unused
./app/__init__.py:11:1: F401 'flask.escape' imported but unused
./app/__init__.py:41:1: F401 'app.proxy_fix' imported but unused
./app/__init__.py:129:5: F821 undefined name 'proxy_fix'
./app/__init__.py:221:19: F821 undefined name 'highlight'
./app/__init__.py:221:35: F821 undefined name 'JavascriptLexer'
./app/__init__.py:221:54: F821 undefined name 'HtmlFormatter'
./app/config.py:2:1: F401 'datetime.timedelta' imported but unused
./app/event_handlers.py:2:1: F401 'flask_login.current_user' imported but unused
./app/utils.py:11:1: F401 'dateutil.parser' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.two_factor' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.notifications' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.add_service' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.forgot_password' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.inbound_number' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.styleguide' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.organisations' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.letter_jobs' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.verify' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.conversation' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.api_keys' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.send' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.dashboard' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.jobs' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.manage_users' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.sign_in' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.sign_out' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.code_not_received' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.invites' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.platform_admin' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.providers' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.service_settings' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.index' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.new_password' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.user_profile' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.feedback' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.choose_service' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.templates' imported but unused
./app/main/__init__.py:5:1: F401 'app.main.views.register' imported but unused
./app/main/forms.py:12:1: F401 'wtforms.SelectField' imported but unused
./app/main/views/api_keys.py:37:29: E241 multiple spaces after ':'
./app/main/views/feedback.py:3:1: F401 'flask.flash' imported but unused
./app/main/views/feedback.py:122:17: E123 closing bracket does not match indentation of opening bracket's line
./app/main/views/inbound_number.py:1:1: F401 'flask.url_for' imported but unused
./app/main/views/inbound_number.py:1:1: F401 'flask.session' imported but unused
./app/main/views/inbound_number.py:1:1: F401 'flask.redirect' imported but unused
./app/main/views/inbound_number.py:1:1: F401 'flask.request' imported but unused
./app/main/views/inbound_number.py:13:1: F401 'flask.jsonify' imported but unused
./app/main/views/jobs.py:31:1: F401 'app.utils.get_template' imported but unused
./app/main/views/letter_jobs.py:1:1: F401 'datetime' imported but unused
./app/main/views/letter_jobs.py:6:1: F401 'app.format_datetime_24h' imported but unused
./app/main/views/manage_users.py:111:9: E123 closing bracket does not match indentation of opening bracket's line
./app/main/views/notifications.py:121:5: F841 local variable 'status_args' is assigned to but never used
./app/main/views/organisations.py:1:1: F401 'flask.request' imported but unused
./app/main/views/service_settings.py:77:9: E123 closing bracket does not match indentation of opening bracket's line
./app/main/views/service_settings.py:82:9: E123 closing bracket does not match indentation of opening bracket's line
./app/main/views/service_settings.py:420:13: E123 closing bracket does not match indentation of opening bracket's line
./app/main/views/sign_in.py:12:1: F401 'flask_login.confirm_login' imported but unused
./app/main/views/sign_in.py:17:1: F401 'app.service_api_client' imported but unused
./app/main/views/sign_in.py:62:13: E123 closing bracket does not match indentation of opening bracket's line
./app/main/views/templates.py:4:1: F401 'flask.json' imported but unused
./app/main/views/templates.py:17:1: F401 'notifications_utils.formatters.escape_html' imported but unused
./app/main/views/templates.py:23:1: F401 'app.utils.get_help_argument' imported but unused
./app/main/views/templates.py:64:13: E123 closing bracket does not match indentation of opening bracket's line
./app/notify_client/service_api_client.py:6:1: F401 '.notification_api_client' imported but unused
./app/notify_client/user_api_client.py:1:1: F401 'uuid' imported but unused
./app/notify_client/user_api_client.py:3:1: F401 'flask.session' imported but unused
./tests/__init__.py:1:1: F401 'csv' imported but unused
./tests/app/main/test_asset_fingerprinter.py:2:1: F401 'os' imported but unused
./tests/app/main/test_asset_fingerprinter.py:4:1: F401 'unittest.mock' imported but unused
./tests/app/main/test_asset_fingerprinter.py:98:9: F841 local variable 'string_with_unicode_character' is assigned to but never used
./tests/app/main/test_errorhandlers.py:2:1: F401 'flask.url_for' imported but unused
./tests/app/main/test_permissions.py:26:13: F841 local variable 'response' is assigned to but never used
./tests/app/main/test_placeholder_form.py:3:1: F401 'wtforms.Label' imported but unused
./tests/app/main/test_placeholder_form.py:11:10: F841 local variable 'req' is assigned to but never used
./tests/app/main/test_two_factor_form.py:10:67: F841 local variable 'req' is assigned to but never used
./tests/app/main/test_two_factor_form.py:23:65: F841 local variable 'req' is assigned to but never used
./tests/app/main/test_two_factor_form.py:37:48: F841 local variable 'req' is assigned to but never used
./tests/app/main/test_two_factor_form.py:51:67: F841 local variable 'req' is assigned to but never used
./tests/app/main/test_two_factor_form.py:65:67: F841 local variable 'req' is assigned to but never used
./tests/app/main/views/test_accept_invite.py:356:5: F841 local variable 'element' is assigned to but never used
./tests/app/main/views/test_activity.py:11:1: F811 redefinition of unused 'mock_get_notifications' from line 11
./tests/app/main/views/test_activity.py:18:1: F401 'datetime.datetime' imported but unused
./tests/app/main/views/test_activity.py:102:5: F841 local variable 'content' is assigned to but never used
./tests/app/main/views/test_activity.py:104:5: F841 local variable 'notification' is assigned to but never used
./tests/app/main/views/test_activity.py:337:5: F841 local variable '_notifications_mock' is assigned to but never used
./tests/app/main/views/test_activity.py:373:13: E126 continuation line over-indented for hanging indent
./tests/app/main/views/test_activity.py:378:9: E121 continuation line under-indented for hanging indent
./tests/app/main/views/test_activity.py:404:13: E126 continuation line over-indented for hanging indent
./tests/app/main/views/test_activity.py:407:9: E121 continuation line under-indented for hanging indent
./tests/app/main/views/test_api_keys.py:354:5: F841 local variable 'response' is assigned to but never used
./tests/app/main/views/test_conversation.py:5:1: F401 'bs4.BeautifulSoup' imported but unused
./tests/app/main/views/test_conversation.py:198:5: F841 local variable 'mock_get_inbound_sms' is assigned to but never used
./tests/app/main/views/test_dashboard.py:53:5: F841 local variable 'mock_template_stats' is assigned to but never used
./tests/app/main/views/test_dashboard.py:72:5: F841 local variable 'mock_template_stats' is assigned to but never used
./tests/app/main/views/test_jobs.py:2:1: F401 'uuid' imported but unused
./tests/app/main/views/test_jobs.py:3:1: F401 'urllib.parse.urlparse' imported but unused
./tests/app/main/views/test_jobs.py:3:1: F401 'urllib.parse.quote' imported but unused
./tests/app/main/views/test_jobs.py:3:1: F401 'urllib.parse.parse_qs' imported but unused
./tests/app/main/views/test_jobs.py:9:1: F401 'app.main.views.jobs.get_status_filters' imported but unused
./tests/app/main/views/test_jobs.py:10:1: F401 'tests.notification_json' imported but unused
./tests/app/main/views/test_letters.py:6:1: F401 'tests.service_json' imported but unused
./tests/app/main/views/test_notifications.py:5:1: F401 'app.utils.REQUESTED_STATUSES' imported but unused
./tests/app/main/views/test_notifications.py:5:1: F401 'app.utils.DELIVERED_STATUSES' imported but unused
./tests/app/main/views/test_notifications.py:5:1: F401 'app.utils.SENDING_STATUSES' imported but unused
./tests/app/main/views/test_notifications.py:5:1: F401 'app.utils.FAILURE_STATUSES' imported but unused
./tests/app/main/views/test_platform_admin.py:242:13: E126 continuation line over-indented for hanging indent
./tests/app/main/views/test_platform_admin.py:247:13: E126 continuation line over-indented for hanging indent
./tests/app/main/views/test_send.py:3:1: F401 'unittest.mock.Mock' imported but unused
./tests/app/main/views/test_send.py:18:1: F811 redefinition of unused 'mock_get_service' from line 18
./tests/app/main/views/test_send.py:18:1: F401 'tests.conftest.multiple_letter_contact_blocks' imported but unused
./tests/app/main/views/test_send.py:18:1: F401 'tests.conftest.no_sms_senders' imported but unused
./tests/app/main/views/test_send.py:18:1: F401 'tests.conftest.multiple_sms_senders' imported but unused
./tests/app/main/views/test_send.py:18:1: F401 'tests.conftest.no_letter_contact_blocks' imported but unused
./tests/app/main/views/test_send.py:102:5: F841 local variable 'response' is assigned to but never used
./tests/app/main/views/test_send.py:870:5: F841 local variable 'response' is assigned to but never used
./tests/app/main/views/test_send.py:1367:5: F841 local variable 'service_id' is assigned to but never used
./tests/app/main/views/test_send.py:1451:13: E126 continuation line over-indented for hanging indent
./tests/app/main/views/test_send.py:1620:80: E226 missing whitespace around arithmetic operator
./tests/app/main/views/test_send.py:1909:13: E126 continuation line over-indented for hanging indent
./tests/app/main/views/test_send.py:1912:9: E121 continuation line under-indented for hanging indent
./tests/app/main/views/test_service_settings.py:13:1: F811 redefinition of unused 'no_reply_to_email_addresses' from line 13
./tests/app/main/views/test_service_settings.py:13:1: F401 'tests.conftest.single_reply_to_email_address' imported but unused
./tests/app/main/views/test_service_settings.py:28:5: E123 closing bracket does not match indentation of opening bracket's line
./tests/app/main/views/test_service_settings.py:104:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:166:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:186:5: F841 local variable 'mocked_get_fn' is assigned to but never used
./tests/app/main/views/test_service_settings.py:217:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:237:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:257:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:307:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:340:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:466:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:555:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:615:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:719:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:874:5: F841 local variable 'page' is assigned to but never used
./tests/app/main/views/test_service_settings.py:902:5: F841 local variable 'page' is assigned to but never used
./tests/app/main/views/test_service_settings.py:954:5: F841 local variable 'page' is assigned to but never used
./tests/app/main/views/test_service_settings.py:986:5: F841 local variable 'page' is assigned to but never used
./tests/app/main/views/test_service_settings.py:1101:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:1121:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:1271:1: F811 redefinition of unused 'test_set_letter_contact_block_saves' from line 1189
./tests/app/main/views/test_service_settings.py:1433:5: F841 local variable 'page' is assigned to but never used
./tests/app/main/views/test_service_settings.py:1495:5: F841 local variable 'mocked_get_fn' is assigned to but never used
./tests/app/main/views/test_service_settings.py:1540:5: F841 local variable 'mocked_get_fn' is assigned to but never used
./tests/app/main/views/test_service_settings.py:1570:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:1589:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:1621:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:1641:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:1658:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:1676:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:1697:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:1759:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_service_settings.py:1775:1: F811 redefinition of unused 'single_reply_to_email_address' from line 13
./tests/app/main/views/test_templates.py:3:1: F401 'uuid' imported but unused
./tests/app/main/views/test_templates.py:11:1: F401 'tests.conftest.mock_get_user' imported but unused
./tests/app/main/views/test_templates.py:514:1: F811 redefinition of unused 'mock_get_user' from line 11
./tests/app/main/views/test_templates.py:672:1: F811 redefinition of unused 'mock_get_user' from line 11
./tests/app/main/views/test_templates.py:795:1: F811 redefinition of unused 'mock_get_user' from line 11
./tests/app/main/views/test_templates.py:835:1: F811 redefinition of unused 'mock_get_user' from line 11
./tests/app/main/views/test_two_factor.py:67:13: E126 continuation line over-indented for hanging indent
./tests/app/notify_client/test_notification_client.py:79:5: F841 local variable 'mock_post' is assigned to but never used
```
1. https://gds-way.cloudapps.digital/manuals/programming-languages/python/linting.html#how-to-use-flake8
2. d5ab8afef4/.flake8
991 lines
30 KiB
Python
991 lines
30 KiB
Python
import json
|
||
from functools import partial
|
||
import copy
|
||
from unittest.mock import call, ANY
|
||
|
||
from flask import url_for
|
||
import pytest
|
||
from bs4 import BeautifulSoup
|
||
from freezegun import freeze_time
|
||
from datetime import datetime
|
||
|
||
from app.main.views.dashboard import (
|
||
get_dashboard_totals,
|
||
format_monthly_stats_to_list,
|
||
get_free_paid_breakdown_for_billable_units,
|
||
aggregate_status_types,
|
||
format_template_stats_to_list,
|
||
get_tuples_of_financial_years
|
||
)
|
||
|
||
from tests import validate_route_permission, validate_route_permission_with_client
|
||
from tests.conftest import (
|
||
SERVICE_ONE_ID,
|
||
mock_get_inbound_sms_summary,
|
||
mock_get_inbound_sms_summary_with_no_messages,
|
||
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,
|
||
mock_get_service_templates_when_no_templates_exist,
|
||
mock_get_jobs,
|
||
mock_get_detailed_service,
|
||
mock_get_usage,
|
||
mock_get_inbound_sms_summary
|
||
):
|
||
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,
|
||
mock_get_service_templates,
|
||
mock_get_jobs,
|
||
mock_get_detailed_service,
|
||
mock_get_usage,
|
||
mock_get_inbound_sms_summary
|
||
):
|
||
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_inbound_messages_not_visible_to_service_without_permissions(
|
||
logged_in_client,
|
||
service_one,
|
||
mock_get_service_templates_when_no_templates_exist,
|
||
mock_get_jobs,
|
||
mock_get_detailed_service,
|
||
mock_get_template_statistics,
|
||
mock_get_usage,
|
||
mock_get_inbound_sms_summary
|
||
):
|
||
|
||
service_one['permissions'] = []
|
||
|
||
response = logged_in_client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
|
||
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||
|
||
assert response.status_code == 200
|
||
assert not page.select('.big-number-meta-wrapper')
|
||
assert mock_get_inbound_sms_summary.called is False
|
||
|
||
|
||
@pytest.mark.parametrize('inbound_summary_mock, expected_text', [
|
||
(mock_get_inbound_sms_summary_with_no_messages, '0 text messages received'),
|
||
(mock_get_inbound_sms_summary, '99 text messages received latest message just now'),
|
||
])
|
||
def test_inbound_messages_shows_count_of_messages(
|
||
logged_in_client,
|
||
mocker,
|
||
service_one,
|
||
mock_get_service_templates_when_no_templates_exist,
|
||
mock_get_jobs,
|
||
mock_get_detailed_service,
|
||
mock_get_template_statistics,
|
||
mock_get_usage,
|
||
inbound_summary_mock,
|
||
expected_text
|
||
):
|
||
|
||
service_one['permissions'] = ['inbound_sms']
|
||
inbound_summary_mock(mocker)
|
||
|
||
response = logged_in_client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
|
||
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||
|
||
assert response.status_code == 200
|
||
assert normalize_spaces(page.select('.big-number-meta-wrapper')[0].text) == expected_text
|
||
assert page.select('.big-number-meta-wrapper a')[0]['href'] == url_for(
|
||
'main.inbox', service_id=SERVICE_ONE_ID
|
||
)
|
||
|
||
|
||
@pytest.mark.parametrize('index, expected_row', enumerate([
|
||
'07900 900000 message-1 1 hour ago',
|
||
'07900 900002 message-4 3 hours ago',
|
||
'07900 900004 message-5 5 hours ago',
|
||
'07900 900006 message-6 7 hours ago',
|
||
'07900 900008 message-7 9 hours ago',
|
||
]))
|
||
def test_inbox_showing_inbound_messages(
|
||
logged_in_client,
|
||
service_one,
|
||
mock_get_service_templates_when_no_templates_exist,
|
||
mock_get_jobs,
|
||
mock_get_detailed_service,
|
||
mock_get_template_statistics,
|
||
mock_get_usage,
|
||
mock_get_inbound_sms,
|
||
index,
|
||
expected_row,
|
||
):
|
||
|
||
service_one['permissions'] = ['inbound_sms']
|
||
|
||
response = logged_in_client.get(url_for('main.inbox', service_id=SERVICE_ONE_ID))
|
||
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||
|
||
assert response.status_code == 200
|
||
rows = page.select('tbody tr')
|
||
assert len(rows) == 5
|
||
assert normalize_spaces(rows[index].text) == expected_row
|
||
assert normalize_spaces(page.select('.table-show-more-link')) == (
|
||
'8 messages from 5 users'
|
||
)
|
||
assert page.select_one('a[download]')['href'] == url_for(
|
||
'main.inbox_download',
|
||
service_id=SERVICE_ONE_ID,
|
||
)
|
||
|
||
|
||
def test_empty_inbox(
|
||
logged_in_client,
|
||
service_one,
|
||
mock_get_service_templates_when_no_templates_exist,
|
||
mock_get_jobs,
|
||
mock_get_detailed_service,
|
||
mock_get_template_statistics,
|
||
mock_get_usage,
|
||
mock_get_inbound_sms_with_no_messages,
|
||
mock_get_inbound_number_for_service,
|
||
):
|
||
|
||
service_one['permissions'] = ['inbound_sms']
|
||
|
||
response = logged_in_client.get(url_for('main.inbox', service_id=SERVICE_ONE_ID))
|
||
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||
|
||
assert response.status_code == 200
|
||
assert normalize_spaces(page.select('tbody tr')) == (
|
||
'When users text your service’s phone number (0781239871) you’ll see the messages here'
|
||
)
|
||
assert not page.select('a[download]')
|
||
|
||
|
||
@pytest.mark.parametrize('endpoint', [
|
||
'main.inbox',
|
||
'main.inbox_updates',
|
||
])
|
||
def test_inbox_not_accessible_to_service_without_permissions(
|
||
logged_in_client,
|
||
service_one,
|
||
endpoint,
|
||
):
|
||
service_one['permissions'] = []
|
||
response = logged_in_client.get(url_for(endpoint, service_id=SERVICE_ONE_ID))
|
||
|
||
assert response.status_code == 403
|
||
|
||
|
||
def test_anyone_can_see_inbox(
|
||
client,
|
||
api_user_active,
|
||
service_one,
|
||
mocker,
|
||
mock_get_inbound_sms_with_no_messages,
|
||
mock_get_inbound_number_for_service,
|
||
):
|
||
|
||
service_one['permissions'] = ['inbound_sms']
|
||
|
||
validate_route_permission_with_client(
|
||
mocker,
|
||
client,
|
||
'GET',
|
||
200,
|
||
url_for('main.inbox', service_id=service_one['id']),
|
||
['view_activity'],
|
||
api_user_active,
|
||
service_one,
|
||
)
|
||
|
||
|
||
def test_view_inbox_updates(
|
||
logged_in_client,
|
||
service_one,
|
||
mocker,
|
||
mock_get_inbound_sms_with_no_messages,
|
||
):
|
||
|
||
mock_get_partials = mocker.patch(
|
||
'app.main.views.dashboard.get_inbox_partials',
|
||
return_value={'messages': 'foo'},
|
||
)
|
||
|
||
response = logged_in_client.get(url_for(
|
||
'main.inbox_updates', service_id=SERVICE_ONE_ID,
|
||
))
|
||
|
||
assert response.status_code == 200
|
||
assert json.loads(response.get_data(as_text=True)) == {'messages': 'foo'}
|
||
|
||
mock_get_partials.assert_called_once_with(SERVICE_ONE_ID)
|
||
|
||
|
||
@freeze_time("2016-07-01 13:00")
|
||
def test_download_inbox(
|
||
logged_in_client,
|
||
mock_get_inbound_sms,
|
||
):
|
||
response = logged_in_client.get(
|
||
url_for('main.inbox_download', service_id=SERVICE_ONE_ID)
|
||
)
|
||
assert response.status_code == 200
|
||
assert response.headers['Content-Type'] == (
|
||
'text/csv; '
|
||
'charset=utf-8'
|
||
)
|
||
assert response.headers['Content-Disposition'] == (
|
||
'inline; '
|
||
'filename="Received text messages 2016-07-01.csv"'
|
||
)
|
||
assert response.get_data(as_text=True) == (
|
||
'Phone number,Message,Received\r\n'
|
||
'07900900000,message-1,2016-07-01 13:00\r\n'
|
||
'07900900000,message-2,2016-07-01 12:59\r\n'
|
||
'07900900000,message-3,2016-07-01 12:59\r\n'
|
||
'07900900002,message-4,2016-07-01 10:59\r\n'
|
||
'07900900004,message-5,2016-07-01 08:59\r\n'
|
||
'07900900006,message-6,2016-07-01 06:59\r\n'
|
||
'07900900008,message-7,2016-07-01 04:59\r\n'
|
||
'07900900008,message-8,2016-07-01 04:59\r\n'
|
||
)
|
||
|
||
|
||
@freeze_time("2016-07-01 13:00")
|
||
@pytest.mark.parametrize('message_content, expected_cell', [
|
||
('=2+5', '2+5'),
|
||
('==2+5', '2+5'),
|
||
('-2+5', '2+5'),
|
||
('+2+5', '2+5'),
|
||
('@2+5', '2+5'),
|
||
('looks safe,=2+5', '"looks safe,=2+5"'),
|
||
])
|
||
def test_download_inbox_strips_formulae(
|
||
mocker,
|
||
logged_in_client,
|
||
fake_uuid,
|
||
message_content,
|
||
expected_cell,
|
||
):
|
||
|
||
mocker.patch(
|
||
'app.service_api_client.get_inbound_sms',
|
||
return_value=[{
|
||
'user_number': 'elevenchars',
|
||
'notify_number': 'foo',
|
||
'content': message_content,
|
||
'created_at': datetime.utcnow().isoformat(),
|
||
'id': fake_uuid,
|
||
}],
|
||
)
|
||
response = logged_in_client.get(
|
||
url_for('main.inbox_download', service_id=SERVICE_ONE_ID)
|
||
)
|
||
assert expected_cell in response.get_data(as_text=True).split('\r\n')[1]
|
||
|
||
|
||
def test_should_show_recent_templates_on_dashboard(
|
||
logged_in_client,
|
||
mocker,
|
||
mock_get_service_templates,
|
||
mock_get_jobs,
|
||
mock_get_detailed_service,
|
||
mock_get_usage,
|
||
mock_get_inbound_sms_summary
|
||
):
|
||
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
|
||
|
||
|
||
@freeze_time("2016-07-01 12:00") # 4 months into 2016 financial year
|
||
@pytest.mark.parametrize('partial_url', [
|
||
partial(url_for),
|
||
partial(url_for, year='2016'),
|
||
])
|
||
def test_should_show_monthly_breakdown_of_template_usage(
|
||
logged_in_client,
|
||
mock_get_monthly_template_statistics,
|
||
partial_url,
|
||
):
|
||
response = logged_in_client.get(
|
||
partial_url('main.template_history', service_id=SERVICE_ONE_ID, _external=True)
|
||
)
|
||
|
||
assert response.status_code == 200
|
||
mock_get_monthly_template_statistics.assert_called_once_with(SERVICE_ONE_ID, 2016)
|
||
|
||
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||
table_rows = page.select('tbody tr')
|
||
|
||
assert ' '.join(table_rows[0].text.split()) == (
|
||
'My first template '
|
||
'Text message template '
|
||
'2'
|
||
)
|
||
|
||
assert len(table_rows) == len(['April'])
|
||
assert len(page.select('.table-no-data')) == len(['May', 'June', 'July'])
|
||
|
||
|
||
def test_anyone_can_see_monthly_breakdown(
|
||
client,
|
||
api_user_active,
|
||
service_one,
|
||
mocker,
|
||
mock_get_monthly_notification_stats,
|
||
):
|
||
validate_route_permission_with_client(
|
||
mocker,
|
||
client,
|
||
'GET',
|
||
200,
|
||
url_for('main.monthly', service_id=service_one['id']),
|
||
['view_activity'],
|
||
api_user_active,
|
||
service_one,
|
||
)
|
||
|
||
|
||
@freeze_time("2016-01-01 11:09:00.061258")
|
||
def test_should_show_upcoming_jobs_on_dashboard(
|
||
logged_in_client,
|
||
mock_get_service_templates,
|
||
mock_get_template_statistics,
|
||
mock_get_detailed_service,
|
||
mock_get_jobs,
|
||
mock_get_usage,
|
||
mock_get_inbound_sms_summary
|
||
):
|
||
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'
|
||
|
||
|
||
@pytest.mark.parametrize('permissions, column_name, expected_column_count', [
|
||
(['email', 'sms'], '.column-half', 2),
|
||
(['email', 'letter'], '.column-third', 3),
|
||
(['email', 'sms', 'letter'], '.column-third', 3)
|
||
])
|
||
def test_correct_columns_display_on_dashboard(
|
||
client_request,
|
||
mock_get_service_templates,
|
||
mock_get_template_statistics,
|
||
mock_get_detailed_service,
|
||
mock_get_jobs,
|
||
service_one,
|
||
permissions,
|
||
expected_column_count,
|
||
column_name
|
||
):
|
||
|
||
service_one['permissions'] = permissions
|
||
|
||
page = client_request.get(
|
||
'main.service_dashboard',
|
||
service_id=service_one['id']
|
||
)
|
||
|
||
assert len(page.select(column_name)) == expected_column_count
|
||
|
||
|
||
@freeze_time("2016-01-01 11:09:00.061258")
|
||
def test_should_show_recent_jobs_on_dashboard(
|
||
logged_in_client,
|
||
mock_get_service_templates,
|
||
mock_get_template_statistics,
|
||
mock_get_detailed_service,
|
||
mock_get_jobs,
|
||
mock_get_usage,
|
||
mock_get_inbound_sms_summary
|
||
):
|
||
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 '252,190' in cols[1].text
|
||
assert 'Text messages' in cols[1].text
|
||
|
||
table = page.find('table').text.strip()
|
||
|
||
assert '249,860 free text messages' in table
|
||
assert '40 free text messages' in table
|
||
assert '960 text messages at 1.65p' in table
|
||
|
||
assert 'April' in table
|
||
assert 'February' in table
|
||
assert 'March' in table
|
||
assert '£15.84' in table
|
||
assert '140 free text messages' in table
|
||
assert '£20.30' in table
|
||
assert '1,230 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
|
||
|
||
|
||
@freeze_time("2012-03-31 12:12:12")
|
||
def test_future_usage_page(
|
||
logged_in_client,
|
||
mock_get_future_usage,
|
||
mock_get_future_billable_units,
|
||
):
|
||
assert logged_in_client.get(url_for('main.usage', service_id=SERVICE_ONE_ID, year=2014)).status_code == 200
|
||
|
||
mock_get_future_billable_units.assert_called_once_with(SERVICE_ONE_ID, 2014)
|
||
mock_get_future_usage.assert_called_once_with(SERVICE_ONE_ID, 2014)
|
||
|
||
|
||
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,
|
||
mock_get_inbound_sms_summary
|
||
):
|
||
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,
|
||
mock_get_inbound_sms_summary
|
||
):
|
||
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,
|
||
mock_get_inbound_sms_summary
|
||
):
|
||
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,
|
||
mock_get_inbound_sms_summary
|
||
):
|
||
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,
|
||
mock_get_inbound_sms_summary
|
||
):
|
||
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,
|
||
mock_get_service_templates,
|
||
mock_get_template_statistics,
|
||
mock_get_detailed_service,
|
||
mock_get_jobs,
|
||
mock_get_usage,
|
||
mock_get_inbound_sms_summary
|
||
):
|
||
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
|
||
|
||
|
||
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:
|
||
billing_units = get_free_paid_breakdown_for_billable_units(
|
||
2016, [
|
||
{
|
||
'month': 'April', 'international': False, 'rate_multiplier': 1,
|
||
'notification_type': 'sms', 'rate': 1.65, 'billing_units': 100000
|
||
},
|
||
{
|
||
'month': 'May', 'international': False, 'rate_multiplier': 1,
|
||
'notification_type': 'sms', 'rate': 1.65, 'billing_units': 100000
|
||
},
|
||
{
|
||
'month': 'June', 'international': False, 'rate_multiplier': 1,
|
||
'notification_type': 'sms', 'rate': 1.65, 'billing_units': 100000
|
||
},
|
||
{
|
||
'month': 'February', 'international': False, 'rate_multiplier': 1,
|
||
'notification_type': 'sms', 'rate': 1.65, 'billing_units': 2000
|
||
},
|
||
]
|
||
)
|
||
assert list(billing_units) == [
|
||
{'free': 100000, 'name': 'April', 'paid': 0},
|
||
{'free': 100000, 'name': 'May', 'paid': 0},
|
||
{'free': 50000, 'name': 'June', 'paid': 50000},
|
||
{'free': 0, 'name': 'July', 'paid': 0},
|
||
{'free': 0, 'name': 'August', 'paid': 0},
|
||
{'free': 0, 'name': 'September', 'paid': 0},
|
||
{'free': 0, 'name': 'October', 'paid': 0},
|
||
{'free': 0, 'name': 'November', 'paid': 0},
|
||
{'free': 0, 'name': 'December', 'paid': 0},
|
||
{'free': 0, 'name': 'January', 'paid': 0},
|
||
{'free': 0, 'name': 'February', 'paid': 2000},
|
||
{'free': 0, 'name': 'March', 'paid': 0}
|
||
][:expected_number_of_months]
|
||
|
||
|
||
def test_format_template_stats_to_list_with_no_stats():
|
||
assert list(format_template_stats_to_list({})) == []
|
||
|
||
|
||
def test_format_template_stats_to_list():
|
||
counts = {
|
||
'created': 1,
|
||
'pending': 1,
|
||
'delivered': 1,
|
||
'failed': 1,
|
||
'temporary-failure': 1,
|
||
'permanent-failure': 1,
|
||
'technical-failure': 1,
|
||
'do-not-count': 999,
|
||
}
|
||
stats_list = list(format_template_stats_to_list({
|
||
'template_2_id': {
|
||
'counts': {},
|
||
'name': 'bar',
|
||
},
|
||
'template_1_id': {
|
||
'counts': counts,
|
||
'name': 'foo',
|
||
},
|
||
}))
|
||
|
||
# we don’t care about the order of this function’s output
|
||
assert len(stats_list) == 2
|
||
assert {
|
||
'counts': counts,
|
||
'name': 'foo',
|
||
'requested_count': 7,
|
||
'id': 'template_1_id',
|
||
} in stats_list
|
||
assert {
|
||
'counts': {},
|
||
'name': 'bar',
|
||
'requested_count': 0,
|
||
'id': 'template_2_id',
|
||
} in stats_list
|
||
|
||
|
||
def test_get_tuples_of_financial_years():
|
||
assert list(get_tuples_of_financial_years(
|
||
lambda year: 'http://example.com?year={}'.format(year),
|
||
start=2040,
|
||
end=2041,
|
||
)) == [
|
||
('financial year', 2040, 'http://example.com?year=2040', '2040 to 2041'),
|
||
('financial year', 2041, 'http://example.com?year=2041', '2041 to 2042'),
|
||
]
|
||
|
||
|
||
def test_get_tuples_of_financial_years_defaults_to_2015():
|
||
assert 2015 in list(get_tuples_of_financial_years(
|
||
lambda year: 'http://example.com?year={}'.format(year),
|
||
end=2040,
|
||
))[0]
|
||
|
||
|
||
@freeze_time("2016-01-01 11:09:00.061258")
|
||
def test_should_show_all_jobs_with_valid_statuses(
|
||
logged_in_client,
|
||
mock_get_template_statistics,
|
||
mock_get_detailed_service,
|
||
mock_get_service_templates_when_no_templates_exist,
|
||
mock_get_jobs,
|
||
mock_get_usage,
|
||
mock_get_inbound_sms_summary
|
||
):
|
||
logged_in_client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
|
||
|
||
first_call = mock_get_jobs.call_args_list[0]
|
||
# first call - scheduled jobs only
|
||
assert first_call == call(ANY, statuses=['scheduled'])
|
||
# second call - everything but scheduled and cancelled
|
||
second_call = mock_get_jobs.call_args_list[1]
|
||
assert second_call == call(ANY, limit_days=ANY, statuses={
|
||
'pending',
|
||
'in progress',
|
||
'finished',
|
||
'sending limits exceeded',
|
||
'ready to send',
|
||
'sent to dvla'
|
||
})
|