def assert_users_count(self, authors=0, curators=0, admins=0): """ Helper function to count users in the different groups """ total = authors + curators + admins self.assertEqual(len(list_users_in_repo(self.repo)), total) self.assertEqual( len(list_users_in_repo(self.repo, BaseGroupTypes.AUTHORS)), authors) self.assertEqual( len(list_users_in_repo(self.repo, BaseGroupTypes.CURATORS)), curators) self.assertEqual( len(list_users_in_repo(self.repo, BaseGroupTypes.ADMINISTRATORS)), admins)
def get_queryset(self): """ Return a list of repository members """ repo = Repository.objects.get(slug=self.kwargs['repo_slug']) group_type = self.kwargs.get('group_type') return list_users_in_repo(repo, group_type)
def test_list_users_in_repo_invalid_group_type_specified(self): """ Test for list_users_in_repo """ self.assertRaises( api.InvalidGroupType, lambda: api.list_users_in_repo(self.repo, 'foo_type') )
def assert_users_count(self, authors=0, curators=0, admins=0): """ Helper function to count users in the different groups """ total = authors + curators + admins self.assertEqual(len(list_users_in_repo(self.repo)), total) self.assertEqual( len(list_users_in_repo(self.repo, BaseGroupTypes.AUTHORS)), authors ) self.assertEqual( len(list_users_in_repo(self.repo, BaseGroupTypes.CURATORS)), curators ) self.assertEqual( len(list_users_in_repo(self.repo, BaseGroupTypes.ADMINISTRATORS)), admins )
def remove_all_users_from_repo(self): """ Helper method to remove all users from all repo groups. """ # Remove all the users from all the groups in the repo. for user_group in list_users_in_repo(self.repo): user = User.objects.get(username=user_group.username) group_type = GroupTypes.get_repo_groupname_by_base( user_group.group_type) remove_user_from_repo_group(user, self.repo, group_type)
def remove_all_users_from_repo(self): """ Helper method to remove all users from all repo groups. """ # Remove all the users from all the groups in the repo. for user_group in list_users_in_repo(self.repo): user = User.objects.get(username=user_group.username) group_type = GroupTypes.get_repo_groupname_by_base( user_group.group_type ) remove_user_from_repo_group(user, self.repo, group_type)
def get_queryset(self): """ Return groups for a repository member """ repo = Repository.objects.get(slug=self.kwargs['repo_slug']) username = self.kwargs.get('username') return list( set( user_group for user_group in list_users_in_repo(repo) if user_group.username == username ) )
def get_object(self): """ Return details about a group for a user in a repo """ repo = Repository.objects.get(slug=self.kwargs['repo_slug']) username = self.kwargs.get('username') group_type = self.kwargs.get('group_type') user_groups = list_users_in_repo(repo, group_type) # There can be max only one user with the specified username in a group for user_group in user_groups: if user_group.username == username: return user_group raise Http404()
def test_list_users_in_repo_base_group_type_specified(self): """ Test for list_users_in_repo """ self.assertListEqual( api.list_users_in_repo(self.repo), [ UserGroup(self.user.username, BaseGroupTypes.ADMINISTRATORS) ] ) self.assertListEqual( api.list_users_in_repo(self.repo, BaseGroupTypes.ADMINISTRATORS), [ UserGroup(self.user.username, BaseGroupTypes.ADMINISTRATORS) ] ) self.assertListEqual( api.list_users_in_repo(self.repo, BaseGroupTypes.CURATORS), [] ) self.assertListEqual( api.list_users_in_repo(self.repo, BaseGroupTypes.AUTHORS), [] )
def test_list_users_in_repo_no_base_group_type_specified(self): """ Test for list_users_in_repo """ self.assertListEqual( api.list_users_in_repo(self.repo), [ UserGroup(self.user.username, BaseGroupTypes.ADMINISTRATORS) ] ) # Remove the user from the administrators. api.remove_user_from_repo_group( self.user, self.repo, GroupTypes.REPO_ADMINISTRATOR ) # No users in the repo. self.assertListEqual( api.list_users_in_repo(self.repo), [] ) # Add user to the curators. api.assign_user_to_repo_group( self.user, self.repo, GroupTypes.REPO_CURATOR ) self.assertListEqual( api.list_users_in_repo(self.repo), [ UserGroup(self.user.username, BaseGroupTypes.CURATORS) ] ) # Add user back to the administrators. api.assign_user_to_repo_group( self.user, self.repo, GroupTypes.REPO_ADMINISTRATOR ) self.assertListEqual( self.sort_user_group( api.list_users_in_repo(self.repo) ), self.sort_user_group( [ UserGroup( self.user.username, BaseGroupTypes.ADMINISTRATORS ), UserGroup( self.user.username, BaseGroupTypes.CURATORS ) ] ) ) # Add another user to the authors. api.assign_user_to_repo_group( self.user_norepo, self.repo, GroupTypes.REPO_AUTHOR ) self.assertListEqual( self.sort_user_group( api.list_users_in_repo(self.repo) ), self.sort_user_group( [ UserGroup( self.user.username, BaseGroupTypes.ADMINISTRATORS ), UserGroup( self.user.username, BaseGroupTypes.CURATORS ), UserGroup( self.user_norepo.username, BaseGroupTypes.AUTHORS ) ] ) ) # Adding again the same user in the same group # will not create multiple instances. api.assign_user_to_repo_group( self.user_norepo, self.repo, GroupTypes.REPO_AUTHOR ) self.assertListEqual( self.sort_user_group( api.list_users_in_repo(self.repo) ), self.sort_user_group( [ UserGroup( self.user.username, BaseGroupTypes.ADMINISTRATORS ), UserGroup( self.user.username, BaseGroupTypes.CURATORS ), UserGroup( self.user_norepo.username, BaseGroupTypes.AUTHORS ) ] ) )