Skip to content

Commit

Permalink
fix: force amr claim to list
Browse files Browse the repository at this point in the history
Apparently, this can be either a string or a list.

Refs: HP-2490
  • Loading branch information
danipran committed Jul 23, 2024
1 parent 333dcba commit 78ce23a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion profiles/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
VerifiedPersonalInformationTemporaryAddress,
)
from .utils import (
force_list,
requester_can_view_verified_personal_information,
requester_has_sufficient_loa_to_perform_gdpr_request,
)
Expand Down Expand Up @@ -604,7 +605,7 @@ def resolve_login_methods(self: Profile, info, **kwargs):
"No permission to read login methods of another user."
)

amr = {info.context.user_auth.data.get("amr")}
amr = set(force_list(info.context.user_auth.data.get("amr")))

# For future software archeologists:
# This field was added to the API to support the front-end's need to know
Expand Down
17 changes: 17 additions & 0 deletions profiles/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest

from profiles.utils import force_list


@pytest.mark.parametrize(
"input_value,expected",
[
([1, 2, 3], [1, 2, 3]),
([], []),
(None, []),
("foo", ["foo"]),
((1, 2), [(1, 2)]), # tuples are treated as single values
],
)
def test_force_list(input_value, expected):
assert force_list(input_value) == expected
11 changes: 11 additions & 0 deletions profiles/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ def requester_has_sufficient_loa_to_perform_gdpr_request(request):
"substantial",
"high",
]


def force_list(value) -> list:
"""
Ensure that the given value is a list. If the value is None, return an empty list.
"""
if value is None:
return []
if isinstance(value, list):
return value
return [value]

0 comments on commit 78ce23a

Please sign in to comment.