|
15 | 15 | from django.contrib.auth.models import Permission |
16 | 16 | from django.http import HttpRequest |
17 | 17 | from django.test import TestCase |
| 18 | +from django.urls import reverse |
18 | 19 |
|
19 | 20 | from enterprise.api.utils import CourseRunProgressStatuses |
20 | 21 | from enterprise.api.v1.serializers import ( |
|
37 | 38 | SystemWideEnterpriseUserRoleAssignment, |
38 | 39 | ) |
39 | 40 | from test_utils import FAKE_UUIDS, TEST_PGP_KEY, TEST_USERNAME, APITest, factories |
| 41 | +from rest_framework.test import APIClient, APITestCase |
40 | 42 |
|
41 | 43 | Application = get_application_model() |
42 | 44 |
|
@@ -573,6 +575,93 @@ def test_serialize_pending_users(self): |
573 | 575 |
|
574 | 576 | self.assertEqual(expected_pending_admin_user, serialized_pending_admin_user) |
575 | 577 |
|
| 578 | +class TestEnterpriseCustomerMembersView(APITestCase): |
| 579 | + def setUp(self): |
| 580 | + self.client = APIClient() |
| 581 | + # Create an enterprise customer |
| 582 | + self.enterprise_customer = factories.EnterpriseCustomerFactory() |
| 583 | + |
| 584 | + # Learner user |
| 585 | + self.learner_user = factories.UserFactory() |
| 586 | + factories.EnterpriseCustomerUserFactory( |
| 587 | + enterprise_customer=self.enterprise_customer, |
| 588 | + user_fk=self.learner_user, |
| 589 | + user_id=self.learner_user.id, |
| 590 | + ) |
| 591 | + |
| 592 | + # Admin user (should be excluded) |
| 593 | + self.admin_user = factories.UserFactory() |
| 594 | + |
| 595 | + # Create admin role |
| 596 | + admin_role = SystemWideEnterpriseRole.objects.create(name="enterprise_admin") |
| 597 | + |
| 598 | + # Assign the role to the admin user |
| 599 | + SystemWideEnterpriseUserRoleAssignment.objects.create( |
| 600 | + user_id=self.admin_user.id, |
| 601 | + role=admin_role, |
| 602 | + ) |
| 603 | + |
| 604 | + # Link the admin user to the enterprise customer |
| 605 | + factories.EnterpriseCustomerUserFactory( |
| 606 | + enterprise_customer=self.enterprise_customer, |
| 607 | + user_fk=self.admin_user, |
| 608 | + user_id=self.admin_user.id, |
| 609 | + ) |
| 610 | + |
| 611 | + def test_only_learners_returned(self): |
| 612 | + url = reverse( |
| 613 | + "enterprise-customer-members", |
| 614 | + kwargs={"enterprise_uuid": self.enterprise_customer.uuid} |
| 615 | + ) |
| 616 | + response = self.client.get(url) |
| 617 | + self.assertEqual(response.status_code, 200) |
| 618 | + |
| 619 | + # Extract user_ids from response |
| 620 | + returned_ids = [u['enterprise_customer_user']['user_id'] for u in response.data['results']] |
| 621 | + self.assertIn(self.learner_user.id, returned_ids) |
| 622 | + self.assertNotIn(self.admin_user.id, returned_ids) |
| 623 | + |
| 624 | + def test_inactive_ecu_records_not_returned(self): |
| 625 | + """ |
| 626 | + Test that inactive EnterpriseCustomerUser records are excluded from the result set. |
| 627 | + """ |
| 628 | + # Create a learner role |
| 629 | + learner_role = SystemWideEnterpriseRole.objects.create(name="enterprise_learner") |
| 630 | + |
| 631 | + # Create another learner user |
| 632 | + inactive_learner_user = factories.UserFactory() |
| 633 | + inactive_ecu = factories.EnterpriseCustomerUserFactory( |
| 634 | + enterprise_customer=self.enterprise_customer, |
| 635 | + user_fk=inactive_learner_user, |
| 636 | + user_id=inactive_learner_user.id, |
| 637 | + active=False, # Mark this user as inactive |
| 638 | + ) |
| 639 | + |
| 640 | + # Assign learner role to both users |
| 641 | + SystemWideEnterpriseUserRoleAssignment.objects.create( |
| 642 | + user_id=self.learner_user.id, |
| 643 | + role=learner_role, |
| 644 | + ) |
| 645 | + SystemWideEnterpriseUserRoleAssignment.objects.create( |
| 646 | + user_id=inactive_learner_user.id, |
| 647 | + role=learner_role, |
| 648 | + ) |
| 649 | + |
| 650 | + url = reverse( |
| 651 | + "enterprise-customer-members", |
| 652 | + kwargs={"enterprise_uuid": self.enterprise_customer.uuid} |
| 653 | + ) |
| 654 | + response = self.client.get(url) |
| 655 | + self.assertEqual(response.status_code, 200) |
| 656 | + |
| 657 | + # Extract user_ids from response |
| 658 | + returned_ids = [u['enterprise_customer_user']['user_id'] for u in response.data['results']] |
| 659 | + |
| 660 | + # Only the active learner should be returned |
| 661 | + self.assertIn(self.learner_user.id, returned_ids) |
| 662 | + # The inactive learner should NOT be returned |
| 663 | + self.assertNotIn(inactive_learner_user.id, returned_ids) |
| 664 | + |
576 | 665 |
|
577 | 666 | class TestEnterpriseMembersSerializer(TestCase): |
578 | 667 | """ |
|
0 commit comments