Skip to content

Commit

Permalink
test: EnterpriseCourseEnrollmentFilterBackend
Browse files Browse the repository at this point in the history
  • Loading branch information
0x29a committed Feb 7, 2023
1 parent f4abe0e commit 454653a
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
37 changes: 37 additions & 0 deletions test_utils/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
EnterpriseCustomerInviteKey,
EnterpriseCustomerReportingConfiguration,
EnterpriseCustomerUser,
EnterpriseFeatureRole,
EnterpriseFeatureUserRoleAssignment,
LicensedEnterpriseCourseEnrollment,
PendingEnrollment,
PendingEnterpriseCustomerAdminUser,
Expand Down Expand Up @@ -263,6 +265,41 @@ class Meta:
date_joined = factory.LazyAttribute(lambda x: FAKER.date_time_this_year(tzinfo=timezone.utc))


class EnterpriseFeatureRoleFactory(factory.django.DjangoModelFactory):
"""
EnterpriseFeatureRole factory.
Creates an instance of EnterpriseFeatureRole with minimal boilerplate.
"""

class Meta:
"""
Meta for EnterpriseFeatureRoleFactory.
"""

model = EnterpriseFeatureRole

name = factory.LazyAttribute(lambda x: FAKER.word())


class EnterpriseFeatureUserRoleAssignmentFactory(factory.django.DjangoModelFactory):
"""
EnterpriseFeatureUserRoleAssignment factory.
Creates an instance of EnterpriseFeatureUserRoleAssignment with minimal boilerplate.
"""

class Meta:
"""
Meta for EnterpriseFeatureUserRoleAssignmentFactory.
"""

model = EnterpriseFeatureUserRoleAssignment

role = factory.SubFactory(EnterpriseFeatureRoleFactory)
user = factory.SubFactory(UserFactory)


class AnonymousUserFactory(factory.Factory):
"""
Anonymous User factory.
Expand Down
58 changes: 57 additions & 1 deletion tests/test_enterprise/api/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

from django.conf import settings

from enterprise.constants import ENTERPRISE_ADMIN_ROLE
from enterprise.constants import ENTERPRISE_ADMIN_ROLE, ENTERPRISE_ENROLLMENT_API_ADMIN_ROLE
from enterprise.models import EnterpriseFeatureRole

from test_utils import FAKE_UUIDS, TEST_EMAIL, TEST_USERNAME, APITest, factories

ENTERPRISE_CUSTOMER_LIST_ENDPOINT = reverse('enterprise-customer-list')
Expand Down Expand Up @@ -80,6 +82,60 @@ def test_filter_for_detail(self, is_staff, is_linked, expected_content_in_respon
assert data[key] == value


@ddt.ddt
@mark.django_db
class TestEnterpriseCourseEnrollmentFilterBackend(APITest):
"""
Test suite for the ``EnterpriseCourseEnrollmentFilterBackend`` filter.
"""

def setUp(self):
super().setUp()
self.other_user= factories.UserFactory()
self.enterprise_customer = factories.EnterpriseCustomerFactory(uuid=FAKE_UUIDS[0])

self.enterprise_customer_user_1 = factories.EnterpriseCustomerUserFactory(
enterprise_customer=self.enterprise_customer,
user_id=self.user.id
)
self.enterprise_customer_user_2 = factories.EnterpriseCustomerUserFactory(
enterprise_customer=self.enterprise_customer,
user_id=self.other_user.id
)
self.course_enrollment_1 = factories.EnterpriseCourseEnrollmentFactory(
enterprise_customer_user=self.enterprise_customer_user_1
)
self.course_enrollment_2 = factories.EnterpriseCourseEnrollmentFactory(
enterprise_customer_user=self.enterprise_customer_user_2
)

self.url = settings.TEST_SERVER + reverse('enterprise-course-enrollment-list')

@ddt.data(
("regular", 1),
("staff", 2),
("enrollment_api_admin", 2),
)
@ddt.unpack
def test_filter_for_list(self, user_role, expected_course_enrollment_count):
"""
Filter objects based off whether the user is a staff, enterprise enrollment api admin, or neither.
"""
if user_role == "staff":
self.user.is_staff = True
self.user.save()
elif user_role == "enrollment_api_admin":
factories.EnterpriseFeatureUserRoleAssignmentFactory(
user=self.user,
role=EnterpriseFeatureRole.objects.get(name=ENTERPRISE_ENROLLMENT_API_ADMIN_ROLE)
)

response = self.client.get(self.url)
assert response.status_code == status.HTTP_200_OK
data = self.load_json(response.content)
assert len(data['results']) == expected_course_enrollment_count


@ddt.ddt
@mark.django_db
class TestEnterpriseCustomerUserFilterBackend(APITest):
Expand Down

0 comments on commit 454653a

Please sign in to comment.