diff --git a/enterprise/api/v1/serializers.py b/enterprise/api/v1/serializers.py index 620898d7e..632206a07 100644 --- a/enterprise/api/v1/serializers.py +++ b/enterprise/api/v1/serializers.py @@ -1918,10 +1918,8 @@ class Meta: fields = ( 'enterprise_customer_user', 'enrollments', - 'full_name', ) - # enterprise_customer_user = UserSerializer(source="user", required=False, default=None) enterprise_customer_user = serializers.SerializerMethodField() enrollments = serializers.SerializerMethodField() @@ -1929,8 +1927,8 @@ def get_enrollments(self, obj): """ Fetch all of user's enterprise enrollments """ - if hasattr(obj, 'user_id'): - user_id = obj.user_id + if user := obj: + user_id = user[0] enrollments = models.EnterpriseCourseEnrollment.objects.filter( enterprise_customer_user=user_id, ) @@ -1943,9 +1941,9 @@ def get_enterprise_customer_user(self, obj): """ if user := obj: return { - "email": user[0], - "joined_org": user[1].strftime("%b %d, %Y"), - "name": user[2], + "email": user[1], + "joined_org": user[2].strftime("%b %d, %Y"), + "name": user[3], } return None diff --git a/enterprise/api/v1/views/enterprise_customer_members.py b/enterprise/api/v1/views/enterprise_customer_members.py index 387ef5a9c..90a884ebb 100644 --- a/enterprise/api/v1/views/enterprise_customer_members.py +++ b/enterprise/api/v1/views/enterprise_customer_members.py @@ -75,6 +75,7 @@ def get_members(self, request, *args, **kwargs): query = """ WITH users AS ( SELECT + au.id, au.email, au.date_joined, coalesce(NULLIF(aup.name, ''), concat(au.first_name, ' ', au.last_name)) as full_name diff --git a/tests/test_enterprise/api/test_serializers.py b/tests/test_enterprise/api/test_serializers.py index 57d57bd0d..e6900fd39 100644 --- a/tests/test_enterprise/api/test_serializers.py +++ b/tests/test_enterprise/api/test_serializers.py @@ -588,27 +588,42 @@ def setUp(self): def test_serialize_users(self): expected_user = { - 'enrollments': 2, 'enterprise_customer_user': { 'email': self.user_1.email, 'joined_org': self.user_1.date_joined.strftime("%b %d, %Y"), 'name': (self.user_1.first_name + ' ' + self.user_1.last_name), }, + 'enrollments': 2, } - serializer = EnterpriseMembersSerializer(self.enterprise_customer_user_1) + + print(type(self.user_1.date_joined)) + serializer_input_1 = [ + self.user_1.id, + self.user_1.email, + self.user_1.date_joined, + self.user_1.first_name + ' ' + self.user_1.last_name, + ] + serializer = EnterpriseMembersSerializer(serializer_input_1) serialized_user = serializer.data self.assertEqual(serialized_user, expected_user) expected_user_2 = { - 'enrollments': 1, 'enterprise_customer_user': { 'email': self.user_2.email, 'joined_org': self.user_2.date_joined.strftime("%b %d, %Y"), 'name': self.user_2.first_name + ' ' + self.user_2.last_name, }, + 'enrollments': 1, } - serializer = EnterpriseMembersSerializer(self.enterprise_customer_user_2) + serializer_input_2 = [ + self.user_2.id, + self.user_2.email, + self.user_2.date_joined, + self.user_2.first_name + ' ' + self.user_2.last_name, + ] + + serializer = EnterpriseMembersSerializer(serializer_input_2) serialized_user = serializer.data self.assertEqual(serialized_user, expected_user_2) diff --git a/tests/test_enterprise/api/test_views.py b/tests/test_enterprise/api/test_views.py index 24a2b8503..9391f4cff 100644 --- a/tests/test_enterprise/api/test_views.py +++ b/tests/test_enterprise/api/test_views.py @@ -10005,74 +10005,6 @@ def test_list_users_filtered(self): assert response.json().get('count') == 1 -@ddt.ddt -@mark.django_db -class TestEnterpriseCustomerMembers(BaseTestEnterpriseAPIViews): - """ - Test enterprise customer members list endpoint - """ - ECM_ENDPOINT = 'enterprise-customer-members' - ECM_KWARG = 'enterprise_uuid' - - def test_get_enterprise_org_members(self): - """ - Assert whether the response is valid. - """ - user_1 = factories.UserFactory(first_name="Rhaenyra", last_name="Targaryen") - user_2 = factories.UserFactory(first_name="Jace", last_name="Targaryen") - user_3 = factories.UserFactory(first_name="Alicent", last_name="Hightower") - user_4 = factories.UserFactory(first_name="Helaena", last_name="Targaryen") - user_5 = factories.UserFactory(first_name="Laenor", last_name="Velaryon") - - enterprise_customer = factories.EnterpriseCustomerFactory(uuid=FAKE_UUIDS[0]) - factories.EnterpriseCustomerUserFactory( - user_id=user_1.id, - enterprise_customer=enterprise_customer - ) - factories.EnterpriseCustomerUserFactory( - user_id=user_2.id, - enterprise_customer=enterprise_customer - ) - factories.EnterpriseCustomerUserFactory( - user_id=user_3.id, - enterprise_customer=enterprise_customer - ) - factories.EnterpriseCustomerUserFactory( - user_id=user_4.id, - enterprise_customer=enterprise_customer - ) - factories.EnterpriseCustomerUserFactory( - user_id=user_5.id, - enterprise_customer=enterprise_customer - ) - - # Test invalid UUID - url = reverse(self.ECM_ENDPOINT, kwargs={self.ECM_KWARG: 123}) - response = self.client.get(settings.TEST_SERVER + url) - self.assertEqual(response.status_code, 404) - - # Test valid UUID - url = reverse(self.ECM_ENDPOINT, kwargs={self.ECM_KWARG: enterprise_customer.uuid}) - response = self.client.get(settings.TEST_SERVER + url) - data = response.json().get('results') - - # list should be sorted alphabetically by name - self.assertEqual(data[0]['enterprise_customer_user']['name'], (user_3.first_name + ' ' + user_3.last_name)) - self.assertEqual(data[1]['enterprise_customer_user']['name'], (user_4.first_name + ' ' + user_4.last_name)) - self.assertEqual(data[2]['enterprise_customer_user']['name'], (user_2.first_name + ' ' + user_2.last_name)) - self.assertEqual(data[3]['enterprise_customer_user']['name'], (user_5.first_name + ' ' + user_5.last_name)) - self.assertEqual(data[4]['enterprise_customer_user']['name'], (user_1.first_name + ' ' + user_1.last_name)) - - # use user query to filter by name - name_query = f'?user_query={user_2.first_name}' - url = reverse(self.ECM_ENDPOINT, kwargs={self.ECM_KWARG: enterprise_customer.uuid}) - url = url + name_query - response = self.client.get(settings.TEST_SERVER + url) - data = response.json().get('results') - self.assertEqual(len(data), 1) - self.assertEqual(data[0]['enterprise_customer_user']['name'], (user_2.first_name + ' ' + user_2.last_name)) - - @ddt.ddt @mark.django_db class TestDefaultEnterpriseEnrollmentIntentionViewSet(BaseTestEnterpriseAPIViews):