|
1 | 1 | """Unit tests for functions defined in src/models/config.py.""" |
2 | 2 |
|
| 3 | +# pylint: disable=too-many-lines |
| 4 | + |
3 | 5 | import json |
4 | 6 | from pathlib import Path |
5 | 7 |
|
|
21 | 23 | AuthenticationConfiguration, |
22 | 24 | Configuration, |
23 | 25 | JwkConfiguration, |
| 26 | + JwtRoleRule, |
| 27 | + JsonPathOperator, |
24 | 28 | LlamaStackConfiguration, |
25 | 29 | ServiceConfiguration, |
26 | 30 | UserDataCollection, |
@@ -1026,3 +1030,115 @@ def test_postgresql_database_configuration_ca_cert_path(subtests) -> None: |
1026 | 1030 | port=1234, |
1027 | 1031 | ca_cert_path=Path("not a file"), |
1028 | 1032 | ) |
| 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