replace "status" with "error" and "msg" in error codes

This commit is contained in:
Thomas Rubini 2023-01-05 14:48:05 +01:00 committed by SIMAILA Djalim
parent 2548cab78e
commit 1ccabac77e
2 changed files with 19 additions and 21 deletions

View File

@ -27,8 +27,8 @@ def createGame(user:User):
if content is None: if content is None:
print("content is none") print("content is none")
raise Exception("Response is null") raise Exception("Response is null")
if content["status"] != "ok": if content["error"] != 0:
print(content["status"]) print(content["msg"])
raise Exception("Status is not ok") raise Exception("Status is not ok")
user.isAdmin = True user.isAdmin = True
return content["game_id"] return content["game_id"]
@ -43,8 +43,8 @@ def joinGame(user:User,game_id:str):
content = responseObject.json content = responseObject.json
if content is None: if content is None:
raise Exception("Response is null") raise Exception("Response is null")
if content["status"] != "ok": if content["error"] != 0:
print(content["status"]) print(content["msg"])
raise Exception("Status is not ok") raise Exception("Status is not ok")
return True return True
@ -56,8 +56,8 @@ def startGame(user:User):
content = responseObject.json content = responseObject.json
if content is None: if content is None:
raise Exception("Response is null") raise Exception("Response is null")
if content["status"] != "ok": if content["error"] != 0:
print(content["status"]) print(content["msg"])
raise Exception("Status is not ok") raise Exception("Status is not ok")
return True return True
@ -96,7 +96,7 @@ def test_that_two_person_having_the_same_pseudo_creating_two_games_results_in_tw
def test_that_not_sending_a_username_results_in_an_error(): def test_that_not_sending_a_username_results_in_an_error():
responseObject = test_app.post("/api/v1/createGame") responseObject = test_app.post("/api/v1/createGame")
assert responseObject.status_code == 200 assert responseObject.status_code == 200
assert responseObject.json["status"] != "ok" assert responseObject.json["error"] != 0
def test_that_sending_a_empty_username_results_in_an_error(): def test_that_sending_a_empty_username_results_in_an_error():
@ -144,20 +144,20 @@ def test_that_people_joining_without_sending_any_data_results_in_an_error():
game_id = createGame(User("neoxyde")) game_id = createGame(User("neoxyde"))
responseObject = test_app.post("/api/v1/joinGame") responseObject = test_app.post("/api/v1/joinGame")
assert responseObject.status_code == 200 assert responseObject.status_code == 200
assert responseObject.json["status"] != "ok" assert responseObject.json["error"] != 0
def test_that_people_joining_without_sending_a_game_id_results_in_an_error(): def test_that_people_joining_without_sending_a_game_id_results_in_an_error():
data={"username":"neomblic"} data={"username":"neomblic"}
responseObject = test_app.post("/api/v1/joinGame",data=data) responseObject = test_app.post("/api/v1/joinGame",data=data)
assert responseObject.status_code == 200 assert responseObject.status_code == 200
assert responseObject.json["status"] != "ok" assert responseObject.json["error"] != 0
def test_that_people_joining_without_sending_an_username_still_results_in_an_error(): def test_that_people_joining_without_sending_an_username_still_results_in_an_error():
game_id = createGame(User("neonyx")) game_id = createGame(User("neonyx"))
data={"game_id":game_id} data={"game_id":game_id}
responseObject = test_app.post("/api/v1/joinGame",data=data) responseObject = test_app.post("/api/v1/joinGame",data=data)
assert responseObject.status_code == 200 assert responseObject.status_code == 200
assert responseObject.json["status"] != "ok" assert responseObject.json["error"] != 0
def test_that_people_joining_with_an_empty_username_still_results_in_an_error(): def test_that_people_joining_with_an_empty_username_still_results_in_an_error():
game_id = createGame(User("neodeur")) game_id = createGame(User("neodeur"))

View File

@ -10,11 +10,11 @@ routes_api = flask.Blueprint("api", __name__)
def create_game(): def create_game():
username = flask.request.values.get("username") username = flask.request.values.get("username")
if username==None: if username==None:
return {"status": "error, username not set"} return {"error": 1, "msg": "username not set"}
response = {} response = {}
response["status"] = "ok" response["error"] = 0
game = game_logic.create_game(owner=username) game = game_logic.create_game(owner=username)
response["game_id"] = game.game_id response["game_id"] = game.game_id
@ -29,11 +29,11 @@ def join_game():
game_id = flask.request.values.get("game_id") game_id = flask.request.values.get("game_id")
username = flask.request.values.get("username") username = flask.request.values.get("username")
if game_id==None or username==None: if game_id==None or username==None:
return {"status": "error, username or game id not set"} return {"error": 1, "msg": "username or game id not set"}
game = game_logic.get_game(game_id) game = game_logic.get_game(game_id)
if game == None: if game == None:
return {"status": "error, game does not exist"} return {"error": 1, "msg": "game does not exist"}
game.add_member(username) game.add_member(username)
@ -42,17 +42,15 @@ def join_game():
flask.session["is_owner"] = False flask.session["is_owner"] = False
flask.session["username"] = username flask.session["username"] = username
response = {} return {"error": 0}
response["status"] = "ok"
return response
@routes_api.route("/startGame", methods=["GET", "POST"]) @routes_api.route("/startGame", methods=["GET", "POST"])
def start_game(): def start_game():
if not flask.session: if not flask.session:
return {"status": "No session"} return {"error": 1, "msg": "No session"}
if not flask.session["is_owner"]: if not flask.session["is_owner"]:
return {"status": "Error, you are not the owner of this game"} return {"error": 1, "msg": "you are not the owner of this game"}
if game_logic.get_game(flask.session["game_id"]) == None: if game_logic.get_game(flask.session["game_id"]) == None:
return {"status": "Error, this game doesn't exist"} return {"error": 1, "msg": "this game doesn't exist"}
return {"status": "ok"} return {"error": 0}