Skip to content

Commit cd674cb

Browse files
committed
Refactored unit tests for JWTRoleRule and LlamaStackConfiguration models
1 parent 8825316 commit cd674cb

3 files changed

Lines changed: 271 additions & 0 deletions

File tree

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
"""Unit tests for JwtRoleRule model."""
2+
3+
import pytest
4+
5+
from pydantic import ValidationError
6+
7+
from models.config import JwtRoleRule, JsonPathOperator
8+
9+
10+
def test_jwt_role_rule_missing_attributes() -> None:
11+
"""Check the JwtRoleRule config class."""
12+
with pytest.raises(ValidationError, match="validation errors"):
13+
_ = JwtRoleRule()
14+
15+
16+
def test_jwt_role_rule_correct_attributes() -> None:
17+
"""Check the JwtRoleRule config class."""
18+
r = JwtRoleRule(
19+
jsonpath="$.id",
20+
negate=False,
21+
value="xyz",
22+
roles=["admin"],
23+
operator=JsonPathOperator.EQUALS,
24+
)
25+
26+
assert r is not None
27+
assert r.compiled_regex is None
28+
29+
30+
def test_jwt_role_rule_invalid_json_path() -> None:
31+
"""Check the JwtRoleRule config class."""
32+
with pytest.raises(ValidationError, match="Invalid JSONPath expression"):
33+
_ = JwtRoleRule(
34+
jsonpath="this/is/not/valid",
35+
negate=False,
36+
value="xyz",
37+
roles=["admin"],
38+
operator=JsonPathOperator.EQUALS,
39+
)
40+
41+
42+
def test_jwt_role_rule_no_roles_specified() -> None:
43+
"""Check the JwtRoleRule config class."""
44+
with pytest.raises(
45+
ValidationError, match="At least one role must be specified in the rule"
46+
):
47+
_ = JwtRoleRule(
48+
jsonpath="$.id",
49+
negate=False,
50+
value="xyz",
51+
roles=[],
52+
operator=JsonPathOperator.EQUALS,
53+
)
54+
55+
56+
def test_jwt_role_rule_star_role_specified() -> None:
57+
"""Check the JwtRoleRule config class."""
58+
with pytest.raises(
59+
ValidationError, match="The wildcard '\\*' role is not allowed in role rules"
60+
):
61+
_ = JwtRoleRule(
62+
jsonpath="$.id",
63+
negate=False,
64+
value="xyz",
65+
roles=["*"],
66+
operator=JsonPathOperator.EQUALS,
67+
)
68+
69+
70+
def test_jwt_role_rule_same_roles() -> None:
71+
"""Check the JwtRoleRule config class."""
72+
with pytest.raises(ValidationError, match="Roles must be unique in the rule"):
73+
_ = JwtRoleRule(
74+
jsonpath="$.id",
75+
negate=False,
76+
value="xyz",
77+
roles=["admin", "admin", "user"],
78+
operator=JsonPathOperator.EQUALS,
79+
)
80+
81+
82+
def test_jwt_role_rule_invalid_value() -> None:
83+
"""Check the JwtRoleRule config class."""
84+
with pytest.raises(
85+
ValidationError, match="MATCH operator requires a string pattern"
86+
):
87+
_ = JwtRoleRule(
88+
jsonpath="$.id",
89+
negate=False,
90+
value=True, # not a string
91+
roles=["admin", "user"],
92+
operator=JsonPathOperator.MATCH,
93+
)
94+
95+
96+
def test_jwt_role_rule_valid_regexp() -> None:
97+
"""Check the JwtRoleRule config class."""
98+
j = JwtRoleRule(
99+
jsonpath="$.id",
100+
negate=False,
101+
value=".*", # valid regexp
102+
roles=["admin", "user"],
103+
operator=JsonPathOperator.MATCH,
104+
)
105+
assert j.compiled_regex is not None
106+
107+
108+
def test_jwt_role_rule_invalid_regexp() -> None:
109+
"""Check the JwtRoleRule config class."""
110+
with pytest.raises(
111+
ValidationError, match="Invalid regex pattern for MATCH operator"
112+
):
113+
_ = JwtRoleRule(
114+
jsonpath="$.id",
115+
negate=False,
116+
value="[[[", # invalid regexp
117+
roles=["admin", "user"],
118+
operator=JsonPathOperator.MATCH,
119+
)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""Unit tests for LlamaStackConfiguration model."""
2+
3+
import pytest
4+
5+
from utils.checks import InvalidConfigurationError
6+
7+
from models.config import LlamaStackConfiguration
8+
9+
10+
def test_llama_stack_configuration_constructor() -> None:
11+
"""
12+
Verify that the LlamaStackConfiguration constructor accepts
13+
valid combinations of parameters and creates instances
14+
successfully.
15+
"""
16+
llama_stack_configuration = LlamaStackConfiguration(
17+
use_as_library_client=True,
18+
library_client_config_path="tests/configuration/run.yaml",
19+
)
20+
assert llama_stack_configuration is not None
21+
22+
llama_stack_configuration = LlamaStackConfiguration(
23+
use_as_library_client=False, url="http://localhost"
24+
)
25+
assert llama_stack_configuration is not None
26+
27+
llama_stack_configuration = LlamaStackConfiguration(url="http://localhost")
28+
assert llama_stack_configuration is not None
29+
30+
llama_stack_configuration = LlamaStackConfiguration(
31+
use_as_library_client=False, url="http://localhost", api_key="foo"
32+
)
33+
assert llama_stack_configuration is not None
34+
35+
36+
def test_llama_stack_configuration_no_run_yaml() -> None:
37+
"""
38+
Verify that constructing a LlamaStackConfiguration with a
39+
non-existent or invalid library_client_config_path raises
40+
InvalidConfigurationError.
41+
"""
42+
with pytest.raises(
43+
InvalidConfigurationError,
44+
match="Llama Stack configuration file 'not a file' is not a file",
45+
):
46+
LlamaStackConfiguration(
47+
use_as_library_client=True,
48+
library_client_config_path="not a file",
49+
)
50+
51+
52+
def test_llama_stack_wrong_configuration_constructor_no_url() -> None:
53+
"""
54+
Verify that constructing a LlamaStackConfiguration without
55+
specifying either a URL or enabling library client mode raises
56+
a ValueError.
57+
"""
58+
with pytest.raises(
59+
ValueError,
60+
match="Llama stack URL is not specified and library client mode is not specified",
61+
):
62+
LlamaStackConfiguration()
63+
64+
65+
def test_llama_stack_wrong_configuration_constructor_library_mode_off() -> None:
66+
"""Test the LlamaStackConfiguration constructor."""
67+
with pytest.raises(
68+
ValueError,
69+
match="Llama stack URL is not specified and library client mode is not enabled",
70+
):
71+
LlamaStackConfiguration(use_as_library_client=False)
72+
73+
74+
def test_llama_stack_wrong_configuration_no_config_file() -> None:
75+
"""Test the LlamaStackConfiguration constructor."""
76+
m = "Llama stack library client mode is enabled but a configuration file path is not specified"
77+
with pytest.raises(ValueError, match=m):
78+
LlamaStackConfiguration(use_as_library_client=True)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""Unit tests for PostgreSQLDatabaseConfiguration model."""
2+
3+
from pathlib import Path
4+
5+
import pytest
6+
7+
from pydantic import ValidationError
8+
9+
from constants import (
10+
POSTGRES_DEFAULT_SSL_MODE,
11+
POSTGRES_DEFAULT_GSS_ENCMODE,
12+
)
13+
14+
from models.config import PostgreSQLDatabaseConfiguration
15+
16+
17+
def test_postgresql_database_configuration() -> None:
18+
"""Test the PostgreSQLDatabaseConfiguration model."""
19+
c = PostgreSQLDatabaseConfiguration(db="db", user="user", password="password")
20+
assert c is not None
21+
assert c.host == "localhost"
22+
assert c.port == 5432
23+
assert c.db == "db"
24+
assert c.user == "user"
25+
assert c.password.get_secret_value() == "password"
26+
assert c.ssl_mode == POSTGRES_DEFAULT_SSL_MODE
27+
assert c.gss_encmode == POSTGRES_DEFAULT_GSS_ENCMODE
28+
assert c.namespace == "lightspeed-stack"
29+
assert c.ca_cert_path is None
30+
31+
32+
def test_postgresql_database_configuration_port_setting(subtests) -> None:
33+
"""Test the PostgreSQLDatabaseConfiguration model."""
34+
with subtests.test(msg="Correct port value"):
35+
c = PostgreSQLDatabaseConfiguration(
36+
db="db", user="user", password="password", port=1234
37+
)
38+
assert c is not None
39+
assert c.port == 1234
40+
41+
with subtests.test(msg="Negative port value"):
42+
with pytest.raises(ValidationError, match="Input should be greater than 0"):
43+
PostgreSQLDatabaseConfiguration(
44+
db="db", user="user", password="password", port=-1
45+
)
46+
47+
with subtests.test(msg="Too big port value"):
48+
with pytest.raises(ValueError, match="Port value should be less than 65536"):
49+
PostgreSQLDatabaseConfiguration(
50+
db="db", user="user", password="password", port=100000
51+
)
52+
53+
54+
def test_postgresql_database_configuration_ca_cert_path(subtests) -> None:
55+
"""Test the PostgreSQLDatabaseConfiguration model."""
56+
with subtests.test(msg="Path exists"):
57+
c = PostgreSQLDatabaseConfiguration(
58+
db="db",
59+
user="user",
60+
password="password",
61+
port=1234,
62+
ca_cert_path=Path("tests/configuration/server.crt"),
63+
)
64+
assert c.ca_cert_path == Path("tests/configuration/server.crt")
65+
66+
with subtests.test(msg="Path does not exist"):
67+
with pytest.raises(ValidationError, match="Path does not point to a file"):
68+
PostgreSQLDatabaseConfiguration(
69+
db="db",
70+
user="user",
71+
password="password",
72+
port=1234,
73+
ca_cert_path=Path("not a file"),
74+
)

0 commit comments

Comments
 (0)