|
1 |
| -from unittest import TestCase, mock |
| 1 | +from unittest import mock |
2 | 2 |
|
3 | 3 | import pytest
|
4 | 4 | from django.db.utils import IntegrityError
|
5 | 5 |
|
6 |
| -from environments.models import Environment |
7 | 6 | from organisations.models import Organisation, OrganisationRole
|
8 | 7 | from organisations.permissions.models import UserOrganisationPermission
|
9 | 8 | from organisations.permissions.permissions import ORGANISATION_PERMISSIONS
|
10 |
| -from projects.models import ( |
11 |
| - Project, |
12 |
| - ProjectPermissionModel, |
13 |
| - UserProjectPermission, |
14 |
| -) |
| 9 | +from projects.models import Project |
15 | 10 | from projects.permissions import VIEW_PROJECT
|
| 11 | +from tests.types import WithProjectPermissionsCallable |
16 | 12 | from users.models import FFAdminUser
|
17 | 13 |
|
18 | 14 |
|
19 |
| -@pytest.mark.django_db |
20 |
| -class FFAdminUserTestCase(TestCase): |
21 |
| - def setUp(self) -> None: |
22 |
| - self. user = FFAdminUser. objects. create( email="[email protected]") |
23 |
| - self.organisation = Organisation.objects.create(name="Test Organisation") |
| 15 | +def test_user_belongs_to_success( |
| 16 | + admin_user: FFAdminUser, |
| 17 | + organisation: Organisation, |
| 18 | +) -> None: |
| 19 | + # Given |
| 20 | + assert organisation in admin_user.organisations.all() |
| 21 | + # Then |
| 22 | + assert admin_user.belongs_to(organisation.id) |
24 | 23 |
|
25 |
| - self.project_1 = Project.objects.create( |
26 |
| - name="Test project 1", organisation=self.organisation |
27 |
| - ) |
28 |
| - self.project_2 = Project.objects.create( |
29 |
| - name="Test project 2", organisation=self.organisation |
30 |
| - ) |
31 | 24 |
|
32 |
| - self.environment_1 = Environment.objects.create( |
33 |
| - name="Test Environment 1", project=self.project_1 |
34 |
| - ) |
35 |
| - self.environment_2 = Environment.objects.create( |
36 |
| - name="Test Environment 2", project=self.project_2 |
37 |
| - ) |
| 25 | +def test_user_belongs_to_fail(admin_user: FFAdminUser) -> None: |
| 26 | + unaffiliated_organisation = Organisation.objects.create(name="Unaffiliated") |
| 27 | + assert not admin_user.belongs_to(unaffiliated_organisation.id) |
38 | 28 |
|
39 |
| - def test_user_belongs_to_success(self): |
40 |
| - self.user.add_organisation(self.organisation, OrganisationRole.USER) |
41 |
| - assert self.user.belongs_to(self.organisation.id) |
42 | 29 |
|
43 |
| - def test_user_belongs_to_fail(self): |
44 |
| - assert not self.user.belongs_to(self.organisation.id) |
| 30 | +def test_get_permitted_projects_for_org_admin_returns_all_projects( |
| 31 | + admin_user: FFAdminUser, |
| 32 | + organisation: Organisation, |
| 33 | + project: Project, |
| 34 | +) -> None: |
| 35 | + # Given |
| 36 | + Project.objects.create(name="Test project 2", organisation=organisation) |
| 37 | + # When |
| 38 | + projects = admin_user.get_permitted_projects(VIEW_PROJECT) |
45 | 39 |
|
46 |
| - def test_get_permitted_projects_for_org_admin_returns_all_projects(self): |
47 |
| - # Given |
48 |
| - self.user.add_organisation(self.organisation, OrganisationRole.ADMIN) |
| 40 | + # Then |
| 41 | + assert projects.count() == 2 |
49 | 42 |
|
50 |
| - # When |
51 |
| - projects = self.user.get_permitted_projects(VIEW_PROJECT) |
52 | 43 |
|
53 |
| - # Then |
54 |
| - assert projects.count() == 2 |
| 44 | +def test_get_permitted_projects_for_user_returns_only_projects_matching_permission( |
| 45 | + staff_user: FFAdminUser, |
| 46 | + with_project_permissions: WithProjectPermissionsCallable, |
| 47 | + project: Project, |
| 48 | +) -> None: |
| 49 | + # Given |
| 50 | + with_project_permissions([VIEW_PROJECT]) |
55 | 51 |
|
56 |
| - def test_get_permitted_projects_for_user_returns_only_projects_matching_permission( |
57 |
| - self, |
58 |
| - ): |
59 |
| - # Given |
60 |
| - self.user.add_organisation(self.organisation, OrganisationRole.USER) |
61 |
| - user_project_permission = UserProjectPermission.objects.create( |
62 |
| - user=self.user, project=self.project_1 |
63 |
| - ) |
64 |
| - read_permission = ProjectPermissionModel.objects.get(key=VIEW_PROJECT) |
65 |
| - user_project_permission.permissions.set([read_permission]) |
| 52 | + # When |
| 53 | + projects = staff_user.get_permitted_projects(permission_key=VIEW_PROJECT) |
66 | 54 |
|
67 |
| - # When |
68 |
| - projects = self.user.get_permitted_projects(permission_key=VIEW_PROJECT) |
| 55 | + # Then |
| 56 | + assert projects.count() == 1 |
| 57 | + assert projects.first() == project |
69 | 58 |
|
70 |
| - # Then |
71 |
| - assert projects.count() == 1 |
72 | 59 |
|
73 |
| - def test_get_admin_organisations(self): |
74 |
| - # Given |
75 |
| - self.user.add_organisation(self.organisation, OrganisationRole.ADMIN) |
| 60 | +def test_get_admin_organisations( |
| 61 | + admin_user: FFAdminUser, |
| 62 | + organisation: Organisation, |
| 63 | +) -> None: |
| 64 | + # Given |
| 65 | + non_admin_organisation = Organisation.objects.create(name="non-admin") |
| 66 | + admin_user.add_organisation(non_admin_organisation, OrganisationRole.USER) |
76 | 67 |
|
77 |
| - # When |
78 |
| - admin_orgs = self.user.get_admin_organisations() |
| 68 | + # When |
| 69 | + admin_orgs = admin_user.get_admin_organisations() |
79 | 70 |
|
80 |
| - # Then |
81 |
| - assert self.organisation in admin_orgs |
| 71 | + # Then |
| 72 | + assert organisation in admin_orgs |
| 73 | + assert non_admin_organisation not in admin_orgs |
82 | 74 |
|
83 |
| - def test_get_permitted_environments_for_org_admin_returns_all_environments_for_project( |
84 |
| - self, |
85 |
| - ): |
86 |
| - # Given |
87 |
| - self.user.add_organisation(self.organisation, OrganisationRole.ADMIN) |
88 | 75 |
|
89 |
| - # When |
90 |
| - environments = self.user.get_permitted_environments( |
91 |
| - "VIEW_ENVIRONMENT", project=self.project_1 |
92 |
| - ) |
93 |
| - |
94 |
| - # Then |
95 |
| - assert environments.count() == self.project_1.environments.count() |
| 76 | +def test_get_permitted_environments_for_org_admin_returns_all_environments_for_project( |
| 77 | + admin_user: FFAdminUser, |
| 78 | + organisation: Organisation, |
| 79 | + project: Project, |
| 80 | +) -> None: |
| 81 | + # When |
| 82 | + environments = admin_user.get_permitted_environments( |
| 83 | + "VIEW_ENVIRONMENT", project=project |
| 84 | + ) |
96 | 85 |
|
97 |
| - def test_get_permitted_environments_for_user_returns_only_environments_matching_permission( |
98 |
| - self, |
99 |
| - ): |
100 |
| - # Given |
101 |
| - self.user.add_organisation(self.organisation, OrganisationRole.USER) |
| 86 | + # Then |
| 87 | + assert environments.count() == project.environments.count() |
102 | 88 |
|
103 |
| - # When |
104 |
| - environments = self.user.get_permitted_environments( |
105 |
| - "VIEW_ENVIRONMENT", project=self.project_1 |
106 |
| - ) |
107 | 89 |
|
108 |
| - # Then |
109 |
| - assert len(list(environments)) == 0 |
| 90 | +def test_get_permitted_environments_for_user_returns_only_environments_matching_permission( |
| 91 | + staff_user: FFAdminUser, |
| 92 | + project: Project, |
| 93 | +) -> None: |
| 94 | + # When |
| 95 | + environments = staff_user.get_permitted_environments( |
| 96 | + "VIEW_ENVIRONMENT", project=project |
| 97 | + ) |
110 | 98 |
|
111 |
| - def test_unique_user_organisation(self): |
112 |
| - # Given organisation and user |
| 99 | + # Then |
| 100 | + assert len(list(environments)) == 0 |
113 | 101 |
|
114 |
| - # When |
115 |
| - self.user.add_organisation(self.organisation, OrganisationRole.ADMIN) |
116 | 102 |
|
117 |
| - # Then |
118 |
| - with pytest.raises(IntegrityError): |
119 |
| - self.user.add_organisation(self.organisation, OrganisationRole.USER) |
| 103 | +def test_unique_user_organisation( |
| 104 | + admin_user: FFAdminUser, |
| 105 | + organisation: Organisation, |
| 106 | +) -> None: |
| 107 | + with pytest.raises(IntegrityError): |
| 108 | + admin_user.add_organisation(organisation, OrganisationRole.USER) |
120 | 109 |
|
121 |
| - def test_has_organisation_permission_is_true_for_organisation_admin(self): |
122 |
| - # Given |
123 |
| - self.user.add_organisation(self.organisation, OrganisationRole.ADMIN) |
124 | 110 |
|
125 |
| - # Then |
126 |
| - assert all( |
127 |
| - self.user.has_organisation_permission( |
128 |
| - organisation=self.organisation, permission_key=permission_key |
129 |
| - ) |
130 |
| - for permission_key, _ in ORGANISATION_PERMISSIONS |
| 111 | +def test_has_organisation_permission_is_true_for_organisation_admin( |
| 112 | + admin_user: FFAdminUser, |
| 113 | + organisation: Organisation, |
| 114 | +) -> None: |
| 115 | + assert ORGANISATION_PERMISSIONS |
| 116 | + assert all( |
| 117 | + admin_user.has_organisation_permission( |
| 118 | + organisation=organisation, permission_key=permission_key |
131 | 119 | )
|
| 120 | + for permission_key, _ in ORGANISATION_PERMISSIONS |
| 121 | + ) |
132 | 122 |
|
133 |
| - def test_has_organisation_permission_is_true_when_user_has_permission(self): |
134 |
| - # Given |
135 |
| - self.user.add_organisation(self.organisation) |
136 |
| - user_organisation_permission = UserOrganisationPermission.objects.create( |
137 |
| - user=self.user, organisation=self.organisation |
| 123 | + |
| 124 | +def test_has_organisation_permission_is_true_when_user_has_permission( |
| 125 | + staff_user: FFAdminUser, |
| 126 | + organisation: Organisation, |
| 127 | +) -> None: |
| 128 | + # Given |
| 129 | + user_organisation_permission = UserOrganisationPermission.objects.create( |
| 130 | + user=staff_user, organisation=organisation |
| 131 | + ) |
| 132 | + for permission_key, _ in ORGANISATION_PERMISSIONS: |
| 133 | + user_organisation_permission.permissions.through.objects.create( |
| 134 | + permissionmodel_id=permission_key, |
| 135 | + userorganisationpermission=user_organisation_permission, |
138 | 136 | )
|
139 |
| - for permission_key, _ in ORGANISATION_PERMISSIONS: |
140 |
| - user_organisation_permission.permissions.through.objects.create( |
141 |
| - permissionmodel_id=permission_key, |
142 |
| - userorganisationpermission=user_organisation_permission, |
143 |
| - ) |
144 |
| - |
145 |
| - # Then |
146 |
| - assert all( |
147 |
| - self.user.has_organisation_permission( |
148 |
| - organisation=self.organisation, permission_key=permission_key |
149 |
| - ) |
150 |
| - for permission_key, _ in ORGANISATION_PERMISSIONS |
| 137 | + |
| 138 | + # Then |
| 139 | + assert all( |
| 140 | + staff_user.has_organisation_permission( |
| 141 | + organisation=organisation, permission_key=permission_key |
151 | 142 | )
|
| 143 | + for permission_key, _ in ORGANISATION_PERMISSIONS |
| 144 | + ) |
152 | 145 |
|
153 |
| - def test_has_organisation_permission_is_false_when_user_does_not_have_permission( |
154 |
| - self, |
155 |
| - ): |
156 |
| - # Given |
157 |
| - self.user.add_organisation(self.organisation) |
158 |
| - |
159 |
| - # Then |
160 |
| - assert not any( |
161 |
| - self.user.has_organisation_permission( |
162 |
| - organisation=self.organisation, permission_key=permission_key |
163 |
| - ) |
164 |
| - for permission_key, _ in ORGANISATION_PERMISSIONS |
| 146 | + |
| 147 | +def test_has_organisation_permission_is_false_when_user_does_not_have_permission( |
| 148 | + staff_user: FFAdminUser, |
| 149 | + organisation: Organisation, |
| 150 | +) -> None: |
| 151 | + assert not any( |
| 152 | + staff_user.has_organisation_permission( |
| 153 | + organisation=organisation, permission_key=permission_key |
165 | 154 | )
|
| 155 | + for permission_key, _ in ORGANISATION_PERMISSIONS |
| 156 | + ) |
166 | 157 |
|
167 | 158 |
|
168 | 159 | @pytest.mark.django_db
|
|
0 commit comments