Files
notifications-admin/tests/app/main/views/test_dashboard.py

1686 lines
52 KiB
Python
Raw Normal View History

import copy
from datetime import datetime
from unittest.mock import patch
from zoneinfo import ZoneInfo
2016-07-20 14:12:03 +01:00
import pytest
2024-08-09 14:29:14 -07:00
from flask import url_for
from freezegun import freeze_time
from app.enums import ServicePermission
from app.main.views.dashboard import (
aggregate_status_types,
2019-01-16 10:54:52 +00:00
aggregate_template_usage,
format_monthly_stats_to_list,
get_dashboard_totals,
get_local_daily_stats_for_last_x_days,
get_tuples_of_financial_years,
)
2025-03-05 14:00:05 -08:00
from tests import organization_json, service_json, validate_route_permission
from tests.conftest import (
ORGANISATION_ID,
SERVICE_ONE_ID,
create_active_caseworking_user,
create_active_user_view_permissions,
normalize_spaces,
)
2024-01-08 14:53:34 -08:00
FAKE_ONE_OFF_NOTIFICATION = {
"links": {},
"notifications": [
{
"api_key": None,
"billable_units": 0,
"carrier": None,
"client_reference": None,
"created_at": "2023-12-14T20:35:55+00:00",
"created_by": {
"email_address": "grsrbsrgsrf@fake.gov",
"id": "de059e0a-42e5-48bb-939e-4f76804ab739",
2024-02-12 15:29:26 -08:00
"name": "mocked_user",
2024-01-08 14:53:34 -08:00
},
"document_download_count": None,
"id": "a3442b43-0ba1-4854-9e0a-d2fba1cc9b81",
"international": False,
"job": {
"id": "55b242b5-9f62-4271-aff7-039e9c320578",
"original_file_name": "1127b78e-a4a8-4b70-8f4f-9f4fbf03ece2.csv",
},
"job_row_number": 0,
"key_name": None,
"key_type": "normal",
"normalised_to": "+16615555555",
"notification_type": "sms",
"personalisation": {
"dayofweek": "2",
"favecolor": "3",
"phonenumber": "+16615555555",
},
"phone_prefix": "1",
"provider_response": None,
"rate_multiplier": 1.0,
"reference": None,
"reply_to_text": "development",
"sent_at": None,
"sent_by": None,
"service": "f62d840f-8bcb-4b36-b959-4687e16dd1a1",
"status": "created",
"template": {
"content": "((day of week)) and ((fave color))",
"id": "bd9caa7e-00ee-4c5a-839e-10ae1a7e6f73",
2024-02-12 15:29:26 -08:00
"name": "Template Testing",
2024-01-08 14:53:34 -08:00
"redact_personalisation": False,
"subject": None,
"template_type": "sms",
"version": 1,
},
"to": "+16615555555",
"updated_at": None,
}
],
"page_size": 50,
"total": 1,
}
2024-01-26 09:43:41 -08:00
MOCK_JOBS = {
"data": [
{
"archived": False,
"created_at": "2024-01-04T20:43:52+00:00",
"created_by": {
"id": "mocked_user_id",
"name": "mocked_user",
},
2024-02-12 15:29:26 -08:00
"id": "55b242b5-9f62-4271-aff7-039e9c320578",
"job_status": "finished",
"notification_count": 1,
"original_file_name": "mocked_file.csv",
"processing_finished": "2024-01-25T23:02:25+00:00",
"processing_started": "2024-01-25T23:02:24+00:00",
"scheduled_for": None,
"service": "21b3ee3d-1cb0-4666-bfa0-9c5ac26d3fe3",
"service_name": {"name": "Mock Texting Service"},
"statistics": [{"count": 1, "status": "sending"}],
"template": "6a456418-498c-4c86-b0cd-9403c14a216c",
"template_name": "Mock Template Name",
2024-01-08 14:53:34 -08:00
"template_type": "sms",
"template_version": 3,
"updated_at": "2024-01-25T23:02:25+00:00",
}
]
2024-01-08 14:53:34 -08:00
}
2024-02-12 15:29:26 -08:00
MOCK_JOBS_AND_NOTIFICATIONS = [
{
"created_at": MOCK_JOBS["data"][0]["created_at"],
"created_by": MOCK_JOBS["data"][0]["created_by"],
"download_link": "/services/21b3ee3d-1cb0-4666-bfa0-9c5ac26d3fe3/jobs/463b5ecb-4e32-43e5-aa90-0234d19fceaa.csv",
"job_id": "55b242b5-9f62-4271-aff7-039e9c320578",
"notification_count": MOCK_JOBS["data"][0]["notification_count"],
"notifications": FAKE_ONE_OFF_NOTIFICATION["notifications"],
"time_left": "Data available for 7 days",
"view_job_link": "/services/21b3ee3d-1cb0-4666-bfa0-9c5ac26d3fe3/jobs/463b5ecb-4e32-43e5-aa90-0234d19fceaa",
2024-02-12 15:29:26 -08:00
},
]
stub_template_stats = [
{
"template_type": "sms",
"template_name": "one",
"template_id": "id-1",
"status": "created",
"count": 50,
2024-08-29 17:10:51 -07:00
"last_used": "2024-01-25T23:02:25+00:00",
"created_by": "Test user",
"created_by_id": "987654",
"template_folder": "Some_folder",
"template_folder_id": "123456",
},
{
"template_type": "email",
"template_name": "two",
"template_id": "id-2",
"status": "created",
"count": 100,
2024-08-29 17:10:51 -07:00
"last_used": "2024-01-25T23:02:25+00:00",
"created_by": "Test user",
"created_by_id": "987654",
"template_folder": "Some_folder",
"template_folder_id": "123456",
2019-01-16 10:54:52 +00:00
},
{
"template_type": "email",
"template_name": "two",
"template_id": "id-2",
"status": "technical-failure",
"count": 100,
2024-08-29 17:10:51 -07:00
"last_used": "2024-01-25T23:02:25+00:00",
"created_by": "Test user",
"created_by_id": "987654",
"template_folder": "Some_folder",
"template_folder_id": "123456",
},
{
"template_type": "sms",
"template_name": "one",
"template_id": "id-1",
"status": "delivered",
"count": 50,
2024-08-29 17:10:51 -07:00
"last_used": "2024-01-25T23:02:25+00:00",
"created_by": "Test user",
"created_by_id": "987654",
"template_folder": "Some_folder",
"template_folder_id": "123456",
},
]
2024-08-09 14:29:14 -07:00
date_range = {"start_date": "2024-01-01", "days": 7}
mock_daily_stats = {
date_range["start_date"]: {
"email": {"delivered": 0, "failure": 0, "requested": 0},
"sms": {"delivered": 0, "failure": 1, "requested": 1},
},
}
mock_daily_stats_by_user = {
date_range["start_date"]: {
"email": {"delivered": 1, "failure": 0, "requested": 1},
"sms": {"delivered": 1, "failure": 0, "requested": 1},
},
}
2025-02-25 14:01:56 -08:00
mock_service_message_ratio = {
2025-02-26 10:19:47 -08:00
"messages_remaining": 71919,
"messages_sent": 28081,
"total_message_limit": 100000,
}
2025-02-25 14:01:56 -08:00
@pytest.mark.parametrize(
"user",
[
create_active_user_view_permissions(),
create_active_caseworking_user(),
],
)
def test_redirect_from_old_dashboard(
client_request,
user,
mocker,
):
mocker.patch("app.user_api_client.get_user", return_value=user)
expected_location = "/services/{}".format(SERVICE_ONE_ID)
client_request.get_url(
"/services/{}/dashboard".format(SERVICE_ONE_ID),
_expected_redirect=expected_location,
)
assert expected_location == url_for(
"main.service_dashboard", service_id=SERVICE_ONE_ID
)
def test_redirect_caseworkers_to_templates(
client_request,
mocker,
active_caseworking_user,
):
client_request.login(active_caseworking_user)
client_request.get(
"main.service_dashboard",
service_id=SERVICE_ONE_ID,
_expected_status=302,
_expected_redirect=url_for(
"main.choose_template",
service_id=SERVICE_ONE_ID,
),
)
def test_get_started(
client_request,
mocker,
mock_get_service_templates_when_no_templates_exist,
mock_has_no_jobs,
mock_get_service_statistics,
mock_get_annual_usage_for_service,
mock_get_free_sms_fragment_limit,
mock_get_inbound_sms_summary,
):
Add flake8 linting to project 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. https://github.com/alphagov/digitalmarketplace-api/blob/d5ab8afef4a0472f9d266d94ab4ffe1333a9aaad/.flake8
2017-10-18 14:51:26 +01:00
mocker.patch(
"app.template_statistics_client.get_template_statistics_for_service",
return_value=copy.deepcopy(stub_template_stats),
Add flake8 linting to project 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. https://github.com/alphagov/digitalmarketplace-api/blob/d5ab8afef4a0472f9d266d94ab4ffe1333a9aaad/.flake8
2017-10-18 14:51:26 +01:00
)
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
page = client_request.get(
"main.service_dashboard",
service_id=SERVICE_ONE_ID,
)
mock_get_service_templates_when_no_templates_exist.assert_called_once_with(
SERVICE_ONE_ID
)
assert "Get started" in page.text
def test_get_started_is_hidden_once_templates_exist(
client_request,
mocker,
mock_get_service_templates,
mock_has_no_jobs,
mock_get_service_statistics,
mock_get_annual_usage_for_service,
mock_get_free_sms_fragment_limit,
mock_get_inbound_sms_summary,
):
Add flake8 linting to project 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. https://github.com/alphagov/digitalmarketplace-api/blob/d5ab8afef4a0472f9d266d94ab4ffe1333a9aaad/.flake8
2017-10-18 14:51:26 +01:00
mocker.patch(
"app.template_statistics_client.get_template_statistics_for_service",
return_value=copy.deepcopy(stub_template_stats),
Add flake8 linting to project 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. https://github.com/alphagov/digitalmarketplace-api/blob/d5ab8afef4a0472f9d266d94ab4ffe1333a9aaad/.flake8
2017-10-18 14:51:26 +01:00
)
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
page = client_request.get(
"main.service_dashboard",
service_id=SERVICE_ONE_ID,
)
mock_get_service_templates.assert_called_once_with(SERVICE_ONE_ID)
assert not page.find("h2", string="Get started")
def test_inbound_messages_not_visible_to_service_without_permissions(
client_request,
mocker,
service_one,
mock_get_service_templates_when_no_templates_exist,
mock_has_no_jobs,
mock_get_service_statistics,
mock_get_template_statistics,
mock_get_annual_usage_for_service,
mock_get_free_sms_fragment_limit,
mock_get_inbound_sms_summary,
):
service_one["permissions"] = []
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
page = client_request.get(
"main.service_dashboard",
service_id=SERVICE_ONE_ID,
)
assert not page.select(".big-number-meta-wrapper")
assert mock_get_inbound_sms_summary.called is False
def test_should_show_recent_templates_on_dashboard(
client_request,
mocker,
mock_get_service_templates,
mock_has_no_jobs,
mock_get_service_statistics,
mock_get_annual_usage_for_service,
mock_get_free_sms_fragment_limit,
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),
)
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
page = client_request.get(
"main.service_dashboard",
service_id=SERVICE_ONE_ID,
)
2016-01-06 16:51:35 +00:00
mock_template_stats.assert_called_once_with(SERVICE_ONE_ID, limit_days=7)
2016-03-09 12:10:50 +00:00
headers = [
header.text.strip() for header in page.find_all("h2") + page.find_all("h1")
]
2024-08-14 16:11:00 -04:00
assert "Total messages" in headers
2025-01-20 14:34:11 -08:00
table_rows = page.find_all("tbody")[1].find_all("tr")
assert len(table_rows) == 2
@pytest.mark.parametrize(
"stats",
[
pytest.param(
[stub_template_stats[0]],
),
pytest.param(
[stub_template_stats[0], stub_template_stats[1]],
# marks=pytest.mark.xfail(raises=AssertionError),
),
],
)
def test_should_not_show_recent_templates_on_dashboard_if_only_one_template_used(
client_request,
mocker,
mock_get_service_templates,
mock_has_no_jobs,
mock_get_service_statistics,
mock_get_annual_usage_for_service,
mock_get_free_sms_fragment_limit,
mock_get_inbound_sms_summary,
stats,
):
mock_template_stats = mocker.patch(
"app.template_statistics_client.get_template_statistics_for_service",
return_value=stats,
)
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
page = client_request.get("main.service_dashboard", service_id=SERVICE_ONE_ID)
main = page.select_one("main").text
mock_template_stats.assert_called_once_with(SERVICE_ONE_ID, limit_days=7)
assert (
stats[0]["template_name"] == "one"
), f"Expected template_name to be 'one', but got {stats[0]['template_name']}"
# Check that "one" is not in the main content
assert (
stats[0]["template_name"] in main
), f"Expected 'one' to not be in main, but it was found in: {main}"
# count appears as total, but not per template
expected_count = stats[0]["count"]
assert expected_count == 50, f"Expected count to be 50, but got {expected_count}"
2024-03-27 10:10:56 -06:00
@freeze_time("2017-01-01 12:00")
@pytest.mark.parametrize(
"extra_args",
[
{},
2024-02-07 09:38:18 -07:00
{"year": "2017"},
],
)
def test_should_show_monthly_breakdown_of_template_usage(
client_request,
mock_get_monthly_template_usage,
extra_args,
):
page = client_request.get(
"main.template_usage", service_id=SERVICE_ONE_ID, **extra_args
)
2024-02-07 09:38:18 -07:00
mock_get_monthly_template_usage.assert_called_once_with(SERVICE_ONE_ID, 2017)
table_rows = page.select("tbody tr")
assert " ".join(table_rows[0].text.split()) == (
"My first template " "Text message template " "2"
)
2024-02-07 09:38:18 -07:00
assert len(table_rows) == len(["January"])
2024-03-27 10:10:56 -06:00
# October is the only month with data, thus it's not in the list.
assert len(page.select(".table-no-data")) == len(
2024-02-07 09:38:18 -07:00
[
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"November",
2024-03-27 10:10:56 -06:00
"December",
2024-02-07 09:38:18 -07:00
]
)
2016-03-09 12:10:50 +00:00
@pytest.mark.parametrize(
"endpoint",
[
"main.template_usage",
],
)
@freeze_time("2015-01-01 15:15:15.000000")
def test_stats_pages_show_last_3_years(
client_request,
endpoint,
mock_get_monthly_notification_stats,
mock_get_monthly_template_usage,
):
page = client_request.get(
endpoint,
service_id=SERVICE_ONE_ID,
)
assert normalize_spaces(page.select_one(".pill").text) == (
2024-02-07 09:38:18 -07:00
"2015 to 2016 fiscal year "
"2014 to 2015 fiscal year "
2024-02-07 09:38:18 -07:00
"2013 to 2014 fiscal year"
)
2023-11-16 12:24:27 -08:00
@freeze_time("2016-01-01 11:09:00.061258")
def test_should_show_upcoming_jobs_on_dashboard(
2024-01-08 14:53:34 -08:00
mocker,
client_request,
mock_get_service_templates,
mock_get_template_statistics,
mock_get_service_statistics,
mock_get_jobs,
mock_get_scheduled_job_stats,
mock_get_annual_usage_for_service,
mock_get_free_sms_fragment_limit,
mock_get_inbound_sms_summary,
):
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
page = client_request.get(
"main.service_dashboard",
service_id=SERVICE_ONE_ID,
)
2024-01-26 11:16:45 -08:00
mock_get_jobs.assert_called_with(SERVICE_ONE_ID)
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
mock_get_scheduled_job_stats.assert_called_once_with(SERVICE_ONE_ID)
assert normalize_spaces(page.select_one("main h2").text) == ("In the next few days")
assert normalize_spaces(page.select_one("a.banner-dashboard").text) == (
2024-08-23 12:59:00 -07:00
"2 files waiting to send " "- sending starts today at 06:09 America/New_York"
)
assert page.select_one("a.banner-dashboard")["href"] == url_for(
"main.uploads", service_id=SERVICE_ONE_ID
)
def test_should_not_show_upcoming_jobs_on_dashboard_if_count_is_0(
mocker,
client_request,
mock_get_service_templates,
mock_get_template_statistics,
mock_get_service_statistics,
mock_has_jobs,
mock_get_annual_usage_for_service,
mock_get_free_sms_fragment_limit,
mock_get_inbound_sms_summary,
):
mocker.patch(
"app.job_api_client.get_scheduled_job_stats",
return_value={
"count": 0,
"soonest_scheduled_for": None,
},
)
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
page = client_request.get(
"main.service_dashboard",
service_id=SERVICE_ONE_ID,
)
mock_has_jobs.assert_called_once_with(SERVICE_ONE_ID)
assert "In the next few days" not in page.select_one("main").text
assert "files waiting to send " not in page.select_one("main").text
def test_should_not_show_upcoming_jobs_on_dashboard_if_service_has_no_jobs(
mocker,
client_request,
mock_get_service_templates,
mock_get_template_statistics,
mock_get_service_statistics,
mock_has_no_jobs,
mock_get_scheduled_job_stats,
mock_get_annual_usage_for_service,
mock_get_free_sms_fragment_limit,
mock_get_inbound_sms_summary,
):
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
page = client_request.get(
"main.service_dashboard",
service_id=SERVICE_ONE_ID,
)
mock_has_no_jobs.assert_called_once_with(SERVICE_ONE_ID)
assert mock_get_scheduled_job_stats.called is False
assert "In the next few days" not in page.select_one("main").text
assert "files waiting to send " not in page.select_one("main").text
@pytest.mark.parametrize(
"permissions",
[
("email", "sms"),
],
)
@pytest.mark.parametrize(
"totals",
[
(
{
"email": {"requested": 0, "delivered": 0, "failed": 0},
"sms": {"requested": 99999, "delivered": 0, "failed": 0},
},
),
(
{
"email": {"requested": 0, "delivered": 0, "failed": 0},
"sms": {"requested": 0, "delivered": 0, "failed": 0},
},
),
],
)
def test_correct_font_size_for_big_numbers(
client_request,
mocker,
mock_get_service_templates,
mock_get_template_statistics,
mock_get_service_statistics,
mock_has_no_jobs,
mock_get_annual_usage_for_service,
mock_get_free_sms_fragment_limit,
service_one,
permissions,
totals,
):
service_one["permissions"] = permissions
mocker.patch("app.main.views.dashboard.get_dashboard_totals", return_value=totals)
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
# mocker.patch(
# "app.notification_api_client.get_notifications_for_service",
# return_value=FAKE_ONE_OFF_NOTIFICATION,
# page = client_request.get(
# "main.service_dashboard",
# service_id=service_one["id"],
# )
# assert (
# (len(page.select_one("[data-key=totals]").select(".grid-col-12")))
# == (
# # len(page.select_one('[data-key=usage]').select('.grid-col-6'))
# # ) == (
# len(page.select(".big-number-with-status .big-number-smaller"))
# )
# == 1
# )
def test_should_not_show_jobs_on_dashboard_for_users_with_uploads_page(
2024-01-08 14:53:34 -08:00
mocker,
client_request,
service_one,
mock_get_service_templates,
mock_get_template_statistics,
mock_get_service_statistics,
mock_get_jobs,
mock_get_scheduled_job_stats,
mock_get_annual_usage_for_service,
mock_get_free_sms_fragment_limit,
mock_get_inbound_sms_summary,
):
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
page = client_request.get(
"main.service_dashboard",
service_id=SERVICE_ONE_ID,
)
2024-01-26 11:16:45 -08:00
mock_get_jobs.assert_called_with(SERVICE_ONE_ID)
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
for filename in {
"export 1/1/2016.xls",
"all email addresses.xlsx",
"applicants.ods",
"thisisatest.csv",
}:
assert filename not in page.select_one("main").text
@freeze_time("2012-03-31 12:12:12")
def test_usage_page(
client_request,
mock_get_annual_usage_for_service,
mock_get_monthly_usage_for_service,
2022-12-02 11:27:41 -05:00
mock_get_free_sms_fragment_limit,
mock_get_monthly_notification_stats,
):
page = client_request.get(
"main.usage",
service_id=SERVICE_ONE_ID,
)
2024-02-07 09:38:18 -07:00
mock_get_monthly_usage_for_service.assert_called_once_with(SERVICE_ONE_ID, 2012)
mock_get_annual_usage_for_service.assert_called_once_with(SERVICE_ONE_ID, 2012)
mock_get_free_sms_fragment_limit.assert_called_with(SERVICE_ONE_ID)
nav = page.find("ul", {"class": "pill"})
unselected_nav_links = nav.select("a:not(.pill-item--selected)")
assert (
normalize_spaces(nav.find("a", {"aria-current": "page"}).text)
2024-02-07 09:38:18 -07:00
== "2012 to 2013 fiscal year"
)
2024-02-07 09:38:18 -07:00
assert normalize_spaces(unselected_nav_links[0].text) == "2011 to 2012 fiscal year"
assert normalize_spaces(unselected_nav_links[1].text) == "2010 to 2011 fiscal year"
2023-10-02 15:12:36 -04:00
annual_usage = page.find_all("div", {"class": "keyline-block"})
# annual stats are shown in two rows, each with three column; email is col 1
2022-12-02 10:30:37 -05:00
# email_column = normalize_spaces(annual_usage[0].text + annual_usage[2].text)
# assert 'Emails' in email_column
# assert '1,000 sent' in email_column
sms_column = normalize_spaces(annual_usage[0].text)
2023-10-02 15:12:36 -04:00
assert (
"You have sent 251,800 text message parts of your 250,000 free message parts allowance."
" You have 0 message parts remaining." in sms_column
2023-10-02 15:12:36 -04:00
)
assert "$29.85 spent" not in sms_column
2023-10-02 15:12:36 -04:00
assert "1,500 at 1.65 pence" not in sms_column
assert "300 at 1.70 pence" not in sms_column
2022-10-17 13:42:36 +00:00
@freeze_time("2012-03-31 12:12:12")
def test_usage_page_no_sms_spend(
mocker,
client_request,
mock_get_monthly_usage_for_service,
2022-12-02 11:27:41 -05:00
mock_get_free_sms_fragment_limit,
mock_get_monthly_notification_stats,
):
mocker.patch(
"app.billing_api_client.get_annual_usage_for_service",
return_value=[
{
"notification_type": "sms",
"chargeable_units": 1000,
"charged_units": 0,
"rate": 0.0165,
"cost": 0,
}
],
)
page = client_request.get(
"main.usage",
service_id=SERVICE_ONE_ID,
)
2023-10-02 15:12:36 -04:00
annual_usage = page.find_all("div", {"class": "keyline-block"})
sms_column = normalize_spaces(annual_usage[0].text)
2023-10-02 15:12:36 -04:00
assert (
"You have sent 1,000 text message parts of your 250,000 free message parts allowance."
" You have 249,000 message parts remaining." in sms_column
2023-10-02 15:12:36 -04:00
)
assert "$0.00 spent" not in sms_column
assert "pence per message" not in sms_column
@freeze_time("2012-03-31 12:12:12")
def test_usage_page_monthly_breakdown(
client_request,
service_one,
mock_get_annual_usage_for_service,
mock_get_monthly_usage_for_service,
2022-12-02 11:27:41 -05:00
mock_get_free_sms_fragment_limit,
mock_get_monthly_notification_stats,
):
page = client_request.get("main.usage", service_id=SERVICE_ONE_ID)
monthly_breakdown = normalize_spaces(page.find("table").text)
2024-02-07 09:38:18 -07:00
assert "January" in monthly_breakdown
assert "October" in monthly_breakdown
assert "February" in monthly_breakdown
assert "March" in monthly_breakdown
@pytest.mark.parametrize(
("now", "expected_number_of_months"),
[
2024-02-07 09:38:18 -07:00
(freeze_time("2017-03-31 11:09:00.061258"), 12),
(freeze_time("2017-01-01 11:09:00.061258"), 12),
],
)
def test_usage_page_monthly_breakdown_shows_months_so_far(
client_request,
service_one,
mock_get_annual_usage_for_service,
mock_get_monthly_usage_for_service,
mock_get_free_sms_fragment_limit,
2022-12-02 11:27:41 -05:00
mock_get_monthly_notification_stats,
now,
expected_number_of_months,
):
with now:
page = client_request.get("main.usage", service_id=SERVICE_ONE_ID)
rows = page.find("table").find_all("tr", class_="table-row")
assert len(rows) == expected_number_of_months
def test_usage_page_with_0_free_allowance(
mocker,
client_request,
mock_get_annual_usage_for_service,
mock_get_monthly_usage_for_service,
2022-12-02 11:27:41 -05:00
mock_get_monthly_notification_stats,
):
mocker.patch(
"app.billing_api_client.get_free_sms_fragment_limit_for_year",
return_value=0,
)
page = client_request.get(
"main.usage",
service_id=SERVICE_ONE_ID,
year=2020,
)
2023-10-02 15:12:36 -04:00
annual_usage = page.select("main .grid-col-12 .keyline-block")
2022-12-02 10:30:37 -05:00
sms_column = normalize_spaces(annual_usage[0].text)
2023-10-02 15:12:36 -04:00
assert (
"You have sent 251,800 text message parts of your 0 free message parts allowance. You have"
2023-10-02 15:12:36 -04:00
in sms_column
)
assert "free allowance remaining" not in sms_column
def test_usage_page_with_year_argument(
client_request,
mock_get_annual_usage_for_service,
mock_get_monthly_usage_for_service,
mock_get_free_sms_fragment_limit,
2022-12-02 11:27:41 -05:00
mock_get_monthly_notification_stats,
):
client_request.get(
"main.usage",
service_id=SERVICE_ONE_ID,
year=2000,
)
mock_get_monthly_usage_for_service.assert_called_once_with(SERVICE_ONE_ID, 2000)
mock_get_annual_usage_for_service.assert_called_once_with(SERVICE_ONE_ID, 2000)
2024-02-07 09:38:18 -07:00
mock_get_free_sms_fragment_limit.assert_called_with(SERVICE_ONE_ID)
2022-12-02 11:27:41 -05:00
mock_get_monthly_notification_stats.assert_called_with(SERVICE_ONE_ID, 2000)
def test_usage_page_for_invalid_year(
client_request,
):
client_request.get(
"main.usage",
service_id=SERVICE_ONE_ID,
year="abcd",
_expected_status=404,
)
@freeze_time("2012-03-31 12:12:12")
2017-05-02 14:10:56 +01:00
def test_future_usage_page(
client_request,
mock_get_annual_usage_for_service_in_future,
mock_get_monthly_usage_for_service_in_future,
2022-12-02 11:27:41 -05:00
mock_get_free_sms_fragment_limit,
mock_get_monthly_notification_stats,
2017-05-02 14:10:56 +01:00
):
client_request.get(
"main.usage",
service_id=SERVICE_ONE_ID,
year=2014,
)
2017-05-02 14:10:56 +01:00
mock_get_monthly_usage_for_service_in_future.assert_called_once_with(
SERVICE_ONE_ID, 2014
)
mock_get_annual_usage_for_service_in_future.assert_called_once_with(
SERVICE_ONE_ID, 2014
)
2024-02-07 09:38:18 -07:00
mock_get_free_sms_fragment_limit.assert_called_with(SERVICE_ONE_ID)
2022-12-02 11:27:41 -05:00
mock_get_monthly_notification_stats.assert_called_with(SERVICE_ONE_ID, 2014)
2017-05-02 14:10:56 +01:00
def _test_dashboard_menu(client_request, mocker, usr, service, permissions):
usr["permissions"][str(service["id"])] = permissions
usr["services"] = [service["id"]]
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_request.login(usr)
return client_request.get("main.service_dashboard", service_id=service["id"])
2016-03-09 12:10:50 +00:00
def _test_settings_menu(client_request, mocker, usr, service, permissions):
usr["permissions"][str(service["id"])] = permissions
usr["services"] = [service["id"]]
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_request.login(usr)
return client_request.get("main.service_dashboard", service_id=service["id"])
def test_menu_send_messages(
client_request,
mocker,
notify_admin,
api_user_active,
service_one,
mock_get_service_templates,
mock_has_no_jobs,
mock_get_template_statistics,
mock_get_service_statistics,
mock_get_annual_usage_for_service,
mock_get_inbound_sms_summary,
mock_get_free_sms_fragment_limit,
):
service_one["permissions"] = ["email", "sms"]
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
page = _test_dashboard_menu(
client_request,
mocker,
api_user_active,
service_one,
2025-07-22 10:06:00 -07:00
[
ServicePermission.VIEW_ACTIVITY,
ServicePermission.SEND_TEXTS,
2025-07-22 10:06:00 -07:00
ServicePermission.SEND_EMAILS,
2025-07-22 11:12:26 -07:00
ServicePermission.MANAGE_SERVICE,
2025-07-22 10:06:00 -07:00
],
)
page = str(page)
assert (
url_for(
"main.choose_template",
service_id=service_one["id"],
)
in page
)
assert url_for("main.manage_users", service_id=service_one["id"]) not in page
assert url_for("main.service_settings", service_id=service_one["id"]) in page
2016-03-09 12:10:50 +00:00
def test_menu_manage_service(
client_request,
mocker,
api_user_active,
service_one,
mock_get_service_templates,
mock_has_no_jobs,
mock_get_template_statistics,
mock_get_service_statistics,
mock_get_annual_usage_for_service,
mock_get_inbound_sms_summary,
mock_get_free_sms_fragment_limit,
):
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
page = _test_dashboard_menu(
client_request,
mocker,
api_user_active,
service_one,
2025-07-22 10:06:00 -07:00
[
ServicePermission.VIEW_ACTIVITY,
ServicePermission.MANAGE_TEMPLATES,
ServicePermission.MANAGE_USERS,
ServicePermission.MANAGE_SETTINGS
2025-07-22 10:06:00 -07:00
],
)
page = str(page)
assert (
url_for(
"main.choose_template",
service_id=service_one["id"],
)
in page
)
assert url_for(".service_dashboard", service_id=service_one["id"]) in page
assert url_for(".choose_template", service_id=service_one["id"]) in page
2016-03-09 12:10:50 +00:00
def test_menu_main_settings(
client_request,
mocker,
api_user_active,
service_one,
mock_get_service_templates,
mock_has_no_jobs,
mock_get_template_statistics,
mock_get_service_statistics,
mock_get_annual_usage_for_service,
mock_get_inbound_sms_summary,
mock_get_free_sms_fragment_limit,
):
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
page = _test_settings_menu(
client_request,
mocker,
api_user_active,
service_one,
2025-07-22 11:12:26 -07:00
[
ServicePermission.VIEW_ACTIVITY,
"user_profile",
ServicePermission.MANAGE_USERS,
ServicePermission.MANAGE_SETTINGS,
2025-07-22 11:12:26 -07:00
],
)
page = str(page)
assert (
url_for(
"main.service_settings",
service_id=service_one["id"],
)
in page
)
assert url_for("main.service_settings", service_id=service_one["id"]) in page
2016-03-09 12:10:50 +00:00
def test_menu_manage_api_keys(
client_request,
mocker,
api_user_active,
service_one,
mock_get_service_templates,
mock_has_no_jobs,
mock_get_template_statistics,
mock_get_service_statistics,
mock_get_annual_usage_for_service,
mock_get_inbound_sms_summary,
mock_get_free_sms_fragment_limit,
):
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
page = _test_dashboard_menu(
client_request,
mocker,
api_user_active,
service_one,
[ServicePermission.VIEW_ACTIVITY],
)
page = str(page)
2016-03-09 12:10:50 +00:00
assert (
url_for(
"main.choose_template",
service_id=service_one["id"],
)
in page
)
def test_menu_all_services_for_platform_admin_user(
client_request,
mocker,
platform_admin_user,
service_one,
mock_get_service_templates,
mock_has_no_jobs,
mock_get_template_statistics,
mock_get_service_statistics,
mock_get_annual_usage_for_service,
mock_get_inbound_sms_summary,
mock_get_free_sms_fragment_limit,
):
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
page = _test_dashboard_menu(
client_request, mocker, platform_admin_user, service_one, []
)
page = str(page)
assert url_for("main.choose_template", 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_route_for_service_permissions(
mocker,
notify_admin,
api_user_active,
service_one,
mock_get_service,
mock_get_user,
mock_get_service_templates,
mock_has_no_jobs,
mock_get_template_statistics,
mock_get_service_statistics,
2024-02-07 14:43:39 -07:00
mock_get_monthly_usage_for_service,
mock_get_annual_usage_for_service,
2024-02-07 14:43:39 -07:00
mock_create_or_update_free_sms_fragment_limit,
mock_get_free_sms_fragment_limit,
mock_get_inbound_sms_summary,
):
with notify_admin.test_request_context():
def _get(mocker):
return {"count": 0}
mocker.patch(
"app.service_api_client.get_global_notification_count", side_effect=_get
)
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
validate_route_permission(
mocker,
notify_admin,
"GET",
200,
url_for("main.service_dashboard", service_id=service_one["id"]),
[ServicePermission.VIEW_ACTIVITY],
api_user_active,
service_one,
)
def test_aggregate_template_stats():
expected = aggregate_template_usage(copy.deepcopy(stub_template_stats))
2022-12-05 15:33:44 -05:00
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"
2016-07-20 14:12:03 +01:00
def test_get_dashboard_totals_adds_percentages():
stats = {
"sms": {"requested": 3, "delivered": 0, "failed": 2},
"email": {"requested": 0, "delivered": 0, "failed": 0},
2016-07-20 14:12:03 +01:00
}
assert get_dashboard_totals(stats)["sms"]["failed_percentage"] == "66.7"
assert get_dashboard_totals(stats)["email"]["failed_percentage"] == "0"
2016-07-20 14:12:03 +01:00
@pytest.mark.parametrize(("failures", "expected"), [(2, False), (3, False), (4, True)])
2016-07-20 14:12:03 +01:00
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,
}
2022-12-05 15:33:44 -05:00
def test_format_monthly_stats_works_for_email():
resp = format_monthly_stats_to_list(
{
"2016-07": {
"sms": {},
"email": {},
}
}
)
assert isinstance(resp[0]["sms_counts"], dict)
assert isinstance(resp[0]["email_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
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,
)
) == [
("fiscal year", 2041, "http://example.com?year=2041", "2041 to 2042"),
("fiscal year", 2040, "http://example.com?year=2040", "2040 to 2041"),
]
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,
)
)[-1]
)
def test_org_breadcrumbs_do_not_show_if_service_has_no_org(
2024-01-08 14:53:34 -08:00
mocker,
client_request,
mock_get_template_statistics,
mock_get_service_templates_when_no_templates_exist,
mock_has_no_jobs,
mock_get_annual_usage_for_service,
mock_get_free_sms_fragment_limit,
):
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
page = client_request.get("main.service_dashboard", service_id=SERVICE_ONE_ID)
assert not page.select(".navigation-organization-link")
def test_org_breadcrumbs_do_not_show_if_user_is_not_an_org_member(
mocker,
mock_get_service_templates_when_no_templates_exist,
mock_has_no_jobs,
active_caseworking_user,
client_request,
mock_get_template_folders,
mock_get_api_keys,
):
# active_caseworking_user is not an org member
service_one_json = service_json(
SERVICE_ONE_ID,
users=[active_caseworking_user["id"]],
restricted=False,
organization_id=ORGANISATION_ID,
)
mocker.patch(
"app.service_api_client.get_service", return_value={"data": service_one_json}
)
client_request.login(active_caseworking_user, service=service_one_json)
page = client_request.get(
"main.service_dashboard", service_id=SERVICE_ONE_ID, _follow_redirects=True
)
assert not page.select(".navigation-organization-link")
def test_org_breadcrumbs_show_if_user_is_a_member_of_the_services_org(
mocker,
mock_get_template_statistics,
mock_get_service_templates_when_no_templates_exist,
mock_has_no_jobs,
mock_get_annual_usage_for_service,
mock_get_free_sms_fragment_limit,
active_user_with_permissions,
client_request,
):
# active_user_with_permissions (used by the client_request) is an org member
service_one_json = service_json(
SERVICE_ONE_ID,
users=[active_user_with_permissions["id"]],
restricted=False,
organization_id=ORGANISATION_ID,
)
mocker.patch(
"app.service_api_client.get_service", return_value={"data": service_one_json}
)
mocker.patch(
"app.organizations_client.get_organization",
return_value=organization_json(
id_=ORGANISATION_ID,
),
)
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
page = client_request.get("main.service_dashboard", service_id=SERVICE_ONE_ID)
assert page.select_one(".navigation-organization-link")["href"] == url_for(
"main.organization_dashboard",
org_id=ORGANISATION_ID,
)
def test_org_breadcrumbs_do_not_show_if_user_is_a_member_of_the_services_org_but_service_is_in_trial_mode(
mocker,
mock_get_template_statistics,
mock_get_service_templates_when_no_templates_exist,
mock_has_no_jobs,
mock_get_annual_usage_for_service,
mock_get_free_sms_fragment_limit,
active_user_with_permissions,
client_request,
):
# active_user_with_permissions (used by the client_request) is an org member
service_one_json = service_json(
SERVICE_ONE_ID,
users=[active_user_with_permissions["id"]],
organization_id=ORGANISATION_ID,
)
mocker.patch(
"app.service_api_client.get_service", return_value={"data": service_one_json}
)
mocker.patch("app.models.service.Organization")
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
2024-01-08 14:53:34 -08:00
page = client_request.get("main.service_dashboard", service_id=SERVICE_ONE_ID)
assert not page.select(".navigation-breadcrumb")
def test_org_breadcrumbs_show_if_user_is_platform_admin(
mocker,
mock_get_template_statistics,
mock_get_service_templates_when_no_templates_exist,
mock_has_no_jobs,
mock_get_annual_usage_for_service,
mock_get_free_sms_fragment_limit,
platform_admin_user,
client_request,
):
service_one_json = service_json(
SERVICE_ONE_ID,
users=[platform_admin_user["id"]],
organization_id=ORGANISATION_ID,
)
mocker.patch(
"app.service_api_client.get_service", return_value={"data": service_one_json}
)
mocker.patch(
"app.organizations_client.get_organization",
return_value=organization_json(
id_=ORGANISATION_ID,
),
)
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-01-08 14:53:34 -08:00
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
2024-01-08 14:53:34 -08:00
client_request.login(platform_admin_user, service_one_json)
page = client_request.get("main.service_dashboard", service_id=SERVICE_ONE_ID)
assert page.select_one(".navigation-organization-link")["href"] == url_for(
"main.organization_dashboard",
org_id=ORGANISATION_ID,
)
def test_breadcrumb_shows_if_service_is_suspended(
mocker,
mock_get_template_statistics,
mock_get_service_templates_when_no_templates_exist,
mock_has_no_jobs,
mock_get_annual_usage_for_service,
mock_get_free_sms_fragment_limit,
active_user_with_permissions,
client_request,
):
service_one_json = service_json(
SERVICE_ONE_ID,
active=False,
users=[active_user_with_permissions["id"]],
)
mocker.patch(
"app.service_api_client.get_service", return_value={"data": service_one_json}
)
2024-01-08 14:53:34 -08:00
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
2024-08-09 14:29:14 -07:00
page = client_request.get("main.service_dashboard", service_id=SERVICE_ONE_ID)
assert "Suspended" in page.select_one(".navigation-service-name").text
@pytest.mark.parametrize(
"permissions",
[
("email", "sms"),
],
)
def test_service_dashboard_shows_usage(
mocker,
client_request,
service_one,
mock_get_service_templates,
mock_get_template_statistics,
mock_has_no_jobs,
mock_get_annual_usage_for_service,
mock_get_free_sms_fragment_limit,
permissions,
):
mocker.patch(
"app.service_api_client.get_global_notification_count",
return_value={
"count": 500,
},
)
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
service_one["permissions"] = permissions
page = client_request.get("main.service_dashboard", service_id=SERVICE_ONE_ID)
2024-01-05 12:45:21 -08:00
usage_table = page.find("table", class_="usage-table")
# Check if the "Usage" table doesn't exist
assert usage_table is None
2024-01-08 15:33:24 -08:00
def test_service_dashboard_shows_free_allowance(
mocker,
client_request,
service_one,
mock_get_service_templates,
mock_get_template_statistics,
mock_has_no_jobs,
mock_get_free_sms_fragment_limit,
):
mocker.patch(
"app.billing_api_client.get_annual_usage_for_service",
return_value=[
{
"notification_type": "sms",
"chargeable_units": 1000,
"charged_units": 0,
"rate": 0.0165,
"cost": 0,
}
],
)
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
2024-01-09 15:48:16 -08:00
def test_service_dashboard_shows_batched_jobs(
mocker,
client_request,
service_one,
mock_get_service_templates,
mock_get_template_statistics,
mock_has_no_jobs,
mock_get_annual_usage_for_service,
mock_get_free_sms_fragment_limit,
2024-01-12 13:58:56 -08:00
):
2024-01-26 09:43:41 -08:00
mocker.patch("app.job_api_client.get_jobs", return_value=MOCK_JOBS)
2024-08-09 15:04:56 -07:00
mocker.patch(
2025-02-25 14:01:56 -08:00
"app.service_api_client.get_service_message_ratio",
return_value=mock_service_message_ratio,
2024-08-09 15:04:56 -07:00
)
2024-01-09 15:48:16 -08:00
2024-08-09 14:29:14 -07:00
page = client_request.get("main.service_dashboard", service_id=SERVICE_ONE_ID)
2024-02-12 15:29:26 -08:00
job_table_body = page.find("table", class_="job-table")
2024-01-09 15:48:16 -08:00
2024-02-12 15:29:26 -08:00
rows = job_table_body.find_all("tbody")[0].find_all("tr")
2025-01-20 14:34:11 -08:00
assert len(rows) == 1
2024-08-13 15:29:19 -04:00
2024-02-12 15:29:26 -08:00
assert job_table_body is not None
@patch("app.main.views.dashboard.datetime", wraps=datetime)
def test_simple_local_conversion(mock_dt):
stats_utc = {
"2025-02-24T15:00:00Z": {
2025-03-04 21:14:53 -08:00
"sms": {"delivered": 1, "failure": 0, "pending": 0, "requested": 1},
"email": {"delivered": 0, "failure": 0, "pending": 0, "requested": 0},
},
"2025-02-25T07:00:00Z": {
2025-03-04 21:14:53 -08:00
"sms": {"delivered": 2, "failure": 0, "pending": 0, "requested": 2},
"email": {"delivered": 0, "failure": 0, "pending": 0, "requested": 0},
},
}
# Mock today's date in local time: 2025-02-25 at 10:00
2025-03-04 21:14:53 -08:00
mock_dt.now.return_value = datetime(
2025, 2, 25, 10, 0, 0, tzinfo=ZoneInfo("America/New_York")
)
local_stats = get_local_daily_stats_for_last_x_days(
2025-03-04 21:14:53 -08:00
stats_utc, "America/New_York", days=2
)
assert len(local_stats) == 2
assert "2025-02-24" in local_stats
assert "2025-02-25" in local_stats
assert local_stats["2025-02-24"]["sms"]["delivered"] == 1
assert local_stats["2025-02-24"]["sms"]["requested"] == 1
assert local_stats["2025-02-25"]["sms"]["delivered"] == 2
assert local_stats["2025-02-25"]["sms"]["requested"] == 2
def test_no_timestamps_returns_zeroed_days():
stats_utc = {}
class MockDateTime(datetime):
@classmethod
def now(cls, tz=None):
return cls(2025, 2, 26, 8, 0, 0, tzinfo=tz)
with patch("app.main.views.dashboard.datetime", MockDateTime):
local_stats = get_local_daily_stats_for_last_x_days(
stats_utc, "America/New_York", days=3
)
assert list(local_stats.keys()) == ["2025-02-24", "2025-02-25", "2025-02-26"]
for day in local_stats:
for msg_type in ["sms", "email"]:
for status in ["delivered", "failure", "pending", "requested"]:
assert local_stats[day][msg_type][status] == 0
def test_timestamp_in_future_time_zone():
stats_utc = {
"2025-02-25T01:00:00Z": {
2025-03-04 21:14:53 -08:00
"sms": {"delivered": 5, "failure": 0, "pending": 0, "requested": 5},
"email": {"delivered": 0, "failure": 0, "pending": 0, "requested": 0},
}
}
class MockDateTime(datetime):
@classmethod
def now(cls, tz=None):
return cls(2025, 2, 25, 10, 0, 0, tzinfo=tz)
with patch("app.main.views.dashboard.datetime", MockDateTime):
local_stats = get_local_daily_stats_for_last_x_days(
stats_utc, "Asia/Shanghai", days=1
)
assert list(local_stats.keys()) == ["2025-02-25"]
assert local_stats["2025-02-25"]["sms"]["delivered"] == 5
def test_many_timestamps_one_local_day():
stats_utc = {
"2025-02-24T05:00:00Z": {
2025-03-04 21:14:53 -08:00
"sms": {"delivered": 2, "failure": 1, "pending": 0, "requested": 3},
"email": {"delivered": 0, "failure": 0, "pending": 0, "requested": 0},
},
"2025-02-24T09:30:00Z": {
2025-03-04 21:14:53 -08:00
"sms": {"delivered": 1, "failure": 0, "pending": 0, "requested": 1},
"email": {"delivered": 2, "failure": 0, "pending": 0, "requested": 2},
},
"2025-02-24T16:59:59Z": {
2025-03-04 21:14:53 -08:00
"sms": {"delivered": 4, "failure": 0, "pending": 0, "requested": 4},
"email": {"delivered": 0, "failure": 0, "pending": 0, "requested": 0},
},
}
class MockDateTime(datetime):
@classmethod
def now(cls, tz=None):
return cls(2025, 2, 24, 20, 0, 0, tzinfo=tz)
with patch("app.main.views.dashboard.datetime", MockDateTime):
local_stats = get_local_daily_stats_for_last_x_days(
stats_utc, "America/New_York", days=1
)
assert list(local_stats.keys()) == ["2025-02-24"]
assert local_stats["2025-02-24"]["sms"]["delivered"] == 7
assert local_stats["2025-02-24"]["sms"]["requested"] == 8
assert local_stats["2025-02-24"]["email"]["delivered"] == 2
assert local_stats["2025-02-24"]["email"]["requested"] == 2
def test_local_conversion_phoenix():
"""Test aggregator logic in Mountain Time, no DST (America/Phoenix)."""
stats_utc = {
"2025-02-25T01:00:00Z": {
2025-03-04 21:14:53 -08:00
"sms": {"delivered": 1, "failure": 0, "pending": 0, "requested": 1},
"email": {"delivered": 0, "failure": 0, "pending": 0, "requested": 0},
},
"2025-02-25T12:00:00Z": {
2025-03-04 21:14:53 -08:00
"sms": {"delivered": 2, "failure": 0, "pending": 0, "requested": 2},
"email": {"delivered": 1, "failure": 0, "pending": 0, "requested": 1},
},
}
with patch("app.main.views.dashboard.datetime", wraps=datetime) as mock_dt:
2025-03-04 21:14:53 -08:00
mock_dt.now.return_value = datetime(
2025, 2, 25, 12, 0, 0, tzinfo=ZoneInfo("America/Phoenix")
)
local_stats = get_local_daily_stats_for_last_x_days(
stats_utc, "America/Phoenix", days=1
)
assert len(local_stats) == 1
(day_key,) = local_stats.keys()
sms_delivered = local_stats[day_key]["sms"]["delivered"]
assert sms_delivered in (2, 3, 4)
def test_local_conversion_honolulu():
"""Test aggregator logic in Hawaii (Pacific/Honolulu)."""
stats_utc = {
"2025-02-25T03:00:00Z": {
2025-03-04 21:14:53 -08:00
"sms": {"delivered": 2, "failure": 0, "pending": 0, "requested": 2},
"email": {"delivered": 0, "failure": 0, "pending": 0, "requested": 0},
},
"2025-02-25T21:00:00Z": {
2025-03-04 21:14:53 -08:00
"sms": {"delivered": 1, "failure": 0, "pending": 0, "requested": 1},
"email": {"delivered": 2, "failure": 0, "pending": 0, "requested": 2},
},
}
with patch("app.main.views.dashboard.datetime", wraps=datetime) as mock_dt:
2025-03-04 21:14:53 -08:00
mock_dt.now.return_value = datetime(
2025, 2, 25, 12, 0, 0, tzinfo=ZoneInfo("Pacific/Honolulu")
)
local_stats = get_local_daily_stats_for_last_x_days(
stats_utc, "Pacific/Honolulu", days=1
)
assert len(local_stats) == 1
(day_key,) = local_stats.keys()
total_requested = local_stats[day_key]["sms"]["requested"]
assert total_requested in (3, 1, 4)