Add endpoint for checking learner program enrollments
JIRA:EDUCATOR-4327
This commit is contained in:
@@ -54,6 +54,7 @@ class ListViewTestMixin(object):
|
||||
def setUpClass(cls):
|
||||
super(ListViewTestMixin, cls).setUpClass()
|
||||
cls.program_uuid = '00000000-1111-2222-3333-444444444444'
|
||||
cls.program_uuid_tmpl = '00000000-1111-2222-3333-4444444444{0:02d}'
|
||||
cls.curriculum_uuid = 'aaaaaaaa-1111-2222-3333-444444444444'
|
||||
cls.other_curriculum_uuid = 'bbbbbbbb-1111-2222-3333-444444444444'
|
||||
|
||||
@@ -77,6 +78,37 @@ class ListViewTestMixin(object):
|
||||
return reverse(self.view_name, kwargs=kwargs)
|
||||
|
||||
|
||||
class LearnerProgramEnrollmentTest(ListViewTestMixin, APITestCase):
|
||||
"""
|
||||
Tests for the LearnerProgramEnrollment view class
|
||||
"""
|
||||
view_name = 'programs_api:v1:learner_program_enrollments'
|
||||
|
||||
def test_401_if_anonymous(self):
|
||||
response = self.client.get(reverse(self.view_name))
|
||||
assert status.HTTP_401_UNAUTHORIZED == response.status_code
|
||||
|
||||
@mock.patch('lms.djangoapps.program_enrollments.api.v1.views.get_programs', autospec=True, return_value=None)
|
||||
def test_200_if_no_programs_enrolled(self, mock_get_programs):
|
||||
self.client.login(username=self.student.username, password=self.password)
|
||||
response = self.client.get(reverse(self.view_name))
|
||||
assert status.HTTP_200_OK == response.status_code
|
||||
assert response.data == []
|
||||
assert mock_get_programs.call_count == 1
|
||||
|
||||
@mock.patch('lms.djangoapps.program_enrollments.api.v1.views.get_programs', autospec=True, return_value=[
|
||||
{'uuid': 'boop', 'marketing_slug': 'garbage-program'},
|
||||
{'uuid': 'boop-boop', 'marketing_slug': 'garbage-study'},
|
||||
{'uuid': 'boop-boop-boop', 'marketing_slug': 'garbage-life'},
|
||||
])
|
||||
def test_200_many_programs(self, mock_get_programs):
|
||||
self.client.login(username=self.student.username, password=self.password)
|
||||
response = self.client.get(reverse(self.view_name))
|
||||
assert status.HTTP_200_OK == response.status_code
|
||||
assert len(response.data) == 3
|
||||
assert mock_get_programs.call_count == 1
|
||||
|
||||
|
||||
class ProgramEnrollmentListTest(ListViewTestMixin, APITestCase):
|
||||
"""
|
||||
Tests for GET calls to the Program Enrollments API.
|
||||
|
||||
@@ -8,12 +8,18 @@ from lms.djangoapps.program_enrollments.api.v1.views import (
|
||||
ProgramEnrollmentsView,
|
||||
ProgramCourseEnrollmentsView,
|
||||
ProgramCourseEnrollmentOverviewView,
|
||||
LearnerProgramEnrollmentsView,
|
||||
)
|
||||
from openedx.core.constants import COURSE_ID_PATTERN
|
||||
|
||||
app_name = 'lms.djangoapps.program_enrollments'
|
||||
|
||||
urlpatterns = [
|
||||
url(
|
||||
r'^programs/enrollments/$',
|
||||
LearnerProgramEnrollmentsView.as_view(),
|
||||
name='learner_program_enrollments'
|
||||
),
|
||||
url(
|
||||
r'^programs/{program_uuid}/enrollments/$'.format(program_uuid=PROGRAM_UUID_PATTERN),
|
||||
ProgramEnrollmentsView.as_view(),
|
||||
|
||||
@@ -440,6 +440,58 @@ class ProgramEnrollmentsView(DeveloperErrorViewMixin, PaginatedAPIView):
|
||||
)
|
||||
|
||||
|
||||
class LearnerProgramEnrollmentsView(DeveloperErrorViewMixin, APIView):
|
||||
"""
|
||||
A view for checking the currently logged-in learner's program enrollments
|
||||
|
||||
Path: `/api/program_enrollments/v1/programs/enrollments/`
|
||||
|
||||
Returns:
|
||||
* 200: OK - Contains a list of all programs in which the learner is enrolled.
|
||||
* 401: The requesting user is not authenticated.
|
||||
|
||||
The list will be a list of objects with the following keys:
|
||||
* `uuid` - the identifier of the program in which the learner is enrolled.
|
||||
* `slug` - the string from which a link to the corresponding program page can be constructed.
|
||||
|
||||
Example:
|
||||
[
|
||||
{
|
||||
'uuid': '00000000-1111-2222-3333-444444444444',
|
||||
'slug': 'deadbeef'
|
||||
},
|
||||
{
|
||||
'uuid': '00000000-1111-2222-3333-444444444445',
|
||||
'slug': 'undead-cattle'
|
||||
}
|
||||
]
|
||||
"""
|
||||
authentication_classes = (
|
||||
JwtAuthentication,
|
||||
OAuth2AuthenticationAllowInactiveUser,
|
||||
SessionAuthenticationAllowInactiveUser,
|
||||
)
|
||||
permission_classes = (IsAuthenticated,)
|
||||
|
||||
def get(self, request):
|
||||
"""
|
||||
How to respond to a GET request to this endpoint
|
||||
"""
|
||||
program_enrollments = ProgramEnrollment.objects.filter(
|
||||
user=request.user,
|
||||
status__in=('enrolled', 'pending')
|
||||
)
|
||||
|
||||
uuids = [enrollment.program_uuid for enrollment in program_enrollments]
|
||||
|
||||
catalog_data_of_programs = get_programs(uuids=uuids) or []
|
||||
programs_in_which_learner_is_enrolled = [{'uuid': program['uuid'], 'slug': program['marketing_slug']}
|
||||
for program
|
||||
in catalog_data_of_programs]
|
||||
|
||||
return Response(programs_in_which_learner_is_enrolled, status.HTTP_200_OK)
|
||||
|
||||
|
||||
class ProgramSpecificViewMixin(object):
|
||||
"""
|
||||
A mixin for views that operate on or within a specific program.
|
||||
|
||||
Reference in New Issue
Block a user