Skip to content

Commit

Permalink
created unit tests for view_tasks()
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan-Sell committed Nov 19, 2024
1 parent fadc4cc commit c454595
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 13 deletions.
27 changes: 19 additions & 8 deletions src/controller/tasks_controller.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
from flask import Blueprint, flash, jsonify, redirect, render_template, url_for
from flask import (
Blueprint,
current_app,
flash,
jsonify,
redirect,
render_template,
url_for,
)
from flask_login import current_user, login_required

from src.forms.task_form import AddTaskForm, DeleteTaskForm, EditTaskForm
Expand All @@ -11,10 +19,11 @@
@tasks_blueprint.route("/<int:user_id>", methods=["GET", "POST"])
@login_required
def view_tasks(user_id):
session = SessionLocal()
session = current_app.session
task_repo = TasksRespository(session)
tasks = task_repo.find_tasks_by_user(user_id)
session.close()
print("Tasks:", tasks)
return render_template("view_tasks.html", tasks=tasks)


Expand All @@ -28,7 +37,8 @@ def add_task(user_id):
description = form.description.data
status = form.status.data

session = SessionLocal()
# 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 = Tasks(
Expand All @@ -55,8 +65,8 @@ def edit_task(user_id):
new_description = form.description.data or None # Convert blank to None
new_status = form.status.data or None # Convert blank to None

# initiate DB and collect relevant data
session = SessionLocal()
# 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)
tasks = task_repo.find_tasks_by_user(user_id)
all_ids = [task.id for task in tasks]
Expand Down Expand Up @@ -91,8 +101,8 @@ def delete_task(user_id):
if form.validate_on_submit():
task_id = form.id.data

# initiate DB and collect relevant data
session = SessionLocal()
# 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)
tasks = task_repo.find_tasks_by_user(user_id)
all_ids = [task.id for task in tasks]
Expand All @@ -112,7 +122,8 @@ def delete_task(user_id):
@tasks_blueprint.route("/api/<int:task_id>")
@login_required
def get_task_details(task_id):
session = SessionLocal()
# 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 = task_repo.find_task_by_id(task_id)
session.close()
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_db():

# Will's tasks
task1 = Tasks(
title="Chill out, maxin'",
title="Chill out, maxin",
description="Relaxing after school",
status="Not Started",
user_id=will.id,
Expand Down
5 changes: 1 addition & 4 deletions tests/test_controller/test_auth_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
from urllib.parse import urlparse

from flask import url_for
from flask_login import login_user

from src.models import Tasks, Users
from src.repository.users_repository import UsersRepository
from src.security import check_password_hash, generate_password_hash
from src.security import check_password_hash


# -- LOGIN TEST CASES --
Expand Down
45 changes: 45 additions & 0 deletions tests/test_controller/test_tasks_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from urllib.parse import urlparse

from flask import url_for
from flask_login import login_user


def test_view_tasks_success(client, app, users_repository, tasks_repository):
with app.app_context():
user = users_repository.find_user_by_username("Will_Smith")
assert user is not None, "User should exist in the test database."

# Simmulate a request context
with client:
# Simulate the user has logged in
with client.session_transaction() as session:
session["_user_id"] = str(user.id)

# Send GET request to the view_tasks endpoint
response = client.get(
url_for("tasks.view_tasks", user_id=user.id), follow_redirects=True
)

# Assert: Request was sucessful
assert response.status_code == 200

print(response.data.decode())

# Assert: Tasks to user are displayed
tasks = tasks_repository.find_tasks_by_user(user.id)
for task in tasks:
assert task.title.encode() in response.data
assert task.description.encode() in response.data


def test_view_tasks_unauthorized_access(app, client):
with app.app_context():

# Send GET request without logging in
response = client.get(url_for("tasks.view_tasks", user_id=1))

# Assert: Redirect to login page
assert response.status_code == 302
assert urlparse(response.location).path == url_for(
"auth.login", _external=False
)

0 comments on commit c454595

Please sign in to comment.