def test_http_forbidden_with_bytes_body(self): response = FakeResponse() helpers.forbidden(response, b'Conflicting request') self.assertEqual(response.content_type, 'application/json; charset=UTF-8') self.assertEqual(json.loads(response.body), { 'title': '403 Forbidden', 'description': 'Conflicting request', })
def on_post(self, request, response): # We do not want to encrypt the plaintext password given in the POST # data. That would hash the password, but we need to have the # plaintext in order to pass into passlib. validator = Validator(cleartext_password=GetterSetter(str)) try: values = validator(request) except ValueError as error: bad_request(response, str(error)) return is_valid, new_hash = config.password_context.verify( values['cleartext_password'], self._user.password) if is_valid: if new_hash is not None: self._user.password = new_hash no_content(response) else: forbidden(response)
def on_post(self, request, response): """Link a user to the address, and create it if needed.""" if self._user: conflict(response) return # When creating a linked user by POSTing, the user either must already # exist, or it can be automatically created, if the auto_create flag # is given and true (if missing, it defaults to true). However, in # this case we do not accept 'email' as a POST field. fields = CREATION_FIELDS.copy() del fields['email'] fields['user_id'] = self.api.to_uuid fields['auto_create'] = as_boolean fields['_optional'] = fields['_optional'] + ('user_id', 'auto_create', 'is_server_owner') try: validator = Validator(**fields) arguments = validator(request) except ValueError as error: bad_request(response, str(error)) return user_manager = getUtility(IUserManager) if 'user_id' in arguments: user_id = arguments['user_id'] user = user_manager.get_user_by_id(user_id) if user is None: bad_request( response, 'No user with ID {}'.format( self.api.from_uuid(user_id)).encode()) return okay(response) else: auto_create = arguments.pop('auto_create', True) if auto_create: # This sets the 201 or 400 status. user = create_user(self.api, arguments, response) if user is None: return else: forbidden(response) return user.link(self._address)
def on_post(self, request, response): """Link a user to the address, and create it if needed.""" if self._user: conflict(response) return # When creating a linked user by POSTing, the user either must already # exist, or it can be automatically created, if the auto_create flag # is given and true (if missing, it defaults to true). However, in # this case we do not accept 'email' as a POST field. fields = CREATION_FIELDS.copy() del fields['email'] fields['user_id'] = int fields['auto_create'] = as_boolean fields['_optional'] = fields['_optional'] + ( 'user_id', 'auto_create', 'is_server_owner') try: validator = Validator(**fields) arguments = validator(request) except ValueError as error: bad_request(response, str(error)) return user_manager = getUtility(IUserManager) if 'user_id' in arguments: raw_uid = arguments['user_id'] user_id = UUID(int=raw_uid) user = user_manager.get_user_by_id(user_id) if user is None: not_found(response, b'No user with ID {}'.format(raw_uid)) return okay(response) else: auto_create = arguments.pop('auto_create', True) if auto_create: # This sets the 201 or 400 status. user = create_user(arguments, request, response) if user is None: return else: forbidden(response) return user.link(self._address)
def test_forbidden_body_is_none(self): response = FakeResponse() helpers.forbidden(response, body=None) self.assertEqual(response.body, "not set")
def test_forbidden_body_is_none(self): response = FakeResponse() helpers.forbidden(response, body=None) self.assertEqual(response.body, 'not set')