1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-22 13:34:15 +03:00

pytest: token_factory separate out list_to_claim() helper

This is so conditional_ace_claims test can create claim objects which
can e.g. have the case sensitive flag set.

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
This commit is contained in:
Douglas Bagnall 2023-11-13 12:57:13 +13:00 committed by Andrew Bartlett
parent fc48014429
commit adf695aa34

View File

@ -30,6 +30,36 @@ CLAIM_VAL_TYPES = {
}
def list_to_claim(k, v):
if isinstance(v, security.CLAIM_SECURITY_ATTRIBUTE_RELATIVE_V1):
# make the name match
v.name = k
return v
if isinstance(v, (str, int)):
v = [v]
if not isinstance(v, list):
raise TypeError(f"expected list of claim values for '{k}', "
f"not {v!r} of type {type(v)}")
c = security.CLAIM_SECURITY_ATTRIBUTE_RELATIVE_V1()
if len(v) == 0:
raise ValueError("empty claim lists are not valid"
f" (for {k})")
t = type(v[0])
for val in v[1:]:
if type(val) != t:
raise TypeError(f"claim values for '{k}' "
"should all be the same type")
c.name = k
c.value_type = CLAIM_VAL_TYPES[t]
c.values = v
c.value_count = len(v)
return c
def _normalise_claims(args):
if isinstance(args, security.CLAIM_SECURITY_ATTRIBUTE_RELATIVE_V1):
return [args]
@ -48,27 +78,7 @@ def _normalise_claims(args):
if isinstance(args, dict):
# the key is the name and the value is a list of claim values
for k, v in args.items():
if not isinstance(k, str):
raise TypeError("expected str for claim name, "
f"not {type(k)} (for {k!r})")
if isinstance(v, (str, int)):
v = [v]
if not isinstance(v, list):
raise TypeError(f"expected list of claim values for '{k}', "
f"not {v!r} of type {type(v)}")
if len(v) == 0:
raise ValueError("empty claim lists are not valid"
f" (for {k})")
t = type(v[0])
for val in v[1:]:
if type(val) != t:
raise TypeError(f"claim values for '{k}' "
"should all be the same type")
c = security.CLAIM_SECURITY_ATTRIBUTE_RELATIVE_V1()
c.name = k
c.value_type = CLAIM_VAL_TYPES[t]
c.values = v
c.value_count = len(v)
c = list_to_claim(k, v)
claims_out.append(c)
return claims_out