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: [ACI-940, ACI-941] implement keypaths exclusion logic #159

Merged
merged 3 commits into from
Apr 25, 2024
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
17 changes: 16 additions & 1 deletion credentials/apps/badges/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

from attr import asdict
from datetime import datetime
from django.conf import settings
from unittest.mock import patch

from openedx_events.learning.data import UserData, UserPersonalData, CourseData, CoursePassingStatusData

from credentials.apps.badges.checks import badges_checks
from credentials.apps.badges.utils import extract_payload, keypath, get_user_data
from credentials.apps.badges.utils import extract_payload, keypath, get_user_data, get_event_type_keypaths


class TestKeypath(unittest.TestCase):
Expand Down Expand Up @@ -124,3 +125,17 @@ def test_badges_checks_non_empty_events(self, mock_get_badging_event_types):
mock_get_badging_event_types.return_value = ["event1", "event2"]
errors = badges_checks()
self.assertEqual(len(errors), 0)


class TestGetEventTypeKeypaths(unittest.TestCase):
def setUp(self):
self.EVENT_TYPE = "org.openedx.learning.course.passing.status.updated.v1"

def test_get_event_type_keypaths(self):
event_type_keypaths = get_event_type_keypaths(self.EVENT_TYPE)
self.assertIsNotNone(event_type_keypaths)
self.assertIn("status", event_type_keypaths)
self.assertIn("course.display_name", event_type_keypaths)

for excluded_keypath in settings.BADGES_CONFIG.get("EXCLUDED_KEY_PATHS", []):
self.assertNotIn(excluded_keypath, event_type_keypaths)
12 changes: 10 additions & 2 deletions credentials/apps/badges/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,23 @@ def get_data_keypaths(data):
keypaths = []
for field in attr.fields(data):
if attr.has(field.type):
keypaths += [f"{field.name}.{keypath}" for keypath in get_data_keypaths(field.type)]
keypaths += [
f"{field.name}.{keypath}"
for keypath in get_data_keypaths(field.type)
]
else:
keypaths.append(field.name)
return keypaths

keypaths = []
for field in attr.fields(data):
if attr.has(field.type):
keypaths += [f"{field.name}.{keypath}" for keypath in get_data_keypaths(field.type)]
keypaths += [
f"{field.name}.{keypath}"
for keypath in get_data_keypaths(field.type)
if f"{field.name}.{keypath}"
not in settings.BADGES_CONFIG.get("EXCLUDED_KEY_PATHS", [])
]
else:
keypaths.append(field.name)
return keypaths
7 changes: 7 additions & 0 deletions credentials/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,4 +609,11 @@
"CREDLY_SANDBOX_API_BASE_URL": "https://sandbox-api.credly.com/v1/",
"USE_SANDBOX": False,
},
"EXCLUDED_KEY_PATHS": [
"user.id",
"user.is_active",
"user.pii.username",
"user.pii.email",
"user.pii.name",
]
}
Loading