Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions tests/unit/models/config/test_authentication_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,13 @@ def test_authentication_configuration_rh_identity_more_entitlements() -> None:


def test_authentication_configuration_rh_identity_but_insufficient_config() -> None:
"""Test the AuthenticationConfiguration with RH identity token."""
"""Test the AuthenticationConfiguration with RH identity token.

Verify that selecting the RH Identity authentication module without
providing a RHIdentityConfiguration raises a validation error.

Expects a ValidationError with the message "RH Identity configuration must be specified".
"""

with pytest.raises(
ValidationError, match="RH Identity configuration must be specified"
Expand Down Expand Up @@ -171,7 +177,15 @@ def test_authentication_configuration_jwk_token() -> None:


def test_authentication_configuration_jwk_token_but_insufficient_config() -> None:
"""Test the AuthenticationConfiguration with JWK token."""
"""Test the AuthenticationConfiguration with JWK token.

Verify that using the JWK token module with an insufficient
`JwkConfiguration` triggers validation.

Attempts to construct an `AuthenticationConfiguration` with
`module=AUTH_MOD_JWK_TOKEN` and an empty `JwkConfiguration` must raise a
`ValidationError` containing the text "JwkConfiguration".
"""

with pytest.raises(ValidationError, match="JwkConfiguration"):
AuthenticationConfiguration(
Expand Down Expand Up @@ -200,7 +214,15 @@ def test_authentication_configuration_jwk_token_but_not_config() -> None:


def test_authentication_configuration_jwk_broken_config() -> None:
"""Test the AuthenticationConfiguration with JWK set, but not configured."""
"""Test the AuthenticationConfiguration with JWK set, but not configured.

Verify that accessing `jwk_configuration` raises a ValueError after the JWK
config is removed.

Creates an AuthenticationConfiguration with a JWK configuration, clears its
`jwk_config`, and asserts that accessing `jwk_configuration` raises
ValueError with message "JWK configuration should not be None".
"""

auth_config = AuthenticationConfiguration(
module=AUTH_MOD_JWK_TOKEN,
Expand All @@ -220,7 +242,13 @@ def test_authentication_configuration_jwk_broken_config() -> None:


def test_authentication_configuration_supported() -> None:
"""Test the AuthenticationConfiguration constructor."""
"""Test the AuthenticationConfiguration constructor.

Verify AuthenticationConfiguration initializes correctly for the K8S authentication module.

Asserts that the module is set to K8S, `skip_tls_verification` is False,
and both `k8s_ca_cert_path` and `k8s_cluster_api` are None.
"""
auth_config = AuthenticationConfiguration(
module=AUTH_MOD_K8S,
skip_tls_verification=False,
Expand Down Expand Up @@ -296,7 +324,18 @@ def test_authentication_configuration_in_config_k8s() -> None:


def test_authentication_configuration_in_config_rh_identity() -> None:
"""Test the authentication configuration in main config."""
"""Test the authentication configuration in main config.

Verify that a Configuration with RH Identity authentication is constructed
with the expected authentication fields.

Asserts that:
- authentication.module is set to RH Identity,
- skip_tls_verification is True,
- k8s_ca_cert_path is converted to a Path for the provided certificate file,
- k8s_cluster_api is None,
- an RHIdentityConfiguration is attached to the authentication.
"""
# pylint: disable=no-member
cfg = Configuration(
name="test_name",
Expand Down
67 changes: 60 additions & 7 deletions tests/unit/models/config/test_conversation_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,25 @@


def test_conversation_cache_no_type_specified() -> None:
"""Check the test for type as optional attribute."""
"""Check the test for type as optional attribute.

Verify that a ConversationHistoryConfiguration created with no arguments
has its type set to None.
"""
c = ConversationHistoryConfiguration()
assert c.type is None


def test_conversation_cache_unknown_type() -> None:
"""Check the test for cache type."""
"""Check the test for cache type.

Verify that providing an invalid conversation cache type raises a
ValidationError with the expected message.

The test constructs a ConversationHistoryConfiguration with an unsupported
type value and asserts that validation fails with the message: "Input
should be 'noop', 'memory', 'sqlite' or 'postgres'".
"""
with pytest.raises(
ValidationError,
match="Input should be 'noop', 'memory', 'sqlite' or 'postgres'",
Expand All @@ -32,7 +44,15 @@ def test_conversation_cache_unknown_type() -> None:


def test_conversation_cache_correct_type_but_not_configured(subtests: SubTests) -> None:
"""Check the test for cache type."""
"""Check the test for cache type.

Verify that specifying a cache type without providing its corresponding
backend configuration raises a ValidationError with the expected message.

Parameters:
subtests (SubTests): pytest_subtests SubTests object used to create
subtests for memory, sqlite, and postgres cases.
"""
with subtests.test(msg="Memory cache"):
with pytest.raises(
ValidationError, match="Memory cache is selected, but not configured"
Expand All @@ -53,7 +73,16 @@ def test_conversation_cache_correct_type_but_not_configured(subtests: SubTests)


def test_conversation_cache_no_type_but_configured(subtests: SubTests) -> None:
"""Check the test for cache type."""
"""Check the test for cache type.

Verify that providing a backend configuration without specifying a
conversation cache type raises a ValidationError.

This test asserts that constructing ConversationHistoryConfiguration with a
memory, sqlite, or postgres backend (but without setting the `type` field)
fails with a ValidationError whose message is "Conversation cache type must
be set when backend configuration is provided".
"""
m = "Conversation cache type must be set when backend configuration is provided"

with subtests.test(msg="Memory cache"):
Expand Down Expand Up @@ -137,7 +166,17 @@ def test_conversation_type_memory() -> None:


def test_conversation_type_memory_wrong_config() -> None:
"""Test the memory conversation cache configuration."""
"""Test the memory conversation cache configuration.

Verify that selecting the memory conversation cache raises validation
errors for missing or invalid memory configuration.

Asserts:
- A missing `max_entries` field in the memory configuration raises a
ValidationError with "Field required".
- A `max_entries` value less than or equal to zero raises a ValidationError
with "Input should be greater than 0".
"""
with pytest.raises(ValidationError, match="Field required"):
_ = ConversationHistoryConfiguration(
type=constants.CACHE_TYPE_MEMORY,
Expand Down Expand Up @@ -165,7 +204,15 @@ def test_conversation_type_sqlite() -> None:


def test_conversation_type_sqlite_wrong_config() -> None:
"""Test the SQLite conversation cache configuration."""
"""Test the SQLite conversation cache configuration.

Validate that selecting the SQLite conversation cache while supplying an
incorrect backend configuration raises a validation error.

This test asserts that when `type` is set to the SQLite cache but a
`memory` configuration is provided instead of an `sqlite` configuration,
model validation fails with a "Field required" error.
"""
with pytest.raises(ValidationError, match="Field required"):
_ = ConversationHistoryConfiguration(
type=constants.CACHE_TYPE_SQLITE,
Expand Down Expand Up @@ -198,7 +245,13 @@ def test_conversation_type_postgres() -> None:


def test_conversation_type_postgres_wrong_config() -> None:
"""Test the SQLite conversation cache configuration."""
"""Test the PostgreSQL conversation cache configuration.

Ensure a ValidationError is raised when PostgreSQL configuration is missing required fields.

This test provides an empty PostgreSQLDatabaseConfiguration and expects
validation to fail with a "Field required" message.
"""
with pytest.raises(ValidationError, match="Field required"):
_ = ConversationHistoryConfiguration(
type=constants.CACHE_TYPE_POSTGRES,
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/models/config/test_dump_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def test_dump_configuration(tmp_path: Path) -> None:
that the resulting file contains all expected sections and values.

Please note that redaction process is not in place.

Parameters:
tmp_path (Path): Directory where the test JSON file will be written.
"""
cfg = Configuration(
name="test_name",
Expand Down
40 changes: 36 additions & 4 deletions tests/unit/models/config/test_jwt_role_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,26 @@


def test_jwt_role_rule_missing_attributes() -> None:
"""Check the JwtRoleRule config class."""
"""Check the JwtRoleRule config class.

Ensure constructing JwtRoleRule without any attributes raises a ValidationError.

Asserts that instantiating JwtRoleRule with no fields provided raises a
ValidationError whose message contains "validation errors".
"""
with pytest.raises(ValidationError, match="validation errors"):
_ = JwtRoleRule()


def test_jwt_role_rule_correct_attributes() -> None:
"""Check the JwtRoleRule config class."""
"""Check the JwtRoleRule config class.

Verify that a JwtRoleRule instantiated with valid attributes is created and
does not compile a regex for non-MATCH operators.

Asserts that an instance is returned and that `compiled_regex` is `None`
when the operator is not `JsonPathOperator.MATCH`.
"""
r = JwtRoleRule(
jsonpath="$.id",
negate=False,
Expand All @@ -28,7 +41,13 @@ def test_jwt_role_rule_correct_attributes() -> None:


def test_jwt_role_rule_invalid_json_path() -> None:
"""Check the JwtRoleRule config class."""
"""Check the JwtRoleRule config class.

Verifies that constructing JwtRoleRule with an invalid JSONPath expression
raises a ValidationError.

Asserts the raised ValidationError contains the message "Invalid JSONPath expression".
"""
with pytest.raises(ValidationError, match="Invalid JSONPath expression"):
_ = JwtRoleRule(
jsonpath="this/is/not/valid",
Expand Down Expand Up @@ -106,7 +125,20 @@ def test_jwt_role_rule_valid_regexp() -> None:


def test_jwt_role_rule_invalid_regexp() -> None:
"""Check the JwtRoleRule config class."""
"""Check the JwtRoleRule config class.

Ensure creating a JwtRoleRule with an invalid regex pattern for the MATCH
operator raises a ValidationError.

Attempts to construct JwtRoleRule with operator set to
JsonPathOperator.MATCH and a malformed regex value; the constructor must
raise a ValidationError with message "Invalid regex pattern for MATCH
operator".

Raises:
ValidationError: if the provided regex pattern for MATCH is invalid
(message contains "Invalid regex pattern for MATCH operator").
"""
with pytest.raises(
ValidationError, match="Invalid regex pattern for MATCH operator"
):
Expand Down
Loading