notify-api-412 use black to enforce python coding style

This commit is contained in:
Kenneth Kehl
2023-08-25 09:12:23 -07:00
parent c6eb007386
commit 8c9721d8e2
201 changed files with 31660 additions and 28105 deletions

View File

@@ -8,48 +8,58 @@ from tests import organization_json
from tests.conftest import create_email_branding
@pytest.mark.parametrize('function', [get_email_choices])
@pytest.mark.parametrize('org_type, expected_options', [
('federal', []),
('state', []),
])
@pytest.mark.parametrize("function", [get_email_choices])
@pytest.mark.parametrize(
"org_type, expected_options",
[
("federal", []),
("state", []),
],
)
def test_get_choices_service_not_assigned_to_org(
service_one,
function,
org_type,
expected_options,
):
service_one['organization_type'] = org_type
service_one["organization_type"] = org_type
service = Service(service_one)
options = function(service)
assert list(options) == expected_options
@pytest.mark.parametrize('org_type, branding_id, expected_options', [
('federal', None, [
('govuk_and_org', 'GOV.UK and Test Organization'),
('organization', 'Test Organization'),
]),
('federal', 'some-branding-id', [
('govuk', 'GOV.UK'), # central orgs can switch back to gsa.gov
('govuk_and_org', 'GOV.UK and Test Organization'),
('organization', 'Test Organization'),
]),
('state', None, [
('organization', 'Test Organization')
]),
('state', 'some-branding-id', [
('organization', 'Test Organization')
]),
# ('nhs_central', None, [
# ('nhs', 'NHS')
# ]),
# ('nhs_central', NHS_EMAIL_BRANDING_ID, [
# # don't show NHS if it's the current branding
# ]),
])
@pytest.mark.skip(reason='Update for TTS')
@pytest.mark.parametrize(
"org_type, branding_id, expected_options",
[
(
"federal",
None,
[
("govuk_and_org", "GOV.UK and Test Organization"),
("organization", "Test Organization"),
],
),
(
"federal",
"some-branding-id",
[
("govuk", "GOV.UK"), # central orgs can switch back to gsa.gov
("govuk_and_org", "GOV.UK and Test Organization"),
("organization", "Test Organization"),
],
),
("state", None, [("organization", "Test Organization")]),
("state", "some-branding-id", [("organization", "Test Organization")]),
# ('nhs_central', None, [
# ('nhs', 'NHS')
# ]),
# ('nhs_central', NHS_EMAIL_BRANDING_ID, [
# # don't show NHS if it's the current branding
# ]),
],
)
@pytest.mark.skip(reason="Update for TTS")
def test_get_email_choices_service_assigned_to_org(
mocker,
service_one,
@@ -57,37 +67,52 @@ def test_get_email_choices_service_assigned_to_org(
branding_id,
expected_options,
mock_get_service_organization,
mock_get_email_branding
mock_get_email_branding,
):
service = Service(service_one)
mocker.patch(
'app.organizations_client.get_organization',
return_value=organization_json(organization_type=org_type)
"app.organizations_client.get_organization",
return_value=organization_json(organization_type=org_type),
)
mocker.patch(
'app.models.service.Service.email_branding_id',
"app.models.service.Service.email_branding_id",
new_callable=PropertyMock,
return_value=branding_id
return_value=branding_id,
)
options = get_email_choices(service)
assert list(options) == expected_options
@pytest.mark.parametrize('org_type, branding_id, expected_options', [
('federal', 'some-branding-id', [
# don't show gsa.gov options as org default supersedes it
('organization', 'Test Organization'),
]),
('federal', 'org-branding-id', [
# also don't show org option if it's the current branding
]),
('state', 'org-branding-id', [
# don't show org option if it's the current branding
]),
])
@pytest.mark.skip(reason='Update for TTS')
@pytest.mark.parametrize(
"org_type, branding_id, expected_options",
[
(
"federal",
"some-branding-id",
[
# don't show gsa.gov options as org default supersedes it
("organization", "Test Organization"),
],
),
(
"federal",
"org-branding-id",
[
# also don't show org option if it's the current branding
],
),
(
"state",
"org-branding-id",
[
# don't show org option if it's the current branding
],
),
],
)
@pytest.mark.skip(reason="Update for TTS")
def test_get_email_choices_org_has_default_branding(
mocker,
service_one,
@@ -95,39 +120,47 @@ def test_get_email_choices_org_has_default_branding(
branding_id,
expected_options,
mock_get_service_organization,
mock_get_email_branding
mock_get_email_branding,
):
service = Service(service_one)
mocker.patch(
'app.organizations_client.get_organization',
"app.organizations_client.get_organization",
return_value=organization_json(
organization_type=org_type,
email_branding_id='org-branding-id'
)
organization_type=org_type, email_branding_id="org-branding-id"
),
)
mocker.patch(
'app.models.service.Service.email_branding_id',
"app.models.service.Service.email_branding_id",
new_callable=PropertyMock,
return_value=branding_id
return_value=branding_id,
)
options = get_email_choices(service)
assert list(options) == expected_options
@pytest.mark.parametrize('branding_name, expected_options', [
('gsa.gov and something else', [
('govuk', 'GOV.UK'),
('govuk_and_org', 'GOV.UK and Test Organization'),
('organization', 'Test Organization'),
]),
('gsa.gov and test OrganisatioN', [
('govuk', 'GOV.UK'),
('organization', 'Test Organization'),
])
])
@pytest.mark.skip(reason='Update for TTS')
@pytest.mark.parametrize(
"branding_name, expected_options",
[
(
"gsa.gov and something else",
[
("govuk", "GOV.UK"),
("govuk_and_org", "GOV.UK and Test Organization"),
("organization", "Test Organization"),
],
),
(
"gsa.gov and test OrganisatioN",
[
("govuk", "GOV.UK"),
("organization", "Test Organization"),
],
),
],
)
@pytest.mark.skip(reason="Update for TTS")
def test_get_email_choices_branding_name_in_use(
mocker,
service_one,
@@ -138,17 +171,17 @@ def test_get_email_choices_branding_name_in_use(
service = Service(service_one)
mocker.patch(
'app.organizations_client.get_organization',
return_value=organization_json(organization_type='central')
"app.organizations_client.get_organization",
return_value=organization_json(organization_type="central"),
)
mocker.patch(
'app.models.service.Service.email_branding_id',
"app.models.service.Service.email_branding_id",
new_callable=PropertyMock,
return_value='some-branding-id',
return_value="some-branding-id",
)
mocker.patch(
'app.email_branding_client.get_email_branding',
return_value=create_email_branding('_id', {'name': branding_name})
"app.email_branding_client.get_email_branding",
return_value=create_email_branding("_id", {"name": branding_name}),
)
options = get_email_choices(service)

View File

@@ -10,19 +10,18 @@ from tests.conftest import fake_uuid
def _get_notifications_csv(
row_number=1,
recipient='foo@bar.com',
template_name='foo',
template_type='sms',
job_name='bar.csv',
status='Delivered',
created_at='1943-04-19 12:00:00',
recipient="foo@bar.com",
template_name="foo",
template_type="sms",
job_name="bar.csv",
status="Delivered",
created_at="1943-04-19 12:00:00",
rows=1,
with_links=False,
job_id=fake_uuid,
created_by_name=None,
created_by_email_address=None,
):
def _get(
service_id,
page=1,
@@ -32,30 +31,33 @@ def _get_notifications_csv(
links = {}
if with_links:
links = {
'prev': '/service/{}/notifications?page=0'.format(service_id),
'next': '/service/{}/notifications?page=1'.format(service_id),
'last': '/service/{}/notifications?page=2'.format(service_id)
"prev": "/service/{}/notifications?page=0".format(service_id),
"next": "/service/{}/notifications?page=1".format(service_id),
"last": "/service/{}/notifications?page=2".format(service_id),
}
data = {
'notifications': [{
"row_number": row_number + i,
"to": recipient,
"recipient": recipient,
"client_reference": 'ref 1234',
"template_name": template_name,
"template_type": template_type,
"template": {"name": template_name, "template_type": template_type},
"job_name": job_name,
"status": status,
"created_at": created_at,
"updated_at": None,
"created_by_name": created_by_name,
"created_by_email_address": created_by_email_address,
} for i in range(rows)],
'total': rows,
'page_size': 50,
'links': links
"notifications": [
{
"row_number": row_number + i,
"to": recipient,
"recipient": recipient,
"client_reference": "ref 1234",
"template_name": template_name,
"template_type": template_type,
"template": {"name": template_name, "template_type": template_type},
"job_name": job_name,
"status": status,
"created_at": created_at,
"updated_at": None,
"created_by_name": created_by_name,
"created_by_email_address": created_by_email_address,
}
for i in range(rows)
],
"total": rows,
"page_size": 50,
"links": links,
}
return data
@@ -63,31 +65,36 @@ def _get_notifications_csv(
return _get
@pytest.fixture(scope='function')
@pytest.fixture(scope="function")
def _get_notifications_csv_mock(
mocker,
api_user_active,
):
return mocker.patch(
'app.notification_api_client.get_notifications_for_service',
side_effect=_get_notifications_csv()
"app.notification_api_client.get_notifications_for_service",
side_effect=_get_notifications_csv(),
)
@pytest.mark.parametrize('created_by_name, expected_content', [
(
None, [
'Recipient,Template,Type,Sent by,Job,Status,Time\n',
'foo@bar.com,foo,sms,,,Delivered,1943-04-19 12:00:00\r\n',
]
),
(
'Anne Example', [
'Recipient,Template,Type,Sent by,Job,Status,Time\n',
'foo@bar.com,foo,sms,Anne Example,,Delivered,1943-04-19 12:00:00\r\n',
]
),
])
@pytest.mark.parametrize(
"created_by_name, expected_content",
[
(
None,
[
"Recipient,Template,Type,Sent by,Job,Status,Time\n",
"foo@bar.com,foo,sms,,,Delivered,1943-04-19 12:00:00\r\n",
],
),
(
"Anne Example",
[
"Recipient,Template,Type,Sent by,Job,Status,Time\n",
"foo@bar.com,foo,sms,Anne Example,,Delivered,1943-04-19 12:00:00\r\n",
],
),
],
)
def test_generate_notifications_csv_without_job(
notify_admin,
mocker,
@@ -95,43 +102,98 @@ def test_generate_notifications_csv_without_job(
expected_content,
):
mocker.patch(
'app.notification_api_client.get_notifications_for_service',
"app.notification_api_client.get_notifications_for_service",
side_effect=_get_notifications_csv(
created_by_name=created_by_name,
created_by_email_address="sender@email.gsa.gov",
job_id=None,
job_name=None
)
job_name=None,
),
)
assert list(generate_notifications_csv(service_id=fake_uuid)) == expected_content
@pytest.mark.parametrize('original_file_contents, expected_column_headers, expected_1st_row', [
(
"""
@pytest.mark.parametrize(
"original_file_contents, expected_column_headers, expected_1st_row",
[
(
"""
phone_number
2028675309
""",
['Row number', 'phone_number', 'Template', 'Type', 'Job', 'Status', 'Time'],
['1', '2028675309', 'foo', 'sms', 'bar.csv', 'Delivered', '1943-04-19 12:00:00'],
),
(
"""
["Row number", "phone_number", "Template", "Type", "Job", "Status", "Time"],
[
"1",
"2028675309",
"foo",
"sms",
"bar.csv",
"Delivered",
"1943-04-19 12:00:00",
],
),
(
"""
phone_number, a, b, c
2028675309, 🐜,🐝,🦀
""",
['Row number', 'phone_number', 'a', 'b', 'c', 'Template', 'Type', 'Job', 'Status', 'Time'],
['1', '2028675309', '🐜', '🐝', '🦀', 'foo', 'sms', 'bar.csv', 'Delivered', '1943-04-19 12:00:00'],
),
(
"""
[
"Row number",
"phone_number",
"a",
"b",
"c",
"Template",
"Type",
"Job",
"Status",
"Time",
],
[
"1",
"2028675309",
"🐜",
"🐝",
"🦀",
"foo",
"sms",
"bar.csv",
"Delivered",
"1943-04-19 12:00:00",
],
),
(
"""
"phone_number", "a", "b", "c"
"2028675309","🐜,🐜","🐝,🐝","🦀"
""",
['Row number', 'phone_number', 'a', 'b', 'c', 'Template', 'Type', 'Job', 'Status', 'Time'],
['1', '2028675309', '🐜,🐜', '🐝,🐝', '🦀', 'foo', 'sms', 'bar.csv', 'Delivered', '1943-04-19 12:00:00'],
),
])
[
"Row number",
"phone_number",
"a",
"b",
"c",
"Template",
"Type",
"Job",
"Status",
"Time",
],
[
"1",
"2028675309",
"🐜,🐜",
"🐝,🐝",
"🦀",
"foo",
"sms",
"bar.csv",
"Delivered",
"1943-04-19 12:00:00",
],
),
],
)
def test_generate_notifications_csv_returns_correct_csv_file(
notify_admin,
mocker,
@@ -141,11 +203,13 @@ def test_generate_notifications_csv_returns_correct_csv_file(
expected_1st_row,
):
mocker.patch(
'app.s3_client.s3_csv_client.s3download',
"app.s3_client.s3_csv_client.s3download",
return_value=original_file_contents,
)
csv_content = generate_notifications_csv(service_id='1234', job_id=fake_uuid, template_type='sms')
csv_file = DictReader(StringIO('\n'.join(csv_content)))
csv_content = generate_notifications_csv(
service_id="1234", job_id=fake_uuid, template_type="sms"
)
csv_file = DictReader(StringIO("\n".join(csv_content)))
assert csv_file.fieldnames == expected_column_headers
assert next(csv_file) == dict(zip(expected_column_headers, expected_1st_row))
@@ -154,7 +218,7 @@ def test_generate_notifications_csv_only_calls_once_if_no_next_link(
notify_admin,
_get_notifications_csv_mock,
):
list(generate_notifications_csv(service_id='1234'))
list(generate_notifications_csv(service_id="1234"))
assert _get_notifications_csv_mock.call_count == 1
@@ -165,9 +229,8 @@ def test_generate_notifications_csv_calls_twice_if_next_link(
mocker,
job_id,
):
mocker.patch(
'app.s3_client.s3_csv_client.s3download',
"app.s3_client.s3_csv_client.s3download",
return_value="""
phone_number
2028675304
@@ -180,45 +243,47 @@ def test_generate_notifications_csv_calls_twice_if_next_link(
2028675307
2028675308
2028675309
"""
""",
)
service_id = '1234'
service_id = "1234"
response_with_links = _get_notifications_csv(rows=7, with_links=True)
response_with_no_links = _get_notifications_csv(rows=3, row_number=8, with_links=False)
response_with_no_links = _get_notifications_csv(
rows=3, row_number=8, with_links=False
)
mock_get_notifications = mocker.patch(
'app.notification_api_client.get_notifications_for_service',
"app.notification_api_client.get_notifications_for_service",
side_effect=[
response_with_links(service_id),
response_with_no_links(service_id),
]
],
)
csv_content = generate_notifications_csv(
service_id=service_id,
job_id=job_id or fake_uuid,
template_type='sms',
template_type="sms",
)
csv = list(DictReader(StringIO('\n'.join(csv_content))))
csv = list(DictReader(StringIO("\n".join(csv_content))))
assert len(csv) == 10
assert csv[0]['phone_number'] == '2028675304'
assert csv[9]['phone_number'] == '2028675309'
assert csv[0]["phone_number"] == "2028675304"
assert csv[9]["phone_number"] == "2028675309"
assert mock_get_notifications.call_count == 2
# mock_calls[0][2] is the kwargs from first call
assert mock_get_notifications.mock_calls[0][2]['page'] == 1
assert mock_get_notifications.mock_calls[1][2]['page'] == 2
assert mock_get_notifications.mock_calls[0][2]["page"] == 1
assert mock_get_notifications.mock_calls[1][2]["page"] == 2
MockRecipients = namedtuple(
'RecipientCSV',
"RecipientCSV",
[
'rows_with_bad_recipients',
'rows_with_missing_data',
'rows_with_message_too_long',
'rows_with_empty_message'
]
"rows_with_bad_recipients",
"rows_with_missing_data",
"rows_with_message_too_long",
"rows_with_empty_message",
],
)
@@ -226,85 +291,64 @@ MockRecipients = namedtuple(
"rows_with_bad_recipients, rows_with_missing_data, "
"rows_with_message_too_long, rows_with_empty_message, template_type, expected_errors",
[
([], [], [], [], "sms", []),
({2}, [], [], [], "sms", ["fix 1 phone number"]),
({2, 4, 6}, [], [], [], "sms", ["fix 3 phone numbers"]),
({1}, [], [], [], "email", ["fix 1 email address"]),
({2, 4, 6}, [], [], [], "email", ["fix 3 email addresses"]),
(
[], [], [], [],
'sms',
[]
{2},
{3},
[],
[],
"sms",
["fix 1 phone number", "enter missing data in 1 row"],
),
(
{2}, [], [], [],
'sms',
['fix 1 phone number']
{2, 4, 6, 8},
{3, 6, 9, 12},
[],
[],
"sms",
["fix 4 phone numbers", "enter missing data in 4 rows"],
),
({}, {}, {3}, [], "sms", ["shorten the message in 1 row"]),
({}, {}, {3, 12}, [], "sms", ["shorten the messages in 2 rows"]),
(
{},
{},
{},
{2},
"sms",
["check you have content for the empty message in 1 row"],
),
(
{2, 4, 6}, [], [], [],
'sms',
['fix 3 phone numbers']
{},
{},
{},
{2, 4, 8},
"sms",
["check you have content for the empty messages in 3 rows"],
),
(
{1}, [], [], [],
'email',
['fix 1 email address']
),
(
{2, 4, 6}, [], [], [],
'email',
['fix 3 email addresses']
),
(
{2}, {3}, [], [],
'sms',
[
'fix 1 phone number',
'enter missing data in 1 row'
]
),
(
{2, 4, 6, 8}, {3, 6, 9, 12}, [], [],
'sms',
[
'fix 4 phone numbers',
'enter missing data in 4 rows'
]
),
(
{}, {}, {3}, [],
'sms',
[
'shorten the message in 1 row'
]
),
(
{}, {}, {3, 12}, [],
'sms',
[
'shorten the messages in 2 rows'
]
),
(
{}, {}, {}, {2},
'sms',
[
'check you have content for the empty message in 1 row'
]
),
(
{}, {}, {}, {2, 4, 8},
'sms',
[
'check you have content for the empty messages in 3 rows'
]
),
]
],
)
def test_get_errors_for_csv(
rows_with_bad_recipients, rows_with_missing_data, rows_with_message_too_long, rows_with_empty_message,
rows_with_bad_recipients,
rows_with_missing_data,
rows_with_message_too_long,
rows_with_empty_message,
template_type,
expected_errors
expected_errors,
):
assert get_errors_for_csv(
MockRecipients(
rows_with_bad_recipients, rows_with_missing_data, rows_with_message_too_long, rows_with_empty_message
),
template_type
) == expected_errors
assert (
get_errors_for_csv(
MockRecipients(
rows_with_bad_recipients,
rows_with_missing_data,
rows_with_message_too_long,
rows_with_empty_message,
),
template_type,
)
== expected_errors
)

View File

@@ -5,15 +5,18 @@ from app.models.user import User
from app.utils.login import email_needs_revalidating
@freeze_time('2020-11-27T12:00:00')
@pytest.mark.parametrize(('email_access_validated_at', 'expected_result'), (
('2020-10-01T11:35:21.726132Z', False),
('2020-07-23T11:35:21.726132Z', True),
))
@freeze_time("2020-11-27T12:00:00")
@pytest.mark.parametrize(
("email_access_validated_at", "expected_result"),
(
("2020-10-01T11:35:21.726132Z", False),
("2020-07-23T11:35:21.726132Z", True),
),
)
def test_email_needs_revalidating(
api_user_active,
email_access_validated_at,
expected_result,
):
api_user_active['email_access_validated_at'] = email_access_validated_at
api_user_active["email_access_validated_at"] = email_access_validated_at
assert email_needs_revalidating(User(api_user_active)) == expected_result

View File

@@ -2,19 +2,21 @@ from app.utils.pagination import generate_next_dict, generate_previous_dict
def test_generate_previous_dict(client_request):
result = generate_previous_dict('main.view_jobs', 'foo', 2, {})
assert 'page=1' in result['url']
assert result['title'] == 'Previous page'
assert result['label'] == 'page 1'
result = generate_previous_dict("main.view_jobs", "foo", 2, {})
assert "page=1" in result["url"]
assert result["title"] == "Previous page"
assert result["label"] == "page 1"
def test_generate_next_dict(client_request):
result = generate_next_dict('main.view_jobs', 'foo', 2, {})
assert 'page=3' in result['url']
assert result['title'] == 'Next page'
assert result['label'] == 'page 3'
result = generate_next_dict("main.view_jobs", "foo", 2, {})
assert "page=3" in result["url"]
assert result["title"] == "Next page"
assert result["label"] == "page 3"
def test_generate_previous_next_dict_adds_other_url_args(client_request):
result = generate_next_dict('main.view_notifications', 'foo', 2, {'message_type': 'blah'})
assert 'notifications/blah' in result['url']
result = generate_next_dict(
"main.view_notifications", "foo", 2, {"message_type": "blah"}
)
assert "notifications/blah" in result["url"]

View File

@@ -4,22 +4,28 @@ from freezegun import freeze_time
from app.utils.time import get_current_financial_year, is_less_than_days_ago
@pytest.mark.parametrize("date_from_db, expected_result", [
('2019-11-17T11:35:21.726132Z', True),
('2019-11-16T11:35:21.726132Z', False),
('2019-11-16T11:35:21+0000', False),
])
@freeze_time('2020-02-14T12:00:00')
@pytest.mark.parametrize(
"date_from_db, expected_result",
[
("2019-11-17T11:35:21.726132Z", True),
("2019-11-16T11:35:21.726132Z", False),
("2019-11-16T11:35:21+0000", False),
],
)
@freeze_time("2020-02-14T12:00:00")
def test_is_less_than_days_ago(date_from_db, expected_result):
assert is_less_than_days_ago(date_from_db, 90) == expected_result
@pytest.mark.parametrize('datetime_string, financial_year', (
('2021-01-01T00:00:00+00:00', 2020), # Start of 2021
('2021-04-01T03:59:59+00:00', 2020), # One minute before midnight (BST)
('2021-10-01T04:05:00+00:00', 2021), # Midnight (BST)
('2021-12-12T12:12:12+01:00', 2021), # Later in the year
))
@pytest.mark.parametrize(
"datetime_string, financial_year",
(
("2021-01-01T00:00:00+00:00", 2020), # Start of 2021
("2021-04-01T03:59:59+00:00", 2020), # One minute before midnight (BST)
("2021-10-01T04:05:00+00:00", 2021), # Midnight (BST)
("2021-12-12T12:12:12+01:00", 2021), # Later in the year
),
)
def test_get_financial_year(datetime_string, financial_year):
with freeze_time(datetime_string):
assert get_current_financial_year() == financial_year

View File

@@ -5,32 +5,39 @@ from werkzeug.exceptions import Forbidden
from app.utils.user import user_has_permissions
@pytest.mark.parametrize('permissions', (
[
# Route has one of the permissions which the user has
'manage_service'
],
[
# Route has more than one of the permissions which the user has
'manage_templates', 'manage_service'
],
[
# Route has one of the permissions which the user has, and one they do not
'manage_service', 'send_messages',
],
[
# Route has no specific permissions required
],
))
@pytest.mark.parametrize(
"permissions",
(
[
# Route has one of the permissions which the user has
"manage_service"
],
[
# Route has more than one of the permissions which the user has
"manage_templates",
"manage_service",
],
[
# Route has one of the permissions which the user has, and one they do not
"manage_service",
"send_messages",
],
[
# Route has no specific permissions required
],
),
)
def test_permissions(
client_request,
permissions,
api_user_active,
):
request.view_args.update({'service_id': 'foo'})
request.view_args.update({"service_id": "foo"})
api_user_active['permissions'] = {'foo': ['manage_users', 'manage_templates', 'manage_settings']}
api_user_active['services'] = ['foo', 'bar']
api_user_active["permissions"] = {
"foo": ["manage_users", "manage_templates", "manage_settings"]
}
api_user_active["services"] = ["foo", "bar"]
client_request.login(api_user_active)
@@ -41,21 +48,26 @@ def test_permissions(
index()
@pytest.mark.parametrize('permissions', (
[
# Route has a permission which the user doesnt have
'send_messages'
],
))
@pytest.mark.parametrize(
"permissions",
(
[
# Route has a permission which the user doesnt have
"send_messages"
],
),
)
def test_permissions_forbidden(
client_request,
permissions,
api_user_active,
):
request.view_args.update({'service_id': 'foo'})
request.view_args.update({"service_id": "foo"})
api_user_active['permissions'] = {'foo': ['manage_users', 'manage_templates', 'manage_settings']}
api_user_active['services'] = ['foo', 'bar']
api_user_active["permissions"] = {
"foo": ["manage_users", "manage_templates", "manage_settings"]
}
api_user_active["services"] = ["foo", "bar"]
client_request.login(api_user_active)
@@ -71,7 +83,7 @@ def test_restrict_admin_usage(
client_request,
platform_admin_user,
):
request.view_args.update({'service_id': 'foo'})
request.view_args.update({"service_id": "foo"})
client_request.login(platform_admin_user)
@user_has_permissions(restrict_admin_usage=True)
@@ -82,9 +94,7 @@ def test_restrict_admin_usage(
index()
def test_no_user_returns_redirect_to_sign_in(
client_request
):
def test_no_user_returns_redirect_to_sign_in(client_request):
client_request.logout()
@user_has_permissions()
@@ -93,17 +103,17 @@ def test_no_user_returns_redirect_to_sign_in(
response = index()
assert response.status_code == 302
assert response.location.startswith('/sign-in?next=')
assert response.location.startswith("/sign-in?next=")
def test_user_has_permissions_for_organization(
client_request,
api_user_active,
):
api_user_active['organizations'] = ['org_1', 'org_2']
api_user_active["organizations"] = ["org_1", "org_2"]
client_request.login(api_user_active)
request.view_args = {'org_id': 'org_2'}
request.view_args = {"org_id": "org_2"}
@user_has_permissions()
def index():
@@ -116,10 +126,10 @@ def test_platform_admin_can_see_orgs_they_dont_have(
client_request,
platform_admin_user,
):
platform_admin_user['organizations'] = []
platform_admin_user["organizations"] = []
client_request.login(platform_admin_user)
request.view_args = {'org_id': 'org_2'}
request.view_args = {"org_id": "org_2"}
@user_has_permissions()
def index():
@@ -148,10 +158,10 @@ def test_user_doesnt_have_permissions_for_organization(
client_request,
api_user_active,
):
api_user_active['organizations'] = ['org_1', 'org_2']
api_user_active["organizations"] = ["org_1", "org_2"]
client_request.login(api_user_active)
request.view_args = {'org_id': 'org_3'}
request.view_args = {"org_id": "org_3"}
@user_has_permissions()
def index():
@@ -165,10 +175,12 @@ def test_user_with_no_permissions_to_service_goes_to_templates(
client_request,
api_user_active,
):
api_user_active['permissions'] = {'foo': ['manage_users', 'manage_templates', 'manage_settings']}
api_user_active['services'] = ['foo', 'bar']
api_user_active["permissions"] = {
"foo": ["manage_users", "manage_templates", "manage_settings"]
}
api_user_active["services"] = ["foo", "bar"]
client_request.login(api_user_active)
request.view_args = {'service_id': 'bar'}
request.view_args = {"service_id": "bar"}
@user_has_permissions()
def index():

View File

@@ -6,16 +6,24 @@ from app.utils.user_permissions import (
)
@pytest.mark.parametrize('db_permissions,expected_ui_permissions', [
(
['manage_templates'],
{'manage_templates'},
),
(
['send_texts', 'send_emails', 'manage_templates', 'some_unknown_permission'],
{'send_messages', 'manage_templates', 'some_unknown_permission'},
),
])
@pytest.mark.parametrize(
"db_permissions,expected_ui_permissions",
[
(
["manage_templates"],
{"manage_templates"},
),
(
[
"send_texts",
"send_emails",
"manage_templates",
"some_unknown_permission",
],
{"send_messages", "manage_templates", "some_unknown_permission"},
),
],
)
def test_translate_permissions_from_db_to_ui(
db_permissions,
expected_ui_permissions,
@@ -25,9 +33,12 @@ def test_translate_permissions_from_db_to_ui(
def test_translate_permissions_from_ui_to_db():
ui_permissions = ['send_messages', 'manage_templates', 'some_unknown_permission']
ui_permissions = ["send_messages", "manage_templates", "some_unknown_permission"]
db_permissions = translate_permissions_from_ui_to_db(ui_permissions)
assert db_permissions == {
'send_texts', 'send_emails', 'manage_templates', 'some_unknown_permission'
"send_texts",
"send_emails",
"manage_templates",
"some_unknown_permission",
}