Skip to content

Commit 3e3c9d5

Browse files
committed
Approperiate formatting
1 parent afa9f23 commit 3e3c9d5

File tree

4 files changed

+87
-28
lines changed

4 files changed

+87
-28
lines changed

dracan/core/app_factory.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
from ..validators.headers_validator import create_header_validator
1212
from ..middleware.payload_limiter import create_payload_size_limiter
1313
from ..utils.metrics import start_metrics_server, register_metrics
14-
from ..utils.config_load import load_proxy_config, load_rules_config, check_required_files
14+
from ..utils.config_load import (
15+
load_proxy_config,
16+
load_rules_config,
17+
check_required_files,
18+
)
1519

1620

1721
def create_app():

dracan/core/proxy.py

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# Set a default timeout in seconds if PROXY_TIMEOUT is not specified in the environment
88
PROXY_TIMEOUT = int(os.getenv("PROXY_TIMEOUT", 180))
99

10+
1011
def forward_request(request, config, sub=None):
1112
"""
1213
Forward the incoming request to the destination service.

dracan/utils/config_load.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import sys
44

5+
56
def get_config_file_path(filename):
67
"""
78
Determine the configuration file path, using CONFIG_LOCATION if set,
@@ -10,6 +11,7 @@ def get_config_file_path(filename):
1011
config_location = os.getenv("CONFIG_LOCATION", "")
1112
return os.path.join(config_location, filename) if config_location else filename
1213

14+
1315
def check_required_files(required_files):
1416
"""
1517
Check for the presence of required configuration files and exit if any are missing.
@@ -21,6 +23,7 @@ def check_required_files(required_files):
2123
print("Visit https://github.com/Veinar/dracan for more information.")
2224
sys.exit(1)
2325

26+
2427
def load_proxy_config():
2528
"""
2629
Load the destination service configuration from a JSON file.
@@ -39,7 +42,9 @@ def load_proxy_config():
3942
destination_config = config["destination"]
4043
if not required_keys.issubset(destination_config.keys()):
4144
missing_keys = required_keys - destination_config.keys()
42-
raise KeyError(f"Missing required fields in 'destination': {', '.join(missing_keys)}")
45+
raise KeyError(
46+
f"Missing required fields in 'destination': {', '.join(missing_keys)}"
47+
)
4348

4449
# Ensure no extra fields are present
4550
extra_keys = set(destination_config.keys()) - required_keys
@@ -48,6 +53,7 @@ def load_proxy_config():
4853

4954
return config
5055

56+
5157
def load_rules_config():
5258
"""
5359
Load the filtering, limiting, and validation rules from a JSON file.

tests/test_config_load.py

+74-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import json
22
import pytest
3-
from dracan.utils.config_load import load_proxy_config, load_rules_config, check_required_files
3+
from dracan.utils.config_load import (
4+
load_proxy_config,
5+
load_rules_config,
6+
check_required_files,
7+
)
8+
49

510
@pytest.fixture
611
def setup_default_config(tmp_path):
@@ -10,11 +15,16 @@ def setup_default_config(tmp_path):
1015
# Create default config files in the temporary directory
1116
proxy_config = tmp_path / "proxy_config.json"
1217
rules_config = tmp_path / "rules_config.json"
13-
proxy_config.write_text('{"destination": {"host": "127.0.0.1", "port": 8080, "path": "/"}}')
14-
rules_config.write_text('{"allowed_methods": ["GET", "POST"], "rate_limit": "10 per minute"}')
18+
proxy_config.write_text(
19+
'{"destination": {"host": "127.0.0.1", "port": 8080, "path": "/"}}'
20+
)
21+
rules_config.write_text(
22+
'{"allowed_methods": ["GET", "POST"], "rate_limit": "10 per minute"}'
23+
)
1524

1625
return tmp_path
1726

27+
1828
@pytest.fixture
1929
def setup_custom_config(tmp_path):
2030
"""
@@ -25,23 +35,31 @@ def setup_custom_config(tmp_path):
2535
custom_config_dir.mkdir()
2636
proxy_config = custom_config_dir / "proxy_config.json"
2737
rules_config = custom_config_dir / "rules_config.json"
28-
proxy_config.write_text('{"destination": {"host": "127.0.0.1", "port": 8080, "path": "/"}}')
29-
rules_config.write_text('{"allowed_methods": ["GET", "POST"], "rate_limit": "10 per minute"}')
38+
proxy_config.write_text(
39+
'{"destination": {"host": "127.0.0.1", "port": 8080, "path": "/"}}'
40+
)
41+
rules_config.write_text(
42+
'{"allowed_methods": ["GET", "POST"], "rate_limit": "10 per minute"}'
43+
)
3044

3145
return custom_config_dir
3246

47+
3348
def test_load_default_config(setup_default_config, monkeypatch):
3449
"""
3550
Test loading configuration from the default location.
3651
"""
37-
monkeypatch.chdir(setup_default_config) # Change to temporary directory with default config files
52+
monkeypatch.chdir(
53+
setup_default_config
54+
) # Change to temporary directory with default config files
3855

3956
proxy_config = load_proxy_config()
4057
rules_config = load_rules_config()
4158

4259
assert proxy_config["destination"]["host"] == "127.0.0.1"
4360
assert rules_config["allowed_methods"] == ["GET", "POST"]
4461

62+
4563
def test_load_custom_config_location(setup_custom_config, monkeypatch):
4664
"""
4765
Test loading configuration from a custom location specified by CONFIG_LOCATION.
@@ -54,6 +72,7 @@ def test_load_custom_config_location(setup_custom_config, monkeypatch):
5472
assert proxy_config["destination"]["host"] == "127.0.0.1"
5573
assert rules_config["allowed_methods"] == ["GET", "POST"]
5674

75+
5776
def test_missing_config_in_custom_location(tmp_path, monkeypatch):
5877
"""
5978
Test behavior when config files are missing from the custom CONFIG_LOCATION.
@@ -68,6 +87,7 @@ def test_missing_config_in_custom_location(tmp_path, monkeypatch):
6887
with pytest.raises(FileNotFoundError, match="rules_config.json"):
6988
load_rules_config()
7089

90+
7191
def test_missing_config_location_var(setup_default_config, monkeypatch):
7292
"""
7393
Test that config loads from the default location when CONFIG_LOCATION is not set.
@@ -80,6 +100,7 @@ def test_missing_config_location_var(setup_default_config, monkeypatch):
80100
assert proxy_config["destination"]["host"] == "127.0.0.1"
81101
assert rules_config["rate_limit"] == "10 per minute"
82102

103+
83104
@pytest.fixture
84105
def setup_invalid_json_config(tmp_path):
85106
"""
@@ -88,11 +109,16 @@ def setup_invalid_json_config(tmp_path):
88109
# Create files with invalid JSON in the temporary directory
89110
proxy_config = tmp_path / "proxy_config.json"
90111
rules_config = tmp_path / "rules_config.json"
91-
proxy_config.write_text('{ "destination": { "host": "127.0.0.1", "port": 8080, "path": "/"') # Malformed JSON
92-
rules_config.write_text('{ "allowed_methods": ["GET", "POST"], "rate_limit": "10 per minute"') # Malformed JSON
112+
proxy_config.write_text(
113+
'{ "destination": { "host": "127.0.0.1", "port": 8080, "path": "/"'
114+
) # Malformed JSON
115+
rules_config.write_text(
116+
'{ "allowed_methods": ["GET", "POST"], "rate_limit": "10 per minute"'
117+
) # Malformed JSON
93118

94119
return tmp_path
95120

121+
96122
@pytest.fixture
97123
def setup_missing_fields_config(tmp_path):
98124
"""
@@ -101,34 +127,43 @@ def setup_missing_fields_config(tmp_path):
101127
# Create files missing required fields in the temporary directory
102128
proxy_config = tmp_path / "proxy_config.json"
103129
rules_config = tmp_path / "rules_config.json"
104-
proxy_config.write_text('{}') # Missing "destination" key
105-
rules_config.write_text('{}') # Make it same...
130+
proxy_config.write_text("{}") # Missing "destination" key
131+
rules_config.write_text("{}") # Make it same...
106132
return tmp_path
107133

134+
108135
@pytest.fixture
109136
def setup_nonexistent_config_location(tmp_path):
110137
"""
111138
Fixture for a nonexistent configuration directory to simulate invalid path.
112139
"""
113140
return tmp_path / "nonexistent_config_dir" # This directory does not exist
114141

142+
115143
def test_invalid_json_in_config_files(setup_invalid_json_config, monkeypatch):
116144
"""
117145
Test loading configuration with invalid JSON syntax.
118146
"""
119-
monkeypatch.chdir(setup_invalid_json_config) # Change to directory with invalid JSON files
147+
monkeypatch.chdir(
148+
setup_invalid_json_config
149+
) # Change to directory with invalid JSON files
120150

121151
with pytest.raises(json.JSONDecodeError):
122152
load_proxy_config()
123153

124154
with pytest.raises(json.JSONDecodeError):
125155
load_rules_config()
126156

127-
def test_missing_required_fields_in_config_files(setup_missing_fields_config, monkeypatch):
157+
158+
def test_missing_required_fields_in_config_files(
159+
setup_missing_fields_config, monkeypatch
160+
):
128161
"""
129162
Test loading configuration files that are missing required fields.
130163
"""
131-
monkeypatch.chdir(setup_missing_fields_config) # Change to directory with missing fields
164+
monkeypatch.chdir(
165+
setup_missing_fields_config
166+
) # Change to directory with missing fields
132167

133168
with pytest.raises(KeyError, match="destination"):
134169
load_proxy_config()
@@ -157,7 +192,7 @@ def test_check_required_files_all_present(tmp_path, monkeypatch, capsys):
157192

158193
# Mock the CONFIG_LOCATION environment variable to use tmp_path
159194
monkeypatch.setenv("CONFIG_LOCATION", str(tmp_path))
160-
195+
161196
# Call the function and capture output
162197
check_required_files(["proxy_config.json", "rules_config.json"])
163198

@@ -182,7 +217,10 @@ def test_check_required_files_missing_one_file(tmp_path, monkeypatch, capsys):
182217

183218
# Capture and check the output for the missing file message
184219
captured = capsys.readouterr()
185-
assert "Error: Required configuration file 'rules_config.json' is missing." in captured.out
220+
assert (
221+
"Error: Required configuration file 'rules_config.json' is missing."
222+
in captured.out
223+
)
186224
assert exit_exception.value.code == 1
187225

188226

@@ -199,7 +237,10 @@ def test_check_required_files_missing_both_files(tmp_path, monkeypatch, capsys):
199237

200238
# Capture and check the output for the missing files message
201239
captured = capsys.readouterr()
202-
assert "Error: Required configuration file 'proxy_config.json' is missing." in captured.out # It is displayed first
240+
assert (
241+
"Error: Required configuration file 'proxy_config.json' is missing."
242+
in captured.out
243+
) # It is displayed first
203244
assert exit_exception.value.code == 1
204245

205246

@@ -210,16 +251,21 @@ def setup_extra_fields_config(tmp_path):
210251
"""
211252
proxy_config = tmp_path / "proxy_config.json"
212253
# Adding an unexpected field 'extra_field' to the 'destination' section
213-
proxy_config.write_text(json.dumps({
214-
"destination": {
215-
"host": "127.0.0.1",
216-
"port": 8080,
217-
"path": "/",
218-
"extra_field": "unexpected_value" # Unexpected field
219-
}
220-
}))
254+
proxy_config.write_text(
255+
json.dumps(
256+
{
257+
"destination": {
258+
"host": "127.0.0.1",
259+
"port": 8080,
260+
"path": "/",
261+
"extra_field": "unexpected_value", # Unexpected field
262+
}
263+
}
264+
)
265+
)
221266
return tmp_path
222267

268+
223269
def test_load_proxy_config_with_extra_fields(setup_extra_fields_config, monkeypatch):
224270
"""
225271
Test loading a proxy configuration with unexpected fields in 'destination'.
@@ -228,5 +274,7 @@ def test_load_proxy_config_with_extra_fields(setup_extra_fields_config, monkeypa
228274
monkeypatch.setenv("CONFIG_LOCATION", str(setup_extra_fields_config))
229275

230276
# Expect KeyError due to the unexpected 'extra_field' in the configuration
231-
with pytest.raises(KeyError, match="Unexpected fields in 'destination': extra_field"):
232-
load_proxy_config()
277+
with pytest.raises(
278+
KeyError, match="Unexpected fields in 'destination': extra_field"
279+
):
280+
load_proxy_config()

0 commit comments

Comments
 (0)