Skip to content
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

feat: add setting configuration for relationships #68

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions eox_nelp/course_experience/api/v1/serializers.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
41 changes: 41 additions & 0 deletions eox_nelp/course_experience/api/v1/tests/mixins_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading