Island: Return 400 from POST /api/island_mode on invalid JSON

This commit is contained in:
Mike Salvatore 2021-07-13 10:58:08 -04:00
parent 26d3782a66
commit a0fb6fa2b6
2 changed files with 12 additions and 2 deletions

View File

@ -14,12 +14,16 @@ logger = logging.getLogger(__name__)
class IslandMode(flask_restful.Resource):
@jwt_required
def post(self):
body = json.loads(request.data)
mode_str = body.get("mode")
try:
body = json.loads(request.data)
mode_str = body.get("mode")
mode = IslandModeEnum(mode_str)
set_mode(mode)
return make_response({}, 200)
except (AttributeError, json.decoder.JSONDecodeError):
return make_response({}, 400)
except ValueError:
return make_response({}, 422)
except Exception:

View File

@ -27,6 +27,12 @@ def test_island_mode_post__invalid_mode(flask_client):
assert resp.status_code == 422
@pytest.mark.parametrize("invalid_json", ["42", "{test"])
def test_island_mode_post__invalid_json(flask_client, invalid_json):
resp = flask_client.post("/api/island-mode", data="{test", follow_redirects=True)
assert resp.status_code == 400
def test_island_mode_post__internal_server_error(monkeypatch, flask_client):
monkeypatch.setattr(island_mode_resource, "set_mode", lambda x: raise_(Exception()))