forked from openedx/edx-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request openedx#30473 from eduNEXT/mfmz/mfe-config-api
feat: add mfe config api
- Loading branch information
Showing
11 changed files
with
277 additions
and
2 deletions.
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.
72 changes: 72 additions & 0 deletions
72
lms/djangoapps/mfe_config_api/docs/decisions/0001-mfe-config-api.rst
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,72 @@ | ||
0001 MFE CONFIG API | ||
#################### | ||
|
||
Status | ||
****** | ||
|
||
Accepted | ||
|
||
Context | ||
******* | ||
|
||
Currently, MFE settings are set via command line environment variables or an .env file that is read during the build process, causing the operators to rebuild mfes each time when any variables are changed. The creation of the ``mfe_config_api`` allows configuration at runtime and avoids rebuilds. | ||
`MFE Configuration during Runtime`_. | ||
|
||
Decision | ||
******** | ||
|
||
- A lightweight API will be created that returns the mfe configuration variables from the site configuration or django settings. `PR Discussion about django settings`_ | ||
- The API will be enabled or disabled using the setting ``ENABLE_MFE_CONFIG_API``. | ||
- The API will take the mfe configuration in the ``MFE_CONFIG`` keyset in the site configuration (admin > site configuration > your domain) or in django settings. | ||
- This API allows to consult the configurations by specific MFE. Making a request like ``api/v1/mfe_config?mfe=mymfe`` will return the configuration defined in ``MFE_CONFIG_MYMFE`` merged with the ``MFE_CONFIG`` configuration. | ||
- The API will have a mechanism to cache the response with ``MFE_CONFIG_API_CACHE_TIMEOUT`` variable. | ||
- The API will live in lms/djangoapps because this is not something Studio needs to serve and it is a lightweight API. `PR Discussion`_ | ||
- The API will not require authentication or authorization. | ||
- The API request and response will be like: | ||
|
||
Request:: | ||
|
||
GET http://lms.base.com/api/v1/mfe_config?mfe=learning | ||
|
||
Response:: | ||
|
||
{ | ||
"BASE_URL": "https://name_of_mfe.example.com", | ||
"LANGUAGE_PREFERENCE_COOKIE_NAME": "example-language-preference", | ||
"CREDENTIALS_BASE_URL": "https://credentials.example.com", | ||
"DISCOVERY_API_BASE_URL": "https://discovery.example.com", | ||
"LMS_BASE_URL": "https://courses.example.com", | ||
"LOGIN_URL": "https://courses.example.com/login", | ||
"LOGOUT_URL": "https://courses.example.com/logout", | ||
"STUDIO_BASE_URL": "https://studio.example.com", | ||
"LOGO_URL": "https://courses.example.com/logo.png" | ||
|
||
} | ||
|
||
Consequences | ||
************ | ||
|
||
- We have to change all the mfes so that they take the information from the API. `Issue MFE runtime configuration in frontend-wg`_ | ||
- Initialize the MFE could have a delay due to the HTTP method. | ||
- `Site configuration is going to be deprecated`_ so later we have to clean the code that uses site configuration. | ||
- The operator is responsible for configuring the settings in site configuration or django settings. | ||
- We can have duplicate keys in site configuration (example: we can have a logo definition for each mfe). | ||
- If the request is made from a domain that does not have a site configuration, it returns django settings. | ||
|
||
Rejected Alternatives | ||
********************** | ||
|
||
- It was not made as a plugin or IDA because it is a lightweight implementation `PR Discussion`_ | ||
|
||
References | ||
********** | ||
|
||
.. _MFE Configuration during Runtime: https://docs.google.com/document/d/1-FHIQmyeQZu3311x8eYUNMru4JX7Yb3UlqjmJxvM8do/edit?usp=sharing | ||
|
||
.. _PR Discussion: https://github.com/openedx/edx-platform/pull/30473#issuecomment-1146176151 | ||
|
||
.. _Site configuration is going to be deprecated: https://github.com/openedx/platform-roadmap/issues/21 | ||
|
||
.. _Issue MFE runtime configuration in frontend-wg: https://github.com/openedx/frontend-wg/issues/103 | ||
|
||
.. _PR Discussion about django settings: https://github.com/openedx/edx-platform/pull/30473#discussion_r916263245 |
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,135 @@ | ||
""" | ||
Test the use cases of the views of the mfe api. | ||
""" | ||
|
||
from unittest.mock import call, patch | ||
|
||
import ddt | ||
from django.conf import settings | ||
from django.test import override_settings | ||
from django.urls import reverse | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase | ||
|
||
|
||
@ddt.ddt | ||
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_config_api:config") | ||
return super().setUp() | ||
|
||
@patch("lms.djangoapps.mfe_config_api.views.configuration_helpers") | ||
def test_get_mfe_config(self, configuration_helpers_mock): | ||
"""Test the get mfe config from site configuration with the mfe api. | ||
Expected result: | ||
- The get_value method of the configuration_helpers in the views is called once with the | ||
parameters ("MFE_CONFIG", getattr(settings, "MFE_CONFIG", {})). | ||
- The status of the response of the request is a HTTP_200_OK. | ||
- The json of the response of the request is equal to the mocked configuration. | ||
""" | ||
configuration_helpers_mock.get_value.return_value = {"EXAMPLE_VAR": "value"} | ||
response = self.client.get(self.mfe_config_api_url) | ||
|
||
configuration_helpers_mock.get_value.assert_called_once_with("MFE_CONFIG", getattr(settings, "MFE_CONFIG", {})) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.json(), {"EXAMPLE_VAR": "value"}) | ||
|
||
@patch("lms.djangoapps.mfe_config_api.views.configuration_helpers") | ||
def test_get_mfe_config_with_queryparam(self, configuration_helpers_mock): | ||
"""Test the get mfe config with a query param from site configuration. | ||
Expected result: | ||
- The get_value method of the configuration_helpers in the views is called twice, once with the | ||
parameters ("MFE_CONFIG", getattr(settings, "MFE_CONFIG", {})) and once with the parameters | ||
("MFE_CONFIG_MYMFE", getattr(settings, "MFE_CONFIG_MYMFE", {})). | ||
and one for get_value("MFE_CONFIG_MYMFE", getattr(settings, "MFE_CONFIG_MYMFE", {})). | ||
- The json of the response is the merge of both mocked configurations. | ||
""" | ||
configuration_helpers_mock.get_value.side_effect = [{"EXAMPLE_VAR": "value", "OTHER": "other"}, | ||
{"EXAMPLE_VAR": "mymfe_value"}] | ||
|
||
response = self.client.get(f"{self.mfe_config_api_url}?mfe=mymfe") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
calls = [call("MFE_CONFIG", getattr(settings, "MFE_CONFIG", {})), | ||
call("MFE_CONFIG_MYMFE", getattr(settings, "MFE_CONFIG_MYMFE", {}))] | ||
configuration_helpers_mock.get_value.assert_has_calls(calls) | ||
self.assertEqual(response.json(), {"EXAMPLE_VAR": "mymfe_value", "OTHER": "other"}) | ||
|
||
@patch("lms.djangoapps.mfe_config_api.views.configuration_helpers") | ||
@ddt.data( | ||
[{}, {}, {}], | ||
[{"EXAMPLE_VAR": "value"}, {}, {"EXAMPLE_VAR": "value"}], | ||
[{}, {"EXAMPLE_VAR": "mymfe_value"}, {"EXAMPLE_VAR": "mymfe_value"}], | ||
[{"EXAMPLE_VAR": "value"}, {"EXAMPLE_VAR": "mymfe_value"}, {"EXAMPLE_VAR": "mymfe_value"}], | ||
[{"EXAMPLE_VAR": "value", "OTHER": "other"}, {"EXAMPLE_VAR": "mymfe_value"}, | ||
{"EXAMPLE_VAR": "mymfe_value", "OTHER": "other"}], | ||
) | ||
@ddt.unpack | ||
def test_get_mfe_config_with_queryparam_multiple_configs( | ||
self, | ||
mfe_config, | ||
mfe_config_mymfe, | ||
expected_response, | ||
configuration_helpers_mock | ||
): | ||
"""Test the get mfe config with a query param and different settings in mfe_config and mfe_config_mfe inside | ||
the site configuration to test that the merge of the configurations is done correctly and mymfe config take | ||
precedence. | ||
In the ddt data the following structure is being passed: | ||
[mfe_config, mfe_config_mymfe, expected_response] | ||
Expected result: | ||
- The get_value method of the configuration_helpers in the views is called twice, once with the | ||
parameters ("MFE_CONFIG", getattr(settings, "MFE_CONFIG", {})) and once with the parameters | ||
("MFE_CONFIG_MYMFE", getattr(settings, "MFE_CONFIG_MYMFE", {})). | ||
- The json of the response is the expected_response passed by ddt.data. | ||
""" | ||
configuration_helpers_mock.get_value.side_effect = [mfe_config, mfe_config_mymfe] | ||
|
||
response = self.client.get(f"{self.mfe_config_api_url}?mfe=mymfe") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
calls = [call("MFE_CONFIG", getattr(settings, "MFE_CONFIG", {})), | ||
call("MFE_CONFIG_MYMFE", getattr(settings, "MFE_CONFIG_MYMFE", {}))] | ||
configuration_helpers_mock.get_value.assert_has_calls(calls) | ||
self.assertEqual(response.json(), expected_response) | ||
|
||
def test_get_mfe_config_from_django_settings(self): | ||
"""Test that when there is no site configuration, the API takes the django settings. | ||
Expected result: | ||
- The status of the response of the request is a HTTP_200_OK. | ||
- The json response is equal to MFE_CONFIG in lms/envs/test.py""" | ||
response = self.client.get(self.mfe_config_api_url) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.json(), getattr(settings, "MFE_CONFIG", {})) | ||
|
||
def test_get_mfe_config_with_queryparam_from_django_settings(self): | ||
"""Test that when there is no site configuration, the API with queryparam takes the django settings. | ||
Expected result: | ||
- The status of the response of the request is a HTTP_200_OK. | ||
- The json response is equal to MFE_CONFIG merged with MFE_CONFIG_MYMFE in lms/envs/test.py | ||
""" | ||
response = self.client.get(f"{self.mfe_config_api_url}?mfe=mymfe") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
expected_response = getattr(settings, "MFE_CONFIG", {}) | ||
expected_response.update(getattr(settings, "MFE_CONFIG_MYMFE", {})) | ||
self.assertEqual(response.json(), expected_response) | ||
|
||
@patch("lms.djangoapps.mfe_config_api.views.configuration_helpers") | ||
@override_settings(ENABLE_MFE_CONFIG_API=False) | ||
def test_404_get_mfe_config(self, configuration_helpers_mock): | ||
"""Test the 404 not found response from get mfe config. | ||
Expected result: | ||
- The get_value method of configuration_helpers is not called. | ||
- The status of the response of the request is a HTTP_404_NOT_FOUND. | ||
""" | ||
response = self.client.get(self.mfe_config_api_url) | ||
configuration_helpers_mock.get_value.assert_not_called() | ||
self.assertEqual(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_config_api.views import MFEConfigView | ||
|
||
app_name = 'mfe_config_api' | ||
urlpatterns = [ | ||
path('', 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 |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
MFE API Views for useful information related to mfes. | ||
""" | ||
|
||
# import json | ||
import logging | ||
import re | ||
|
||
|
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