11"""Unit tests for functions defined in src/models/config.py."""
22
3+ # pylint: disable=too-many-lines
4+
35import json
46from pathlib import Path
57
2123 AuthenticationConfiguration ,
2224 Configuration ,
2325 JwkConfiguration ,
26+ JwtRoleRule ,
27+ JsonPathOperator ,
2428 LlamaStackConfiguration ,
2529 ServiceConfiguration ,
2630 UserDataCollection ,
@@ -805,6 +809,13 @@ def test_authentication_configuration() -> None:
805809 assert auth_config .k8s_ca_cert_path is None
806810 assert auth_config .k8s_cluster_api is None
807811
812+ # try to retrieve JWK configuration
813+ with pytest .raises (
814+ ValueError ,
815+ match = "JWK configuration is only available for JWK token authentication module" ,
816+ ):
817+ _ = auth_config .jwk_configuration
818+
808819
809820def test_authentication_configuration_jwk_token () -> None :
810821 """Test the AuthenticationConfiguration with JWK token."""
@@ -822,6 +833,9 @@ def test_authentication_configuration_jwk_token() -> None:
822833 assert auth_config .k8s_ca_cert_path is None
823834 assert auth_config .k8s_cluster_api is None
824835
836+ # try to retrieve JWK configuration
837+ assert auth_config .jwk_configuration is not None
838+
825839
826840def test_authentication_configuration_jwk_token_but_insufficient_config () -> None :
827841 """Test the AuthenticationConfiguration with JWK token."""
@@ -852,6 +866,26 @@ def test_authentication_configuration_jwk_token_but_not_config() -> None:
852866 )
853867
854868
869+ def test_authentication_configuration_jwk_broken_config () -> None :
870+ """Test the AuthenticationConfiguration with JWK set, but not configured."""
871+
872+ auth_config = AuthenticationConfiguration (
873+ module = AUTH_MOD_JWK_TOKEN ,
874+ skip_tls_verification = False ,
875+ k8s_ca_cert_path = None ,
876+ k8s_cluster_api = None ,
877+ jwk_config = JwkConfiguration (url = "http://foo.bar.baz" ),
878+ )
879+ assert auth_config is not None
880+
881+ # emulate broken config
882+ auth_config .jwk_config = None
883+ # try to retrieve JWK configuration
884+
885+ with pytest .raises (ValueError , match = "JWK configuration should not be None" ):
886+ _ = auth_config .jwk_configuration
887+
888+
855889def test_authentication_configuration_supported () -> None :
856890 """Test the AuthenticationConfiguration constructor."""
857891 auth_config = AuthenticationConfiguration (
@@ -893,6 +927,7 @@ def test_database_configuration(subtests) -> None:
893927 assert d .sqlite is None
894928 assert d .postgres is not None
895929 assert d .db_type == "postgres"
930+ assert d .config is d1
896931
897932 with subtests .test (msg = "SQLite" ):
898933 d1 = SQLiteDatabaseConfiguration (
@@ -903,6 +938,7 @@ def test_database_configuration(subtests) -> None:
903938 assert d .sqlite is not None
904939 assert d .postgres is None
905940 assert d .db_type == "sqlite"
941+ assert d .config is d1
906942
907943
908944def test_no_databases_configuration () -> None :
@@ -918,9 +954,13 @@ def test_no_databases_configuration() -> None:
918954 d .postgres = None
919955
920956 with pytest .raises (ValueError , match = "No database configuration found" ):
921- # access propery to call it's getter
957+ # access property to call its getter
922958 _ = d .db_type
923959
960+ with pytest .raises (ValueError , match = "No database configuration found" ):
961+ # access property to call its getter
962+ _ = d .config
963+
924964
925965def test_two_databases_configuration () -> None :
926966 """Test if two databases configuration is checked."""
@@ -990,3 +1030,115 @@ def test_postgresql_database_configuration_ca_cert_path(subtests) -> None:
9901030 port = 1234 ,
9911031 ca_cert_path = Path ("not a file" ),
9921032 )
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