|
| 1 | +"""This file contains all tests for the endpoints within routes/problems.py""" |
| 2 | + |
| 3 | + |
| 4 | +import pytest |
| 5 | +import json |
| 6 | +from unittest.mock import Mock, patch, AsyncMock |
| 7 | +from fastapi import status, FastAPI |
| 8 | +from fastapi.testclient import TestClient |
| 9 | +from sqlalchemy.orm import Session |
| 10 | +from datetime import datetime, timedelta |
| 11 | +import uuid |
| 12 | +from models.user import User as UserModel |
| 13 | +from schemas.problem import ProblemBase, ProblemCreate |
| 14 | +from services.code_execution_service import CodeExecutionService |
| 15 | +from services.problem_service import ProblemService, ProblemCreate, ProblemUpdate |
| 16 | +from exceptions import NotFoundError, ValidationError, AuthorisationError |
| 17 | +from routes.problems import router as problem_router |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | + |
| 21 | +def problem_app(): |
| 22 | + """Create a FastApi instance with the problem routes included""" |
| 23 | + app = FastAPI() |
| 24 | + app.include_router(problem_router) |
| 25 | + return app |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def problem_client(problem_app, db_session, test_user): |
| 30 | + """This creates a test client with database and authentication overides""" |
| 31 | + |
| 32 | + def get_test_db(): |
| 33 | + """This overides the database dependency for testing""" |
| 34 | + yield db_session |
| 35 | + |
| 36 | + def get_current_user(): |
| 37 | + """This overides the authentication dependency for testsing""" |
| 38 | + yield test_user |
| 39 | + |
| 40 | + from database import get_db |
| 41 | + from dependencies import get_current_user as get_current_user_dep |
| 42 | + |
| 43 | + problem_app.dependency_overrides[get_db] = get_test_db |
| 44 | + problem_app.dependency_overrides[get_current_user_dep] = get_current_user |
| 45 | + |
| 46 | + with TestClient(problem_app) as client: |
| 47 | + yield client |
| 48 | + |
| 49 | + problem_app.dependency_overrides.clear() |
| 50 | + |
| 51 | + |
| 52 | +def test_create_problem_success(problem_client, test_user): |
| 53 | + """Test successful problem creation""" |
| 54 | + form_data = { |
| 55 | + "title": "Test Problem", |
| 56 | + "description": "Test description", |
| 57 | + "code_snippet": "print('Hello')", |
| 58 | + "expected_output": "Hello", |
| 59 | + "language": "python", |
| 60 | + "is_public": True |
| 61 | + } |
| 62 | + |
| 63 | + response = problem_client.post("/problems", data=form_data) |
| 64 | + data = response.json() |
| 65 | + |
| 66 | + assert response.status_code == status.HTTP_200_OK |
| 67 | + assert data["message"] == "Problem created successfully" |
| 68 | + assert "access_code" in data |
| 69 | + assert "problem_id" in data |
| 70 | + |
| 71 | +def test_create_problem_missing_fields(problem_client): |
| 72 | + """Test creation with missing required fields""" |
| 73 | + form_data = { |
| 74 | + "title": "Incomplete Problem", |
| 75 | + "code_snippet": "print('Hello')", |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + response = problem_client.post("/problems", data=form_data) |
| 80 | + assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY |
| 81 | + |
| 82 | +def test_get_user_problems_success(problem_client, test_user, db_session): |
| 83 | + """Test retrieving user's problems""" |
| 84 | + |
| 85 | + problem_service = ProblemService(db_session) |
| 86 | + for _ in range(3): |
| 87 | + problem_service.create_problem( |
| 88 | + ProblemCreate( |
| 89 | + title="Test", |
| 90 | + description="Test", |
| 91 | + code_snippet="code", |
| 92 | + expected_output="output", |
| 93 | + language="python", |
| 94 | + is_public=True |
| 95 | + ), |
| 96 | + test_user |
| 97 | + ) |
| 98 | + |
| 99 | + response = problem_client.get("/problems") |
| 100 | + data = response.json() |
| 101 | + |
| 102 | + assert response.status_code == status.HTTP_200_OK |
| 103 | + assert len(data) == 3 |
| 104 | + for problem in data: |
| 105 | + assert problem["creator_id"] == test_user.id |
| 106 | + |
| 107 | +def test_get_problem_by_access_code_success(problem_client, test_user, db_session): |
| 108 | + """Test retrieving problem by valid access code""" |
| 109 | + problem_service = ProblemService(db_session) |
| 110 | + problem = problem_service.create_problem( |
| 111 | + ProblemCreate( |
| 112 | + title="Access Test", |
| 113 | + description="Test", |
| 114 | + code_snippet="code", |
| 115 | + expected_output="output", |
| 116 | + language="python", |
| 117 | + is_public=False |
| 118 | + ), |
| 119 | + test_user |
| 120 | + ) |
| 121 | + |
| 122 | + response = problem_client.get(f"/problems/access/{problem.access_code}") |
| 123 | + data = response.json() |
| 124 | + |
| 125 | + assert response.status_code == status.HTTP_200_OK |
| 126 | + assert data["id"] == problem.id |
| 127 | + assert data["access_code"] == problem.access_code |
| 128 | + |
| 129 | +def test_get_problem_by_access_code_not_found(problem_client): |
| 130 | + """Test invalid access code returns 404""" |
| 131 | + response = problem_client.get("/problems/access/invalid_code") |
| 132 | + assert response.status_code == status.HTTP_404_NOT_FOUND |
| 133 | + |
| 134 | +def test_delete_problem_success(problem_client, test_user, db_session): |
| 135 | + """Test successful problem deletion""" |
| 136 | + problem_service = ProblemService(db_session) |
| 137 | + problem = problem_service.create_problem( |
| 138 | + ProblemCreate( |
| 139 | + title="Delete Test", |
| 140 | + description="Test", |
| 141 | + code_snippet="code", |
| 142 | + expected_output="output", |
| 143 | + language="python", |
| 144 | + is_public=True |
| 145 | + ), |
| 146 | + test_user |
| 147 | + ) |
| 148 | + |
| 149 | + response = problem_client.delete(f"/problems/{problem.id}") |
| 150 | + data = response.json() |
| 151 | + |
| 152 | + assert response.status_code == status.HTTP_200_OK |
| 153 | + assert data["message"] == "Problem deleted successfully" |
| 154 | + |
| 155 | + response = problem_client.get(f"/problems/access/{problem.access_code}") |
| 156 | + assert response.status_code == status.HTTP_404_NOT_FOUND |
| 157 | + |
| 158 | +def test_delete_problem_not_found(problem_client): |
| 159 | + """Test deleting non-existent problem""" |
| 160 | + response = problem_client.delete("/problems/9999") |
| 161 | + assert response.status_code == status.HTTP_404_NOT_FOUND |
| 162 | + |
| 163 | +def test_delete_problem_unauthorized(problem_client, db_session, test_user): |
| 164 | + """Test deleting another user's problem""" |
| 165 | + |
| 166 | + other_user = UserModel( |
| 167 | + email="other@test.com", |
| 168 | + password_hash="password", |
| 169 | + username="otheruser" |
| 170 | + ) |
| 171 | + db_session.add(other_user) |
| 172 | + db_session.commit() |
| 173 | + db_session.refresh(other_user) |
| 174 | + |
| 175 | + |
| 176 | + problem_service = ProblemService(db_session) |
| 177 | + problem = problem_service.create_problem( |
| 178 | + ProblemCreate( |
| 179 | + title="Unauthorized Delete", |
| 180 | + description="Test", |
| 181 | + code_snippet="code", |
| 182 | + expected_output="output", |
| 183 | + language="python", |
| 184 | + is_public=True |
| 185 | + ), |
| 186 | + other_user |
| 187 | + ) |
| 188 | + |
| 189 | + response = problem_client.delete(f"/problems/{problem.id}") |
| 190 | + assert response.status_code == status.HTTP_403_FORBIDDEN |
| 191 | + |
| 192 | + |
| 193 | +def test_unauthenticated_access(problem_client, problem_app): |
| 194 | + """Test endpoints without authentication""" |
| 195 | + |
| 196 | + from dependencies import get_current_user as get_current_user_dep |
| 197 | + problem_app.dependency_overrides.pop(get_current_user_dep) |
| 198 | + |
| 199 | + response = problem_client.post("/problems", data={}) |
| 200 | + assert response.status_code == status.HTTP_401_UNAUTHORIZED |
| 201 | + |
| 202 | + response = problem_client.get("/problems") |
| 203 | + assert response.status_code == status.HTTP_401_UNAUTHORIZED |
| 204 | + |
| 205 | + response = problem_client.delete("/problems/1") |
| 206 | + assert response.status_code == status.HTTP_401_UNAUTHORIZED |
0 commit comments