Exemple #1
0
    def test_skip_existing_groups(self):
        # same test-setup as above, only we skip existing groups
        backend.create_user(username1)
        backend.create_user(username2)
        backend.create_user(username3)
        backend.create_user(username4)  # new user

        # this group already exists and has some memberships
        backend.create_group(group=groupname2, service=self.service)
        backend.add_member(group=groupname2, service=self.service, user=username1)
        backend.add_member(group=groupname2, service=self.service, user=username4)

        path = os.path.join(self.base, 'groups1.json')
        with capture() as (stdout, stderr):
            try:
                restauth_import(['--skip-existing-groups', path])
            except SystemExit:
                self.fail(stderr.getvalue())
            self.assertHasLine(stdout, r'^Groups:$')
            self.assertHasLine(stdout, r'^\* %s: created\.$' % groupname1)
            self.assertHasLine(stdout, r'^\* %s: Already exists, skipping\.$' % groupname2)
            self.assertHasLine(stdout, r'^\* %s: created\.$' % groupname3)
            self.assertHasLine(stdout, r'^\* %s: created\.$' % groupname4)

        # test memberships
        self.assertCountEqual(backend.members(group=groupname1, service=None), [])
        self.assertCountEqual(backend.members(group=groupname2, service=self.service), [username1, username4])

        # group3 now is not a subgroup, because group2 already existed and we skipped its data
        self.assertEqual(backend.members(group=groupname3, service=self.service), [username3])
        self.assertEqual(backend.members(group=groupname4, service=None), [])
Exemple #2
0
    def test_existing_groups(self):
        backend.create_user(username1)
        backend.create_user(username2)
        backend.create_user(username3)
        backend.create_user(username4)  # new user

        # this group already exists and has some memberships
        backend.create_group(group=groupname2, service=self.service)
        backend.add_member(group=groupname2, service=self.service, user=username1)
        backend.add_member(group=groupname2, service=self.service, user=username4)

        path = os.path.join(self.base, 'groups1.json')
        with capture() as (stdout, stderr):
            restauth_import([path])
            self.assertEqual(stderr.getvalue(), '')
            self.assertHasLine(stdout, r'^Groups:$')
            self.assertHasLine(stdout, r'^\* %s: created\.$' % groupname1)
            self.assertHasLine(stdout, r'^\* %s: Already exists, adding memberships\.$' % groupname2)
            self.assertHasLine(stdout, r'^\* %s: created\.$' % groupname3)
            self.assertHasLine(stdout, r'^\* %s: created\.$' % groupname4)

        # test memberships
        self.assertCountEqual(backend.members(group=groupname1, service=None), [])
        self.assertCountEqual(backend.members(group=groupname2, service=self.service),
                              [username1, username2, username4])
        if backend.SUPPORTS_SUBGROUPS is True:
            self.assertCountEqual(backend.members(group=groupname3, service=self.service),
                                  [username1, username2, username3, username4])
            self.assertCountEqual(backend.members(group=groupname4, service=None),
                                  [username1, username2, username4])
        else:
            self.assertCountEqual(backend.members(group=groupname3, service=self.service),
                                  [username3])
            self.assertCountEqual(backend.members(group=groupname4, service=None), [])
Exemple #3
0
    def post(self, request, largs, dry=False):
        """Create a new group."""

        if not request.user.has_perm('Groups.group_create'):
            return HttpResponseForbidden()

        # If BadRequest: 400 Bad Request
        name, users = self._parse_post(request)
        name = stringcheck(name)

        # If ResourceExists: 409 Conflict
        # If UserNotFound: 404 Not Found
        backend.create_group(service=request.user, group=name, users=users, dry=dry)

        self.log.info('%s: Created group', name, extra=largs)
        return HttpResponseCreated()  # Created
def save_groups(groups, args, parser):
    if groups:
        print("Groups:")

    subgroups = {}
    for name, data in six.iteritems(groups):
        service = data.pop('service', None)
        if service:
            service = Service.objects.get(username=service)

        try:
            backend.create_group(group=name, service=service)
            print("* %s: created." % name)
        except GroupExists:
            if args.skip_existing_groups:
                print("* %s: Already exists, skipping." % name)
                continue
            else:
                print("* %s: Already exists, adding memberships." % name)

        for username in data.get('users', []):
            backend.add_member(group=name, service=service, user=username)

        if 'subgroups' in data:
            subgroups[(name, service)] = data.pop('subgroups')

    # add group-memberships *after* we created all groups to make sure
    # groups already exist.
    if backend.SUPPORTS_SUBGROUPS:
        for group, subgroups_data in six.iteritems(subgroups):
            for subgroup_data in subgroups_data:
                name = subgroup_data['name']
                service = subgroup_data.get('service')
                if service:
                    service = Service.objects.get(username=service)

                backend.add_subgroup(group=group[0], service=group[1], subgroup=name,
                                     subservice=service)
    else:
        print('Warning: Backend does not support subgroups, subgroups discarded.')
def _main(args):
    # Actions that do not act on an existing group:
    if args.action == 'add':
        try:
            backend.create_group(group=args.group, service=args.service)
        except GroupExists:
            parser.error('Group already exists.')
    elif args.action in ['list', 'ls']:
        groups = backend.list_groups(service=args.service)

        for name in sorted(groups):
            print(name)
    elif args.action == 'view':
        explicit_users = sorted(backend.members(group=args.group, service=args.service, depth=0))
        effective_users = sorted(backend.members(group=args.group, service=args.service))

        if explicit_users:
            print('* Explicit members: %s' % ', '.join(explicit_users))
        else:
            print('* No explicit members')
        if backend.SUPPORTS_SUBGROUPS:
            if effective_users:
                print('* Effective members: %s' % ', '.join(effective_users))
            else:
                print('* No effective members')

        if backend.SUPPORTS_SUBGROUPS is True:
            parents = backend.parents(group=args.group, service=args.service)
            sub_groups = backend.subgroups(group=args.group, service=args.service, filter=False)

            if parents:
                if backend.SUPPORTS_GROUP_VISIBILITY:
                    print('* Parent groups:')
                    parents = sorted(parents, key=lambda g: g[1].username if g[1] else '')
                    print_by_service(parents, '    ')
                else:
                    print('* Parent groups: %s' % ', '.join([g[0] for g in parents]))
            else:
                print('* No parent groups')

            if sub_groups:
                if backend.SUPPORTS_GROUP_VISIBILITY:
                    print('* Subgroups:')
                    sub_groups = sorted(sub_groups, key=lambda g: g[1].username if g[1] else '')
                    print_by_service(sub_groups, '    ')
                else:
                    print('* Subgroups: %s' % ', '.join([g[0] for g in sub_groups]))
            else:
                print('* No subgroups')
    elif args.action == 'set-service':
        if backend.SUPPORTS_GROUP_VISIBILITY is False:
            parser.error('Backend does not support group visiblity.')
        backend.set_service(group=args.group, service=args.service,
                                  new_service=args.new_service)
    elif args.action == 'add-user':
        backend.add_member(group=args.group, service=args.service, user=args.user)
    elif args.action == 'add-group':
        if backend.SUPPORTS_SUBGROUPS is False:
            parser.error('Backend does not support subgroups.')
        backend.add_subgroup(group=args.group, service=args.service, subgroup=args.subgroup,
                             subservice=args.sub_service)
    elif args.action in ['delete', 'del', 'rm']:
        backend.remove_group(group=args.group, service=args.service)
    elif args.action in ['remove-user', 'rm-user', 'del-user']:
        try:
            backend.remove_member(group=args.group, service=args.service, user=args.user)
        except UserNotFound:
            parser.error('User "%s" not member of group "%s".' % (args.user, args.group))
    elif args.action == 'rename':
        backend.rename_group(args.group, args.name, service=args.service)
    elif args.action in ['remove-group', 'rm-group', 'del-group']:  # pragma: no branch
        if backend.SUPPORTS_SUBGROUPS is False:
            parser.error('Backend does not support subgroups.')
        try:
            backend.remove_subgroup(group=args.group, service=args.service, subgroup=args.subgroup,
                                    subservice=args.sub_service)
        except GroupNotFound:
            parser.error('Group "%s" is not a subgroup of "%s".' % (args.subgroup, args.group))
Exemple #6
0
    def test_dry_run_create_existing_group(self):
        backend.create_group(service=self.service, group=groupname1)

        resp = self.post('/test/groups/', {'group': groupname1})
        self.assertEqual(resp.status_code, http_client.CONFLICT)
        self.assertCountEqual([groupname1], backend.list_groups(service=self.service))