Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions courses/serializers/v2/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,9 @@ class CourseWithCourseRunsSerializer(CourseSerializer):

@extend_schema_field(CourseRunSerializer(many=True))
def get_courseruns(self, instance):
# Use prefetched course runs to preserve prefetched products
courseruns = instance.courseruns.all()
# Use prefetched course runs to preserve prefetched products, but
# restrict to runs that are currently enrollable.
courseruns = instance.courseruns.enrollable().all()
if "org_id" in self.context:
courseruns = [
run
Expand Down
18 changes: 18 additions & 0 deletions courses/serializers/v2/courses_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ def test_serialize_course_required_prerequisites(
)


def test_course_with_course_runs_only_includes_enrollable_runs(mock_context, settings):
"""CourseWithCourseRunsSerializer should only return enrollable course runs."""
# Create a course with two runs: one enrollable (default factory), one not enrollable
enrollable_run = CourseRunFactory.create()
course = enrollable_run.course

# Make a non-enrollable run for the same course by setting enrollment_end in the past
non_enrollable_run = CourseRunFactory.create(
course=course, past_enrollment_end=True
)

data = CourseWithCourseRunsSerializer(instance=course, context=mock_context).data

# Only the enrollable run should be present
returned_run_ids = {run["id"] for run in data["courseruns"]}
assert returned_run_ids == {enrollable_run.id}


class TestCourseRunEnrollmentSerializerV2:
"""Test the v2 CourseRunEnrollmentSerializer."""

Expand Down
Loading