From 1bc8d476b43a58312bd4f1cd2d86df25fc0619e6 Mon Sep 17 00:00:00 2001 From: Johan Castiblanco Date: Mon, 10 Jul 2023 18:10:28 -0500 Subject: [PATCH] feat: add setting configuration for relationships MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This add the possibility to configure the course_overview mapping and user mapping via django settings using `COURSE_EXPERIENCE_SETTINGS` chore: apply suggestions from code review Co-authored-by: Andrey CaƱon chore: docstring typo --- .../course_experience/api/v1/serializers.py | 21 +++++++--- .../api/v1/tests/mixins_helpers.py | 41 +++++++++++++++++++ 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/eox_nelp/course_experience/api/v1/serializers.py b/eox_nelp/course_experience/api/v1/serializers.py index de726245..055b0510 100644 --- a/eox_nelp/course_experience/api/v1/serializers.py +++ b/eox_nelp/course_experience/api/v1/serializers.py @@ -1,4 +1,5 @@ """Serializers used for the experience views.""" +from django.conf import settings from django.contrib.auth import get_user_model from rest_framework_json_api import serializers @@ -32,9 +33,13 @@ def get_course_extra_attributes(value=None): Returns: dict: dict object too add course extra fields """ - return { - "attributes": map_instance_attributes_to_dict(value, COURSE_OVERVIEW_EXTRA_FIELD_MAPPING) - } + course_overview_mapping = getattr( + settings, + "COURSE_EXPERIENCE_SETTINGS", + {}, + ).get("COURSE_OVERVIEW_EXTRA_FIELD_MAPPING", COURSE_OVERVIEW_EXTRA_FIELD_MAPPING) + + return {"attributes": map_instance_attributes_to_dict(value, course_overview_mapping)} def get_user_extra_attributes(value=None): @@ -46,9 +51,13 @@ def get_user_extra_attributes(value=None): Returns: dict: dict object too add user extra fields """ - return { - "attributes": map_instance_attributes_to_dict(value, USER_EXTRA_FIELD_MAPPING) - } + user_mapping = getattr( + settings, + "COURSE_EXPERIENCE_SETTINGS", + {}, + ).get("USER_EXTRA_FIELD_MAPPING", USER_EXTRA_FIELD_MAPPING) + + return {"attributes": map_instance_attributes_to_dict(value, user_mapping)} class ExperienceSerializer(serializers.ModelSerializer): diff --git a/eox_nelp/course_experience/api/v1/tests/mixins_helpers.py b/eox_nelp/course_experience/api/v1/tests/mixins_helpers.py index d381d352..4788a0a2 100644 --- a/eox_nelp/course_experience/api/v1/tests/mixins_helpers.py +++ b/eox_nelp/course_experience/api/v1/tests/mixins_helpers.py @@ -6,7 +6,9 @@ """ from urllib.parse import quote +from django.conf import settings from django.contrib.auth import get_user_model +from django.test import override_settings from django.urls import reverse from mock import patch from rest_framework import status @@ -79,6 +81,45 @@ def test_get_object_list_by_user(self): self.assertEqual(element["attributes"]["username"], self.user.username) self.assertEqual(element["relationships"]["author"]["data"]["id"], str(self.user.id)) + @override_settings() + def test_get_relationships_keys_mapped_for_settings(self): + """ Test the course experiences api returns the keys configured via the + COURSE_EXPERIENCE_SETTINGS` in settings. + Expected behavior: + - Return expected content type. + - Status code 200. + - Check all the elements have the relationship attrs keys presented for course according setting config. + - Check all the elements have the relationship attrs keys for user(author) according setting config. + + """ + url_endpoint = reverse(self.reverse_viewname_list) + test_course_experience_settings = { + "COURSE_OVERVIEW_EXTRA_FIELD_MAPPING": { + "ultra_name": "courseultra", + "course_key": "instance__field", + }, + "USER_EXTRA_FIELD_MAPPING": { + "mega_name": "userultra", + "user_key": "user__field", + } + } + setattr(settings, "COURSE_EXPERIENCE_SETTINGS", test_course_experience_settings) + + response = self.client.get(url_endpoint) + + self.assertIn(response.headers["Content-Type"], RESPONSE_CONTENT_TYPES) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertTrue(response.json()['data']) + for element in response.json()['data']: + self.assertEqual( + element["relationships"]["course_id"]["data"]["attributes"].keys(), + test_course_experience_settings["COURSE_OVERVIEW_EXTRA_FIELD_MAPPING"].keys(), + ) + self.assertEqual( + element["relationships"]["author"]["data"]["attributes"].keys(), + test_course_experience_settings["USER_EXTRA_FIELD_MAPPING"].keys(), + ) + def test_not_authenticated_user(self): """ Test disallow by credentials the get request to the list endpoint