diff --git a/server/app/apis/questions.py b/server/app/apis/questions.py index 48857dcb93926c7ddf1598d90381c6c984865e11..55db2819949adae273c230c0f9548a6b2843db37 100644 --- a/server/app/apis/questions.py +++ b/server/app/apis/questions.py @@ -27,6 +27,7 @@ class QuestionsList(Resource): @jwt_required def post(self, SID, CID): args = question_parser.parse_args(strict=True) + del args["slide_id"] item_slide = dbc.get.slide(CID, SID) item = dbc.add.question(item_slide=item_slide, **args) @@ -39,20 +40,20 @@ class QuestionsList(Resource): class Questions(Resource): @jwt_required def get(self, CID, SID, QID): - item_question = dbc.get.question(CID, QID) + item_question = dbc.get.question(CID, SID, QID) return item_response(schema.dump(item_question)) @jwt_required def put(self, CID, SID, QID): args = question_parser.parse_args(strict=True) - item_question = dbc.get.question(CID, QID) - item_question = dbc.edit.question(item_question, slide_id=SID, **args) + item_question = dbc.get.question(CID, SID, QID) + item_question = dbc.edit.question(item_question, **args) return item_response(schema.dump(item_question)) @jwt_required def delete(self, CID, SID, QID): - item_question = dbc.get.question(CID, QID) + item_question = dbc.get.question(CID, SID, QID) dbc.delete.question(item_question) return {}, codes.NO_CONTENT diff --git a/server/app/core/parsers.py b/server/app/core/parsers.py index 527961e5114b36f45d3e72832c2d7f3f10fe9e98..54f54239c61f4b81c7f6903d70e6d2c38b9d1ca7 100644 --- a/server/app/core/parsers.py +++ b/server/app/core/parsers.py @@ -58,7 +58,7 @@ question_parser = reqparse.RequestParser() question_parser.add_argument("name", type=str, default=None, location="json") question_parser.add_argument("total_score", type=int, default=None, location="json") question_parser.add_argument("type_id", type=int, default=None, location="json") - +question_parser.add_argument("slide_id", type=int, location="json") ###TEAM#### team_parser = reqparse.RequestParser() diff --git a/server/app/database/controller/get.py b/server/app/database/controller/get.py index 892d251b2999102dc185f3748dc7c8fe4ad68b06..1669f0ae3c1fdd2eb9125586a84d070eacb14acc 100644 --- a/server/app/database/controller/get.py +++ b/server/app/database/controller/get.py @@ -56,9 +56,9 @@ def team(CID, TID, required=True, error_msg=None): return Team.query.filter((Team.competition_id == CID) & (Team.id == TID)).first_extended(required, error_msg) -def question(CID, QID, required=True, error_msg=None): +def question(CID, SID, QID, required=True, error_msg=None): return ( - Question.query.join(Slide, (Slide.competition_id == CID) & (Slide.id == Question.slide_id)) + Question.query.join(Slide, (Slide.competition_id == CID) & (Slide.id == SID) & (Slide.id == Question.slide_id)) .filter(Question.id == QID) .first_extended(required, error_msg) ) diff --git a/server/tests/test_app.py b/server/tests/test_app.py index 7effe7caf15976760ccd4fe8cea9bcfb88fd72b8..8086774e1412c02c480ce3a57463ff6e09aab5f0 100644 --- a/server/tests/test_app.py +++ b/server/tests/test_app.py @@ -2,8 +2,7 @@ import app.core.http_codes as codes from app.database.models import Slide from tests import app, client, db -from tests.test_helpers import (add_default_values, change_order_test, delete, - get, post, put) +from tests.test_helpers import add_default_values, change_order_test, delete, get, post, put def test_misc_api(client): @@ -376,6 +375,7 @@ def test_question_api(client): # Get question QID = 4 + SID = 4 response, item_question = get(client, f"/api/competitions/{CID}/slides/{SID}/questions/{QID}", headers=headers) assert response.status_code == codes.OK assert item_question["id"] == QID @@ -384,7 +384,7 @@ def test_question_api(client): name = "Nyare namn" # total_score = 2 type_id = 3 - slide_id = 1 + SID = 1 QID = 1 response, _ = put( client, @@ -402,31 +402,32 @@ def test_question_api(client): name = "Nyare namn" # total_score = 2 type_id = 3 - slide_id = 5 + SID = 4 + NEW_SID = 5 QID = 4 assert item_question["name"] != name # assert item_question["total_score"] != total_score assert item_question["type"]["id"] != type_id - assert item_question["slide_id"] != slide_id + assert item_question["slide_id"] != NEW_SID response, item_question = put( client, f"/api/competitions/{CID}/slides/{SID}/questions/{QID}", # {"name": name, "total_score": total_score, "type_id": type_id, "slide_id": slide_id}, - {"name": name, "type_id": type_id}, + {"name": name, "type_id": type_id, "slide_id": NEW_SID}, headers=headers, ) assert response.status_code == codes.OK assert item_question["name"] == name # # assert item_question["total_score"] == total_score assert item_question["type"]["id"] == type_id - assert item_question["slide_id"] == slide_id + assert item_question["slide_id"] == NEW_SID # Checks number of questions response, body = get(client, f"/api/competitions/{CID}/questions", headers=headers) assert response.status_code == codes.OK assert body["count"] == num_questions # Delete question - response, _ = delete(client, f"/api/competitions/{CID}/slides/{SID}/questions/{QID}", headers=headers) + response, _ = delete(client, f"/api/competitions/{CID}/slides/{NEW_SID}/questions/{QID}", headers=headers) num_questions -= 1 assert response.status_code == codes.NO_CONTENT @@ -436,5 +437,5 @@ def test_question_api(client): assert body["count"] == num_questions # Tries to delete question again - response, _ = delete(client, f"/api/competitions/{CID}/slides/{SID}/questions/{QID}", headers=headers) + response, _ = delete(client, f"/api/competitions/{CID}/slides/{NEW_SID}/questions/{QID}", headers=headers) assert response.status_code == codes.NOT_FOUND