Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add test cases for newly added unlink_self endpoint #2282

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion enterprise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Your project description goes here.
"""

__version__ = "4.31.1"
__version__ = "4.31.2"
59 changes: 59 additions & 0 deletions tests/test_enterprise/api/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
Loading