Skip to content
Snippets Groups Projects
Commit a0618abb authored by Carl Schönfelder's avatar Carl Schönfelder
Browse files

Resolve "Fix api response"

parent 833fe128
No related branches found
No related tags found
1 merge request!73Resolve "Fix api response"
Pipeline #41338 passed with warnings
......@@ -29,7 +29,6 @@ def refresh(item):
db.session.refresh(item)
def commit(item):
def commit():
""" Commits. """
db.session.commit()
from app.core import bcrypt, db
from sqlalchemy.ext.hybrid import hybrid_method, hybrid_property
from app.database import Dictionary
from sqlalchemy.ext.hybrid import hybrid_method, hybrid_property
STRING_SIZE = 254
......@@ -88,7 +89,7 @@ class Competition(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(STRING_SIZE), unique=True)
year = db.Column(db.Integer, nullable=False, default=2020)
font = db.Column(db.String(STRING_SIZE), nullable=False)
city_id = db.Column(db.Integer, db.ForeignKey("city.id"), nullable=False)
background_image_id = db.Column(db.Integer, db.ForeignKey("media.id"), nullable=True)
......@@ -101,6 +102,7 @@ class Competition(db.Model):
self.name = name
self.year = year
self.city_id = city_id
self.font = "Calibri"
class Team(db.Model):
......@@ -130,6 +132,7 @@ class Slide(db.Model):
background_image = db.relationship("Media", uselist=False)
components = db.relationship("Component", backref="slide")
questions = db.relationship("Question", backref="questions")
def __init__(self, order, competition_id):
self.order = order
......@@ -144,7 +147,6 @@ class Question(db.Model):
type_id = db.Column(db.Integer, db.ForeignKey("question_type.id"), nullable=False)
slide_id = db.Column(db.Integer, db.ForeignKey("slide.id"), nullable=False)
slide = db.relationship("Slide", backref="questions")
question_answers = db.relationship("QuestionAnswer", backref="question")
alternatives = db.relationship("QuestionAlternative", backref="question")
......@@ -182,9 +184,6 @@ class QuestionAnswer(db.Model):
self.team_id = team_id
class Component(db.Model):
id = db.Column(db.Integer, primary_key=True)
x = db.Column(db.Integer, nullable=False, default=0)
......
......@@ -13,14 +13,21 @@ class Config:
JWT_BLACKLIST_TOKEN_CHECKS = ["access", "refresh"]
JWT_ACCESS_TOKEN_EXPIRES = timedelta(days=2)
JWT_REFRESH_TOKEN_EXPIRES = timedelta(days=30)
UPLOADED_PHOTOS_DEST = "static/images" # os.getcwd()
UPLOADED_PHOTOS_DEST = os.path.join(os.getcwd(), "app/static/images")
THUMBNAIL_SIZE = (120, 120)
SECRET_KEY = os.urandom(24)
SQLALCHEMY_ECHO = False
class DevelopmentConfig(Config):
DEBUG = True
SQLALCHEMY_DATABASE_URI = "sqlite:///database.db"
# HOST = "localhost"
# PORT = 5432
# USER = "postgres"
# PASSWORD = "password"
# DATABASE = "teknik8"
# SQLALCHEMY_DATABASE_URI = "sqlite:///database.db"
# SQLALCHEMY_DATABASE_URI = "postgresql://" + USER + ":" + PASSWORD + "@" + HOST + ":" + str(PORT) + "/" + DATABASE
SQLALCHEMY_ECHO = False
......@@ -33,7 +40,7 @@ class ProductionConfig(Config):
SQLALCHEMY_DATABASE_URI = "sqlite:///database.db"
# HOST = 'postgresql'
# PORT = 5432
# USER = 'asd'
# PASSWORD = 'asd'
# DATABASE = 'asd'
# DATABASE_URI = 'postgresql://'+USER+":"+PASSWORD+"@"+HOST+":"+str(PORT)+"/"+DATABASE
# USER = 'postgres'
# PASSWORD = 'password'
# DATABASE = 'teknik8'
# SQLALCHEMY_DATABASE_URI = 'postgresql://'+USER+":"+PASSWORD+"@"+HOST+":"+str(PORT)+"/"+DATABASE
......@@ -143,13 +143,13 @@ def test_auth_and_user_api(client):
response, body = put(client, "/api/users", {"name": "carl carlsson", "city_id": 2, "role_id": 1}, headers=headers)
assert response.status_code == codes.OK
assert body["name"] == "Carl Carlsson"
assert body["city"]["id"] == 2 and body["role"]["id"] == 1
assert body["city_id"] == 2 and body["role_id"] == 1
# Find other user
response, body = get(
client,
"/api/users/search",
query_string={"name": "Olle Olsson", "email": "test@test.se", "role_id": 1, "city_id": 1},
query_string={"name": "Carl Carlsson"},
headers=headers,
)
assert response.status_code == codes.OK
......@@ -162,17 +162,22 @@ def test_auth_and_user_api(client):
assert response.status_code == codes.OK
assert searched_user["name"] == body["name"]
assert searched_user["email"] == body["email"]
assert searched_user["role_id"] == body["role"]["id"]
assert searched_user["city_id"] == body["city"]["id"]
assert searched_user["role_id"] == body["role_id"]
assert searched_user["city_id"] == body["city_id"]
assert searched_user["id"] == body["id"]
# Login as admin
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"]}
# Edit user from ID
response, body = put(client, f"/api/users/{user_id}", {"email": "carl@carlsson.test"}, headers=headers)
assert response.status_code == codes.OK
assert body["email"] == "carl@carlsson.test"
# assert body["email"] == "carl@carlsson.test"
# Edit user from ID but add the same email as other user
response, body = put(client, f"/api/users/{user_id}", {"email": "test1@test.se"}, headers=headers)
response, body = put(client, f"/api/users/{user_id}", {"email": "test@test.se"}, headers=headers)
assert response.status_code == codes.BAD_REQUEST
# Delete other user
......@@ -193,25 +198,25 @@ def test_auth_and_user_api(client):
assert response.status_code == codes.UNAUTHORIZED
# Login in again with default user
response, body = post(client, "/api/auth/login", {"email": "test1@test.se", "password": "abc123"})
assert response.status_code == codes.OK
headers = {"Authorization": "Bearer " + body["access_token"]}
# TODO: Add test for refresh api for current user
# response, body = post(client, "/api/auth/refresh", headers={**headers, "refresh_token": refresh_token})
# response, body = post(client, "/api/auth/login", {"email": "test1@test.se", "password": "abc123"})
# assert response.status_code == codes.OK
# headers = {"Authorization": "Bearer " + body["access_token"]}
# Find current user
response, body = get(client, "/api/users", headers=headers)
assert response.status_code == codes.OK
assert body["email"] == "test1@test.se"
assert body["city"]["id"] == 2
assert body["role"]["id"] == 1
# # TODO: Add test for refresh api for current user
# # response, body = post(client, "/api/auth/refresh", headers={**headers, "refresh_token": refresh_token})
# # assert response.status_code == codes.OK
# Delete current user
user_id = body["id"]
response, body = delete(client, f"/api/auth/delete/{user_id}", headers=headers)
assert response.status_code == codes.OK
# # Find current user
# response, body = get(client, "/api/users", headers=headers)
# assert response.status_code == codes.OK
# assert body["email"] == "test1@test.se"
# assert body["city_id"] == 2
# assert body["role_id"] == 1
# # Delete current user
# user_id = body["id"]
# response, body = delete(client, f"/api/auth/delete/{user_id}", headers=headers)
# assert response.status_code == codes.OK
# TODO: Check that user was blacklisted
# Look for current users jwt in blacklist
......@@ -332,7 +337,7 @@ def test_question_api(client):
num_questions = 4
assert response.status_code == codes.OK
assert item_question["name"] == name
assert item_question["type"]["id"] == type_id
assert item_question["type_id"] == type_id
# Checks number of questions
response, body = get(client, f"/api/competitions/{CID}/questions", headers=headers)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment