Skip to content

Commit

Permalink
fix: test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kiram15 committed Nov 20, 2024
1 parent 88785e2 commit f14c1c6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 79 deletions.
12 changes: 5 additions & 7 deletions enterprise/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1918,19 +1918,17 @@ 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()

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,
)
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions enterprise/api/v1/views/enterprise_customer_members.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def get_members(self, request, *args, **kwargs):
query = """

Check warning on line 75 in enterprise/api/v1/views/enterprise_customer_members.py

View check run for this annotation

Codecov / codecov/patch

enterprise/api/v1/views/enterprise_customer_members.py#L75

Added line #L75 was not covered by tests
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
Expand Down
23 changes: 19 additions & 4 deletions tests/test_enterprise/api/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
68 changes: 0 additions & 68 deletions tests/test_enterprise/api/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit f14c1c6

Please sign in to comment.