Skip to content
Snippets Groups Projects
Commit 08cc17b6 authored by Joffjoff5000's avatar Joffjoff5000
Browse files

Test slides api (almost) fully

parent 6e08b4d3
No related branches found
No related tags found
1 merge request!55Resolve "Add more api calls"
...@@ -22,11 +22,11 @@ def admin_required(): ...@@ -22,11 +22,11 @@ def admin_required():
return wrapper return wrapper
def text_response(message, code=200): def text_response(message, code=codes.OK):
return {"message": message}, 200 return {"message": message}, codes.OK
def list_response(items, total=None, code=200): def list_response(items, total=None, code=codes.OK):
if type(items) is not list: if type(items) is not list:
abort(codes.INTERNAL_SERVER_ERROR) abort(codes.INTERNAL_SERVER_ERROR)
if not total: if not total:
...@@ -34,7 +34,7 @@ def list_response(items, total=None, code=200): ...@@ -34,7 +34,7 @@ def list_response(items, total=None, code=200):
return {"items": items, "count": len(items), "total_count": total}, code return {"items": items, "count": len(items), "total_count": total}, code
def item_response(item, code=200): def item_response(item, code=codes.OK):
if isinstance(item, list): if isinstance(item, list):
abort(codes.INTERNAL_SERVER_ERROR) abort(codes.INTERNAL_SERVER_ERROR)
return item, code return item, code
......
...@@ -22,8 +22,8 @@ class QuestionsList(Resource): ...@@ -22,8 +22,8 @@ class QuestionsList(Resource):
name = args.get("name") name = args.get("name")
# total_score = args.get("total_score") # total_score = args.get("total_score")
slide_id = args.get("slide_id")
type_id = args.get("type_id") type_id = args.get("type_id")
slide_id = args.get("slide_id")
item_slide = dbc.get.slide(CID, slide_id) item_slide = dbc.get.slide(CID, slide_id)
item = dbc.add.question(name, item_slide.order, type_id, item_slide) item = dbc.add.question(name, item_slide.order, type_id, item_slide)
...@@ -36,8 +36,12 @@ class QuestionsList(Resource): ...@@ -36,8 +36,12 @@ class QuestionsList(Resource):
class Questions(Resource): class Questions(Resource):
@jwt_required @jwt_required
def get(self, CID, QID): def get(self, CID, QID):
item = Question.query.filter(Question.id == QID).first() item_question = Question.query.filter(Question.id == int(QID)).first()
return item_response(schema.dump(item))
if item_question.slide.competition.id != int(CID):
api.abort(codes.NOT_FOUND, f"Could not find question with id {QID} in competition with id {CID}.")
return item_response(schema.dump(item_question))
@jwt_required @jwt_required
def put(self, CID, QID): def put(self, CID, QID):
...@@ -48,6 +52,9 @@ class Questions(Resource): ...@@ -48,6 +52,9 @@ class Questions(Resource):
type_id = args.get("type_id") type_id = args.get("type_id")
item_question = Question.query.filter(Question.id == QID).first() item_question = Question.query.filter(Question.id == QID).first()
if item_question.slide.competition.id != int(CID):
api.abort(codes.NOT_FOUND, f"Could not find question with id {QID} in competition with id {CID}.")
item_question = dbc.edit.question(item_question, name=name, slide_id=slide_id, type_id=type_id) item_question = dbc.edit.question(item_question, name=name, slide_id=slide_id, type_id=type_id)
return item_response(schema.dump(item_question)) return item_response(schema.dump(item_question))
...@@ -74,17 +81,18 @@ class QuestionSearch(Resource): ...@@ -74,17 +81,18 @@ class QuestionSearch(Resource):
# total_score = args.get("total_score") # total_score = args.get("total_score")
slide_id = args.get("slide_id") slide_id = args.get("slide_id")
type_id = args.get("type_id") type_id = args.get("type_id")
competition_id = args.get("competition_id")
page = args.get("page", 0) page = args.get("page", 0)
page_size = args.get("page_size", 15) page_size = args.get("page_size", 15)
order = args.get("order", 1) order = args.get("order", 1)
order_by = args.get("order_by") order_by = args.get("order_by")
# print(args)
items, total = dbc.get.search_questions( items, total = dbc.get.search_questions(
name=name, name=name,
type_id=type_id, type_id=type_id,
slide_id=slide_id, slide_id=slide_id,
competition_id=competition_id, competition_id=CID,
page=page, page=page,
page_size=page_size, page_size=page_size,
order=order, order=order,
......
...@@ -68,7 +68,6 @@ question_search_parser.add_argument("name", type=str, default=None, location="js ...@@ -68,7 +68,6 @@ question_search_parser.add_argument("name", type=str, default=None, location="js
# question_parser.add_argument("total_score", type=int) # question_parser.add_argument("total_score", type=int)
question_search_parser.add_argument("slide_id", type=int, default=None, location="json") question_search_parser.add_argument("slide_id", type=int, default=None, location="json")
question_search_parser.add_argument("type_id", type=int, default=None, location="json") question_search_parser.add_argument("type_id", type=int, default=None, location="json")
question_search_parser.add_argument("competition_id", type=int, default=None, location="json")
###TEAM#### ###TEAM####
team_parser = reqparse.RequestParser() team_parser = reqparse.RequestParser()
......
import app.core.http_codes as codes import app.core.http_codes as codes
from app.core.models import Slide from app.core.models import Slide
from flask_sqlalchemy import model
from tests import app, client, db 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
...@@ -54,7 +53,7 @@ def test_misc_api(client): ...@@ -54,7 +53,7 @@ def test_misc_api(client):
assert body["items"][1]["name"] == "Testköping" assert body["items"][1]["name"] == "Testköping"
assert body["items"][2]["name"] == "Gbg" assert body["items"][2]["name"] == "Gbg"
# Deletes city # Deletes city
response, body = delete(client, "/api/misc/cities/3", {}, headers=headers) response, body = delete(client, "/api/misc/cities/3", headers=headers)
assert response.status_code == codes.OK assert response.status_code == codes.OK
assert body["count"] == 2 assert body["count"] == 2
assert body["items"][0]["name"] == "Linköping" assert body["items"][0]["name"] == "Linköping"
...@@ -84,7 +83,7 @@ def test_competition_api(client): ...@@ -84,7 +83,7 @@ def test_competition_api(client):
assert response.status_code == codes.OK assert response.status_code == codes.OK
assert body["name"] == "c1" assert body["name"] == "c1"
response, body = post(client, f"/api/competitions/{competition_id}/slides", {}, headers=headers) response, body = post(client, f"/api/competitions/{competition_id}/slides", headers=headers)
assert response.status_code == codes.OK assert response.status_code == codes.OK
response, body = get(client, f"/api/competitions/{competition_id}/slides", headers=headers) response, body = get(client, f"/api/competitions/{competition_id}/slides", headers=headers)
...@@ -104,7 +103,7 @@ def test_competition_api(client): ...@@ -104,7 +103,7 @@ def test_competition_api(client):
assert len(body["items"]) == 1 assert len(body["items"]) == 1
assert body["items"][0]["name"] == "t1" assert body["items"][0]["name"] == "t1"
response, body = delete(client, f"/api/competitions/{competition_id}", {}, headers=headers) response, body = delete(client, f"/api/competitions/{competition_id}", headers=headers)
assert response.status_code == codes.OK assert response.status_code == codes.OK
...@@ -237,7 +236,8 @@ def test_slide_api(client): ...@@ -237,7 +236,8 @@ def test_slide_api(client):
headers = {"Authorization": "Bearer " + body["access_token"]} headers = {"Authorization": "Bearer " + body["access_token"]}
# Get slides from empty competition # Get slides from empty competition
response, body = get(client, "/api/competitions/1/slides", headers=headers) CID = 1
response, body = get(client, f"/api/competitions/{CID}/slides", headers=headers)
assert response.status_code == codes.OK assert response.status_code == codes.OK
assert body["count"] == 0 assert body["count"] == 0
...@@ -249,7 +249,7 @@ def test_slide_api(client): ...@@ -249,7 +249,7 @@ def test_slide_api(client):
assert body["count"] == num_slides assert body["count"] == num_slides
# Add slide # Add slide
response, body = post(client, f"/api/competitions/{CID}/slides", {}, headers=headers) response, body = post(client, f"/api/competitions/{CID}/slides", headers=headers)
num_slides += 1 num_slides += 1
assert response.status_code == codes.OK assert response.status_code == codes.OK
assert body["count"] == num_slides assert body["count"] == num_slides
...@@ -284,7 +284,7 @@ def test_slide_api(client): ...@@ -284,7 +284,7 @@ def test_slide_api(client):
assert item_slide["timer"] == timer assert item_slide["timer"] == timer
# Delete slide # Delete slide
response, _ = delete(client, f"/api/competitions/{CID}/slides/{SID}", {}, headers=headers) response, _ = delete(client, f"/api/competitions/{CID}/slides/{SID}", headers=headers)
num_slides -= 1 num_slides -= 1
assert response.status_code == codes.NO_CONTENT assert response.status_code == codes.NO_CONTENT
# Checks that there are fewer slides # Checks that there are fewer slides
...@@ -293,7 +293,7 @@ def test_slide_api(client): ...@@ -293,7 +293,7 @@ def test_slide_api(client):
assert body["count"] == num_slides assert body["count"] == num_slides
# Tries to delete slide again # Tries to delete slide again
response, _ = delete(client, f"/api/competitions/{CID}/slides/{SID}", {}, headers=headers) response, _ = delete(client, f"/api/competitions/{CID}/slides/{SID}", headers=headers)
assert response.status_code == codes.NOT_FOUND assert response.status_code == codes.NOT_FOUND
# Changes the order to the same order # Changes the order to the same order
...@@ -309,3 +309,146 @@ def test_slide_api(client): ...@@ -309,3 +309,146 @@ def test_slide_api(client):
# Changes order to 0 # Changes order to 0
SID = 7 SID = 7
change_order_test(client, CID, SID, -1, headers) change_order_test(client, CID, SID, -1, headers)
def test_question_api(client):
add_default_values()
# Login in with default user
response, body = post(client, "/api/auth/login", {"email": "test@test.se", "password": "password"})
assert response.status_code == codes.OK
headers = {"Authorization": "Bearer " + body["access_token"]}
# Get questions from empty competition
CID = 1 # TODO: Fix api-calls so that the ones not using CID don't require one
response, body = get(client, f"/api/competitions/{CID}/questions/search", headers=headers)
assert response.status_code == codes.OK
assert body["count"] == 0
# Get questions from another competition that should have some questions
CID = 3
num_questions = 3
response, body = get(client, f"/api/competitions/{CID}/questions/search", headers=headers)
assert response.status_code == codes.OK
assert body["count"] == num_questions
# print(f"338: {body['items']}")
# # Get specific question
# name = "Q2"
# # total_score = 2
# type_id = 5
# slide_id = 5
# response, body = get(
# client,
# f"/api/competitions/{CID}/questions/search",
# data={
# "name": name,
# # "total_score": total_score,
# "type_id": type_id,
# "slide_id": slide_id,
# },
# headers=headers,
# )
# print(f"357: {body['items']}")
# assert response.status_code == codes.OK
# assert body["count"] == 1
# item_question = body["items"][0]
# # print(f"338: {item_question}")
# 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
# Add question
name = "Nytt namn"
# total_score = 2
type_id = 2
slide_id = 5
response, item_question = post(
client,
f"/api/competitions/{CID}/questions",
{"name": name, "type_id": type_id, "slide_id": slide_id},
headers=headers,
)
num_questions += 1
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
# Checks number of questions
response, body = get(client, f"/api/competitions/{CID}/questions/search", headers=headers)
# print(f"387: {body['items']}")
assert response.status_code == codes.OK
assert body["count"] == num_questions
# Try to get question in another competition
QID = 1
response, item_question = get(client, f"/api/competitions/{CID}/questions/{QID}", headers=headers)
assert response.status_code == codes.NOT_FOUND
# Get question
QID = 4
response, item_question = get(client, f"/api/competitions/{CID}/questions/{QID}", headers=headers)
assert response.status_code == codes.OK
assert item_question["id"] == QID
# Try to edit question in another competition
name = "Nyare namn"
# total_score = 2
type_id = 3
slide_id = 1
QID = 1
response, _ = put(
client,
f"/api/competitions/{CID}/questions/{QID}",
# {"name": name, "total_score": total_score, "type_id": type_id, "slide_id": slide_id},
{"name": name, "type_id": type_id, "slide_id": slide_id},
headers=headers,
)
assert response.status_code == codes.NOT_FOUND
# Checks number of questions
response, body = get(client, f"/api/competitions/{CID}/questions/search", headers=headers)
assert response.status_code == codes.OK
assert body["count"] == num_questions
# Edit question
name = "Nyare namn"
# total_score = 2
type_id = 3
slide_id = 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
response, item_question = put(
client,
f"/api/competitions/{CID}/questions/{QID}",
# {"name": name, "total_score": total_score, "type_id": type_id, "slide_id": slide_id},
{"name": name, "type_id": type_id, "slide_id": slide_id},
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
# Checks number of questions
response, body = get(client, f"/api/competitions/{CID}/questions/search", headers=headers)
assert response.status_code == codes.OK
assert body["count"] == num_questions
# Delete question
response, _ = delete(client, f"/api/competitions/{CID}/questions/{QID}", headers=headers)
num_questions -= 1
assert response.status_code == codes.NO_CONTENT
# Checks that there are fewer questions
response, body = get(client, f"/api/competitions/{CID}/questions/search", headers=headers)
assert response.status_code == codes.OK
assert body["count"] == num_questions
# Tries to delete question again
response, _ = delete(client, f"/api/competitions/{CID}/questions/{QID}", headers=headers)
assert response.status_code == codes.NOT_FOUND
...@@ -49,7 +49,7 @@ def add_default_values(): ...@@ -49,7 +49,7 @@ def add_default_values():
# item_slide.settings = "{}" # item_slide.settings = "{}"
# Add question to competition # Add question to competition
dbc.add.question(f"Q{i+1}", i + 1, i, item_slide) dbc.add.question(f"Q{i+1}", i + 1, i + 1, item_slide)
def get_body(response): def get_body(response):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment