Skip to content

Commit 9069d46

Browse files
committed
Add test for object re-creation
1 parent 113ac6f commit 9069d46

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

ansible_base/rbac/claims.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def get_or_create_resource(objects: dict, content_type: str, data: dict) -> Tupl
286286
organization_data = objects["organization"][org_id]
287287

288288
# Now that we have the org we can build a team
289-
org_resource, _ = get_or_create_resource("organization", organization_data)
289+
org_resource, _ = get_or_create_resource(objects, "organization", organization_data)
290290

291291
resource = resource_cls.create_resource(
292292
resource_type_cls.objects.get(name="shared.team"),

test_app/tests/rbac/test_claims.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,55 @@ def test_serialize_and_save_claims(self, claims_scenario, scenario_name):
404404
# Verify hashes are identical
405405
assert user1_hash == user2_hash
406406

407+
@pytest.mark.parametrize(
408+
"scenario_name",
409+
[
410+
'no_permissions',
411+
'first_org_only',
412+
'odds_org_admin',
413+
'evens_org_admin',
414+
'first_three_teams',
415+
'platform_auditor_only',
416+
'mixed_small',
417+
'mixed_large',
418+
'all_org_admin',
419+
'scattered_permissions',
420+
'teams_no_orgs',
421+
],
422+
)
423+
def test_rebuild_single_user(self, claims_scenario, scenario_name):
424+
"""Get claims hash, then just delete everything and save_user_claims should build it back up"""
425+
user = get_user_model().objects.create(username=f'test_hash_user1_{scenario_name}')
426+
427+
# Apply the scenario
428+
claims_scenario.apply_scenario(scenario_name, user)
429+
430+
# Save the claims to reapply later
431+
user_claims = get_user_claims(user)
432+
claims_hash = get_claims_hash(get_user_claims_hashable_form(user_claims))
433+
434+
# Rage delete everything
435+
for team in Team.objects.all():
436+
team.delete()
437+
438+
for org in Organization.objects.all():
439+
org.delete()
440+
441+
# If we deleted everything, the current object-related claims should be empty
442+
# The global roles might still have some content, we do not care here
443+
wrecked_user_claims = get_user_claims(user)
444+
assert wrecked_user_claims['objects'] == {'organization': [], 'team': []} # we deleted them all
445+
assert wrecked_user_claims['object_roles'] == {}
446+
447+
# Save the old claims, should rebuild all the objects, potentially complex operation, highly abstracted here
448+
save_user_claims(user, **user_claims)
449+
450+
# Now that everything is created anew, claims should match
451+
assert get_user_claims(user) == user_claims
452+
453+
# Verify current claims hash also matches
454+
assert get_claims_hash(get_user_claims_hashable_form(get_user_claims(user))) == claims_hash
455+
407456
@override_settings(DEBUG=True)
408457
def test_claims_query_performance_baseline(self, claims_scenario):
409458
"""Performance test to measure database queries for claims generation.

0 commit comments

Comments
 (0)