Skip to content

Commit

Permalink
created unit tests for get_task_details()
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan-Sell committed Nov 20, 2024
1 parent 92148b3 commit c43eb9a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/controller/tasks_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand Down
81 changes: 47 additions & 34 deletions tests/test_controller/test_tasks_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
# 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"]

0 comments on commit c43eb9a

Please sign in to comment.