@@ -12,26 +12,13 @@ class ConfigurationManagerTest(unittest.TestCase):
1212 def setUp (self ):
1313 self .config_file_path = "/tmp/securenative.ini"
1414
15- def create_ini_file (self , conf ):
16- os .environ ["SECURENATIVE_COMFIG_FILE" ] = self .config_file_path
17-
15+ def clean_settings (self ):
1816 try :
1917 os .remove (self .config_file_path )
2018 except FileNotFoundError :
2119 pass
2220
23- config = configparser .ConfigParser ()
24-
25- for key , value in conf .items ():
26- config .set ("DEFAULT" , key .upper (), value )
27-
28- with open (self .config_file_path , "w" ) as configfile :
29- config .write (configfile )
30-
31- @unittest .skipIf (platform .system () == "Windows" or platform .system () == "windows" , "test not supported on windows" )
32- def test_parse_config_file_correctly (self ):
3321 try :
34- os .remove (self .config_file_path )
3522 del os .environ ["SECURENATIVE_API_KEY" ]
3623 del os .environ ["SECURENATIVE_API_URL" ]
3724 del os .environ ["SECURENATIVE_INTERVAL" ]
@@ -41,11 +28,29 @@ def test_parse_config_file_correctly(self):
4128 del os .environ ["SECURENATIVE_DISABLE" ]
4229 del os .environ ["SECURENATIVE_LOG_LEVEL" ]
4330 del os .environ ["SECURENATIVE_FAILOVER_STRATEGY" ]
44- except FileNotFoundError :
45- pass
31+ del os .environ ["SECURENATIVE_PROXY_HEADERS" ]
4632 except KeyError :
4733 pass
4834
35+ def create_ini_file (self , conf ):
36+ os .environ ["SECURENATIVE_CONFIG_FILE" ] = self .config_file_path
37+
38+ try :
39+ os .remove (self .config_file_path )
40+ except FileNotFoundError :
41+ pass
42+
43+ config = configparser .ConfigParser ()
44+
45+ for key , value in conf .items ():
46+ config .set ("DEFAULT" , key .upper (), value )
47+
48+ with open (self .config_file_path , "w" ) as configfile :
49+ config .write (configfile )
50+
51+ @unittest .skipIf (platform .system () == "Windows" or platform .system () == "windows" , "test not supported on windows" )
52+ def test_parse_config_file_correctly (self ):
53+ self .clean_settings ()
4954 config = {
5055 "SECURENATIVE_API_KEY" : "SOME_API_KEY" ,
5156 "SECURENATIVE_APP_NAME" : "SOME_APP_NAME" ,
@@ -57,7 +62,8 @@ def test_parse_config_file_correctly(self):
5762 "SECURENATIVE_AUTO_SEND" : "True" ,
5863 "SECURENATIVE_DISABLE" : "False" ,
5964 "SECURENATIVE_LOG_LEVEL" : "Critical" ,
60- "SECURENATIVE_FAILOVER_STRATEGY" : "fail-closed"
65+ "SECURENATIVE_FAILOVER_STRATEGY" : "fail-closed" ,
66+ "SECURENATIVE_PROXY_HEADERS" : "CF-Connecting-IP,Some-Random-Ip"
6167 }
6268
6369 self .create_ini_file (config )
@@ -73,25 +79,11 @@ def test_parse_config_file_correctly(self):
7379 self .assertEqual (options .log_level , "Critical" )
7480 self .assertEqual (options .max_events , "100" )
7581 self .assertEqual (options .timeout , "1500" )
82+ self .assertEqual (options .proxy_headers , ["CF-Connecting-IP" , "Some-Random-Ip" ])
7683
7784 @unittest .skipIf (platform .system () == "Windows" or platform .system () == "windows" , "test not supported on windows" )
7885 def test_ignore_unknown_config_in_properties_file (self ):
79- try :
80- os .remove (self .config_file_path )
81- del os .environ ["SECURENATIVE_API_KEY" ]
82- del os .environ ["SECURENATIVE_API_URL" ]
83- del os .environ ["SECURENATIVE_INTERVAL" ]
84- del os .environ ["SECURENATIVE_MAX_EVENTS" ]
85- del os .environ ["SECURENATIVE_TIMEOUT" ]
86- del os .environ ["SECURENATIVE_AUTO_SEND" ]
87- del os .environ ["SECURENATIVE_DISABLE" ]
88- del os .environ ["SECURENATIVE_LOG_LEVEL" ]
89- del os .environ ["SECURENATIVE_FAILOVER_STRATEGY" ]
90- except FileNotFoundError :
91- pass
92- except KeyError :
93- pass
94-
86+ self .clean_settings ()
9587 config = {
9688 "SECURENATIVE_TIMEOUT" : "1500" ,
9789 "SECURENATIVE_UNKNOWN_KEY" : "SOME_UNKNOWN_KEY"
@@ -105,22 +97,7 @@ def test_ignore_unknown_config_in_properties_file(self):
10597
10698 @unittest .skipIf (platform .system () == "Windows" or platform .system () == "windows" , "test not supported on windows" )
10799 def test_handle_invalid_config_file (self ):
108- try :
109- os .remove (self .config_file_path )
110- del os .environ ["SECURENATIVE_API_KEY" ]
111- del os .environ ["SECURENATIVE_API_URL" ]
112- del os .environ ["SECURENATIVE_INTERVAL" ]
113- del os .environ ["SECURENATIVE_MAX_EVENTS" ]
114- del os .environ ["SECURENATIVE_TIMEOUT" ]
115- del os .environ ["SECURENATIVE_AUTO_SEND" ]
116- del os .environ ["SECURENATIVE_DISABLE" ]
117- del os .environ ["SECURENATIVE_LOG_LEVEL" ]
118- del os .environ ["SECURENATIVE_FAILOVER_STRATEGY" ]
119- except FileNotFoundError :
120- pass
121- except KeyError :
122- pass
123-
100+ self .clean_settings ()
124101 config = {"bla" : "bla" }
125102
126103 self .create_ini_file (config )
@@ -130,27 +107,12 @@ def test_handle_invalid_config_file(self):
130107
131108 @unittest .skipIf (platform .system () == "Windows" or platform .system () == "windows" , "test not supported on windows" )
132109 def ignore_invalid_config_file_entries (self ):
133- try :
134- os .remove (self .config_file_path )
135- del os .environ ["SECURENATIVE_API_KEY" ]
136- del os .environ ["SECURENATIVE_API_URL" ]
137- del os .environ ["SECURENATIVE_INTERVAL" ]
138- del os .environ ["SECURENATIVE_MAX_EVENTS" ]
139- del os .environ ["SECURENATIVE_TIMEOUT" ]
140- del os .environ ["SECURENATIVE_AUTO_SEND" ]
141- del os .environ ["SECURENATIVE_DISABLE" ]
142- del os .environ ["SECURENATIVE_LOG_LEVEL" ]
143- del os .environ ["SECURENATIVE_FAILOVER_STRATEGY" ]
144- except FileNotFoundError :
145- pass
146- except KeyError :
147- pass
148-
110+ self .clean_settings ()
149111 config = {
150112 "SECURENATIVE_API_KEY" : 1 ,
151113 "SECURENATIVE_API_URL" : 3 ,
152114 "SECURENATIVE_TIMEOUT" : "bad timeout" ,
153- "SECURENATIVE_FAILOVER_STRATEGY" : "fail-what"
115+ "SECURENATIVE_FAILOVER_STRATEGY" : "fail-what" ,
154116 }
155117
156118 self .create_ini_file (config )
@@ -161,22 +123,7 @@ def ignore_invalid_config_file_entries(self):
161123
162124 @unittest .skipIf (platform .system () == "Windows" or platform .system () == "windows" , "test not supported on windows" )
163125 def test_load_default_config (self ):
164- try :
165- os .remove (self .config_file_path )
166- del os .environ ["SECURENATIVE_API_KEY" ]
167- del os .environ ["SECURENATIVE_API_URL" ]
168- del os .environ ["SECURENATIVE_INTERVAL" ]
169- del os .environ ["SECURENATIVE_MAX_EVENTS" ]
170- del os .environ ["SECURENATIVE_TIMEOUT" ]
171- del os .environ ["SECURENATIVE_AUTO_SEND" ]
172- del os .environ ["SECURENATIVE_DISABLE" ]
173- del os .environ ["SECURENATIVE_LOG_LEVEL" ]
174- del os .environ ["SECURENATIVE_FAILOVER_STRATEGY" ]
175- except FileNotFoundError :
176- pass
177- except KeyError :
178- pass
179-
126+ self .clean_settings ()
180127 options = ConfigurationManager .load_config (None )
181128
182129 self .assertIsNotNone (options )
@@ -189,24 +136,11 @@ def test_load_default_config(self):
189136 self .assertEqual (str (options .disable ), "False" )
190137 self .assertEqual (options .log_level , "CRITICAL" )
191138 self .assertEqual (options .fail_over_strategy , FailOverStrategy .FAIL_OPEN .value )
139+ self .assertEqual (len (options .proxy_headers ), 0 )
192140
193141 @unittest .skipIf (platform .system () == "Windows" or platform .system () == "windows" , "test not supported on windows" )
194142 def test_get_config_from_env_variables (self ):
195- try :
196- os .remove (self .config_file_path )
197- del os .environ ["SECURENATIVE_API_KEY" ]
198- del os .environ ["SECURENATIVE_API_URL" ]
199- del os .environ ["SECURENATIVE_INTERVAL" ]
200- del os .environ ["SECURENATIVE_MAX_EVENTS" ]
201- del os .environ ["SECURENATIVE_TIMEOUT" ]
202- del os .environ ["SECURENATIVE_AUTO_SEND" ]
203- del os .environ ["SECURENATIVE_DISABLE" ]
204- del os .environ ["SECURENATIVE_LOG_LEVEL" ]
205- del os .environ ["SECURENATIVE_FAILOVER_STRATEGY" ]
206- except FileNotFoundError :
207- pass
208- except KeyError :
209- pass
143+ self .clean_settings ()
210144
211145 os .environ ["SECURENATIVE_API_KEY" ] = "SOME_ENV_API_KEY"
212146 os .environ ["SECURENATIVE_API_URL" ] = "SOME_API_URL"
@@ -217,6 +151,7 @@ def test_get_config_from_env_variables(self):
217151 os .environ ["SECURENATIVE_DISABLE" ] = "True"
218152 os .environ ["SECURENATIVE_LOG_LEVEL" ] = "Debug"
219153 os .environ ["SECURENATIVE_FAILOVER_STRATEGY" ] = "fail-closed"
154+ os .environ ["SECURENATIVE_PROXY_HEADERS" ] = "CF-Connecting-IP"
220155
221156 options = ConfigurationManager .load_config (None )
222157
@@ -229,25 +164,11 @@ def test_get_config_from_env_variables(self):
229164 self .assertEqual (options .disable , "True" )
230165 self .assertEqual (options .log_level , "Debug" )
231166 self .assertEqual (options .fail_over_strategy , FailOverStrategy .FAIL_CLOSED .value )
167+ self .assertEqual (options .proxy_headers , "CF-Connecting-IP" )
232168
233169 @unittest .skipIf (platform .system () == "Windows" or platform .system () == "windows" , "test not supported on windows" )
234170 def test_default_values_for_invalid_enum_config_props (self ):
235- try :
236- os .remove (self .config_file_path )
237- del os .environ ["SECURENATIVE_API_KEY" ]
238- del os .environ ["SECURENATIVE_API_URL" ]
239- del os .environ ["SECURENATIVE_INTERVAL" ]
240- del os .environ ["SECURENATIVE_MAX_EVENTS" ]
241- del os .environ ["SECURENATIVE_TIMEOUT" ]
242- del os .environ ["SECURENATIVE_AUTO_SEND" ]
243- del os .environ ["SECURENATIVE_DISABLE" ]
244- del os .environ ["SECURENATIVE_LOG_LEVEL" ]
245- del os .environ ["SECURENATIVE_FAILOVER_STRATEGY" ]
246- except FileNotFoundError :
247- pass
248- except KeyError :
249- pass
250-
171+ self .clean_settings ()
251172 config = {
252173 "SECURENATIVE_FAILOVER_STRATEGY" : "fail-something"
253174 }
0 commit comments