Skip to content

Commit

Permalink
fix test RDI views, fix fixture for user permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
pkujawa committed Jan 31, 2025
1 parent 296ba44 commit 0b23a42
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def set_up(self, api_client: Callable, afghanistan: BusinessAreaFactory, id_to_b
)

@pytest.mark.parametrize(
"permissions, partner_permissions, access_to_program, expected_status",
"user_permissions, partner_permissions, is_permission_in_correct_program, expected_status",
[
([], [], True, status.HTTP_403_FORBIDDEN),
([Permissions.RDI_VIEW_LIST], [], True, status.HTTP_200_OK),
Expand All @@ -91,9 +91,9 @@ def set_up(self, api_client: Callable, afghanistan: BusinessAreaFactory, id_to_b
)
def test_list_registration_data_imports_permission(
self,
permissions: list,
user_permissions: list,
partner_permissions: list,
access_to_program: bool,
is_permission_in_correct_program: bool,
expected_status: str,
api_client: Callable,
afghanistan: BusinessAreaFactory,
Expand All @@ -102,15 +102,13 @@ def test_list_registration_data_imports_permission(
id_to_base64: Callable,
) -> None:
self.set_up(api_client, afghanistan, id_to_base64)
create_user_role_with_permissions(
self.user,
permissions,
self.afghanistan,
)
create_partner_role_with_permissions(self.partner, partner_permissions, self.afghanistan)
if access_to_program:
create_partner_role_with_permissions(self.partner, partner_permissions, self.afghanistan, self.program2)

if is_permission_in_correct_program:
create_user_role_with_permissions(self.user, user_permissions, self.afghanistan, self.program1)
create_partner_role_with_permissions(self.partner, partner_permissions, self.afghanistan, self.program1)
else:
# role will be created for different program
create_user_role_with_permissions(self.user, user_permissions, self.afghanistan)
create_partner_role_with_permissions(self.partner, partner_permissions, self.afghanistan)

response = self.client.get(self.url_list)
Expand Down Expand Up @@ -229,7 +227,7 @@ def test_list_registration_data_imports_caching(

etag = response.headers["etag"]
assert json.loads(cache.get(etag)[0].decode("utf8")) == response.json()
assert len(ctx.captured_queries) == 12
assert len(ctx.captured_queries) == 15

# Test that reoccurring requests use cached data
with CaptureQueriesContext(connection) as ctx:
Expand All @@ -238,7 +236,7 @@ def test_list_registration_data_imports_caching(

etag_second_call = response.headers["etag"]
assert json.loads(cache.get(response.headers["etag"])[0].decode("utf8")) == response.json()
assert len(ctx.captured_queries) == 5
assert len(ctx.captured_queries) == 3

assert etag_second_call == etag

Expand All @@ -251,7 +249,7 @@ def test_list_registration_data_imports_caching(

etag_call_after_update = response.headers["etag"]
assert json.loads(cache.get(response.headers["etag"])[0].decode("utf8")) == response.json()
assert len(ctx.captured_queries) == 12
assert len(ctx.captured_queries) == 10 # less than the first call because of cached permissions

assert etag_call_after_update != etag

Expand All @@ -262,6 +260,6 @@ def test_list_registration_data_imports_caching(

etag_call_after_update_second_call = response.headers["etag"]
assert json.loads(cache.get(response.headers["etag"])[0].decode("utf8")) == response.json()
assert len(ctx.captured_queries) == 5
assert len(ctx.captured_queries) == 3

assert etag_call_after_update_second_call == etag_call_after_update
4 changes: 3 additions & 1 deletion tests/unit/fixtures/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def _create_user_role_with_permissions(
# whole_business_area is used to create a role for all programs in a business area (program=None)
if not program and not whole_business_area_access:
program = ProgramFactory(business_area=business_area, name="Program for User Role")
user_role, _ = RoleAssignment.objects.get_or_create(user=user, role=role, business_area=business_area)
user_role, _ = RoleAssignment.objects.get_or_create(
user=user, role=role, business_area=business_area, program=program
)

# set admin area limits
if program and areas:
Expand Down

0 comments on commit 0b23a42

Please sign in to comment.