From 2e6c0e685caa90c478134593ca19f8b69f1f2f2d Mon Sep 17 00:00:00 2001 From: jajjibhai008 Date: Tue, 5 Nov 2024 18:03:24 +0500 Subject: [PATCH] feat: add test cases for newly added unlink_self endpoint --- CHANGELOG.rst | 4 ++ enterprise/__init__.py | 2 +- tests/test_enterprise/api/test_views.py | 59 +++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 5dad7fd76..388bf1677 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -17,6 +17,10 @@ Unreleased ---------- * nothing unreleased +[4.31.2] +-------- +* feat: add test cases for newly added unlink_self endpoint. + [4.31.1] -------- * fix: fixed query for monthly_impact_report command. diff --git a/enterprise/__init__.py b/enterprise/__init__.py index ed23c74c3..aac295279 100644 --- a/enterprise/__init__.py +++ b/enterprise/__init__.py @@ -2,4 +2,4 @@ Your project description goes here. """ -__version__ = "4.31.1" +__version__ = "4.31.2" diff --git a/tests/test_enterprise/api/test_views.py b/tests/test_enterprise/api/test_views.py index c2e2a126a..066984521 100644 --- a/tests/test_enterprise/api/test_views.py +++ b/tests/test_enterprise/api/test_views.py @@ -161,6 +161,7 @@ ENTERPRISE_LEARNER_LIST_ENDPOINT = reverse('enterprise-learner-list') ENTERPRISE_CUSTOMER_WITH_ACCESS_TO_ENDPOINT = reverse('enterprise-customer-with-access-to') ENTERPRISE_CUSTOMER_UNLINK_USERS_ENDPOINT = reverse('enterprise-customer-unlink-users', kwargs={'pk': FAKE_UUIDS[0]}) +ENTERPRISE_CUSTOMER_UNLINK_SELF_ENDPOINT = reverse('enterprise-customer-unlink-self', kwargs={'pk': FAKE_UUIDS[0]}) PENDING_ENTERPRISE_LEARNER_LIST_ENDPOINT = reverse('pending-enterprise-learner-list') PENDING_ENTERPRISE_CUSTOMER_ADMIN_LIST_ENDPOINT = reverse('pending-enterprise-admin-list') LICENSED_ENTERPRISE_COURSE_ENROLLMENTS_REVOKE_ENDPOINT = reverse( @@ -2478,6 +2479,64 @@ def test_unlink_users(self, enterprise_role, enterprise_uuid_for_role, is_relink assert enterprise_customer_user_2.is_relinkable == is_relinkable assert enterprise_customer_user_2.is_relinkable == is_relinkable + def test_unlink_self(self): + """ + Test that a user can unlink themselves from the enterprise. + """ + email = 'user@test.com' + + # Create a user and set it as the request user + user = factories.UserFactory(email=email) + self.client.force_authenticate(user=user) + + # Create an enterprise customer and link the user to it + enterprise_customer = factories.EnterpriseCustomerFactory(uuid=FAKE_UUIDS[0], slug='test-enterprise-slug') + enterprise_customer_user = factories.EnterpriseCustomerUserFactory( + user_id=user.id, + enterprise_customer=enterprise_customer, + linked=True + ) + + # Ensure the user is initially linked + assert enterprise_customer_user.linked is True + + # Make the unlink request + response = self.client.post(ENTERPRISE_CUSTOMER_UNLINK_SELF_ENDPOINT) + + # Verify the response status code + assert response.status_code == 200 + + # Refresh the object from the database to check if it's unlinked + enterprise_customer_user.refresh_from_db() + assert enterprise_customer_user.linked is False + assert enterprise_customer_user.is_relinkable is True + + def test_unlink_self_not_linked(self): + """ + Test that a user cannot unlink themselves if they are not already linked to the enterprise. + """ + email = 'user@test.com' + + # Create a user and set it as the request user + user = factories.UserFactory(email=email) + self.client.force_authenticate(user=user) + + # Create an enterprise customer and ensure the user is not linked + enterprise_customer = factories.EnterpriseCustomerFactory(uuid=FAKE_UUIDS[0], slug='test-enterprise-slug') + enterprise_customer_user = factories.EnterpriseCustomerUserFactory( + user_id=user.id, + enterprise_customer=enterprise_customer, + linked=False + ) + + # Ensure the user is initially not linked + assert enterprise_customer_user.linked is False + # Make the unlink request + response = self.client.post(ENTERPRISE_CUSTOMER_UNLINK_SELF_ENDPOINT) + + # Verify the response status code + assert response.status_code == 404 + @ddt.ddt @mark.django_db