Пример #1
0
    def test_prohibited(self):
        chars = 0
        for char in prohibition_table:
            if isinstance(char, tuple):
                for c in self.unirange(char[0], char[1]):
                    with self.assertRaises(PreconditionFailed):
                        chars += 1
                        stringcheck(c)
            else:
                if char in b1_table:
                    continue
                chars += 1

                with self.assertRaises(PreconditionFailed):
                    stringcheck(char)
Пример #2
0
    def put(self, request, largs, name):
        """Set multiple properties."""

        if not request.user.has_perm('Users.prop_create'):
            return HttpResponseForbidden()

        properties = {stringcheck(k): v for k, v in six.iteritems(parse_dict(request))}

        # If UserNotFound: 404 Not Found
        backend.set_properties(user=name, properties=properties)
        return HttpResponseNoContent()
Пример #3
0
    def post(self, request, largs, dry=False):
        """Create a new user."""

        if not request.user.has_perm('Users.user_create'):
            return HttpResponseForbidden()

        name, password, properties, groups = self._parse_post(request)
        name = stringcheck(name)

        # If UsernameInvalid: 412 Precondition Failed
        validate_username(name)

        # check password:
        if password:
            if len(password) < settings.MIN_PASSWORD_LENGTH:
                # If PasswordInvalid: 412 Precondition Failed
                raise PasswordInvalid("Password too short")
        else:
            password = None

        # normalize properties, add date-joined if not present
        if properties is None:
            properties = {
                'date joined': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            }
        else:
            properties = {stringcheck(k): v for k, v in six.iteritems(properties)}
            if 'date joined' not in properties:
                properties['date joined'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

        # normalize groups
        if groups:
            groups = [(stringcheck(g), request.user) for g in groups]

        # If UserExists: 409 Conflict
        backend.create_user(user=name, password=password, properties=properties, groups=groups,
                            dry=dry)
        self.log.info('%s: Created user', name, extra=largs)

        return HttpResponseCreated()
Пример #4
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
Пример #5
0
    def post(self, request, largs, name, dry=False):
        """Create a new property."""

        if not request.user.has_perm('Users.prop_create'):
            return HttpResponseForbidden()

        # If AssertionError: 400 Bad Request
        key, value = self._parse_post(request)
        key = stringcheck(key)

        # If UserNotFound: 404 Not Found
        # If PropertyExists: 409 Conflict
        backend.create_property(user=name, key=key, value=value, dry=dry)

        self.log.info('Created property "%s" as "%s"', key, value, extra=largs)
        return HttpResponseCreated()
Пример #6
0
    def __call__(self, parser, namespace, value, option_string):
        if not six.PY3:  # pragma: no branch, pragma: py2
            value = value.decode('utf-8')
        user = stringprep(value)

        if namespace.create_user:
            try:
                user = stringcheck(user)
            except PreconditionFailed:
                raise ArgumentError(self, "Username contains invalid characters")

            try:
                validate_username(user)
                backend.create_user(user=user)
            except UserExists:
                raise ArgumentError(self, 'User already exists.')
            except PreconditionFailed as e:
                raise ArgumentError(self, e)

        setattr(namespace, self.dest, user)
Пример #7
0
 def test_b1(self):
     teststr = ''.join(b1_table)
     self.assertEqual('', stringprep(teststr))
     self.assertEqual('', stringcheck(teststr))
Пример #8
0
 def test_ok(self):
     self.assertEqual(username1, stringprep(username1))
     self.assertEqual(username2, stringprep(username2))
     self.assertEqual(username1, stringcheck(username1))
     self.assertEqual(username2, stringcheck(username2))