diff --git a/courses/serializers/v2/courses.py b/courses/serializers/v2/courses.py index a19198f6f6..d099bc08e8 100644 --- a/courses/serializers/v2/courses.py +++ b/courses/serializers/v2/courses.py @@ -31,6 +31,7 @@ class CourseSerializer(BaseCourseSerializer): page = CoursePageSerializer(read_only=True) programs = serializers.SerializerMethodField() topics = serializers.SerializerMethodField() + certificate_type = serializers.SerializerMethodField() def get_next_run_id(self, instance): """Get next run id""" @@ -54,6 +55,13 @@ def get_topics(self, instance): ) return [] + def get_certificate_type(self, instance): + if instance.programs: + program = instance.programs[0] + if "MicroMasters" in program.program_type: + return "MicroMasters Credential" + return "Certificate of Completion" + class Meta: model = models.Course fields = [ @@ -65,6 +73,7 @@ class Meta: "page", "programs", "topics", + "certificate_type", ] diff --git a/courses/serializers/v2/courses_test.py b/courses/serializers/v2/courses_test.py new file mode 100644 index 0000000000..538f4bf522 --- /dev/null +++ b/courses/serializers/v2/courses_test.py @@ -0,0 +1,73 @@ +import pytest +from django.contrib.auth.models import AnonymousUser + +from cms.serializers import CoursePageSerializer +from courses.factories import ( + CourseRunEnrollmentFactory, + CourseRunFactory, + ProgramFactory, +) +from courses.models import CoursesTopic, Department +from courses.serializers.v1.base import BaseProgramSerializer +from courses.serializers.v2.courses import ( + CourseRunSerializer, + CourseWithCourseRunsSerializer, +) +from main.test_utils import assert_drf_json_equal + +pytestmark = [pytest.mark.django_db] + + +@pytest.mark.parametrize("is_anonymous", [True, False]) +@pytest.mark.parametrize("all_runs", [True, False]) +@pytest.mark.parametrize( + "certificate_type", ["MicroMasters Credential", "Certificate of Completion"] +) +def test_serialize_course( + mocker, mock_context, is_anonymous, all_runs, certificate_type +): + """Test Course serialization""" + if is_anonymous: + mock_context["request"].user = AnonymousUser() + if all_runs: + mock_context["all_runs"] = True + user = mock_context["request"].user + courseRun1 = CourseRunFactory.create() + courseRun2 = CourseRunFactory.create(course=courseRun1.course) + course = courseRun1.course + topics = [CoursesTopic.objects.create(name=f"topic{num}") for num in range(3)] + course.page.topics.set([topics[0], topics[1], topics[2]]) + department = "a course departments" + course.departments.set([Department.objects.create(name=department)]) + program = ProgramFactory.create(program_type="Series") + if certificate_type == "MicroMasters Credential": + program.program_type = "MicroMasters®" + program.add_requirement(course) + program.save() + + CourseRunEnrollmentFactory.create( + run=courseRun1, **({} if is_anonymous else {"user": user}) + ) + + data = CourseWithCourseRunsSerializer(instance=course, context=mock_context).data + + assert_drf_json_equal( + data, + { + "title": course.title, + "readable_id": course.readable_id, + "id": course.id, + "courseruns": [ + CourseRunSerializer(courseRun1).data, + CourseRunSerializer(courseRun2).data, + ], + "next_run_id": course.first_unexpired_run.id, + "departments": [{"name": department}], + "page": CoursePageSerializer(course.page).data, + "certificate_type": certificate_type, + "topics": [{"name": topic.name} for topic in topics], + "programs": BaseProgramSerializer(course.programs, many=True).data + if all_runs + else None, + }, + ) diff --git a/courses/serializers/v2/programs.py b/courses/serializers/v2/programs.py index 113a892cd0..d1791494d5 100644 --- a/courses/serializers/v2/programs.py +++ b/courses/serializers/v2/programs.py @@ -22,6 +22,7 @@ class ProgramSerializer(serializers.ModelSerializer): page = serializers.SerializerMethodField() departments = serializers.StringRelatedField(many=True, read_only=True) topics = serializers.SerializerMethodField() + certificate_type = serializers.SerializerMethodField() def get_courses(self, instance): return [course[0].id for course in instance.courses if course[0].live] @@ -56,6 +57,11 @@ def get_topics(self, instance): ) return [{"name": topic} for topic in sorted(topics)] + def get_certificate_type(self, instance): + if "MicroMasters" in instance.program_type: + return "MicroMasters Credential" + return "Certificate of Completion" + class Meta: model = Program fields = [ @@ -67,6 +73,7 @@ class Meta: "req_tree", "page", "program_type", + "certificate_type", "departments", "live", "topics", diff --git a/courses/serializers/v2/programs_test.py b/courses/serializers/v2/programs_test.py index fa75cfbf3a..b15b953e88 100644 --- a/courses/serializers/v2/programs_test.py +++ b/courses/serializers/v2/programs_test.py @@ -19,12 +19,20 @@ pytestmark = [pytest.mark.django_db] +@pytest.mark.parametrize("remove_tree", [True, False]) @pytest.mark.parametrize( - "remove_tree", - [True, False], + "certificate_type", ["MicroMasters Credential", "Certificate of Completion"] ) -def test_serialize_program(mock_context, remove_tree, program_with_empty_requirements): # noqa: F811 +def test_serialize_program( + mock_context, + remove_tree, + certificate_type, + program_with_empty_requirements, # noqa: F811 +): """Test Program serialization""" + if certificate_type == "MicroMasters Credential": + program_with_empty_requirements.program_type = "MicroMasters®" + program_with_empty_requirements.save() run1 = CourseRunFactory.create( course__page=None, start_date=now() + timedelta(hours=1), @@ -79,7 +87,8 @@ def test_serialize_program(mock_context, remove_tree, program_with_empty_require program_with_empty_requirements.requirements_root ).data, "page": ProgramPageSerializer(program_with_empty_requirements.page).data, - "program_type": "Series", + "program_type": program_with_empty_requirements.program_type, + "certificate_type": certificate_type, "departments": [], "live": True, "topics": [{"name": topic.name} for topic in topics],