forked from openedx/openedx-platform
-
Notifications
You must be signed in to change notification settings - Fork 15
feat: remove enterprise enrollment imports and adds CourseEnrollmentViewStarted filter #306
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
168 changes: 168 additions & 0 deletions
168
openedx/core/djangoapps/enrollments/tests/test_filters.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| """ | ||
| Test for CourseEnrollmentViewStarted filter in enrollment views. | ||
| """ | ||
| import json | ||
|
|
||
| from django.test import override_settings | ||
| from django.urls import reverse | ||
| from openedx_filters import PipelineStep | ||
| from openedx_filters.learning.filters import CourseEnrollmentViewStarted | ||
| from rest_framework import status | ||
| from rest_framework.test import APITestCase | ||
|
|
||
| from common.djangoapps.course_modes.tests.factories import CourseModeFactory | ||
| from common.djangoapps.student.tests.factories import UserFactory, UserProfileFactory | ||
| from openedx.core.djangolib.testing.utils import skip_unless_lms | ||
|
Copilot marked this conversation as resolved.
|
||
| from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase | ||
| from xmodule.modulestore.tests.factories import CourseFactory | ||
|
|
||
| TEST_STEP_PATH = "openedx.core.djangoapps.enrollments.tests.test_filters.TestCourseEnrollmentViewStartedPipelineStep" | ||
|
|
||
|
|
||
| class TestCourseEnrollmentViewStartedPipelineStep(PipelineStep): | ||
| """ | ||
| Utility pipeline step that prevents enrollment via CourseEnrollmentViewStarted filter. | ||
| """ | ||
|
|
||
| def run_filter(self, user, course_key, requester_is_backend_service): # pylint: disable=arguments-differ | ||
| """Pipeline step that prevents enrollment for specific users or conditions.""" | ||
| if user.username == "blocked_user": | ||
| raise CourseEnrollmentViewStarted.PreventEnrollment( | ||
| "User is not allowed to enroll in this course." | ||
| ) | ||
| # Set a side effect on the user to verify the filter was executed | ||
| user.profile.set_meta({"enrollment_filter_executed": True}) | ||
| user.profile.save() | ||
|
kiram15 marked this conversation as resolved.
|
||
| return { | ||
| "user": user, | ||
| "course_key": course_key, | ||
| "requester_is_backend_service": requester_is_backend_service, | ||
| } | ||
|
|
||
|
|
||
| @skip_unless_lms | ||
| class CourseEnrollmentViewStartedFilterTest(APITestCase, ModuleStoreTestCase): | ||
|
kiram15 marked this conversation as resolved.
|
||
| """ | ||
| Tests for the CourseEnrollmentViewStarted filter in the enrollment view. | ||
|
|
||
| This class guarantees that the following filters are triggered when attempting | ||
| to enroll a user through the enrollment API endpoint: | ||
|
|
||
| - CourseEnrollmentViewStarted | ||
| """ | ||
|
|
||
| def setUp(self): # pylint: disable=arguments-differ | ||
| super().setUp() | ||
| self.course = CourseFactory.create(emit_signals=True) | ||
| self.user = UserFactory.create( | ||
| username="test_user", | ||
| email="test@example.com", | ||
| password="password", | ||
| ) | ||
| self.blocked_user = UserFactory.create( | ||
| username="blocked_user", | ||
| email="blocked@example.com", | ||
| password="password", | ||
| ) | ||
| UserProfileFactory.create(user=self.user, name="Test User") | ||
| UserProfileFactory.create(user=self.blocked_user, name="Blocked User") | ||
|
|
||
| # Create course modes | ||
|
Copilot marked this conversation as resolved.
|
||
| CourseModeFactory.create( | ||
|
Copilot marked this conversation as resolved.
|
||
| course_id=self.course.id, | ||
| mode_slug='audit', | ||
| mode_display_name='Audit' | ||
| ) | ||
|
|
||
| @override_settings( | ||
| OPEN_EDX_FILTERS_CONFIG={ | ||
| "org.openedx.learning.course.enrollment.view.started.v1": { | ||
| "pipeline": [ | ||
| TEST_STEP_PATH, | ||
| ], | ||
| "fail_silently": False, | ||
| }, | ||
| }, | ||
| ) | ||
| def test_course_enrollment_view_started_filter_executed(self): | ||
| """ | ||
| Test that the CourseEnrollmentViewStarted filter is executed when enrolling through the view. | ||
|
|
||
| Expected result: | ||
| - CourseEnrollmentViewStarted is triggered and executes the pipeline step. | ||
| - The enrollment is created successfully if the filter allows it. | ||
| - The filter pipeline step sets a side effect on the user's profile. | ||
| """ | ||
| self.client.login(username=self.user.username, password="password") | ||
|
|
||
| response = self.client.post( | ||
|
Copilot marked this conversation as resolved.
Copilot marked this conversation as resolved.
|
||
| reverse('courseenrollments'), | ||
| { | ||
| "course_details": {"course_id": str(self.course.id)}, | ||
| "mode": "audit", | ||
| }, | ||
| format="json", | ||
| ) | ||
|
|
||
| assert response.status_code == status.HTTP_200_OK | ||
| # Verify the filter was executed by checking the side effect | ||
| self.user.profile.refresh_from_db() | ||
| meta = self.user.profile.get_meta() | ||
| assert meta.get("enrollment_filter_executed") is True | ||
|
kiram15 marked this conversation as resolved.
|
||
|
|
||
|
kiram15 marked this conversation as resolved.
|
||
| @override_settings( | ||
| OPEN_EDX_FILTERS_CONFIG={ | ||
| "org.openedx.learning.course.enrollment.view.started.v1": { | ||
| "pipeline": [ | ||
| TEST_STEP_PATH, | ||
| ], | ||
| "fail_silently": False, | ||
| }, | ||
| }, | ||
| ) | ||
| def test_course_enrollment_view_started_filter_prevent_enrollment(self): | ||
| """ | ||
| Test that enrollment is prevented when filter raises PreventEnrollment. | ||
|
|
||
| Expected result: | ||
| - CourseEnrollmentViewStarted is triggered and executes the pipeline step. | ||
| - The pipeline step raises PreventEnrollment for blocked_user. | ||
| - The endpoint returns HTTP 400 Bad Request. | ||
|
kiram15 marked this conversation as resolved.
|
||
| """ | ||
| self.client.login(username=self.blocked_user.username, password="password") | ||
|
|
||
| response = self.client.post( | ||
| reverse('courseenrollments'), | ||
| { | ||
| "course_details": {"course_id": str(self.course.id)}, | ||
| "mode": "audit", | ||
| }, | ||
| format="json", | ||
| ) | ||
|
|
||
| assert response.status_code == status.HTTP_400_BAD_REQUEST | ||
| response_data = json.loads(response.content.decode('utf-8')) | ||
| assert "message" in response_data | ||
| assert "An error occurred while creating the new course enrollment" in response_data["message"] | ||
|
kiram15 marked this conversation as resolved.
|
||
|
|
||
| @override_settings(OPEN_EDX_FILTERS_CONFIG={}) | ||
| def test_course_enrollment_view_started_without_filter_configuration(self): | ||
| """ | ||
| Test enrollment without filter configuration (no filter interference). | ||
|
|
||
| Expected result: | ||
| - CourseEnrollmentViewStarted does not have any effect. | ||
| - The enrollment process succeeds without filter intervention. | ||
| """ | ||
| self.client.login(username=self.user.username, password="password") | ||
|
|
||
| response = self.client.post( | ||
|
Copilot marked this conversation as resolved.
Copilot marked this conversation as resolved.
|
||
| reverse('courseenrollments'), | ||
| { | ||
| "course_details": {"course_id": str(self.course.id)}, | ||
| "mode": "audit", | ||
| }, | ||
| format="json", | ||
| ) | ||
|
|
||
| assert response.status_code == status.HTTP_200_OK | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.