Files
notifications-admin/tests/app/models/test_template_list.py

123 lines
3.7 KiB
Python
Raw Normal View History

import uuid
import pytest
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"
2024-07-15 08:07:18 -07:00
@pytest.fixture
def _mock_get_hierarchy_of_folders(
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": "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 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": "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 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"]],
},
]
@pytest.mark.usefixtures("_mock_get_hierarchy_of_folders")
def test_template_list_yields_folders_visible_to_user(
mock_get_service_templates,
service_one,
active_user_with_permissions,
):
service = Service(service_one)
user = User(active_user_with_permissions)
result_folder_names = tuple(
result.name
for result in TemplateList(service=service, user=user)
if result.is_folder
)
assert result_folder_names == (
["Parent 1 - invisible", "1's Visible child"],
["Parent 1 - invisible", ["1's Invisible child", "1's Visible grandchild"]],
"Parent 2 - visible",
"2's Visible child",
["2's Invisible child", "2's Visible grandchild"],
)
@pytest.mark.usefixtures("_mock_get_hierarchy_of_folders")
Simplify tests for TemplateList class In response to: [^1][^2][^3]. Originally there were three tests: - One was testing the "get_user_template_folders" method directly. - The other two were testing "get_template_folders", which calls the "_user_" method if a user is passed - this is what the tests were primarily checking, by passing or not passing a User object. The two tests of "get_template_folders" also implicitly checked that it filtered folders based on a "parent_folder_id". I wanted to emulate the tests but it makes more sense to simplify them, as the methods are now only used by TemplateList internally. To simplify, we just keep just two tests to check that the overall set of folders is filtered based on the presence of a User. We do lose the implicit check of filtering by "parent_folder_id", but: - We are still checking this implicitly in the sense that the loop to generate the overall set of folders terminates. If the method didn't do any filtering, the loop would be infinite. - We need to acknowledge that these tests were incomplete to begin with. There is also lots of coverage in higher level tests [^4], although it's harder to reason exactly what is covered. [^1]: https://github.com/alphagov/notifications-admin/pull/4258#discussion_r890144076 [^2]: https://github.com/alphagov/notifications-admin/pull/4258#discussion_r890151220 [^3]: https://github.com/alphagov/notifications-admin/pull/4258#discussion_r890147506 [^4]: https://github.com/alphagov/notifications-admin/blob/1787f9f42f0f71854c9fb621c2f0a8f4f7c76286/tests/app/main/views/test_template_folders.py
2022-06-07 11:14:23 +01:00
def test_template_list_yields_all_folders_without_user(
mock_get_service_templates,
service_one,
):
service = Service(service_one)
result_folder_names = tuple(
result.name for result in TemplateList(service=service) if result.is_folder
)
assert result_folder_names == (
Simplify tests for TemplateList class In response to: [^1][^2][^3]. Originally there were three tests: - One was testing the "get_user_template_folders" method directly. - The other two were testing "get_template_folders", which calls the "_user_" method if a user is passed - this is what the tests were primarily checking, by passing or not passing a User object. The two tests of "get_template_folders" also implicitly checked that it filtered folders based on a "parent_folder_id". I wanted to emulate the tests but it makes more sense to simplify them, as the methods are now only used by TemplateList internally. To simplify, we just keep just two tests to check that the overall set of folders is filtered based on the presence of a User. We do lose the implicit check of filtering by "parent_folder_id", but: - We are still checking this implicitly in the sense that the loop to generate the overall set of folders terminates. If the method didn't do any filtering, the loop would be infinite. - We need to acknowledge that these tests were incomplete to begin with. There is also lots of coverage in higher level tests [^4], although it's harder to reason exactly what is covered. [^1]: https://github.com/alphagov/notifications-admin/pull/4258#discussion_r890144076 [^2]: https://github.com/alphagov/notifications-admin/pull/4258#discussion_r890151220 [^3]: https://github.com/alphagov/notifications-admin/pull/4258#discussion_r890147506 [^4]: https://github.com/alphagov/notifications-admin/blob/1787f9f42f0f71854c9fb621c2f0a8f4f7c76286/tests/app/main/views/test_template_folders.py
2022-06-07 11:14:23 +01:00
"Invisible folder",
"Parent 1 - invisible",
"1's Invisible child",
"1's Visible grandchild",
"1's Visible child",
"Parent 2 - visible",
"2's Invisible child",
Simplify tests for TemplateList class In response to: [^1][^2][^3]. Originally there were three tests: - One was testing the "get_user_template_folders" method directly. - The other two were testing "get_template_folders", which calls the "_user_" method if a user is passed - this is what the tests were primarily checking, by passing or not passing a User object. The two tests of "get_template_folders" also implicitly checked that it filtered folders based on a "parent_folder_id". I wanted to emulate the tests but it makes more sense to simplify them, as the methods are now only used by TemplateList internally. To simplify, we just keep just two tests to check that the overall set of folders is filtered based on the presence of a User. We do lose the implicit check of filtering by "parent_folder_id", but: - We are still checking this implicitly in the sense that the loop to generate the overall set of folders terminates. If the method didn't do any filtering, the loop would be infinite. - We need to acknowledge that these tests were incomplete to begin with. There is also lots of coverage in higher level tests [^4], although it's harder to reason exactly what is covered. [^1]: https://github.com/alphagov/notifications-admin/pull/4258#discussion_r890144076 [^2]: https://github.com/alphagov/notifications-admin/pull/4258#discussion_r890151220 [^3]: https://github.com/alphagov/notifications-admin/pull/4258#discussion_r890147506 [^4]: https://github.com/alphagov/notifications-admin/blob/1787f9f42f0f71854c9fb621c2f0a8f4f7c76286/tests/app/main/views/test_template_folders.py
2022-06-07 11:14:23 +01:00
"2's Visible grandchild",
"2's Visible child",
)