diff --git a/test_utils/factories.py b/test_utils/factories.py index ad1e3078fc..5a73ffc31f 100644 --- a/test_utils/factories.py +++ b/test_utils/factories.py @@ -24,6 +24,8 @@ EnterpriseCustomerInviteKey, EnterpriseCustomerReportingConfiguration, EnterpriseCustomerUser, + EnterpriseFeatureRole, + EnterpriseFeatureUserRoleAssignment, LicensedEnterpriseCourseEnrollment, PendingEnrollment, PendingEnterpriseCustomerAdminUser, @@ -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. diff --git a/tests/test_enterprise/api/test_filters.py b/tests/test_enterprise/api/test_filters.py index c4a35d1510..d5daf59458 100644 --- a/tests/test_enterprise/api/test_filters.py +++ b/tests/test_enterprise/api/test_filters.py @@ -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') @@ -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):