def test_implicit_deletes(alice): 'Ensures implicit resources and roles delete themselves' delorg = Organization.objects.create(name='test-org') child = Role.objects.create() child.parents.add(delorg.admin_role) delorg.admin_role.members.add(alice) admin_role_id = delorg.admin_role.id auditor_role_id = delorg.auditor_role.id assert child.ancestors.count() > 1 assert Role.objects.filter(id=admin_role_id).count() == 1 assert Role.objects.filter(id=auditor_role_id).count() == 1 n_alice_roles = alice.roles.count() n_system_admin_children = Role.singleton( 'system_administrator').children.count() delorg.delete() assert Role.objects.filter(id=admin_role_id).count() == 0 assert Role.objects.filter(id=auditor_role_id).count() == 0 assert alice.roles.count() == (n_alice_roles - 1) assert Role.singleton('system_administrator').children.count() == ( n_system_admin_children - 1) assert child.ancestors.count() == 1 assert child.ancestors.all()[0] == child
def sync_superuser_status_to_rbac(instance, **kwargs): 'When the is_superuser flag is changed on a user, reflect that in the membership of the System Admnistrator role' update_fields = kwargs.get('update_fields', None) if update_fields and 'is_superuser' not in update_fields: return if instance.is_superuser: Role.singleton(ROLE_SINGLETON_SYSTEM_ADMINISTRATOR).members.add(instance) else: Role.singleton(ROLE_SINGLETON_SYSTEM_ADMINISTRATOR).members.remove(instance)
def test_consistency_of_is_superuser_flag(reverse): users = [User.objects.create(username='******'.format(i)) for i in range(2)] for u in users: assert u.is_superuser is False system_admin = Role.singleton('system_administrator') if reverse: for u in users: u.roles.add(system_admin) else: system_admin.members.add(*[u.id for u in users]) # like .add(42, 54) for u in users: u.refresh_from_db() assert u.is_superuser is True users[0].roles.clear() for u in users: u.refresh_from_db() assert users[0].is_superuser is False assert users[1].is_superuser is True system_admin.members.clear() for u in users: u.refresh_from_db() assert u.is_superuser is False
def test_indirect_access_list(get, organization, project, team_factory, user, admin): project_admin = user('project_admin') project_admin_team_member = user('project_admin_team_member') team_admin = user('team_admin') project_admin_team = team_factory('project-admin-team') project.admin_role.members.add(project_admin) project_admin_team.member_role.members.add(project_admin_team_member) project_admin_team.member_role.children.add(project.admin_role) project_admin_team.admin_role.members.add(team_admin) result = get(reverse('api:project_access_list', kwargs={'pk': project.id}), admin) assert result.status_code == 200 # Result should be: # project_admin should have direct access, # project_team_admin should have "direct" access through being a team member -> project admin, # team_admin should have direct access the same as the project_team_admin, # admin should have access through system admin -> org admin -> project admin assert result.data['count'] == 4 project_admin_res = [ r for r in result.data['results'] if r['id'] == project_admin.id ][0] team_admin_res = [ r for r in result.data['results'] if r['id'] == team_admin.id ][0] project_admin_team_member_res = [ r for r in result.data['results'] if r['id'] == project_admin_team_member.id ][0] admin_res = [r for r in result.data['results'] if r['id'] == admin.id][0] assert len(project_admin_res['summary_fields']['direct_access']) == 1 assert len(project_admin_res['summary_fields']['indirect_access']) == 0 assert len(team_admin_res['summary_fields']['direct_access']) == 1 assert len(team_admin_res['summary_fields']['indirect_access']) == 0 assert len(admin_res['summary_fields']['direct_access']) == 0 assert len(admin_res['summary_fields']['indirect_access']) == 1 project_admin_entry = project_admin_res['summary_fields']['direct_access'][ 0]['role'] assert project_admin_entry['id'] == project.admin_role.id # assure that results for team admin are the same as for team member team_admin_entry = team_admin_res['summary_fields']['direct_access'][0][ 'role'] assert team_admin_entry['id'] == project.admin_role.id assert team_admin_entry['name'] == 'Admin' project_admin_team_member_entry = project_admin_team_member_res[ 'summary_fields']['direct_access'][0]['role'] assert project_admin_team_member_entry['id'] == project.admin_role.id assert project_admin_team_member_entry['team_id'] == project_admin_team.id assert project_admin_team_member_entry[ 'team_name'] == project_admin_team.name admin_entry = admin_res['summary_fields']['indirect_access'][0]['role'] assert admin_entry['name'] == Role.singleton('system_administrator').name