diff --git a/src/controller/tasks_controller.py b/src/controller/tasks_controller.py index fcb4110..a519e54 100644 --- a/src/controller/tasks_controller.py +++ b/src/controller/tasks_controller.py @@ -39,7 +39,7 @@ def add_task(user_id): # Use database connection that is currently being handled by the Flask app, i.e. test and prod session = current_app.session - task_repo = TasksRespository(session) + task_repo = TasksRespository(session) task = Tasks( title=title, description=description, status=status, user_id=user_id @@ -66,7 +66,7 @@ def edit_task(user_id): new_status = form.status.data or None # Convert blank to None # Use database connection that is currently being handled by the Flask app, i.e. test and prod - session = current_app.session + session = current_app.session task_repo = TasksRespository(session) tasks = task_repo.find_tasks_by_user(user_id) all_ids = [task.id for task in tasks] diff --git a/tests/conftest.py b/tests/conftest.py index dd5f87d..7219ac2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,5 @@ import pytest -from flask import Flask +from flask import Flask, current_app from flask.testing import FlaskClient from flask_login import AnonymousUserMixin, LoginManager from sqlalchemy import create_engine @@ -111,7 +111,7 @@ def app(test_db): # Enable the loading of users during the test @login_manager.user_loader def load_user(user_id): - session = test_db + session = current_app.session user_repo = UsersRepository(session) return user_repo.find_user_by_id(user_id) diff --git a/tests/test_controller/test_tasks_controller.py b/tests/test_controller/test_tasks_controller.py index 879762a..ba82097 100644 --- a/tests/test_controller/test_tasks_controller.py +++ b/tests/test_controller/test_tasks_controller.py @@ -62,40 +62,53 @@ def test_add_task_page_loads(client, app): assert b"Status" in response.data -def test_add_task_success(client, app, tasks_repository, users_repository): +# -- GET_TASK_DETAILS TEST CASES -- +def test_get_task_details_success(client, app, users_repository, tasks_repository): with app.app_context(): - username = "Carlton_Banks" - # Use app.session to ensure user is bound to the session - user = app.session.query(Users).filter_by(username=username).first() - - # Explicity refresh the user to avoid DetachedInstance Error - # app.session.refresh(user) - - # Simulate user login - with client.session_transaction() as session: - session["_user_id"] = str(user.id) - - task_data = { - "title": "Organize bowties", - "description": "Sort bowties by color and season.", - "status": "In Progress", - } - - response = client.post( - url_for("tasks.add_task", user_id=user.id), - data=task_data, - follow_redirects=True, - ) + user = users_repository.find_user_by_username("Will_Smith") + tasks = tasks_repository.find_tasks_by_user(user.id) - # Assert: Redirect to the view_tasks page - assert response.status_code == 200 - assert b"Task successfully created." in response.data + # Simulate login + with client: + with client.session_transaction() as session: + session["_user_id"] = str(user.id) + + # Get the details for the first task + task_id = tasks[0].id + response = client.get(url_for("tasks.get_task_details", task_id=task_id)) - # Assert: Task exists in the database - tasks = tasks_repository.find_tasks_by_user(user.id) - assert len(tasks) == 3 - assert any( - task.title == "Organize bowties" - and task.description == "Sort bowties by color and season." - for task in tasks - ) \ No newline at end of file + # Assert: Request was successful + assert response.status_code == 200 + task_data = response.get_json() + assert task_data["title"] == tasks[0].title + assert task_data["description"] == tasks[0].description + assert task_data["status"] == tasks[0].status + + +def test_get_task_details_not_found(client, app, users_repository): + with app.app_context(): + user = users_repository.find_user_by_username("Will_Smith") + + # Simulate login + with client: + with client.session_transaction() as session: + session["_user_id"] = str(user.id) + + # Try to fetch a task doesn't exist + invalid_task_id = 99999 + response = client.get(url_for("tasks.get_task_details", task_id=invalid_task_id)) + + # Assert: Task not found + assert response.status_code == 404 + error_data = response.get_json() + assert error_data["error"] == f"Task ID {invalid_task_id} does not exist for this user." + + +def test_get_task_details_unauthenticated(client, app): + with app.app_context(): + # Attempt to retrive task details without logging in + response = client.get(url_for("tasks.get_task_details", task_id=1)) + + # Assert: User is redirected to login page + assert response.status_code == 302 + assert url_for("auth.login", _external=False) in response.headers["Location"] \ No newline at end of file