forked from openedx/edx-platform
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds MFE API. This is part of the work that is being done to obtain the MFE Runtime Configurations and that has been discussed in the BTR WG. Discussion: https://discuss.openedx.org/t/how-to-use-microfrontend-in-a-multitenant-instance/6936/14?u=mafermazu MFE Runtime configuration - eduNEXT: https://docs.google.com/document/d/1-FHIQmyeQZu3311x8eYUNMru4JX7Yb3UlqjmJxvM8do/edit?usp=sharing feat: add lms setting to set mfe config cache (#262) Co-authored-by: María Fernanda Magallanes Z <[email protected]> feat: make mfe config api disabled by default (#263) * feat: make mfe config api disabled by default * fix: simple is better than complex test: add mfe config tests (#264) * test: add mfe config tests * test: fix it and simplify it * test: correct pylint issues fix: correct pep 8 violations fix: add mfe api unit test in github workflow fix: correct unit tests refactor: move mfe api to lms fix: try mfe api urls without regex fix: add app_namespace in lms urls fix: try url without conditional Revert "fix: try url without conditional" This reverts commit 694aab546134b4bd9ad2642e24927b42cac24459. fix: set enable_mfe_config_api feature to true in the tests test: try to add failed test case Revert "test: try to add failed test case" This reverts commit cee6bf656ab1b96492b0b6199ddff32a6d6a65bd. docs: improve explanation and documentation fix: ensure the response is a json object refactor: be consistent with the variable names fix: allow overriding mfe api config cache timeout in production fix: handle 404 response in view refactor: use a guard instead if-else feat: add the possibility to show mfe specific config
- Loading branch information
Showing
11 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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
Empty file.
Empty file.
This file contains 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,43 @@ | ||
""" | ||
Test the use cases of the views of the mfe api. | ||
""" | ||
|
||
from unittest.mock import patch | ||
|
||
from django.conf import settings | ||
from django.urls import reverse | ||
from rest_framework import status | ||
|
||
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers | ||
from openedx.core.lib.api.test_utils import ApiTestCase | ||
|
||
|
||
class MFEConfigTestCase(ApiTestCase): | ||
""" | ||
Test the use case that exposes the site configuration with the mfe api. | ||
""" | ||
def setUp(self): | ||
self.mfe_config_api_url = reverse('mfe_api:config') | ||
return super().setUp() | ||
|
||
def test_get_mfe_config(self): | ||
"""Test the get mfe config from site configuration with the mfe api. | ||
Expected result: | ||
- Inside self.get_json pass the response is a json and the status is 200 asserts. | ||
- The configuration obtained by the api is equal to its site configuration in the | ||
MFE_CONFIG key. | ||
""" | ||
mfe_config = configuration_helpers.get_value('MFE_CONFIG', {}) | ||
response_json = self.get_json(self.mfe_config_api_url) | ||
assert response_json == mfe_config | ||
|
||
@patch.dict(settings.FEATURES, {'ENABLE_MFE_API': False}) | ||
def test_404_get_mfe_config(self): | ||
"""Test the 404 not found response from get mfe config. | ||
Expected result: | ||
- Response status code equal to 404 | ||
""" | ||
response = self.client.get(self.mfe_config_api_url) | ||
assert response.status_code == status.HTTP_404_NOT_FOUND |
This file contains 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,10 @@ | ||
""" URLs configuration for the mfe api.""" | ||
|
||
from django.urls import path | ||
|
||
from lms.djangoapps.mfe_api.views import MFEConfigView | ||
|
||
app_name = 'mfe_api' | ||
urlpatterns = [ | ||
path('v1/config', MFEConfigView.as_view(), name='config'), | ||
] |
This file contains 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,42 @@ | ||
""" | ||
MFE API Views for useful information related to mfes. | ||
""" | ||
|
||
from django.conf import settings | ||
from django.http import JsonResponse | ||
from django.utils.decorators import method_decorator | ||
from django.views.decorators.cache import cache_page | ||
from rest_framework import status | ||
from rest_framework.views import APIView | ||
|
||
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers | ||
|
||
|
||
class MFEConfigView(APIView): | ||
""" | ||
Provides an API endpoint to get the MFE_CONFIG from site configuration. | ||
""" | ||
|
||
@method_decorator(cache_page(settings.MFE_API_CONFIG_CACHE_TIMEOUT)) | ||
def get(self, request): | ||
""" | ||
GET /api/mfe/v1/config | ||
**GET Response Values** | ||
``` | ||
{ | ||
"LOGO_URL": "https://example.com/logo.png", | ||
} | ||
``` | ||
""" | ||
|
||
if not settings.FEATURES.get('ENABLE_MFE_API'): | ||
msg = 'MFE API not found. Try setting FEATURES["ENABLE_MFE_API"] to true.' | ||
return JsonResponse({'message': msg}, status=status.HTTP_404_NOT_FOUND) | ||
|
||
mfe_config = {'MFE_CONFIG': configuration_helpers.get_value('MFE_CONFIG', {})} | ||
if request.query_params.get('mfe'): | ||
mfe = str(request.query_params.get('mfe')).upper() | ||
mfe_config[f'MFE_CONFIG_{mfe}']= configuration_helpers.get_value(f'MFE_CONFIG_{mfe}',{}) | ||
|
||
return JsonResponse(mfe_config, status=status.HTTP_200_OK) |
This file contains 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 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 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 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