Skip to content

Commit

Permalink
feat: saml extra association filter
Browse files Browse the repository at this point in the history
With this change we ensure to send certificates if the user was created with a saml extra
association that add more sufix data different than current username. So this ensure the national_id
have to be processed with the 10 first chars.
  • Loading branch information
johanseto committed Aug 16, 2023
1 parent a4e68bd commit 88ba287
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
75 changes: 75 additions & 0 deletions eox_nelp/signals/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
User = get_user_model()

WRONG_NATIONAL_IDS = [0, "", "324234", "VADER", "3666888999", "166688899", "فيدر"]
SAML_EXTRA_ASSOCIATIONS_LIST = ["1666888998ASDF", "1222666444a6ca", "12226664443242344334534543", "1222666444#@$%"]


class UserHasPassingGradeTestCase(unittest.TestCase):
Expand Down Expand Up @@ -179,3 +180,77 @@ def test_invalid_mational_id(self, wrong_national_id, generate_certificate_mock,
_generate_external_certificate_data,
**external_certificate_data,
)

@override_settings(EXTERNAL_CERTIFICATES_GROUP_CODES={"course-v1:test+Cx105+2022_T4": "ABC123"})
@patch("eox_nelp.signals.utils._user_has_passing_grade")
@patch("eox_nelp.signals.utils.GeneratedCertificate")
@data(*SAML_EXTRA_ASSOCIATIONS_LIST)
def test_generate_certificate_data_saml_extra_association(
self,
saml_extra_assocation,
generate_certificate_mock,
passing_mock
):
"""This tests the normal behavior ofa user with saml_extra_association
the method `_generate_external_certificate_data`
Expected behavior:
- Result is as the expected value
- GeneratedCertificate mock is called with the right parameters.
- _user_has_passing_grade is called with the right parameters.
"""
time = timezone.now()
certificate = Mock()
certificate.id = 99
generate_certificate_mock.objects.get.return_value = certificate
passing_mock.return_value = True
saml_association_user, _ = User.objects.get_or_create(
username=saml_extra_assocation,
)
certificate_data = CertificateData(
user=UserData(
pii=UserPersonalData(
username=saml_association_user.username,
email="[email protected]",
name="Harry Potter",
),
id=saml_association_user.id,
is_active=True,
),
course=CourseData(
course_key=CourseKey.from_string("course-v1:test+Cx105+2022_T4"),
),
mode="audit",
grade=0.88,
current_status="non-passing",
download_url="",
name="",
)
national_id = saml_association_user.username[:10]

expected_value = {
"id": certificate.id,
'reference_id': f'{national_id}~course-v1:test+Cx105+2022_T4',
"created_at": str(time.date()),
"expiration_date": None,
"grade": certificate_data.grade * 100,
"is_passing": True,
"group_code": settings.EXTERNAL_CERTIFICATES_GROUP_CODES[str(self.certificate_data.course.course_key)],
"user": {
"national_id": national_id,
"english_name": certificate_data.user.pii.name,
"arabic_name": "",
}
}

result = _generate_external_certificate_data(time, certificate_data)

self.assertEqual(result, expected_value)
generate_certificate_mock.objects.get.assert_called_once_with(
user=saml_association_user,
course_id=certificate_data.course.course_key,
)
passing_mock.assert_called_once_with(
saml_association_user,
str(certificate_data.course.course_key)
)
13 changes: 7 additions & 6 deletions eox_nelp/signals/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ def _generate_external_certificate_data(time, certificate_data):
group_codes = getattr(settings, "EXTERNAL_CERTIFICATES_GROUP_CODES", {})
course_id = str(certificate_data.course.course_key)
extra_info = getattr(user, "extrainfo", None)

national_id = user.username[:10] # saml association extra filter
return {
"id": certificate.id,
"reference_id": generate_reference_id(user.username, course_id),
"reference_id": generate_reference_id(national_id, course_id),
"created_at": str(time.date()),
"expiration_date": None,
"grade": float(certificate_data.grade) * 100,
"is_passing": _user_has_passing_grade(user, course_id),
"group_code": group_codes[course_id],
"user": {
"national_id": user.username,
"national_id": national_id,
"english_name": certificate_data.user.pii.name,
"arabic_name": extra_info.arabic_name if extra_info else "",
}
Expand All @@ -72,7 +72,7 @@ def _user_has_passing_grade(user, course_id):
return course_grade.passed


def generate_reference_id(username, course_id):
def generate_reference_id(national_id, course_id):
"""Generate string of reference_id shape.
username<string>: Username of the user(national_id).
course_id<str>: Unique course identifier.
Expand All @@ -82,5 +82,6 @@ def generate_reference_id(username, course_id):
Raise:
ValueError: National Id is not valid.
"""
is_valid_national_id(username, raise_exception=True)
return f"{username}~{course_id}"
is_valid_national_id(national_id, raise_exception=True)

return f"{national_id}~{course_id}"

0 comments on commit 88ba287

Please sign in to comment.