From 81a5e59757b27944788b06fff66660a0008e4080 Mon Sep 17 00:00:00 2001 From: Zach Aysan Date: Wed, 31 Jan 2024 11:39:22 -0500 Subject: [PATCH] chore: Convert organisations permissions TestCase to normal test function (#3323) --- .../test_unit_organisations_permissions.py | 80 +++++++++---------- 1 file changed, 38 insertions(+), 42 deletions(-) diff --git a/api/tests/unit/organisations/permissions/test_unit_organisations_permissions.py b/api/tests/unit/organisations/permissions/test_unit_organisations_permissions.py index 8b1bfed9aff3..186924812daf 100644 --- a/api/tests/unit/organisations/permissions/test_unit_organisations_permissions.py +++ b/api/tests/unit/organisations/permissions/test_unit_organisations_permissions.py @@ -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="user@test.com") - self.org_admin = FFAdminUser.objects.create(email="admin@test.com") - - 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