From 931f0ae5f5a91478021038f8b032fcc3eb71b026 Mon Sep 17 00:00:00 2001
From: Alex Wang <awang@edx.org>
Date: Tue, 16 Jun 2020 11:57:23 -0400
Subject: [PATCH] add username and email to getProgramEnrollment response
 (#24186)

MST-234
---
 .../rest_api/v1/serializers.py                | 12 +++++++++++
 .../rest_api/v1/tests/test_views.py           | 21 +++++++++++--------
 2 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/lms/djangoapps/program_enrollments/rest_api/v1/serializers.py b/lms/djangoapps/program_enrollments/rest_api/v1/serializers.py
index 9618021a696..009585e6280 100644
--- a/lms/djangoapps/program_enrollments/rest_api/v1/serializers.py
+++ b/lms/djangoapps/program_enrollments/rest_api/v1/serializers.py
@@ -37,6 +37,8 @@ class ProgramEnrollmentSerializer(serializers.Serializer):
     status = serializers.CharField()
     account_exists = serializers.SerializerMethodField()
     curriculum_uuid = serializers.UUIDField()
+    username = serializers.SerializerMethodField()
+    email = serializers.SerializerMethodField()
 
     class Meta(object):
         model = ProgramEnrollment
@@ -44,6 +46,16 @@ class ProgramEnrollmentSerializer(serializers.Serializer):
     def get_account_exists(self, obj):
         return bool(obj.user)
 
+    def get_username(self, obj):
+        if obj.user:
+            return obj.user.username
+        return ""
+
+    def get_email(self, obj):
+        if obj.user:
+            return obj.user.email
+        return ""
+
 
 class ProgramEnrollmentRequestMixin(InvalidStatusMixin, serializers.Serializer):
     """
diff --git a/lms/djangoapps/program_enrollments/rest_api/v1/tests/test_views.py b/lms/djangoapps/program_enrollments/rest_api/v1/tests/test_views.py
index dace29ab72f..ea88bc4fb9d 100644
--- a/lms/djangoapps/program_enrollments/rest_api/v1/tests/test_views.py
+++ b/lms/djangoapps/program_enrollments/rest_api/v1/tests/test_views.py
@@ -231,7 +231,10 @@ class ProgramEnrollmentsGetTests(EnrollmentsDataMixin, APITestCase):
         for i in range(2, 4):
             user_key = 'user-{}'.format(i)
             ProgramEnrollmentFactory.create(
-                program_uuid=self.program_uuid, curriculum_uuid=self.curriculum_uuid, external_user_key=user_key,
+                program_uuid=self.program_uuid,
+                curriculum_uuid=self.curriculum_uuid,
+                user=UserFactory.create(username='student-{}'.format(i), email='email-{}'.format(i)),
+                external_user_key=user_key,
             )
 
         self.addCleanup(self.destroy_program_enrollments)
@@ -283,19 +286,19 @@ class ProgramEnrollmentsGetTests(EnrollmentsDataMixin, APITestCase):
             'results': [
                 {
                     'student_key': 'user-0', 'status': 'pending', 'account_exists': False,
-                    'curriculum_uuid': text_type(self.curriculum_uuid),
+                    'curriculum_uuid': text_type(self.curriculum_uuid), 'username': "", 'email': ""
                 },
                 {
                     'student_key': 'user-1', 'status': 'pending', 'account_exists': False,
-                    'curriculum_uuid': text_type(self.curriculum_uuid),
+                    'curriculum_uuid': text_type(self.curriculum_uuid), 'username': "", 'email': ""
                 },
                 {
                     'student_key': 'user-2', 'status': 'enrolled', 'account_exists': True,
-                    'curriculum_uuid': text_type(self.curriculum_uuid),
+                    'curriculum_uuid': text_type(self.curriculum_uuid), 'username': "student-2", 'email': "email-2"
                 },
                 {
                     'student_key': 'user-3', 'status': 'enrolled', 'account_exists': True,
-                    'curriculum_uuid': text_type(self.curriculum_uuid),
+                    'curriculum_uuid': text_type(self.curriculum_uuid), 'username': "student-3", 'email': "email-3"
                 },
             ],
         }
@@ -312,11 +315,11 @@ class ProgramEnrollmentsGetTests(EnrollmentsDataMixin, APITestCase):
         expected_results = [
             {
                 'student_key': 'user-0', 'status': 'pending', 'account_exists': False,
-                'curriculum_uuid': text_type(self.curriculum_uuid),
+                'curriculum_uuid': text_type(self.curriculum_uuid), 'username': "", 'email': ""
             },
             {
                 'student_key': 'user-1', 'status': 'pending', 'account_exists': False,
-                'curriculum_uuid': text_type(self.curriculum_uuid),
+                'curriculum_uuid': text_type(self.curriculum_uuid), 'username': "", 'email': ""
             },
         ]
         assert expected_results == response.data['results']
@@ -331,11 +334,11 @@ class ProgramEnrollmentsGetTests(EnrollmentsDataMixin, APITestCase):
         next_expected_results = [
             {
                 'student_key': 'user-2', 'status': 'enrolled', 'account_exists': True,
-                'curriculum_uuid': text_type(self.curriculum_uuid),
+                'curriculum_uuid': text_type(self.curriculum_uuid), 'username': "student-2", 'email': "email-2"
             },
             {
                 'student_key': 'user-3', 'status': 'enrolled', 'account_exists': True,
-                'curriculum_uuid': text_type(self.curriculum_uuid),
+                'curriculum_uuid': text_type(self.curriculum_uuid), 'username': "student-3", 'email': "email-3"
             },
         ]
         assert next_expected_results == next_response.data['results']
-- 
GitLab