diff --git a/backend/Dockerfile_auth b/backend/Dockerfile_auth_test similarity index 77% rename from backend/Dockerfile_auth rename to backend/Dockerfile_auth_test index d2671b6e..d7541b0d 100644 --- a/backend/Dockerfile_auth +++ b/backend/Dockerfile_auth_test @@ -2,7 +2,7 @@ FROM python:3.9 RUN mkdir /auth-app WORKDIR /auth-app ADD ./test_auth_server /auth-app/ -COPY requirements.txt /auth-app/requirements.txt +COPY auth_requirements.txt /auth-app/requirements.txt RUN pip3 install -r requirements.txt COPY . /auth-app ENTRYPOINT ["python"] diff --git a/backend/auth_requirements.txt b/backend/auth_requirements.txt new file mode 100644 index 00000000..2a0efdf3 --- /dev/null +++ b/backend/auth_requirements.txt @@ -0,0 +1,4 @@ +flask~=3.0.2 +flask-restful +python-dotenv~=1.0.1 +psycopg2-binary \ No newline at end of file diff --git a/backend/test_auth_server/__init__.py b/backend/test_auth_server/__init__.py deleted file mode 100644 index 4aa542de..00000000 --- a/backend/test_auth_server/__init__.py +++ /dev/null @@ -1,36 +0,0 @@ -""" Flask API base file -This file is the base of the Flask API. It contains the basic structure of the API. -""" - -from flask import Flask -from test_auth_server.db_in import db -from test_auth_server.index import index_bp - -def create_app(): - """ - Create a Flask application instance. - Returns: - Flask -- A Flask application instance - """ - - app = Flask(__name__) - app.register_blueprint(index_bp) - - return app - -def create_app_with_db(db_uri: str): - """ - Initialize the database with the given uri - and connect it to the app made with create_app. - Parameters: - db_uri (str): The URI of the database to initialize. - Returns: - Flask -- A Flask application instance - """ - - app = create_app() - app.config["SQLALCHEMY_DATABASE_URI"] = db_uri - app.config["UPLOAD_FOLDER"] = "/" - db.init_app(app) - - return app \ No newline at end of file diff --git a/backend/test_auth_server/__main__.py b/backend/test_auth_server/__main__.py index b1d7e093..b86e31dd 100644 --- a/backend/test_auth_server/__main__.py +++ b/backend/test_auth_server/__main__.py @@ -1,10 +1,48 @@ """Main entry point for the application.""" from dotenv import load_dotenv -from test_auth_server import create_app_with_db -from test_auth_server.db_in import url +from flask import Flask + +"""Index api point""" +from flask import Blueprint, request +from flask_restful import Resource, Api + +index_bp = Blueprint("index", __name__) +index_endpoint = Api(index_bp) + +token_dict = { + "teacher1":{ + "id":"brinkmann", + "jobCategory":"teacher" + }, + "teacher2":{ + "id":"laermans", + "jobCategory":"teacher" + } +} + +class Index(Resource): + """Api endpoint for the / route""" + + def get(self): + auth = request.headers.get("Authorization") + if not auth: + return {"message":"Please give authorization"}, 401 + bearer, token = auth.split(" ") + if bearer != "Bearer": + return {"message":"Not this kind of authorization"}, 401 + if token in token_dict.keys(): + return token_dict[token], 200 + return {"message":"Wrong address"}, 401 + + +index_bp.add_url_rule("/", view_func=Index.as_view("index")) if __name__ == "__main__": load_dotenv() - app = create_app_with_db(url) - app.run(debug=True, host='0.0.0.0') \ No newline at end of file + + app = Flask(__name__) + app.register_blueprint(index_bp) + + app.run(debug=True, host='0.0.0.0') + diff --git a/backend/test_auth_server/db_construct.sql b/backend/test_auth_server/db_construct.sql deleted file mode 100644 index 8b59f6d3..00000000 --- a/backend/test_auth_server/db_construct.sql +++ /dev/null @@ -1,6 +0,0 @@ -CREATE TABLE user_data ( - access_token VARCHAR(255), - id VARCHAR(255), - jobCategory VARCHAR(255), - PRIMARY KEY(access_token) -); \ No newline at end of file diff --git a/backend/test_auth_server/db_in.py b/backend/test_auth_server/db_in.py deleted file mode 100644 index 22a15e9b..00000000 --- a/backend/test_auth_server/db_in.py +++ /dev/null @@ -1,20 +0,0 @@ -"""db initialization""" - -import os -from flask_sqlalchemy import SQLAlchemy -from sqlalchemy import URL - -db = SQLAlchemy() - -DATABSE_NAME = os.getenv("POSTGRES_DB") -DATABASE_USER = os.getenv("POSTGRES_USER") -DATABASE_PASSWORD = os.getenv("POSTGRES_PASSWORD") -DATABASE_HOST = os.getenv("POSTGRES_HOST") - -url = URL.create( - drivername="postgresql", - username=DATABASE_USER, - host=DATABASE_HOST, - database=DATABSE_NAME, - password=DATABASE_PASSWORD, -) \ No newline at end of file diff --git a/backend/test_auth_server/index.py b/backend/test_auth_server/index.py deleted file mode 100644 index 1ac5a26d..00000000 --- a/backend/test_auth_server/index.py +++ /dev/null @@ -1,36 +0,0 @@ -"""Index api point""" -import os -from flask import Blueprint, request -from flask_restful import Resource, Api - -from test_auth_server.db_in import db -from test_auth_server.user_data import User_data - -from sqlalchemy.exc import SQLAlchemyError - -index_bp = Blueprint("index", __name__) -index_endpoint = Api(index_bp) - -class Index(Resource): - """Api endpoint for the / route""" - - def get(self): - auth = request.headers.get("Authorization") - if not auth: - return {"message":"Please give authorization"}, 401 - bearer, token = auth.split(" ") - if bearer != "Bearer": - return {"message":"Not this kind of authorization"}, 401 - try: - user_data = db.session.get(User_data, token) - except SQLAlchemyError: - # every exception should result in a rollback - db.session.rollback() - return {"message":"An unexpected database error occured while fetching the user"}, 500 - if user_data: - return (200, {"id":user_data.id, - "jobCategory":user_data.jobCategory}) - return {"message":"Wrong address man"}, 401 - - -index_bp.add_url_rule("/", view_func=Index.as_view("index")) \ No newline at end of file diff --git a/backend/test_auth_server/sessionmaker.py b/backend/test_auth_server/sessionmaker.py deleted file mode 100644 index e523ad3a..00000000 --- a/backend/test_auth_server/sessionmaker.py +++ /dev/null @@ -1,15 +0,0 @@ -"""initialise a datab session""" -from os import getenv -from sqlalchemy import create_engine, URL -from sqlalchemy.orm import sessionmaker - -url = URL.create( - drivername="postgresql", - username=getenv("POSTGRES_USER"), - password=getenv("POSTGRES_PASSWORD"), - host=getenv("POSTGRES_HOST"), - database=getenv("POSTGRES_DB") -) - -engine = create_engine(url) -Session = sessionmaker(bind=engine) \ No newline at end of file diff --git a/backend/test_auth_server/user_data.py b/backend/test_auth_server/user_data.py deleted file mode 100644 index 45fb903c..00000000 --- a/backend/test_auth_server/user_data.py +++ /dev/null @@ -1,12 +0,0 @@ -import dataclasses - -from sqlalchemy import Boolean, Column, String -from test_auth_server.db_in import db - -@dataclasses.dataclass -class User_data(db.Model): - - __tablename__ = "user_data" - access_token:str= Column(String(255), primary_key=True) - id:str = Column(String(255)) - jobCategory:str = Column(String(255), nullable=True) \ No newline at end of file diff --git a/backend/tests.yaml b/backend/tests.yaml index e4d8c884..bb33426d 100644 --- a/backend/tests.yaml +++ b/backend/tests.yaml @@ -15,36 +15,15 @@ services: start_period: 5s volumes: - ./db_construct.sql:/docker-entrypoint-initdb.d/init.sql - auth-db: - image: postgres:latest - environment: - POSTGRES_USER: auth-user - POSTGRES_PASSWORD: auth-password - POSTGRES_DB: auth-database - healthcheck: - test: ["CMD-SHELL", "pg_isready -U auth-user -d auth-database"] - interval: 5s - timeout: 3s - retries: 3 - start_period: 5s - volumes: - - ./test_auth_server/db_construct.sql:/docker-entrypoint-initdb.d/init.sql auth-server: build: context: . - dockerfile: ./Dockerfile_auth - depends_on: - auth-db: - condition: service_healthy + dockerfile: ./Dockerfile_auth_test environment: - POSTGRES_HOST: auth-db # Use the service name defined in Docker Compose - POSTGRES_USER: auth-user - POSTGRES_PASSWORD: auth-password - POSTGRES_DB: auth-database API_HOST: http://auth-server volumes: - .:/auth-app - command: ["test_auth_server"] + command: ["CMD", "flask run"] test-runner: @@ -54,6 +33,8 @@ services: depends_on: postgres: condition: service_healthy + auth-server: + condition: service_started environment: POSTGRES_HOST: postgres # Use the service name defined in Docker Compose POSTGRES_USER: test_user