Skip to content

Commit

Permalink
chore: Convert project permissions TestCase to normal test function (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zachaysan authored Apr 17, 2024
1 parent bc39a63 commit ad4f884
Show file tree
Hide file tree
Showing 4 changed files with 340 additions and 307 deletions.
29 changes: 28 additions & 1 deletion api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
MetadataModelFieldRequirement,
)
from organisations.models import Organisation, OrganisationRole, Subscription
from organisations.permissions.models import OrganisationPermissionModel
from organisations.permissions.models import (
OrganisationPermissionModel,
UserOrganisationPermission,
)
from organisations.permissions.permissions import (
CREATE_PROJECT,
MANAGE_USER_GROUPS,
Expand All @@ -55,6 +58,7 @@
from task_processor.task_run_method import TaskRunMethod
from tests.types import (
WithEnvironmentPermissionsCallable,
WithOrganisationPermissionsCallable,
WithProjectPermissionsCallable,
)
from users.models import FFAdminUser, UserPermissionGroup
Expand Down Expand Up @@ -235,6 +239,29 @@ def _with_environment_permissions(
return _with_environment_permissions


@pytest.fixture()
def with_organisation_permissions(
organisation: Organisation, staff_user: FFAdminUser
) -> WithOrganisationPermissionsCallable:
"""
Add organisation permissions to the staff_user fixture.
Defaults to associating to the organisation fixture.
"""

def _with_organisation_permissions(
permission_keys: list[str], organisation_id: int | None = None
) -> UserOrganisationPermission:
organisation_id = organisation_id or organisation.id
uop, __ = UserOrganisationPermission.objects.get_or_create(
organisation_id=organisation_id, user=staff_user
)
uop.permissions.add(*permission_keys)

return uop

return _with_organisation_permissions


@pytest.fixture()
def with_project_permissions(
project: Project, staff_user: FFAdminUser
Expand Down
3 changes: 0 additions & 3 deletions api/projects/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ def has_object_permission(self, request, view, obj):
):
return True

if view.action in ("update", "destroy") and request.user.is_project_admin(obj):
return True

if view.action == "user_permissions":
return True

Expand Down
10 changes: 7 additions & 3 deletions api/tests/types.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from typing import Callable

from environments.permissions.models import UserEnvironmentPermission
from projects.models import Project, UserProjectPermission
from organisations.permissions.models import UserOrganisationPermission
from projects.models import UserProjectPermission

WithProjectPermissionsCallable = Callable[
[list[str], Project | None], UserProjectPermission
[list[str], int | None, bool], UserProjectPermission
]
WithOrganisationPermissionsCallable = Callable[
[list[str], int | None], UserOrganisationPermission
]
WithEnvironmentPermissionsCallable = Callable[
[list[str], Project | None], UserEnvironmentPermission
[list[str], int | None], UserEnvironmentPermission
]
Loading

0 comments on commit ad4f884

Please sign in to comment.