diff --git a/courses/urls.py b/courses/urls.py index 83f18942d..a0aae1d17 100644 --- a/courses/urls.py +++ b/courses/urls.py @@ -4,6 +4,7 @@ from rest_framework import routers from courses.views import v1 +from courses.views.v1 import EmeritusCourseListView router = routers.SimpleRouter() router.register(r"programs", v1.ProgramViewSet, basename="programs_api") @@ -29,4 +30,9 @@ re_path( r"^api/enrollments/", v1.UserEnrollmentsView.as_view(), name="user-enrollments" ), + path( + "api/emeritus_courses/", + EmeritusCourseListView.as_view(), + name="emeritus_courses", + ), ] diff --git a/courses/views/v1/__init__.py b/courses/views/v1/__init__.py index 0ee5dd328..b6afeeb89 100644 --- a/courses/views/v1/__init__.py +++ b/courses/views/v1/__init__.py @@ -27,6 +27,7 @@ ProgramEnrollmentSerializer, ProgramSerializer, ) +from courses.sync_external_courses.emeritus_api import fetch_emeritus_courses from ecommerce.models import Product @@ -202,3 +203,22 @@ def get_queryset(self): Returns parent topics with course count > 0. """ return CourseTopic.parent_topics_with_courses() + + +class EmeritusCourseListView(APIView): + """ + ReadOnly View to list Emeritus courses. + """ + + def get(self, request, *args, **kwargs): # noqa: ARG002 + """ + Get Emeritus courses list from the Emeritus API and return it. + """ + try: + data = fetch_emeritus_courses() + return Response(data, status=status.HTTP_200_OK) + except Exception: # noqa: BLE001 + return Response( + {"error": "Some error occurred."}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR, + ) diff --git a/courses/views_test.py b/courses/views_test.py index 2c735eac6..5d86e55da 100644 --- a/courses/views_test.py +++ b/courses/views_test.py @@ -2,8 +2,10 @@ Tests for course views """ +import json import operator as op from datetime import timedelta +from pathlib import Path import pytest from django.contrib.auth.models import AnonymousUser @@ -611,3 +613,23 @@ def test_course_topics_api(client, django_assert_num_queries): assert len(resp_json) == 1 assert resp_json[0]["name"] == parent_topic.name assert resp_json[0]["course_count"] == 4 + + +def test_emeritus_course_list_view(admin_drf_client, mocker): + """ + Test that the Emeritus API List calls fetch_emeritus_courses and returns its mocked response. + """ + with Path( + "courses/sync_external_courses/test_data/batch_test.json" + ).open() as test_data_file: + mocked_response = json.load(test_data_file) + + patched_fetch_emeritus_courses = mocker.patch( + "courses.sync_external_courses.emeritus_api.fetch_emeritus_courses", + return_value=mocked_response, + ) + + response = admin_drf_client.get(reverse("emeritus_courses")) + assert response.json() == mocked_response + assert response.status_code == 200 + patched_fetch_emeritus_courses.assert_called_once()