mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-24 16:24:08 -04:00
notify-api-412 use black to enforce python coding style
This commit is contained in:
@@ -4,35 +4,34 @@ from app.models import JSONModel
|
||||
|
||||
|
||||
def test_looks_up_from_dict():
|
||||
|
||||
class Custom(JSONModel):
|
||||
ALLOWED_PROPERTIES = {'foo'}
|
||||
ALLOWED_PROPERTIES = {"foo"}
|
||||
|
||||
assert Custom({'foo': 'bar'}).foo == 'bar'
|
||||
assert Custom({"foo": "bar"}).foo == "bar"
|
||||
|
||||
|
||||
def test_raises_when_overriding_custom_properties():
|
||||
|
||||
class Custom(JSONModel):
|
||||
|
||||
ALLOWED_PROPERTIES = {'foo'}
|
||||
ALLOWED_PROPERTIES = {"foo"}
|
||||
|
||||
@property
|
||||
def foo(self):
|
||||
pass
|
||||
|
||||
with pytest.raises(AttributeError) as e:
|
||||
Custom({'foo': 'NOPE'})
|
||||
Custom({"foo": "NOPE"})
|
||||
|
||||
assert str(e.value) == "can't set attribute"
|
||||
|
||||
|
||||
@pytest.mark.parametrize('json_response', (
|
||||
{},
|
||||
{'foo': 'bar'}, # Should still raise an exception
|
||||
))
|
||||
@pytest.mark.parametrize(
|
||||
"json_response",
|
||||
(
|
||||
{},
|
||||
{"foo": "bar"}, # Should still raise an exception
|
||||
),
|
||||
)
|
||||
def test_model_raises_for_unknown_attributes(json_response):
|
||||
|
||||
class Custom(JSONModel):
|
||||
ALLOWED_PROPERTIES = set()
|
||||
|
||||
@@ -46,9 +45,8 @@ def test_model_raises_for_unknown_attributes(json_response):
|
||||
|
||||
|
||||
def test_model_raises_keyerror_if_item_missing_from_dict():
|
||||
|
||||
class Custom(JSONModel):
|
||||
ALLOWED_PROPERTIES = {'foo'}
|
||||
ALLOWED_PROPERTIES = {"foo"}
|
||||
|
||||
with pytest.raises(AttributeError) as e:
|
||||
Custom({}).foo
|
||||
@@ -56,30 +54,31 @@ def test_model_raises_keyerror_if_item_missing_from_dict():
|
||||
assert str(e.value) == "'Custom' object has no attribute 'foo'"
|
||||
|
||||
|
||||
@pytest.mark.parametrize('json_response', (
|
||||
{},
|
||||
{'foo': 'bar'}, # Should be ignored
|
||||
))
|
||||
@pytest.mark.parametrize(
|
||||
"json_response",
|
||||
(
|
||||
{},
|
||||
{"foo": "bar"}, # Should be ignored
|
||||
),
|
||||
)
|
||||
def test_model_doesnt_swallow_attribute_errors(json_response):
|
||||
|
||||
class Custom(JSONModel):
|
||||
ALLOWED_PROPERTIES = set()
|
||||
|
||||
@property
|
||||
def foo(self):
|
||||
raise AttributeError('Something has gone wrong')
|
||||
raise AttributeError("Something has gone wrong")
|
||||
|
||||
with pytest.raises(AttributeError) as e:
|
||||
Custom(json_response).foo
|
||||
|
||||
assert str(e.value) == 'Something has gone wrong'
|
||||
assert str(e.value) == "Something has gone wrong"
|
||||
|
||||
|
||||
def test_dynamic_properties_are_introspectable():
|
||||
|
||||
class Custom(JSONModel):
|
||||
ALLOWED_PROPERTIES = {'foo', 'bar', 'baz'}
|
||||
ALLOWED_PROPERTIES = {"foo", "bar", "baz"}
|
||||
|
||||
model = Custom({'foo': None, 'bar': None, 'baz': None})
|
||||
model = Custom({"foo": None, "bar": None, "baz": None})
|
||||
|
||||
assert dir(model)[-3:] == ['bar', 'baz', 'foo']
|
||||
assert dir(model)[-3:] == ["bar", "baz", "foo"]
|
||||
|
||||
@@ -4,62 +4,74 @@ from app.models.event import ServiceEvent
|
||||
from tests.conftest import sample_uuid
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key, value_from, value_to, expected', (
|
||||
('restricted', True, False, (
|
||||
'Made this service live'
|
||||
)),
|
||||
('restricted', False, True, (
|
||||
'Put this service back into trial mode'
|
||||
)),
|
||||
('active', False, True, (
|
||||
'Unsuspended this service'
|
||||
)),
|
||||
('active', True, False, (
|
||||
'Deleted this service'
|
||||
)),
|
||||
('contact_link', 'x', 'y', (
|
||||
'Set the contact details for this service to ‘y’'
|
||||
)),
|
||||
('email_branding', 'foo', 'bar', (
|
||||
'Updated this service’s email branding'
|
||||
)),
|
||||
('inbound_api', 'foo', 'bar', (
|
||||
'Updated the callback for received text messages'
|
||||
)),
|
||||
('message_limit', 1, 2, (
|
||||
'Increased this service’s daily message limit from 1 to 2'
|
||||
)),
|
||||
('message_limit', 2, 1, (
|
||||
'Reduced this service’s daily message limit from 2 to 1'
|
||||
)),
|
||||
('name', 'Old', 'New', (
|
||||
'Renamed this service from ‘Old’ to ‘New’'
|
||||
)),
|
||||
('permissions', ['a', 'b', 'c'], ['a', 'b', 'c', 'd'], (
|
||||
'Added ‘d’ to this service’s permissions'
|
||||
)),
|
||||
('permissions', ['a', 'b', 'c'], ['a', 'b'], (
|
||||
'Removed ‘c’ from this service’s permissions'
|
||||
)),
|
||||
('permissions', ['a', 'b', 'c'], ['c', 'd', 'e'], (
|
||||
'Removed ‘a’ and ‘b’ from this service’s permissions, added ‘d’ and ‘e’'
|
||||
)),
|
||||
('prefix_sms', True, False, (
|
||||
'Set text messages to not start with the name of this service'
|
||||
)),
|
||||
('prefix_sms', False, True, (
|
||||
'Set text messages to start with the name of this service'
|
||||
)),
|
||||
('research_mode', True, False, (
|
||||
'Took this service out of research mode'
|
||||
)),
|
||||
('research_mode', False, True, (
|
||||
'Put this service into research mode'
|
||||
)),
|
||||
('service_callback_api', 'foo', 'bar', (
|
||||
'Updated the callback for delivery receipts'
|
||||
)),
|
||||
))
|
||||
@pytest.mark.parametrize(
|
||||
"key, value_from, value_to, expected",
|
||||
(
|
||||
("restricted", True, False, ("Made this service live")),
|
||||
("restricted", False, True, ("Put this service back into trial mode")),
|
||||
("active", False, True, ("Unsuspended this service")),
|
||||
("active", True, False, ("Deleted this service")),
|
||||
("contact_link", "x", "y", ("Set the contact details for this service to ‘y’")),
|
||||
("email_branding", "foo", "bar", ("Updated this service’s email branding")),
|
||||
(
|
||||
"inbound_api",
|
||||
"foo",
|
||||
"bar",
|
||||
("Updated the callback for received text messages"),
|
||||
),
|
||||
(
|
||||
"message_limit",
|
||||
1,
|
||||
2,
|
||||
("Increased this service’s daily message limit from 1 to 2"),
|
||||
),
|
||||
(
|
||||
"message_limit",
|
||||
2,
|
||||
1,
|
||||
("Reduced this service’s daily message limit from 2 to 1"),
|
||||
),
|
||||
("name", "Old", "New", ("Renamed this service from ‘Old’ to ‘New’")),
|
||||
(
|
||||
"permissions",
|
||||
["a", "b", "c"],
|
||||
["a", "b", "c", "d"],
|
||||
("Added ‘d’ to this service’s permissions"),
|
||||
),
|
||||
(
|
||||
"permissions",
|
||||
["a", "b", "c"],
|
||||
["a", "b"],
|
||||
("Removed ‘c’ from this service’s permissions"),
|
||||
),
|
||||
(
|
||||
"permissions",
|
||||
["a", "b", "c"],
|
||||
["c", "d", "e"],
|
||||
("Removed ‘a’ and ‘b’ from this service’s permissions, added ‘d’ and ‘e’"),
|
||||
),
|
||||
(
|
||||
"prefix_sms",
|
||||
True,
|
||||
False,
|
||||
("Set text messages to not start with the name of this service"),
|
||||
),
|
||||
(
|
||||
"prefix_sms",
|
||||
False,
|
||||
True,
|
||||
("Set text messages to start with the name of this service"),
|
||||
),
|
||||
("research_mode", True, False, ("Took this service out of research mode")),
|
||||
("research_mode", False, True, ("Put this service into research mode")),
|
||||
(
|
||||
"service_callback_api",
|
||||
"foo",
|
||||
"bar",
|
||||
("Updated the callback for delivery receipts"),
|
||||
),
|
||||
),
|
||||
)
|
||||
def test_service_event(
|
||||
key,
|
||||
value_from,
|
||||
@@ -68,9 +80,9 @@ def test_service_event(
|
||||
):
|
||||
event = ServiceEvent(
|
||||
{
|
||||
'created_at': 'foo',
|
||||
'updated_at': 'bar',
|
||||
'created_by_id': sample_uuid(),
|
||||
"created_at": "foo",
|
||||
"updated_at": "bar",
|
||||
"created_by_id": sample_uuid(),
|
||||
},
|
||||
key,
|
||||
value_from,
|
||||
|
||||
@@ -6,26 +6,23 @@ from tests.conftest import SERVICE_ONE_ID
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'job_status, num_notifications_created, expected_still_processing',
|
||||
"job_status, num_notifications_created, expected_still_processing",
|
||||
[
|
||||
('scheduled', 0, True),
|
||||
('cancelled', 10, True),
|
||||
('finished', 5, True),
|
||||
('finished', 10, False),
|
||||
]
|
||||
("scheduled", 0, True),
|
||||
("cancelled", 10, True),
|
||||
("finished", 5, True),
|
||||
("finished", 10, False),
|
||||
],
|
||||
)
|
||||
def test_still_processing(
|
||||
notify_admin,
|
||||
job_status,
|
||||
num_notifications_created,
|
||||
expected_still_processing
|
||||
notify_admin, job_status, num_notifications_created, expected_still_processing
|
||||
):
|
||||
json = job_json(
|
||||
service_id=SERVICE_ONE_ID,
|
||||
created_by=user_json(),
|
||||
notification_count=10,
|
||||
notifications_requested=num_notifications_created,
|
||||
job_status=job_status
|
||||
job_status=job_status,
|
||||
)
|
||||
job = Job(json)
|
||||
assert job.still_processing == expected_still_processing
|
||||
|
||||
@@ -4,10 +4,12 @@ from app.models.organization import Organization
|
||||
from tests import organization_json
|
||||
|
||||
|
||||
@pytest.mark.parametrize("purchase_order_number,expected_result", [
|
||||
[None, None],
|
||||
["PO1234", [None, None, None, "PO1234"]]
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"purchase_order_number,expected_result",
|
||||
[[None, None], ["PO1234", [None, None, None, "PO1234"]]],
|
||||
)
|
||||
def test_organization_billing_details(purchase_order_number, expected_result):
|
||||
organization = Organization(organization_json(purchase_order_number=purchase_order_number))
|
||||
organization = Organization(
|
||||
organization_json(purchase_order_number=purchase_order_number)
|
||||
)
|
||||
assert organization.billing_details == expected_result
|
||||
|
||||
@@ -6,68 +6,72 @@ from tests import organization_json, service_json
|
||||
from tests.conftest import ORGANISATION_ID, create_folder, create_template
|
||||
|
||||
|
||||
def test_organization_type_when_services_organization_has_no_org_type(mocker, service_one):
|
||||
def test_organization_type_when_services_organization_has_no_org_type(
|
||||
mocker, service_one
|
||||
):
|
||||
service = Service(service_one)
|
||||
service._dict['organization_id'] = ORGANISATION_ID
|
||||
service._dict["organization_id"] = ORGANISATION_ID
|
||||
org = organization_json(organization_type=None)
|
||||
mocker.patch('app.organizations_client.get_organization', return_value=org)
|
||||
mocker.patch("app.organizations_client.get_organization", return_value=org)
|
||||
|
||||
assert not org['organization_type']
|
||||
assert service.organization_type == 'federal'
|
||||
assert not org["organization_type"]
|
||||
assert service.organization_type == "federal"
|
||||
|
||||
|
||||
def test_organization_type_when_service_and_its_org_both_have_an_org_type(mocker, service_one):
|
||||
def test_organization_type_when_service_and_its_org_both_have_an_org_type(
|
||||
mocker, service_one
|
||||
):
|
||||
# service_one has an organization_type of 'central'
|
||||
service = Service(service_one)
|
||||
service._dict['organization'] = ORGANISATION_ID
|
||||
org = organization_json(organization_type='local')
|
||||
mocker.patch('app.organizations_client.get_organization', return_value=org)
|
||||
service._dict["organization"] = ORGANISATION_ID
|
||||
org = organization_json(organization_type="local")
|
||||
mocker.patch("app.organizations_client.get_organization", return_value=org)
|
||||
|
||||
assert service.organization_type == 'local'
|
||||
assert service.organization_type == "local"
|
||||
|
||||
|
||||
def test_organization_name_comes_from_cache(mocker, service_one):
|
||||
mock_redis_get = mocker.patch(
|
||||
'app.extensions.RedisClient.get',
|
||||
"app.extensions.RedisClient.get",
|
||||
return_value=b'"Borchester Council"',
|
||||
)
|
||||
mock_get_organization = mocker.patch('app.organizations_client.get_organization')
|
||||
mock_get_organization = mocker.patch("app.organizations_client.get_organization")
|
||||
service = Service(service_one)
|
||||
service._dict['organization'] = ORGANISATION_ID
|
||||
service._dict["organization"] = ORGANISATION_ID
|
||||
|
||||
assert service.organization_name == 'Borchester Council'
|
||||
mock_redis_get.assert_called_once_with(f'organization-{ORGANISATION_ID}-name')
|
||||
assert service.organization_name == "Borchester Council"
|
||||
mock_redis_get.assert_called_once_with(f"organization-{ORGANISATION_ID}-name")
|
||||
assert mock_get_organization.called is False
|
||||
|
||||
|
||||
def test_organization_name_goes_into_cache(mocker, service_one):
|
||||
mocker.patch(
|
||||
'app.extensions.RedisClient.get',
|
||||
"app.extensions.RedisClient.get",
|
||||
return_value=None,
|
||||
)
|
||||
mock_redis_set = mocker.patch(
|
||||
'app.extensions.RedisClient.set',
|
||||
"app.extensions.RedisClient.set",
|
||||
)
|
||||
mocker.patch(
|
||||
'app.organizations_client.get_organization',
|
||||
"app.organizations_client.get_organization",
|
||||
return_value=organization_json(),
|
||||
)
|
||||
service = Service(service_one)
|
||||
service._dict['organization'] = ORGANISATION_ID
|
||||
service._dict["organization"] = ORGANISATION_ID
|
||||
|
||||
assert service.organization_name == 'Test Organization'
|
||||
assert service.organization_name == "Test Organization"
|
||||
mock_redis_set.assert_called_once_with(
|
||||
f'organization-{ORGANISATION_ID}-name',
|
||||
f"organization-{ORGANISATION_ID}-name",
|
||||
'"Test Organization"',
|
||||
ex=604800,
|
||||
)
|
||||
|
||||
|
||||
def test_service_without_organization_doesnt_need_org_api(mocker, service_one):
|
||||
mock_redis_get = mocker.patch('app.extensions.RedisClient.get')
|
||||
mock_get_organization = mocker.patch('app.organizations_client.get_organization')
|
||||
mock_redis_get = mocker.patch("app.extensions.RedisClient.get")
|
||||
mock_get_organization = mocker.patch("app.organizations_client.get_organization")
|
||||
service = Service(service_one)
|
||||
service._dict['organization'] = None
|
||||
service._dict["organization"] = None
|
||||
|
||||
assert service.organization_id is None
|
||||
assert service.organization_name is None
|
||||
@@ -79,17 +83,17 @@ def test_service_without_organization_doesnt_need_org_api(mocker, service_one):
|
||||
|
||||
def test_bad_permission_raises(service_one):
|
||||
with pytest.raises(KeyError) as e:
|
||||
Service(service_one).has_permission('foo')
|
||||
Service(service_one).has_permission("foo")
|
||||
assert str(e.value) == "'foo is not a service permission'"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("purchase_order_number,expected_result", [
|
||||
[None, None],
|
||||
["PO1234", [None, None, None, "PO1234"]]
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"purchase_order_number,expected_result",
|
||||
[[None, None], ["PO1234", [None, None, None, "PO1234"]]],
|
||||
)
|
||||
def test_service_billing_details(purchase_order_number, expected_result):
|
||||
service = Service(service_json(purchase_order_number=purchase_order_number))
|
||||
service._dict['purchase_order_number'] = purchase_order_number
|
||||
service._dict["purchase_order_number"] = purchase_order_number
|
||||
assert service.billing_details == expected_result
|
||||
|
||||
|
||||
@@ -99,15 +103,15 @@ def test_has_templates_of_type_includes_folders(
|
||||
mock_get_template_folders,
|
||||
):
|
||||
mocker.patch(
|
||||
'app.service_api_client.get_service_templates',
|
||||
return_value={'data': [create_template(
|
||||
folder='something', template_type='sms'
|
||||
)]}
|
||||
"app.service_api_client.get_service_templates",
|
||||
return_value={
|
||||
"data": [create_template(folder="something", template_type="sms")]
|
||||
},
|
||||
)
|
||||
|
||||
mocker.patch(
|
||||
'app.template_folder_api_client.get_template_folders',
|
||||
return_value=[create_folder(id='something')]
|
||||
"app.template_folder_api_client.get_template_folders",
|
||||
return_value=[create_folder(id="something")],
|
||||
)
|
||||
|
||||
assert Service(service_one).has_templates_of_type('sms')
|
||||
assert Service(service_one).has_templates_of_type("sms")
|
||||
|
||||
@@ -7,36 +7,43 @@ from app.models.spreadsheet import Spreadsheet
|
||||
|
||||
|
||||
def test_can_create_spreadsheet_from_large_excel_file():
|
||||
with open(str(Path.cwd() / 'tests' / 'spreadsheet_files' / 'excel 2007.xlsx'), 'rb') as xl:
|
||||
ret = Spreadsheet.from_file(xl, filename='xl.xlsx')
|
||||
with open(
|
||||
str(Path.cwd() / "tests" / "spreadsheet_files" / "excel 2007.xlsx"), "rb"
|
||||
) as xl:
|
||||
ret = Spreadsheet.from_file(xl, filename="xl.xlsx")
|
||||
assert ret.as_csv_data
|
||||
|
||||
|
||||
def test_can_create_spreadsheet_from_dict():
|
||||
assert Spreadsheet.from_dict(OrderedDict(
|
||||
foo='bar',
|
||||
name='Jane',
|
||||
)).as_csv_data == (
|
||||
"foo,name\r\n"
|
||||
"bar,Jane\r\n"
|
||||
)
|
||||
assert Spreadsheet.from_dict(
|
||||
OrderedDict(
|
||||
foo="bar",
|
||||
name="Jane",
|
||||
)
|
||||
).as_csv_data == ("foo,name\r\n" "bar,Jane\r\n")
|
||||
|
||||
|
||||
def test_can_create_spreadsheet_from_dict_with_filename():
|
||||
assert Spreadsheet.from_dict({}, filename='empty.csv').as_dict['file_name'] == "empty.csv"
|
||||
assert (
|
||||
Spreadsheet.from_dict({}, filename="empty.csv").as_dict["file_name"]
|
||||
== "empty.csv"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('args, kwargs', (
|
||||
@pytest.mark.parametrize(
|
||||
"args, kwargs",
|
||||
(
|
||||
('hello', ['hello']),
|
||||
{},
|
||||
(
|
||||
("hello", ["hello"]),
|
||||
{},
|
||||
),
|
||||
((), {"csv_data": "hello", "rows": ["hello"]}),
|
||||
),
|
||||
(
|
||||
(),
|
||||
{'csv_data': 'hello', 'rows': ['hello']}
|
||||
),
|
||||
))
|
||||
)
|
||||
def test_spreadsheet_checks_for_bad_arguments(args, kwargs):
|
||||
with pytest.raises(TypeError) as exception:
|
||||
Spreadsheet(*args, **kwargs)
|
||||
assert str(exception.value) == 'Spreadsheet must be created from either rows or CSV data'
|
||||
assert (
|
||||
str(exception.value)
|
||||
== "Spreadsheet must be created from either rows or CSV data"
|
||||
)
|
||||
|
||||
@@ -6,71 +6,70 @@ from app.models.service import Service
|
||||
from app.models.template_list import TemplateList
|
||||
from app.models.user import User
|
||||
|
||||
INV_PARENT_FOLDER_ID = '7e979e79-d970-43a5-ac69-b625a8d147b0'
|
||||
INV_CHILD_1_FOLDER_ID = '92ee1ee0-e4ee-4dcc-b1a7-a5da9ebcfa2b'
|
||||
VIS_PARENT_FOLDER_ID = 'bbbb222b-2b22-2b22-222b-b222b22b2222'
|
||||
INV_CHILD_2_FOLDER_ID = 'fafe723f-1d39-4a10-865f-e551e03d8886'
|
||||
INV_PARENT_FOLDER_ID = "7e979e79-d970-43a5-ac69-b625a8d147b0"
|
||||
INV_CHILD_1_FOLDER_ID = "92ee1ee0-e4ee-4dcc-b1a7-a5da9ebcfa2b"
|
||||
VIS_PARENT_FOLDER_ID = "bbbb222b-2b22-2b22-222b-b222b22b2222"
|
||||
INV_CHILD_2_FOLDER_ID = "fafe723f-1d39-4a10-865f-e551e03d8886"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_get_hierarchy_of_folders(
|
||||
mock_get_template_folders,
|
||||
active_user_with_permissions
|
||||
mock_get_template_folders, active_user_with_permissions
|
||||
):
|
||||
mock_get_template_folders.return_value = [
|
||||
{
|
||||
'name': "Invisible folder",
|
||||
'id': str(uuid.uuid4()),
|
||||
'parent_id': None,
|
||||
'users_with_permission': []
|
||||
"name": "Invisible folder",
|
||||
"id": str(uuid.uuid4()),
|
||||
"parent_id": None,
|
||||
"users_with_permission": [],
|
||||
},
|
||||
{
|
||||
'name': "Parent 1 - invisible",
|
||||
'id': INV_PARENT_FOLDER_ID,
|
||||
'parent_id': None,
|
||||
'users_with_permission': []
|
||||
"name": "Parent 1 - invisible",
|
||||
"id": INV_PARENT_FOLDER_ID,
|
||||
"parent_id": None,
|
||||
"users_with_permission": [],
|
||||
},
|
||||
{
|
||||
'name': "1's Visible child",
|
||||
'id': str(uuid.uuid4()),
|
||||
'parent_id': INV_PARENT_FOLDER_ID,
|
||||
'users_with_permission': [active_user_with_permissions['id']],
|
||||
"name": "1's Visible child",
|
||||
"id": str(uuid.uuid4()),
|
||||
"parent_id": INV_PARENT_FOLDER_ID,
|
||||
"users_with_permission": [active_user_with_permissions["id"]],
|
||||
},
|
||||
{
|
||||
'name': "1's Invisible child",
|
||||
'id': INV_CHILD_1_FOLDER_ID,
|
||||
'parent_id': INV_PARENT_FOLDER_ID,
|
||||
'users_with_permission': []
|
||||
"name": "1's Invisible child",
|
||||
"id": INV_CHILD_1_FOLDER_ID,
|
||||
"parent_id": INV_PARENT_FOLDER_ID,
|
||||
"users_with_permission": [],
|
||||
},
|
||||
{
|
||||
'name': "1's Visible grandchild",
|
||||
'id': str(uuid.uuid4()),
|
||||
'parent_id': INV_CHILD_1_FOLDER_ID,
|
||||
'users_with_permission': [active_user_with_permissions['id']],
|
||||
"name": "1's Visible grandchild",
|
||||
"id": str(uuid.uuid4()),
|
||||
"parent_id": INV_CHILD_1_FOLDER_ID,
|
||||
"users_with_permission": [active_user_with_permissions["id"]],
|
||||
},
|
||||
{
|
||||
'name': "Parent 2 - visible",
|
||||
'id': VIS_PARENT_FOLDER_ID,
|
||||
'parent_id': None,
|
||||
'users_with_permission': [active_user_with_permissions['id']],
|
||||
"name": "Parent 2 - visible",
|
||||
"id": VIS_PARENT_FOLDER_ID,
|
||||
"parent_id": None,
|
||||
"users_with_permission": [active_user_with_permissions["id"]],
|
||||
},
|
||||
{
|
||||
'name': "2's Visible child",
|
||||
'id': str(uuid.uuid4()),
|
||||
'parent_id': VIS_PARENT_FOLDER_ID,
|
||||
'users_with_permission': [active_user_with_permissions['id']],
|
||||
"name": "2's Visible child",
|
||||
"id": str(uuid.uuid4()),
|
||||
"parent_id": VIS_PARENT_FOLDER_ID,
|
||||
"users_with_permission": [active_user_with_permissions["id"]],
|
||||
},
|
||||
{
|
||||
'name': "2's Invisible child",
|
||||
'id': INV_CHILD_2_FOLDER_ID,
|
||||
'parent_id': VIS_PARENT_FOLDER_ID,
|
||||
'users_with_permission': []
|
||||
"name": "2's Invisible child",
|
||||
"id": INV_CHILD_2_FOLDER_ID,
|
||||
"parent_id": VIS_PARENT_FOLDER_ID,
|
||||
"users_with_permission": [],
|
||||
},
|
||||
{
|
||||
'name': "2's Visible grandchild",
|
||||
'id': str(uuid.uuid4()),
|
||||
'parent_id': INV_CHILD_2_FOLDER_ID,
|
||||
'users_with_permission': [active_user_with_permissions['id']],
|
||||
"name": "2's Visible grandchild",
|
||||
"id": str(uuid.uuid4()),
|
||||
"parent_id": INV_CHILD_2_FOLDER_ID,
|
||||
"users_with_permission": [active_user_with_permissions["id"]],
|
||||
},
|
||||
]
|
||||
|
||||
@@ -85,8 +84,8 @@ def test_template_list_yields_folders_visible_to_user(
|
||||
user = User(active_user_with_permissions)
|
||||
|
||||
result_folder_names = tuple(
|
||||
result.name for result in
|
||||
TemplateList(service=service, user=user)
|
||||
result.name
|
||||
for result in TemplateList(service=service, user=user)
|
||||
if result.is_folder
|
||||
)
|
||||
|
||||
@@ -107,9 +106,7 @@ def test_template_list_yields_all_folders_without_user(
|
||||
service = Service(service_one)
|
||||
|
||||
result_folder_names = tuple(
|
||||
result.name for result in
|
||||
TemplateList(service=service)
|
||||
if result.is_folder
|
||||
result.name for result in TemplateList(service=service) if result.is_folder
|
||||
)
|
||||
|
||||
assert result_folder_names == (
|
||||
|
||||
@@ -14,21 +14,22 @@ def test_anonymous_user(notify_admin):
|
||||
|
||||
|
||||
def test_user(notify_admin):
|
||||
user_data = {'id': 1,
|
||||
'name': 'Test User',
|
||||
'email_address': 'test@user.gsa.gov',
|
||||
'mobile_number': '+12021231234',
|
||||
'state': 'pending',
|
||||
'failed_login_count': 0,
|
||||
'platform_admin': False,
|
||||
}
|
||||
user_data = {
|
||||
"id": 1,
|
||||
"name": "Test User",
|
||||
"email_address": "test@user.gsa.gov",
|
||||
"mobile_number": "+12021231234",
|
||||
"state": "pending",
|
||||
"failed_login_count": 0,
|
||||
"platform_admin": False,
|
||||
}
|
||||
user = User(user_data)
|
||||
|
||||
assert user.id == 1
|
||||
assert user.name == 'Test User'
|
||||
assert user.email_address == 'test@user.gsa.gov'
|
||||
assert user.mobile_number == '+12021231234'
|
||||
assert user.state == 'pending'
|
||||
assert user.name == "Test User"
|
||||
assert user.email_address == "test@user.gsa.gov"
|
||||
assert user.mobile_number == "+12021231234"
|
||||
assert user.state == "pending"
|
||||
|
||||
# user has ten failed logins before being locked
|
||||
assert user.MAX_FAILED_LOGIN_COUNT == 10
|
||||
@@ -40,37 +41,45 @@ def test_user(notify_admin):
|
||||
assert user.locked is True
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
user.has_permissions('to_do_bad_things')
|
||||
user.has_permissions("to_do_bad_things")
|
||||
|
||||
|
||||
def test_activate_user(notify_admin, api_user_pending, mock_activate_user):
|
||||
assert User(api_user_pending).activate() == User(api_user_pending)
|
||||
mock_activate_user.assert_called_once_with(api_user_pending['id'])
|
||||
mock_activate_user.assert_called_once_with(api_user_pending["id"])
|
||||
|
||||
|
||||
def test_activate_user_already_active(notify_admin, api_user_active, mock_activate_user):
|
||||
def test_activate_user_already_active(
|
||||
notify_admin, api_user_active, mock_activate_user
|
||||
):
|
||||
assert User(api_user_active).activate() == User(api_user_active)
|
||||
assert mock_activate_user.called is False
|
||||
|
||||
|
||||
@pytest.mark.parametrize('is_platform_admin, value_in_session, expected_result', [
|
||||
(True, True, False),
|
||||
(True, False, True),
|
||||
(True, None, True),
|
||||
(False, True, False),
|
||||
(False, False, False),
|
||||
(False, None, False),
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"is_platform_admin, value_in_session, expected_result",
|
||||
[
|
||||
(True, True, False),
|
||||
(True, False, True),
|
||||
(True, None, True),
|
||||
(False, True, False),
|
||||
(False, False, False),
|
||||
(False, None, False),
|
||||
],
|
||||
)
|
||||
def test_platform_admin_flag_set_in_session(
|
||||
client_request, mocker, is_platform_admin, value_in_session, expected_result
|
||||
):
|
||||
session_dict = {}
|
||||
if value_in_session is not None:
|
||||
session_dict['disable_platform_admin_view'] = value_in_session
|
||||
session_dict["disable_platform_admin_view"] = value_in_session
|
||||
|
||||
mocker.patch.dict('app.models.user.session', values=session_dict, clear=True)
|
||||
mocker.patch.dict("app.models.user.session", values=session_dict, clear=True)
|
||||
|
||||
assert User({'id': 1, 'platform_admin': is_platform_admin}).platform_admin == expected_result
|
||||
assert (
|
||||
User({"id": 1, "platform_admin": is_platform_admin}).platform_admin
|
||||
== expected_result
|
||||
)
|
||||
|
||||
|
||||
def test_has_live_services(
|
||||
@@ -78,10 +87,12 @@ def test_has_live_services(
|
||||
mock_get_non_empty_organizations_and_services_for_user,
|
||||
fake_uuid,
|
||||
):
|
||||
user = User({
|
||||
'id': fake_uuid,
|
||||
'platform_admin': False,
|
||||
})
|
||||
user = User(
|
||||
{
|
||||
"id": fake_uuid,
|
||||
"platform_admin": False,
|
||||
}
|
||||
)
|
||||
assert len(user.live_services) == 5
|
||||
for service in user.live_services:
|
||||
assert service.live
|
||||
@@ -92,10 +103,15 @@ def test_has_live_services_when_there_are_no_services(
|
||||
mock_get_organizations_and_services_for_user,
|
||||
fake_uuid,
|
||||
):
|
||||
assert User({
|
||||
'id': fake_uuid,
|
||||
'platform_admin': False,
|
||||
}).live_services == []
|
||||
assert (
|
||||
User(
|
||||
{
|
||||
"id": fake_uuid,
|
||||
"platform_admin": False,
|
||||
}
|
||||
).live_services
|
||||
== []
|
||||
)
|
||||
|
||||
|
||||
def test_has_live_services_when_service_is_not_live(
|
||||
@@ -103,49 +119,62 @@ def test_has_live_services_when_service_is_not_live(
|
||||
mock_get_empty_organizations_and_one_service_for_user,
|
||||
fake_uuid,
|
||||
):
|
||||
assert User({
|
||||
'id': fake_uuid,
|
||||
'platform_admin': False,
|
||||
}).live_services == []
|
||||
assert (
|
||||
User(
|
||||
{
|
||||
"id": fake_uuid,
|
||||
"platform_admin": False,
|
||||
}
|
||||
).live_services
|
||||
== []
|
||||
)
|
||||
|
||||
|
||||
def test_invited_user_from_session_uses_id(client_request, mocker, mock_get_invited_user_by_id):
|
||||
session_dict = {'invited_user_id': USER_ONE_ID}
|
||||
mocker.patch.dict('app.models.user.session', values=session_dict, clear=True)
|
||||
def test_invited_user_from_session_uses_id(
|
||||
client_request, mocker, mock_get_invited_user_by_id
|
||||
):
|
||||
session_dict = {"invited_user_id": USER_ONE_ID}
|
||||
mocker.patch.dict("app.models.user.session", values=session_dict, clear=True)
|
||||
|
||||
assert InvitedUser.from_session().id == USER_ONE_ID
|
||||
|
||||
mock_get_invited_user_by_id.assert_called_once_with(USER_ONE_ID)
|
||||
|
||||
|
||||
def test_invited_user_from_session_returns_none_if_nothing_present(client_request, mocker):
|
||||
mocker.patch.dict('app.models.user.session', values={}, clear=True)
|
||||
def test_invited_user_from_session_returns_none_if_nothing_present(
|
||||
client_request, mocker
|
||||
):
|
||||
mocker.patch.dict("app.models.user.session", values={}, clear=True)
|
||||
assert InvitedUser.from_session() is None
|
||||
|
||||
|
||||
def test_invited_org_user_from_session_uses_id(
|
||||
client_request, mocker, mock_get_invited_org_user_by_id, sample_org_invite
|
||||
):
|
||||
session_dict = {'invited_org_user_id': sample_org_invite['id']}
|
||||
mocker.patch.dict('app.models.user.session', values=session_dict, clear=True)
|
||||
session_dict = {"invited_org_user_id": sample_org_invite["id"]}
|
||||
mocker.patch.dict("app.models.user.session", values=session_dict, clear=True)
|
||||
|
||||
assert InvitedOrgUser.from_session().id == sample_org_invite['id']
|
||||
assert InvitedOrgUser.from_session().id == sample_org_invite["id"]
|
||||
|
||||
mock_get_invited_org_user_by_id.assert_called_once_with(sample_org_invite['id'])
|
||||
mock_get_invited_org_user_by_id.assert_called_once_with(sample_org_invite["id"])
|
||||
|
||||
|
||||
def test_invited_org_user_from_session_returns_none_if_nothing_present(client_request, mocker):
|
||||
mocker.patch.dict('app.models.user.session', values={}, clear=True)
|
||||
def test_invited_org_user_from_session_returns_none_if_nothing_present(
|
||||
client_request, mocker
|
||||
):
|
||||
mocker.patch.dict("app.models.user.session", values={}, clear=True)
|
||||
assert InvitedOrgUser.from_session() is None
|
||||
|
||||
|
||||
def test_set_permissions(client_request, mocker, active_user_view_permissions, fake_uuid):
|
||||
mock_api = mocker.patch('app.models.user.user_api_client.set_user_permissions')
|
||||
mock_event = mocker.patch('app.models.user.create_set_user_permissions_event')
|
||||
def test_set_permissions(
|
||||
client_request, mocker, active_user_view_permissions, fake_uuid
|
||||
):
|
||||
mock_api = mocker.patch("app.models.user.user_api_client.set_user_permissions")
|
||||
mock_event = mocker.patch("app.models.user.create_set_user_permissions_event")
|
||||
|
||||
User(active_user_view_permissions).set_permissions(
|
||||
service_id=SERVICE_ONE_ID,
|
||||
permissions={'manage_templates'},
|
||||
permissions={"manage_templates"},
|
||||
folder_permissions=[],
|
||||
set_by_id=fake_uuid,
|
||||
)
|
||||
@@ -153,20 +182,20 @@ def test_set_permissions(client_request, mocker, active_user_view_permissions, f
|
||||
mock_api.assert_called_once()
|
||||
mock_event.assert_called_once_with(
|
||||
service_id=SERVICE_ONE_ID,
|
||||
user_id=active_user_view_permissions['id'],
|
||||
original_ui_permissions={'view_activity'},
|
||||
new_ui_permissions={'manage_templates'},
|
||||
user_id=active_user_view_permissions["id"],
|
||||
original_ui_permissions={"view_activity"},
|
||||
new_ui_permissions={"manage_templates"},
|
||||
set_by_id=fake_uuid,
|
||||
)
|
||||
|
||||
|
||||
def test_add_to_service(client_request, mocker, api_user_active, fake_uuid):
|
||||
mock_api = mocker.patch('app.models.user.user_api_client.add_user_to_service')
|
||||
mock_event = mocker.patch('app.models.user.create_add_user_to_service_event')
|
||||
mock_api = mocker.patch("app.models.user.user_api_client.add_user_to_service")
|
||||
mock_event = mocker.patch("app.models.user.create_add_user_to_service_event")
|
||||
|
||||
User(api_user_active).add_to_service(
|
||||
service_id=SERVICE_ONE_ID,
|
||||
permissions={'manage_templates'},
|
||||
permissions={"manage_templates"},
|
||||
folder_permissions=[],
|
||||
invited_by_id=fake_uuid,
|
||||
)
|
||||
@@ -174,7 +203,7 @@ def test_add_to_service(client_request, mocker, api_user_active, fake_uuid):
|
||||
mock_api.assert_called_once()
|
||||
mock_event.assert_called_once_with(
|
||||
service_id=SERVICE_ONE_ID,
|
||||
user_id=api_user_active['id'],
|
||||
user_id=api_user_active["id"],
|
||||
invited_by_id=fake_uuid,
|
||||
ui_permissions={'manage_templates'},
|
||||
ui_permissions={"manage_templates"},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user