@@ -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