1
1
import json
2
2
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
+
4
9
5
10
@pytest .fixture
6
11
def setup_default_config (tmp_path ):
@@ -10,11 +15,16 @@ def setup_default_config(tmp_path):
10
15
# Create default config files in the temporary directory
11
16
proxy_config = tmp_path / "proxy_config.json"
12
17
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
+ )
15
24
16
25
return tmp_path
17
26
27
+
18
28
@pytest .fixture
19
29
def setup_custom_config (tmp_path ):
20
30
"""
@@ -25,23 +35,31 @@ def setup_custom_config(tmp_path):
25
35
custom_config_dir .mkdir ()
26
36
proxy_config = custom_config_dir / "proxy_config.json"
27
37
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
+ )
30
44
31
45
return custom_config_dir
32
46
47
+
33
48
def test_load_default_config (setup_default_config , monkeypatch ):
34
49
"""
35
50
Test loading configuration from the default location.
36
51
"""
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
38
55
39
56
proxy_config = load_proxy_config ()
40
57
rules_config = load_rules_config ()
41
58
42
59
assert proxy_config ["destination" ]["host" ] == "127.0.0.1"
43
60
assert rules_config ["allowed_methods" ] == ["GET" , "POST" ]
44
61
62
+
45
63
def test_load_custom_config_location (setup_custom_config , monkeypatch ):
46
64
"""
47
65
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):
54
72
assert proxy_config ["destination" ]["host" ] == "127.0.0.1"
55
73
assert rules_config ["allowed_methods" ] == ["GET" , "POST" ]
56
74
75
+
57
76
def test_missing_config_in_custom_location (tmp_path , monkeypatch ):
58
77
"""
59
78
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):
68
87
with pytest .raises (FileNotFoundError , match = "rules_config.json" ):
69
88
load_rules_config ()
70
89
90
+
71
91
def test_missing_config_location_var (setup_default_config , monkeypatch ):
72
92
"""
73
93
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):
80
100
assert proxy_config ["destination" ]["host" ] == "127.0.0.1"
81
101
assert rules_config ["rate_limit" ] == "10 per minute"
82
102
103
+
83
104
@pytest .fixture
84
105
def setup_invalid_json_config (tmp_path ):
85
106
"""
@@ -88,11 +109,16 @@ def setup_invalid_json_config(tmp_path):
88
109
# Create files with invalid JSON in the temporary directory
89
110
proxy_config = tmp_path / "proxy_config.json"
90
111
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
93
118
94
119
return tmp_path
95
120
121
+
96
122
@pytest .fixture
97
123
def setup_missing_fields_config (tmp_path ):
98
124
"""
@@ -101,34 +127,43 @@ def setup_missing_fields_config(tmp_path):
101
127
# Create files missing required fields in the temporary directory
102
128
proxy_config = tmp_path / "proxy_config.json"
103
129
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...
106
132
return tmp_path
107
133
134
+
108
135
@pytest .fixture
109
136
def setup_nonexistent_config_location (tmp_path ):
110
137
"""
111
138
Fixture for a nonexistent configuration directory to simulate invalid path.
112
139
"""
113
140
return tmp_path / "nonexistent_config_dir" # This directory does not exist
114
141
142
+
115
143
def test_invalid_json_in_config_files (setup_invalid_json_config , monkeypatch ):
116
144
"""
117
145
Test loading configuration with invalid JSON syntax.
118
146
"""
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
120
150
121
151
with pytest .raises (json .JSONDecodeError ):
122
152
load_proxy_config ()
123
153
124
154
with pytest .raises (json .JSONDecodeError ):
125
155
load_rules_config ()
126
156
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
+ ):
128
161
"""
129
162
Test loading configuration files that are missing required fields.
130
163
"""
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
132
167
133
168
with pytest .raises (KeyError , match = "destination" ):
134
169
load_proxy_config ()
@@ -157,7 +192,7 @@ def test_check_required_files_all_present(tmp_path, monkeypatch, capsys):
157
192
158
193
# Mock the CONFIG_LOCATION environment variable to use tmp_path
159
194
monkeypatch .setenv ("CONFIG_LOCATION" , str (tmp_path ))
160
-
195
+
161
196
# Call the function and capture output
162
197
check_required_files (["proxy_config.json" , "rules_config.json" ])
163
198
@@ -182,7 +217,10 @@ def test_check_required_files_missing_one_file(tmp_path, monkeypatch, capsys):
182
217
183
218
# Capture and check the output for the missing file message
184
219
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
+ )
186
224
assert exit_exception .value .code == 1
187
225
188
226
@@ -199,7 +237,10 @@ def test_check_required_files_missing_both_files(tmp_path, monkeypatch, capsys):
199
237
200
238
# Capture and check the output for the missing files message
201
239
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
203
244
assert exit_exception .value .code == 1
204
245
205
246
@@ -210,16 +251,21 @@ def setup_extra_fields_config(tmp_path):
210
251
"""
211
252
proxy_config = tmp_path / "proxy_config.json"
212
253
# 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
+ )
221
266
return tmp_path
222
267
268
+
223
269
def test_load_proxy_config_with_extra_fields (setup_extra_fields_config , monkeypatch ):
224
270
"""
225
271
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
228
274
monkeypatch .setenv ("CONFIG_LOCATION" , str (setup_extra_fields_config ))
229
275
230
276
# 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