Skip to content

Commit cde3731

Browse files
committed
Unit tests for JWT role settings
1 parent b625b4b commit cde3731

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

tests/unit/models/test_config.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Unit tests for functions defined in src/models/config.py."""
22

3+
# pylint: disable=too-many-lines
4+
35
import json
46
from pathlib import Path
57

@@ -21,6 +23,8 @@
2123
AuthenticationConfiguration,
2224
Configuration,
2325
JwkConfiguration,
26+
JwtRoleRule,
27+
JsonPathOperator,
2428
LlamaStackConfiguration,
2529
ServiceConfiguration,
2630
UserDataCollection,
@@ -1026,3 +1030,115 @@ def test_postgresql_database_configuration_ca_cert_path(subtests) -> None:
10261030
port=1234,
10271031
ca_cert_path=Path("not a file"),
10281032
)
1033+
1034+
1035+
def test_jwt_role_rule_missing_attributes() -> None:
1036+
"""Check the JwtRoleRule config class."""
1037+
with pytest.raises(ValidationError, match="validation errors"):
1038+
_ = JwtRoleRule()
1039+
1040+
1041+
def test_jwt_role_rule_correct_attributes() -> None:
1042+
"""Check the JwtRoleRule config class."""
1043+
r = JwtRoleRule(
1044+
jsonpath="$.id",
1045+
negate=False,
1046+
value="xyz",
1047+
roles=["admin"],
1048+
operator=JsonPathOperator.EQUALS,
1049+
)
1050+
1051+
assert r is not None
1052+
assert r.compiled_regex is None
1053+
1054+
1055+
def test_jwt_role_rule_invalid_json_path() -> None:
1056+
"""Check the JwtRoleRule config class."""
1057+
with pytest.raises(ValidationError, match="Invalid JSONPath expression"):
1058+
_ = JwtRoleRule(
1059+
jsonpath="this/is/not/valid",
1060+
negate=False,
1061+
value="xyz",
1062+
roles=["admin"],
1063+
operator=JsonPathOperator.EQUALS,
1064+
)
1065+
1066+
1067+
def test_jwt_role_rule_no_roles_specified() -> None:
1068+
"""Check the JwtRoleRule config class."""
1069+
with pytest.raises(
1070+
ValidationError, match="At least one role must be specified in the rule"
1071+
):
1072+
_ = JwtRoleRule(
1073+
jsonpath="$.id",
1074+
negate=False,
1075+
value="xyz",
1076+
roles=[],
1077+
operator=JsonPathOperator.EQUALS,
1078+
)
1079+
1080+
1081+
def test_jwt_role_rule_star_role_specified() -> None:
1082+
"""Check the JwtRoleRule config class."""
1083+
with pytest.raises(
1084+
ValidationError, match="The wildcard '\*' role is not allowed in role rules"
1085+
):
1086+
_ = JwtRoleRule(
1087+
jsonpath="$.id",
1088+
negate=False,
1089+
value="xyz",
1090+
roles=["*"],
1091+
operator=JsonPathOperator.EQUALS,
1092+
)
1093+
1094+
1095+
def test_jwt_role_rule_same_roles() -> None:
1096+
"""Check the JwtRoleRule config class."""
1097+
with pytest.raises(ValidationError, match="Roles must be unique in the rule"):
1098+
_ = JwtRoleRule(
1099+
jsonpath="$.id",
1100+
negate=False,
1101+
value="xyz",
1102+
roles=["admin", "admin", "user"],
1103+
operator=JsonPathOperator.EQUALS,
1104+
)
1105+
1106+
1107+
def test_jwt_role_rule_invalid_value() -> None:
1108+
"""Check the JwtRoleRule config class."""
1109+
with pytest.raises(
1110+
ValidationError, match="MATCH operator requires a string pattern"
1111+
):
1112+
_ = JwtRoleRule(
1113+
jsonpath="$.id",
1114+
negate=False,
1115+
value=True, # not a string
1116+
roles=["admin", "user"],
1117+
operator=JsonPathOperator.MATCH,
1118+
)
1119+
1120+
1121+
def test_jwt_role_rule_valid_regexp() -> None:
1122+
"""Check the JwtRoleRule config class."""
1123+
j = JwtRoleRule(
1124+
jsonpath="$.id",
1125+
negate=False,
1126+
value=".*", # valid regexp
1127+
roles=["admin", "user"],
1128+
operator=JsonPathOperator.MATCH,
1129+
)
1130+
assert j.compiled_regex is not None
1131+
1132+
1133+
def test_jwt_role_rule_invalid_regexp() -> None:
1134+
"""Check the JwtRoleRule config class."""
1135+
with pytest.raises(
1136+
ValidationError, match="Invalid regex pattern for MATCH operator"
1137+
):
1138+
_ = JwtRoleRule(
1139+
jsonpath="$.id",
1140+
negate=False,
1141+
value="[[[", # invalid regexp
1142+
roles=["admin", "user"],
1143+
operator=JsonPathOperator.MATCH,
1144+
)

0 commit comments

Comments
 (0)