11import importlib
22
3+ import pytest
34from gs .backend .config import config
45from gs .backend .config .cors_config import CORSConfig
6+ from gs .backend .config .database_config import DatabaseConfig
57from gs .backend .config .logger_config import LoggerConfig
8+ from pydantic import ValidationError
69
710
811def test_logger_config_default ():
912 cfg = LoggerConfig ()
13+
1014 assert cfg .excluded_endpoints == []
1115
1216
@@ -21,9 +25,48 @@ def test_cors_config_default():
2125
2226def test_backend_configuration_from_env (monkeypatch ):
2327 monkeypatch .setenv ("LOGGER_EXCLUDED_ENDPOINTS" , '["/test"]' )
24- monkeypatch .setenv ("CORS_ALLOW_ORIGINS" , '["http://test.com"]' )
28+ monkeypatch .setenv ("CORS_ALLOW_ORIGINS" , '["http://localhost:5173"]' )
29+ monkeypatch .setenv ("CORS_ALLOW_CREDENTIALS" , "True" )
30+ monkeypatch .setenv ("CORS_ALLOW_METHODS" , '["*"]' )
31+ monkeypatch .setenv ("CORS_ALLOW_HEADERS" , '["*"]' )
2532
2633 importlib .reload (config )
27- cfg = config .backend_config
28- assert "/test" in cfg .logger_config .excluded_endpoints
29- assert "http://test.com" in cfg .cors_config .allow_origins
34+ cfg = config .settings
35+
36+ assert "/test" in cfg .logger .excluded_endpoints
37+ assert "http://localhost:5173" in cfg .cors .allow_origins
38+ assert cfg .db .user == "testuser"
39+ assert cfg .db .password .get_secret_value () == "testpassword"
40+ assert cfg .db .location == "localhost"
41+ assert cfg .db .port == 5432
42+ assert cfg .db .name == "testdb"
43+
44+
45+ def test_database_connection_string ():
46+ db = config .settings .db
47+
48+ assert db .password .get_secret_value () == "testpassword"
49+ expected_url = "postgresql://testuser:testpassword@localhost:5432/testdb"
50+ assert db .connection_string () == expected_url
51+
52+
53+ def test_database_missing_env (monkeypatch ):
54+ monkeypatch .delenv ("GS_DATABASE_PASSWORD" )
55+
56+ with pytest .raises (ValidationError ):
57+ DatabaseConfig ()
58+
59+
60+ def test_invalid_env (monkeypatch ):
61+ monkeypatch .setenv ("GS_DATABASE_PORT" , "test" )
62+ monkeypatch .setenv ("CORS_ALLOW_CREDENTIALS" , "3" )
63+ monkeypatch .setenv ("LOGGER_EXCLUDED_ENDPOINTS" , "3" )
64+
65+ with pytest .raises (ValidationError ):
66+ DatabaseConfig ()
67+
68+ with pytest .raises (ValidationError ):
69+ CORSConfig ()
70+
71+ with pytest .raises (ValidationError ):
72+ LoggerConfig ()
0 commit comments