Skip to content

Commit

Permalink
chore: Convert organisations permissions TestCase to normal test func…
Browse files Browse the repository at this point in the history
…tion (#3323)
  • Loading branch information
zachaysan authored Jan 31, 2024
1 parent 6501829 commit 81a5e59
Showing 1 changed file with 38 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,47 +1,43 @@
from unittest import TestCase, mock
from unittest import mock

import pytest

from organisations.models import Organisation, OrganisationRole
from organisations.models import Organisation
from organisations.permissions.permissions import OrganisationUsersPermission
from users.models import FFAdminUser

mock_request = mock.MagicMock
mock_view = mock.MagicMock


@pytest.mark.django_db
class OrganisationUsersPermissionTestCase(TestCase):
def setUp(self) -> None:
self.organisation = Organisation.objects.create(name="Test")
self.user = FFAdminUser.objects.create(email="[email protected]")
self.org_admin = FFAdminUser.objects.create(email="[email protected]")

self.user.add_organisation(self.organisation)
self.org_admin.add_organisation(self.organisation, OrganisationRole.ADMIN)

self.permissions = OrganisationUsersPermission()

mock_view.kwargs = {"organisation_pk": self.organisation.id}

def test_org_user_can_list_users(self):
# Given
mock_request.user = self.user
mock_view.action = "list"

# When
result = self.permissions.has_permission(mock_request, mock_view)

# Then
assert result

def test_org_user_cannot_create_user(self):
# Given
mock_request.user = self.user
mock_view.action = "create"

# When
result = self.permissions.has_permission(mock_request, mock_view)

# Then
assert not result
def test_org_user_can_list_users(
staff_user: FFAdminUser,
organisation: Organisation,
) -> None:
# Given
mock_request = mock.MagicMock(user=staff_user)
mock_view = mock.MagicMock(
action="list",
kwargs={"organisation_pk": organisation.id},
)
permissions = OrganisationUsersPermission()

# When
result = permissions.has_permission(mock_request, mock_view)

# Then
assert result is True


def test_org_user_cannot_create_user(
staff_user: FFAdminUser,
organisation: Organisation,
) -> None:
# Given
mock_request = mock.MagicMock(user=staff_user)
mock_view = mock.MagicMock(
action="create",
kwargs={"organisation_pk": organisation.id},
)
permissions = OrganisationUsersPermission()

# When
result = permissions.has_permission(mock_request, mock_view)

# Then
assert result is False

0 comments on commit 81a5e59

Please sign in to comment.