17 Commits

Author SHA1 Message Date
d3d3c98f99 Moved wait_for_db function to utils module 2025-06-11 19:48:58 +00:00
9e010ed389 Implemented waiting for db readiness 2025-06-11 19:43:47 +00:00
636a382cf5 Deleted jenkins pipeline from main branch 2025-06-11 17:13:27 +00:00
76a351710f Added variable APP_PORT to customize application port 2025-05-04 16:42:43 +00:00
c1f0da4a9c Extended Goss sleep 2025-05-04 15:37:47 +00:00
eefc952ff0 Updated app port in Goss YAML config 2025-05-04 15:26:21 +00:00
8c35b3bd8c Changed development server to production 2025-05-04 15:23:05 +00:00
60011b1c72 Added GOSS_SLEEP flag to wait for container full start before tests 2025-05-04 11:03:12 +00:00
859a962c12 Corrected python command name in Goss config YAML 2025-05-04 11:02:09 +00:00
0e9df4f859 Corrected command to run tests in Goss 2025-05-04 10:14:36 +00:00
1554404657 Corrected port to check in Goss YAML config 2025-05-04 10:14:18 +00:00
925af7d314 Corrected commands to test python app 2025-05-04 06:55:10 +00:00
fb260a0f6d Corrected directory in jenkins pipeline 2025-05-03 20:21:02 +00:00
dcd9a39b46 Corrected shell commands in jenkins pipeline 2025-05-03 20:05:32 +00:00
8194e3e9fe Added Jenkins pipeline to test code and container 2025-05-03 19:47:27 +00:00
0006044ae4 Added goss tests 2025-05-03 19:45:40 +00:00
74a58879ce Refactored code responsible for finding user in database 2025-04-02 20:02:34 +00:00
4 changed files with 37 additions and 10 deletions

View File

@ -4,7 +4,7 @@ from flask_jwt_extended import JWTManager
from jwt import ExpiredSignatureError
from models import db, RevokedToken
import os
from utils import init_db
from utils import init_db, wait_for_db
from views import user_bp
from werkzeug.exceptions import HTTPException
@ -53,6 +53,7 @@ def create_app(config_name="default"):
# Fill database by initial values (only if we are not testing)
with app.app_context():
wait_for_db()
db.create_all()
if config_name != "testing":
init_db()
@ -61,5 +62,7 @@ def create_app(config_name="default"):
# Server start only if we run app directly
if __name__ == "__main__":
from waitress import serve
app = create_app()
app.run(host="0.0.0.0")
port = os.getenv("APP_PORT", "80")
serve(app, host="0.0.0.0", port=port)

View File

@ -11,4 +11,5 @@ mysql-connector-python==9.2.0
python-dotenv==1.0.0
SQLAlchemy==2.0.23
typing_extensions==4.8.0
waitress==3.0.2
Werkzeug==3.0.1

View File

@ -2,6 +2,9 @@ from flask import abort
from flask_jwt_extended import get_jwt_identity
from models import User, db
import os
from sqlalchemy import text
from sqlalchemy.exc import DatabaseError
import time
from werkzeug.security import generate_password_hash
@ -19,6 +22,30 @@ def validate_access(owner_id, message='Access denied.'):
abort(403, message)
def get_user_or_404(user_id):
"Get user from database or abort 404"
user = db.session.get(User, user_id)
if user is None:
abort(404, "User not found")
return user
MAX_RETRIES = 100
def wait_for_db():
for retries in range(MAX_RETRIES):
try:
with db.engine.connect() as connection:
connection.execute(text("SELECT 1"))
print("Successfully connected with database.")
return
except DatabaseError:
print(f"Waiting for database... (retry {retries + 1})")
time.sleep(3)
print("Failed to connect to database.")
raise Exception("Database not ready after multiple retries.")
def init_db():
"""Create default admin account if database is empty"""
with db.session.begin():

View File

@ -2,7 +2,7 @@ from flask import Blueprint, jsonify, request, abort
from flask_jwt_extended import create_access_token, set_access_cookies, jwt_required, \
verify_jwt_in_request, get_jwt_identity, unset_jwt_cookies, get_jwt
from models import db, RevokedToken, User
from utils import admin_required, validate_access
from utils import admin_required, validate_access, get_user_or_404
from werkzeug.security import check_password_hash, generate_password_hash
user_bp = Blueprint('user_bp', __name__)
@ -23,9 +23,7 @@ def get_all_users():
@jwt_required()
def get_user(user_id):
validate_access(user_id) # check if user tries to read other user account details
user = db.session.get(User, user_id)
if user is None:
abort(404, "User not found.")
user = get_user_or_404(user_id)
return jsonify(user.to_dict())
@ -59,7 +57,7 @@ def edit_user(user_id):
if request_fields != editable_fields:
abort(400, "Invalid request data structure.")
user_to_update = User.query.get_or_404(user_id)
user_to_update = get_user_or_404(user_id)
for field_name in editable_fields:
requested_value = request_data.get(field_name)
if requested_value is None:
@ -75,9 +73,7 @@ def edit_user(user_id):
@jwt_required()
def remove_user(user_id):
validate_access(user_id) # Only admin can remove other users accounts
user_to_delete = db.session.get(User, user_id)
if user_to_delete is None:
abort(404, "User not found.")
user_to_delete = get_user_or_404(user_id)
db.session.delete(user_to_delete)
db.session.commit()
return jsonify({"msg": "User removed successfully."})