-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Add openedx-filters hook to account settings before rendering it context #31295
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
mariajgrimaldi
merged 6 commits into
openedx:master
from
eduNEXT:hpg/add-filters-hook-registrationrenred-settingsrender
Mar 9, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
99dd634
feat: add filter hooks before regitration render and settings context
Henrrypg 98333c8
fix: add exceptions and test to account settings filter
Henrrypg c2523c6
fix: change implementation and add tests
Henrrypg b056cbf
fix: fix tests
Henrrypg 4a0b6f2
chore: upgrade openedx-filters
Henrrypg 776e65b
fix: add auth on test filters
Henrrypg 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
There are no files selected for viewing
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
241 changes: 241 additions & 0 deletions
241
openedx/core/djangoapps/user_api/accounts/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,241 @@ | ||
| """ | ||
| Test that various filters are fired for views in the certificates app. | ||
| """ | ||
| from django.http import HttpResponse | ||
| from django.test import override_settings | ||
| from django.urls import reverse | ||
| from openedx_filters import PipelineStep | ||
| from openedx_filters.learning.filters import AccountSettingsRenderStarted | ||
| from rest_framework import status | ||
| from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase | ||
|
|
||
| from openedx.core.djangolib.testing.utils import skip_unless_lms | ||
| from common.djangoapps.student.tests.factories import UserFactory | ||
|
|
||
|
|
||
| class TestRenderInvalidAccountSettings(PipelineStep): | ||
| """ | ||
| Utility class used when getting steps for pipeline. | ||
| """ | ||
|
|
||
| def run_filter(self, context, template_name): # pylint: disable=arguments-differ | ||
| """ | ||
| Pipeline step that stops the course about render process. | ||
| """ | ||
| raise AccountSettingsRenderStarted.RenderInvalidAccountSettings( | ||
| "You can't access the account settings page.", | ||
| account_settings_template="static_templates/server-error.html", | ||
| ) | ||
|
|
||
|
|
||
| class TestRedirectToPage(PipelineStep): | ||
| """ | ||
| Utility class used when getting steps for pipeline. | ||
| """ | ||
|
|
||
| def run_filter(self, context, template_name): # pylint: disable=arguments-differ | ||
| """ | ||
| Pipeline step that redirects to dashboard before rendering the account settings page. | ||
|
|
||
| When raising RedirectToPage, this filter uses a redirect_to field handled by | ||
| the course about view that redirects to that URL. | ||
| """ | ||
| raise AccountSettingsRenderStarted.RedirectToPage( | ||
| "You can't access this page, redirecting to dashboard.", | ||
| redirect_to="/courses", | ||
| ) | ||
|
|
||
|
|
||
| class TestRedirectToDefaultPage(PipelineStep): | ||
| """ | ||
| Utility class used when getting steps for pipeline. | ||
| """ | ||
|
|
||
| def run_filter(self, context, template_name): # pylint: disable=arguments-differ | ||
| """ | ||
| Pipeline step that redirects to dashboard before rendering the account settings page. | ||
|
|
||
| When raising RedirectToPage, this filter uses a redirect_to field handled by | ||
| the course about view that redirects to that URL. | ||
| """ | ||
| raise AccountSettingsRenderStarted.RedirectToPage( | ||
| "You can't access this page, redirecting to dashboard." | ||
| ) | ||
|
|
||
|
|
||
| class TestRenderCustomResponse(PipelineStep): | ||
| """ | ||
| Utility class used when getting steps for pipeline. | ||
| """ | ||
|
|
||
| def run_filter(self, context, template_name): # pylint: disable=arguments-differ | ||
| """Pipeline step that returns a custom response when rendering the account settings page.""" | ||
| response = HttpResponse("Here's the text of the web page.") | ||
| raise AccountSettingsRenderStarted.RenderCustomResponse( | ||
| "You can't access this page.", | ||
| response=response, | ||
| ) | ||
|
|
||
|
|
||
| class TestAccountSettingsRender(PipelineStep): | ||
| """ | ||
| Utility class used when getting steps for pipeline. | ||
| """ | ||
|
|
||
| def run_filter(self, context, template_name): # pylint: disable=arguments-differ | ||
| """Pipeline step that returns a custom response when rendering the account settings page.""" | ||
| template_name = 'static_templates/about.html' | ||
| return { | ||
| "context": context, "template_name": template_name, | ||
| } | ||
|
|
||
|
|
||
| @skip_unless_lms | ||
| class TestAccountSettingsFilters(SharedModuleStoreTestCase): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd use these tests as a template for testing this kind of filters: Those are precisely the same as here. I know this needs to be documented somewhere -we're working on it- but again, you could use them as a guide. |
||
| """ | ||
| Tests for the Open edX Filters associated with the account settings proccess. | ||
|
|
||
| This class guarantees that the following filters are triggered during the user's account settings rendering: | ||
|
|
||
| - AccountSettingsRenderStarted | ||
| """ | ||
| def setUp(self): # pylint: disable=arguments-differ | ||
| super().setUp() | ||
| self.user = UserFactory.create( | ||
| username="somestudent", | ||
| first_name="Student", | ||
| last_name="Person", | ||
| email="robot@robot.org", | ||
| is_active=True, | ||
| password="password", | ||
| ) | ||
| self.client.login(username=self.user.username, password="password") | ||
| self.account_settings_url = '/account/settings' | ||
|
|
||
| @override_settings( | ||
| OPEN_EDX_FILTERS_CONFIG={ | ||
| "org.openedx.learning.student.settings.render.started.v1": { | ||
| "pipeline": [ | ||
| "openedx.core.djangoapps.user_api.accounts.tests.test_filters.TestAccountSettingsRender", | ||
| ], | ||
| "fail_silently": False, | ||
| }, | ||
| }, | ||
| ) | ||
| def test_account_settings_render_filter_executed(self): | ||
| """ | ||
| Test whether the account settings filter is triggered before the user's | ||
| account settings page is rendered. | ||
|
|
||
| Expected result: | ||
| - AccountSettingsRenderStarted is triggered and executes TestAccountSettingsRender | ||
| """ | ||
| response = self.client.get(self.account_settings_url) | ||
| self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
| self.assertContains(response, "This page left intentionally blank. Feel free to add your own content.") | ||
|
|
||
| @override_settings( | ||
| OPEN_EDX_FILTERS_CONFIG={ | ||
| "org.openedx.learning.student.settings.render.started.v1": { | ||
| "pipeline": [ | ||
| "openedx.core.djangoapps.user_api.accounts.tests.test_filters.TestRenderInvalidAccountSettings", # pylint: disable=line-too-long | ||
| ], | ||
| "fail_silently": False, | ||
| }, | ||
| }, | ||
| PLATFORM_NAME="My site", | ||
| ) | ||
| def test_account_settings_render_alternative(self): | ||
| """ | ||
| Test whether the account settings filter is triggered before the user's | ||
| account settings page is rendered. | ||
|
|
||
| Expected result: | ||
| - AccountSettingsRenderStarted is triggered and executes TestRenderInvalidAccountSettings # pylint: disable=line-too-long | ||
| """ | ||
| response = self.client.get(self.account_settings_url) | ||
|
|
||
| self.assertContains(response, "There has been a 500 error on the <em>My site</em> servers") | ||
|
|
||
| @override_settings( | ||
| OPEN_EDX_FILTERS_CONFIG={ | ||
| "org.openedx.learning.student.settings.render.started.v1": { | ||
| "pipeline": [ | ||
| "openedx.core.djangoapps.user_api.accounts.tests.test_filters.TestRenderCustomResponse", | ||
| ], | ||
| "fail_silently": False, | ||
| }, | ||
| }, | ||
| ) | ||
| def test_account_settings_render_custom_response(self): | ||
| """ | ||
| Test whether the account settings filter is triggered before the user's | ||
| account settings page is rendered. | ||
|
|
||
| Expected result: | ||
| - AccountSettingsRenderStarted is triggered and executes TestRenderCustomResponse | ||
| """ | ||
| response = self.client.get(self.account_settings_url) | ||
|
|
||
| self.assertEqual(response.content, b"Here's the text of the web page.") | ||
|
|
||
| @override_settings( | ||
| OPEN_EDX_FILTERS_CONFIG={ | ||
| "org.openedx.learning.student.settings.render.started.v1": { | ||
| "pipeline": [ | ||
| "openedx.core.djangoapps.user_api.accounts.tests.test_filters.TestRedirectToPage", | ||
| ], | ||
| "fail_silently": False, | ||
| }, | ||
| }, | ||
| ) | ||
| def test_account_settings_redirect_to_page(self): | ||
| """ | ||
| Test whether the account settings filter is triggered before the user's | ||
| account settings page is rendered. | ||
|
|
||
| Expected result: | ||
| - AccountSettingsRenderStarted is triggered and executes TestRedirectToPage | ||
| """ | ||
| response = self.client.get(self.account_settings_url) | ||
|
|
||
| self.assertEqual(response.status_code, status.HTTP_302_FOUND) | ||
| self.assertEqual('/courses', response.url) | ||
|
|
||
| @override_settings( | ||
| OPEN_EDX_FILTERS_CONFIG={ | ||
| "org.openedx.learning.student.settings.render.started.v1": { | ||
| "pipeline": [ | ||
| "openedx.core.djangoapps.user_api.accounts.tests.test_filters.TestRedirectToDefaultPage", | ||
| ], | ||
| "fail_silently": False, | ||
| }, | ||
| }, | ||
| ) | ||
| def test_account_settings_redirect_default(self): | ||
| """ | ||
| Test whether the account settings filter is triggered before the user's | ||
| account settings page is rendered. | ||
|
|
||
| Expected result: | ||
| - AccountSettingsRenderStarted is triggered and executes TestRedirectToDefaultPage | ||
| """ | ||
| response = self.client.get(self.account_settings_url) | ||
|
|
||
| self.assertEqual(response.status_code, status.HTTP_302_FOUND) | ||
| self.assertEqual(f"{reverse('dashboard')}", response.url) | ||
|
|
||
| @override_settings(OPEN_EDX_FILTERS_CONFIG={}) | ||
| def test_account_settings_render_without_filter_config(self): | ||
| """ | ||
| Test whether the course about filter is triggered before the course about | ||
| render without affecting its execution flow. | ||
|
|
||
| Expected result: | ||
| - AccountSettingsRenderStarted executes a noop (empty pipeline). Without any | ||
| modification comparing it with the effects of TestAccountSettingsRender. | ||
| - The view response is HTTP_200_OK. | ||
| """ | ||
| response = self.client.get(self.account_settings_url) | ||
|
|
||
| self.assertNotContains(response, "This page left intentionally blank. Feel free to add your own content.") | ||
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
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.