From 6b38fb5ea83617733afd5c418db1d5739e0ef2a0 Mon Sep 17 00:00:00 2001 From: Marcos Prieto Date: Thu, 1 Aug 2024 10:21:21 +0200 Subject: [PATCH] Don't include public_id on the dashbaord config when not present This way we avoid having to deal with undefined vs null values in the frontend. --- lms/js_config_types.py | 2 +- lms/resources/_js_config/__init__.py | 3 ++- .../unit/lms/resources/_js_config/__init___test.py | 14 +++++++++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lms/js_config_types.py b/lms/js_config_types.py index 03eac49f9b..99fad098d9 100644 --- a/lms/js_config_types.py +++ b/lms/js_config_types.py @@ -101,7 +101,7 @@ class User(TypedDict): class DashboardConfig(TypedDict): user: User - organization_public_id: str + organization_public_id: NotRequired[str] """ Filtering by organization is not like other filters: - It's only available to staff members diff --git a/lms/resources/_js_config/__init__.py b/lms/resources/_js_config/__init__.py index bced8b5916..301f2cf637 100644 --- a/lms/resources/_js_config/__init__.py +++ b/lms/resources/_js_config/__init__.py @@ -257,7 +257,6 @@ def enable_dashboard_mode(self) -> None: "mode": JSConfig.Mode.DASHBOARD, "dashboard": DashboardConfig( user=self._get_user_info(), - organization_public_id=self._request.params.get("public_id"), routes=DashboardRoutes( assignment=self._to_frontend_template( "api.dashboard.assignment" @@ -281,6 +280,8 @@ def enable_dashboard_mode(self) -> None: ), } ) + if organization_public_id := self._request.params.get("public_id"): + self._config["dashboard"]["organization_public_id"] = organization_public_id def enable_lti_launch_mode(self, course, assignment: Assignment): """ diff --git a/tests/unit/lms/resources/_js_config/__init___test.py b/tests/unit/lms/resources/_js_config/__init___test.py index 27d338598f..9a24e963b5 100644 --- a/tests/unit/lms/resources/_js_config/__init___test.py +++ b/tests/unit/lms/resources/_js_config/__init___test.py @@ -694,17 +694,13 @@ def LTIEvent(self, patch): class TestEnableDashboardMode: - @pytest.mark.parametrize("public_id", ["PUBLIC_ID", None]) - def test_it(self, js_config, lti_user, pyramid_request, public_id): - pyramid_request.params["public_id"] = public_id - + def test_it(self, js_config, lti_user): js_config.enable_dashboard_mode() config = js_config.asdict() assert config["mode"] == JSConfig.Mode.DASHBOARD assert config["dashboard"] == { "user": {"display_name": lti_user.display_name, "is_staff": False}, - "organization_public_id": public_id, "routes": { "assignment": "/api/dashboard/assignments/:assignment_id", "students_metrics": "/api/dashboard/students/metrics", @@ -717,6 +713,14 @@ def test_it(self, js_config, lti_user, pyramid_request, public_id): }, } + def test_it_when_organizataion_public_id_present(self, js_config, pyramid_request): + pyramid_request.params["public_id"] = sentinel.public_id + + js_config.enable_dashboard_mode() + config = js_config.asdict() + + assert config["dashboard"]["organization_public_id"] == sentinel.public_id + def test_user_when_staff(self, js_config, pyramid_request_staff_member, context): js_config = JSConfig(context, pyramid_request_staff_member) js_config.enable_dashboard_mode()