forked from openedx/openedx-platform
-
Notifications
You must be signed in to change notification settings - Fork 14
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
Open
kiram15
wants to merge
5
commits into
release-ulmo
Choose a base branch
from
kiram15/ENT-11570
base: release-ulmo
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
82533f2
feat: remove enterprise enrollment imports and adds CourseEnrollmentV…
kiram15 520fb70
Merge branch 'release-ulmo' into kiram15/ENT-11570
kiram15 61da77e
fix: version bump and add test
kiram15 1568606
Merge branch 'kiram15/ENT-11570' of github.com:edx/edx-platform into …
kiram15 12a7943
Merge branch 'release-ulmo' into kiram15/ENT-11570
kiram15 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
167 changes: 167 additions & 0 deletions
167
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,167 @@ | ||
| """ | ||
| 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 xmodule.modulestore.tests.django_utils import ModuleStoreTestCase | ||
| from xmodule.modulestore.tests.factories import CourseFactory | ||
|
|
||
| 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.
|
||
|
|
||
| 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() | ||
| return { | ||
| "user": user, | ||
| "course_key": course_key, | ||
| "requester_is_backend_service": requester_is_backend_service, | ||
| } | ||
|
|
||
|
|
||
| @skip_unless_lms | ||
| class CourseEnrollmentViewStartedFilterTest(APITestCase, ModuleStoreTestCase): | ||
|
|
||
| """ | ||
| 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": [ | ||
| 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", | ||
| ) | ||
|
|
||
| self.assertEqual(status.HTTP_200_OK, response.status_code) | ||
| # Verify the filter was executed by checking the side effect | ||
| self.user.profile.refresh_from_db() | ||
| self.assertTrue(self.user.profile.get_meta("enrollment_filter_executed")) | ||
|
|
||
|
kiram15 marked this conversation as resolved.
|
||
| @override_settings( | ||
| OPEN_EDX_FILTERS_CONFIG={ | ||
| "org.openedx.learning.course.enrollment.view.started.v1": { | ||
| "pipeline": [ | ||
| 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 403 Forbidden. | ||
| """ | ||
| 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", | ||
| ) | ||
|
|
||
| self.assertEqual(status.HTTP_403_FORBIDDEN, response.status_code) | ||
| response_data = json.loads(response.content.decode('utf-8')) | ||
| self.assertIn("message", response_data) | ||
| self.assertIn("User is not allowed to enroll", response_data["message"]) | ||
|
|
||
| @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", | ||
| ) | ||
|
|
||
| self.assertEqual(status.HTTP_200_OK, response.status_code) | ||
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.