diff --git a/project_amber/const.py b/project_amber/const.py index 33bd46f..cdb829f 100644 --- a/project_amber/const.py +++ b/project_amber/const.py @@ -2,4 +2,8 @@ EMPTY_RESP = dumps({}) # Empty response, to be used in requests. -TASK_MSG_NOT_FOUND = "This task does not exist." +MSG_NO_TOKEN = "No X-Auth-Token present" +MSG_INVALID_TOKEN = "Invalid token" +MSG_USER_NOT_FOUND = "This user does not exist" + +MSG_TASK_NOT_FOUND = "This task does not exist" diff --git a/project_amber/helpers/auth.py b/project_amber/helpers/auth.py index cda9baa..fa319d1 100644 --- a/project_amber/helpers/auth.py +++ b/project_amber/helpers/auth.py @@ -5,6 +5,7 @@ from bcrypt import hashpw, gensalt, checkpw from flask import request +from project_amber.const import MSG_NO_TOKEN, MSG_INVALID_TOKEN, MSG_USER_NOT_FOUND from project_amber.db import db from project_amber.errors import Unauthorized, BadRequest, NotFound, InternalServerError from project_amber.models.auth import User, Session @@ -34,13 +35,13 @@ def handleChecks() -> LoginUser: raise BadRequest token = request.headers.get("X-Auth-Token") if token is None: - raise Unauthorized("No X-Auth-Token present") + raise Unauthorized(MSG_NO_TOKEN) user_session = db.session.query(Session).filter_by(token=token).first() if user_session is None: - raise Unauthorized("Invalid token") + raise Unauthorized(MSG_INVALID_TOKEN) user = db.session.query(User).filter_by(id=user_session.user).first() if user is None: - raise InternalServerError("The user is missing") + raise InternalServerError(MSG_USER_NOT_FOUND) user_details = LoginUser(user.name, user.id, token) return user_details @@ -61,7 +62,7 @@ def removeUser(uid: int) -> int: """ user = db.session.query(User).filter_by(id=uid).first() if user is None: - raise NotFound("User not found") + raise NotFound(MSG_USER_NOT_FOUND) db.session.delete(user) db.session.commit() return uid diff --git a/project_amber/helpers/task.py b/project_amber/helpers/task.py index 0bf28cc..80ca7a4 100644 --- a/project_amber/helpers/task.py +++ b/project_amber/helpers/task.py @@ -1,6 +1,6 @@ from time import time -from project_amber.const import TASK_MSG_NOT_FOUND +from project_amber.const import MSG_TASK_NOT_FOUND from project_amber.db import db from project_amber.errors import NotFound from project_amber.models.task import Task @@ -21,7 +21,7 @@ def updateTask(new_text: str, task_id: int, uid: int) -> int: """ task = db.session.query(Task).filter_by(id=task_id, owner=uid).first() if task is None: - raise NotFound(TASK_MSG_NOT_FOUND) + raise NotFound(MSG_TASK_NOT_FOUND) task.text = new_text db.session.commit() return task_id @@ -33,7 +33,7 @@ def removeTask(task_id: int, uid: int) -> int: """ task = db.session.query(Task).filter_by(id=task_id, owner=uid).first() if task is None: - raise NotFound(TASK_MSG_NOT_FOUND) + raise NotFound(MSG_TASK_NOT_FOUND) db.session.delete(task) db.session.commit() return task_id