Add support for handling double posting of same user.
Fix non-json test to make builds green.
This commit is contained in:
parent
673a4180ee
commit
18c3ce53f0
|
@ -33,6 +33,11 @@ class InvalidOperationError(Exception):
|
||||||
self.message = message
|
self.message = message
|
||||||
|
|
||||||
|
|
||||||
|
class ConflictingResourceError(Exception):
|
||||||
|
def __init__(self, message=http.HTTPStatus.CONFLICT.description):
|
||||||
|
self.message = message
|
||||||
|
|
||||||
|
|
||||||
def get_message_for_status(http_code):
|
def get_message_for_status(http_code):
|
||||||
try:
|
try:
|
||||||
http_status = http.HTTPStatus(http_code)
|
http_status = http.HTTPStatus(http_code)
|
||||||
|
@ -76,6 +81,19 @@ def error_message_from_invalid_op_error(error: InvalidOperationError):
|
||||||
return flask.jsonify(error_message), http.HTTPStatus.FORBIDDEN
|
return flask.jsonify(error_message), http.HTTPStatus.FORBIDDEN
|
||||||
|
|
||||||
|
|
||||||
|
def error_message_from_conflicting_resource_error(error: ConflictingResourceError):
|
||||||
|
logger.error(f'Conflicting resource for "{flask.request.url}" because "{error.message}"')
|
||||||
|
error_message = {
|
||||||
|
'error': {
|
||||||
|
'status_code': http.HTTPStatus.CONFLICT,
|
||||||
|
'message': f'{error.message}',
|
||||||
|
'resource': flask.request.url,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return flask.jsonify(error_message), http.HTTPStatus.CONFLICT
|
||||||
|
|
||||||
|
|
||||||
def error_message_from_exception(error: Exception):
|
def error_message_from_exception(error: Exception):
|
||||||
logger.exception(f'Uncaught error while processing "{flask.request.url}":')
|
logger.exception(f'Uncaught error while processing "{flask.request.url}":')
|
||||||
error_message = {
|
error_message = {
|
||||||
|
@ -106,5 +124,6 @@ def attach_error_handlers_to_api_app(app):
|
||||||
app.register_error_handler(http_code, error_message_from_http_code)
|
app.register_error_handler(http_code, error_message_from_http_code)
|
||||||
|
|
||||||
app.register_error_handler(InvalidOperationError, error_message_from_invalid_op_error)
|
app.register_error_handler(InvalidOperationError, error_message_from_invalid_op_error)
|
||||||
|
app.register_error_handler(ConflictingResourceError, error_message_from_conflicting_resource_error)
|
||||||
app.register_error_handler(jsonschema.ValidationError, error_message_from_json_schema_validation_error)
|
app.register_error_handler(jsonschema.ValidationError, error_message_from_json_schema_validation_error)
|
||||||
app.register_error_handler(Exception, error_message_from_exception)
|
app.register_error_handler(Exception, error_message_from_exception)
|
||||||
|
|
|
@ -60,7 +60,10 @@ def create_user():
|
||||||
if requesting_user_role != models.UserRole.admin:
|
if requesting_user_role != models.UserRole.admin:
|
||||||
flask.abort(http.HTTPStatus.UNAUTHORIZED)
|
flask.abort(http.HTTPStatus.UNAUTHORIZED)
|
||||||
|
|
||||||
# Creates a user from the json.
|
# Creates a user from the json if no user exists with the given username.
|
||||||
|
if models.User.query.filter_by(username=incoming_request['username']).first():
|
||||||
|
raise errors.ConflictingResourceError('Existing resource already found. PUT to the resource to update it.')
|
||||||
|
|
||||||
user = models.User.from_json(incoming_request)
|
user = models.User.from_json(incoming_request)
|
||||||
db.session.add(user)
|
db.session.add(user)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
|
@ -171,11 +171,11 @@ def modify_non_existent_user_response(requester, api_base_uri):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@bdd.given(parsers.parse('I try to create an user with a non-JSON request'), target_fixture='user_response')
|
@bdd.given(parsers.parse('I try to create an user with a non-json request'), target_fixture='user_response')
|
||||||
def invalid_non_json_create_new_user_response(requester, api_base_uri):
|
def invalid_non_json_create_new_user_response(requester, api_base_uri):
|
||||||
return requests.post(
|
return requests.post(
|
||||||
url=f'{api_base_uri}/api/users',
|
url=f'{api_base_uri}/api/users',
|
||||||
body='',
|
data='',
|
||||||
headers={
|
headers={
|
||||||
'Authorization': f'JWT {requester.token}',
|
'Authorization': f'JWT {requester.token}',
|
||||||
},
|
},
|
||||||
|
@ -193,11 +193,11 @@ def invalid_empty_create_new_user_response(requester, api_base_uri):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@bdd.given(parsers.parse('I try to modify an user with a non-JSON request'), target_fixture='user_response')
|
@bdd.given(parsers.parse('I try to modify an user with a non-json request'), target_fixture='user_response')
|
||||||
def invalid_non_json_modify_user_response(requester, api_base_uri):
|
def invalid_non_json_modify_user_response(requester, api_base_uri):
|
||||||
return requests.put(
|
return requests.put(
|
||||||
url=f'{api_base_uri}/api/users/test_user',
|
url=f'{api_base_uri}/api/users/test_user',
|
||||||
body='',
|
data='',
|
||||||
headers={
|
headers={
|
||||||
'Authorization': f'JWT {requester.token}',
|
'Authorization': f'JWT {requester.token}',
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue