|
| 1 | +import pytest |
| 2 | +from django.conf import settings |
| 3 | +from django.urls import reverse |
| 4 | +from rest_framework.test import APIClient |
| 5 | +from apps.web.tests import factories |
| 6 | + |
| 7 | +# ------------------------------------------------------------------------- |
| 8 | +# 1. Clients & Authentication |
| 9 | +# ------------------------------------------------------------------------- |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def base_client(): |
| 14 | + """Returns an unauthenticated API client.""" |
| 15 | + return APIClient() |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def user(db): |
| 20 | + """Returns a saved user instance.""" |
| 21 | + return factories.UserFactory() |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def auth_client(user, base_client): |
| 26 | + """Returns an API client authenticated as the 'user' fixture.""" |
| 27 | + base_client.force_authenticate(user=user) |
| 28 | + return base_client |
| 29 | + |
| 30 | + |
| 31 | +# ------------------------------------------------------------------------- |
| 32 | +# 2. Data Fixtures (Models) |
| 33 | +# ------------------------------------------------------------------------- |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture |
| 37 | +def course(db): |
| 38 | + """Returns a saved course instance.""" |
| 39 | + return factories.CourseFactory() |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def course_batch(db): |
| 44 | + """Returns a batch of 3 general courses.""" |
| 45 | + return factories.CourseFactory.create_batch(3) |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture |
| 49 | +def department_mixed_courses(db): |
| 50 | + """Returns a mixed set of courses for filtering/sorting tests.""" |
| 51 | + # Note: Using course_title to match current course model field |
| 52 | + return [ |
| 53 | + factories.CourseFactory( |
| 54 | + department="MATH", |
| 55 | + course_title="Honors Calculus II", |
| 56 | + course_code="MATH1560J", |
| 57 | + ), |
| 58 | + factories.CourseFactory( |
| 59 | + department="MATH", course_title="Calculus II", course_code="MATH1160J" |
| 60 | + ), |
| 61 | + factories.CourseFactory( |
| 62 | + department="CHEM", course_title="Chemistry", course_code="CHEM2100J" |
| 63 | + ), |
| 64 | + ] |
| 65 | + |
| 66 | + |
| 67 | +@pytest.fixture |
| 68 | +def review(db, course, user, min_len): |
| 69 | + """Returns a saved review instance belonging to 'user'.""" |
| 70 | + return factories.ReviewFactory(course=course, user=user, comments="a" * min_len) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.fixture |
| 74 | +def other_review(db): |
| 75 | + """Returns a review belonging to a different user for security testing.""" |
| 76 | + from apps.web.tests.factories import UserFactory, ReviewFactory |
| 77 | + |
| 78 | + return ReviewFactory(user=UserFactory()) |
| 79 | + |
| 80 | + |
| 81 | +@pytest.fixture |
| 82 | +def course_factory(db): |
| 83 | + """Access the factory class directly for custom batch creation.""" |
| 84 | + return factories.CourseFactory |
| 85 | + |
| 86 | + |
| 87 | +# ------------------------------------------------------------------------- |
| 88 | +# 3. Validation & Payloads |
| 89 | +# ------------------------------------------------------------------------- |
| 90 | + |
| 91 | + |
| 92 | +@pytest.fixture |
| 93 | +def min_len(): |
| 94 | + """Retrieves the minimum comment length from project settings.""" |
| 95 | + return settings.WEB["REVIEW"]["COMMENT_MIN_LENGTH"] |
| 96 | + |
| 97 | + |
| 98 | +@pytest.fixture |
| 99 | +def valid_review_data(min_len): |
| 100 | + """Generates a valid payload for review creation/update tests.""" |
| 101 | + return { |
| 102 | + "term": "23F", |
| 103 | + "professor": "Dr. Testing", |
| 104 | + "comments": "a" * min_len, |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | +# ------------------------------------------------------------------------- |
| 109 | +# 4. URL Fixtures (Routing) |
| 110 | +# ------------------------------------------------------------------------- |
| 111 | + |
| 112 | + |
| 113 | +@pytest.fixture |
| 114 | +def course_reviews_url(course): |
| 115 | + """URL for listing/posting reviews for a specific course.""" |
| 116 | + return reverse("course_review_api", kwargs={"course_id": course.id}) |
| 117 | + |
| 118 | + |
| 119 | +@pytest.fixture |
| 120 | +def personal_reviews_list_url(): |
| 121 | + """URL for the current user's personal review list.""" |
| 122 | + return reverse("user_reviews_api") |
| 123 | + |
| 124 | + |
| 125 | +@pytest.fixture |
| 126 | +def personal_review_detail_url(review): |
| 127 | + """URL for GET/PUT/DELETE a specific review owned by the user.""" |
| 128 | + return reverse("user_review_api", kwargs={"review_id": review.id}) |
| 129 | + |
| 130 | + |
| 131 | +@pytest.fixture |
| 132 | +def other_review_detail_url(other_review): |
| 133 | + """URL for a review NOT owned by the current user (used for 404/Security).""" |
| 134 | + return reverse("user_review_api", kwargs={"review_id": other_review.id}) |
0 commit comments