|
3 | 3 | import pytest |
4 | 4 | from django.db import connection |
5 | 5 |
|
6 | | -from ansible_base.authentication.models import AuthenticatorUser |
| 6 | +from ansible_base.authentication.models import AuthenticatorMap, AuthenticatorUser |
7 | 7 | from ansible_base.authentication.utils import claims |
8 | 8 | from test_app.tests.authentication.conftest import SYSTEM_ROLE_NAME |
9 | 9 |
|
|
20 | 20 | True, |
21 | 21 | True, |
22 | 22 | {"team_membership": {}, "organization_membership": {}, 'rbac_roles': {'system': {'roles': {}}, 'organizations': {}}}, |
23 | | - [{1: True}], |
| 23 | + [{1: True, 'enabled': True}], |
24 | 24 | id="Set flag 'is_superuser' to True (trigger 'always')", |
25 | 25 | ), |
26 | 26 | pytest.param( |
|
32 | 32 | True, |
33 | 33 | False, |
34 | 34 | {"team_membership": {}, "organization_membership": {}, 'rbac_roles': {'system': {'roles': {}}, 'organizations': {}}}, |
35 | | - [{1: False}], |
| 35 | + [{1: False, 'enabled': True}], |
36 | 36 | id="Set flag 'is_superuser' to False (trigger 'never')", |
37 | 37 | ), |
38 | 38 | pytest.param( |
|
44 | 44 | True, |
45 | 45 | None, |
46 | 46 | {"team_membership": {}, "organization_membership": {}, 'rbac_roles': {'system': {'roles': {}}, 'organizations': {}}}, |
47 | | - [{1: "invalid"}], |
| 47 | + [{1: "invalid", 'enabled': True}], |
48 | 48 | id="Wrong trigger, thus flag 'is_superuser' is not set, auth. map is ignored", |
49 | 49 | ), |
50 | 50 | pytest.param( |
|
56 | 56 | True, |
57 | 57 | None, |
58 | 58 | {"team_membership": {}, "organization_membership": {}, 'rbac_roles': {'system': {'roles': {}}, 'organizations': {}}}, |
59 | | - [{1: "skipped"}], |
| 59 | + [{1: "skipped", 'enabled': True}], |
60 | 60 | id="Define no trigger, thus flag 'is_superuser' is not set", |
61 | 61 | ), |
62 | 62 | pytest.param( |
|
68 | 68 | False, |
69 | 69 | None, |
70 | 70 | {"team_membership": {}, "organization_membership": {}, 'rbac_roles': {'system': {'roles': {}}, 'organizations': {}}}, |
71 | | - [{1: False}], |
| 71 | + [{1: False, 'enabled': True}], |
72 | 72 | id="map_type 'allow' with trigger 'never' sets 'access_allowed' to False", |
73 | 73 | ), |
74 | 74 | pytest.param( |
|
84 | 84 | "team_membership": {"testorg": {"testteam": True}}, |
85 | 85 | 'rbac_roles': {'system': {'roles': {}}, 'organizations': {'testorg': {'roles': {}, 'teams': {'testteam': {'roles': {'Team Member': True}}}}}}, |
86 | 86 | }, |
87 | | - [{1: True}], |
| 87 | + [{1: True, 'enabled': True}], |
88 | 88 | id="Assign 'Team Member' role to team 'testteam'", |
89 | 89 | ), |
90 | 90 | pytest.param( |
|
100 | 100 | "team_membership": {"testorg": {"testteam": False}}, |
101 | 101 | 'rbac_roles': {'system': {'roles': {}}, 'organizations': {'testorg': {'roles': {}, 'teams': {'testteam': {'roles': {'Team Member': False}}}}}}, |
102 | 102 | }, |
103 | | - [{1: False}], |
| 103 | + [{1: False, 'enabled': True}], |
104 | 104 | id="Remove 'Team Member' role from team 'testteam'", |
105 | 105 | ), |
106 | 106 | pytest.param( |
|
116 | 116 | "team_membership": {}, |
117 | 117 | 'rbac_roles': {'system': {'roles': {}}, 'organizations': {'testorg': {'roles': {'Organization Member': True}, 'teams': {}}}}, |
118 | 118 | }, |
119 | | - [{1: True}], |
| 119 | + [{1: True, 'enabled': True}], |
120 | 120 | id="Assign 'Organization Member' role to organization 'testorg'", |
121 | 121 | ), |
122 | 122 | pytest.param( |
|
132 | 132 | "team_membership": {}, |
133 | 133 | 'rbac_roles': {'system': {'roles': {}}, 'organizations': {'testorg': {'roles': {'Organization Member': False}, 'teams': {}}}}, |
134 | 134 | }, |
135 | | - [{1: False}], |
| 135 | + [{1: False, 'enabled': True}], |
136 | 136 | id="Remove 'Organization Member' role from organization 'testorg'", |
137 | 137 | ), |
138 | 138 | pytest.param( |
|
148 | 148 | "team_membership": {"testorg": {"testteam": True}}, |
149 | 149 | 'rbac_roles': {'system': {'roles': {}}, 'organizations': {'testorg': {'roles': {}, 'teams': {'testteam': {'roles': {'Team Member': True}}}}}}, |
150 | 150 | }, |
151 | | - [{1: True}], |
| 151 | + [{1: True, 'enabled': True}], |
152 | 152 | id="Assign 'Team Member' role to team 'testteam' using map_type 'role'", |
153 | 153 | ), |
154 | 154 | pytest.param( |
|
164 | 164 | "team_membership": {}, |
165 | 165 | 'rbac_roles': {'system': {'roles': {}}, 'organizations': {'testorg': {'roles': {'Organization Member': True}, 'teams': {}}}}, |
166 | 166 | }, |
167 | | - [{1: True}], |
| 167 | + [{1: True, 'enabled': True}], |
168 | 168 | id="Assign 'Organization Member' role to organization 'testorg' using map_type 'role'", |
169 | 169 | ), |
170 | 170 | pytest.param( |
|
176 | 176 | True, |
177 | 177 | None, |
178 | 178 | {"organization_membership": {}, "team_membership": {}, 'rbac_roles': {'system': {'roles': {SYSTEM_ROLE_NAME: True}}, 'organizations': {}}}, |
179 | | - [{1: True}], |
| 179 | + [{1: True, 'enabled': True}], |
180 | 180 | id="Assign System role to user", |
181 | 181 | ), |
182 | 182 | pytest.param( |
|
188 | 188 | True, |
189 | 189 | None, |
190 | 190 | {"organization_membership": {}, "team_membership": {}, 'rbac_roles': {'system': {'roles': {}}, 'organizations': {}}}, |
191 | | - [{1: False}], |
| 191 | + [{1: False, 'enabled': True}], |
192 | 192 | id="Wrong map type, this auth. map is ignored", |
193 | 193 | ), |
194 | 194 | ], |
@@ -324,9 +324,9 @@ def test_create_claims_revoke(local_authenticator_map, process_function, trigger |
324 | 324 | assert res["is_superuser"] is granted |
325 | 325 | assert res["claims"] == {"team_membership": {}, "organization_membership": {}, "rbac_roles": default_rbac_roles_claims} |
326 | 326 | if revoke: |
327 | | - assert res["last_login_map_results"] == [{local_authenticator_map.pk: False}] |
| 327 | + assert res["last_login_map_results"] == [{local_authenticator_map.pk: False, 'enabled': True}] |
328 | 328 | else: |
329 | | - assert res["last_login_map_results"] == [{local_authenticator_map.pk: "skipped"}] |
| 329 | + assert res["last_login_map_results"] == [{local_authenticator_map.pk: "skipped", 'enabled': True}] |
330 | 330 |
|
331 | 331 |
|
332 | 332 | @pytest.mark.parametrize( |
@@ -772,3 +772,22 @@ def test_update_user_claims_groups(user, local_authenticator_map): |
772 | 772 | assert local_authenticator_map.authenticator == authenticator_user.provider # sanity check |
773 | 773 | result = claims.update_user_claims(user, authenticator, ["foo"]) |
774 | 774 | assert result is user |
| 775 | + |
| 776 | + |
| 777 | +@pytest.mark.parametrize("enabled", [True, False]) |
| 778 | +def test_create_claims_with_map_enabled_or_disabled(enabled, local_authenticator): |
| 779 | + # Create an AuthenticatorMap object with the parameterized "enabled" value |
| 780 | + AuthenticatorMap.objects.create( |
| 781 | + authenticator=local_authenticator, |
| 782 | + triggers={"always": {}}, |
| 783 | + map_type="is_superuser", |
| 784 | + enabled=enabled, |
| 785 | + ) |
| 786 | + |
| 787 | + result = claims.create_claims(local_authenticator, "testuser", {}, []) |
| 788 | + |
| 789 | + # Assert based on the "enabled" value |
| 790 | + if enabled: |
| 791 | + assert result["is_superuser"] is not None, "Claim should be present when enabled is True" |
| 792 | + else: |
| 793 | + assert result["is_superuser"] is None, "Claim should be None when enabled is False" |
0 commit comments