|
19 | 19 |
|
20 | 20 | import json |
21 | 21 | import os |
22 | | -from contextlib import redirect_stdout |
23 | | -from io import StringIO |
24 | 22 |
|
25 | 23 | import pytest |
26 | 24 | import yaml |
| 25 | +from airflowctl.api.datamodels.generated import VariableResponse |
27 | 26 | from sqlalchemy import select |
28 | 27 |
|
29 | 28 | from airflow import models |
@@ -229,101 +228,90 @@ def test_variables_set_different_types(self): |
229 | 228 |
|
230 | 229 | os.remove("variables_types.json") |
231 | 230 |
|
232 | | - def test_variables_list(self): |
| 231 | + def test_variables_list(self, mock_cli_api_client): |
233 | 232 | """Test variable_list command""" |
234 | | - # Test command is received |
| 233 | + mock_cli_api_client.variables.list.return_value.variables = [] |
235 | 234 | variable_command.variables_list(self.parser.parse_args(["variables", "list"])) |
| 235 | + mock_cli_api_client.variables.list.assert_called_once() |
236 | 236 |
|
237 | | - def test_variables_list_show_values(self): |
| 237 | + def test_variables_list_show_values(self, mock_cli_api_client, stdout_capture): |
238 | 238 | """Test variables list with --show-values flag shows actual values.""" |
239 | | - # Create test variables |
240 | | - Variable.set("test_key1", "test_value1") |
241 | | - Variable.set("test_key2", "test_value2") |
| 239 | + mock_cli_api_client.variables.list.return_value.variables = [ |
| 240 | + VariableResponse(key="test_key1", value="test_value1", is_encrypted=False), |
| 241 | + VariableResponse(key="test_key2", value="test_value2", is_encrypted=False), |
| 242 | + ] |
242 | 243 |
|
243 | 244 | args = self.parser.parse_args(["variables", "list", "--output", "json", "--show-values"]) |
244 | | - with redirect_stdout(StringIO()) as stdout_io: |
| 245 | + with stdout_capture as stdout: |
245 | 246 | variable_command.variables_list(args) |
246 | | - output = stdout_io.getvalue() |
247 | 247 |
|
248 | | - # Parse JSON output and verify values are shown |
249 | | - data = json.loads(output) |
250 | | - assert len(data) >= 2 |
| 248 | + data = json.loads(stdout.getvalue()) |
251 | 249 | key_value_map = {item["key"]: item["val"] for item in data} |
252 | | - assert "test_value1" in key_value_map["test_key1"] |
253 | | - assert "test_value2" in key_value_map["test_key2"] |
| 250 | + assert key_value_map["test_key1"] == "test_value1" |
| 251 | + assert key_value_map["test_key2"] == "test_value2" |
254 | 252 |
|
255 | | - def test_variables_list_hide_sensitive(self): |
| 253 | + def test_variables_list_hide_sensitive(self, mock_cli_api_client, stdout_capture): |
256 | 254 | """Test variables list with --hide-sensitive masks all values.""" |
257 | | - # Create test variables |
258 | | - Variable.set("test_key1", "test_value1") |
259 | | - Variable.set("test_key2", "test_value2") |
| 255 | + mock_cli_api_client.variables.list.return_value.variables = [ |
| 256 | + VariableResponse(key="test_key1", value="test_value1", is_encrypted=False), |
| 257 | + VariableResponse(key="test_key2", value="test_value2", is_encrypted=False), |
| 258 | + ] |
260 | 259 |
|
261 | 260 | args = self.parser.parse_args( |
262 | 261 | ["variables", "list", "--output", "json", "--show-values", "--hide-sensitive"] |
263 | 262 | ) |
264 | | - with redirect_stdout(StringIO()) as stdout_io: |
| 263 | + with stdout_capture as stdout: |
265 | 264 | variable_command.variables_list(args) |
266 | | - output = stdout_io.getvalue() |
267 | 265 |
|
268 | | - # Parse JSON output and verify values are masked |
269 | | - data = json.loads(output) |
270 | | - assert len(data) >= 2 |
| 266 | + data = json.loads(stdout.getvalue()) |
| 267 | + assert len(data) == 2 |
271 | 268 | for item in data: |
272 | | - if "test_key" in item["key"]: |
273 | | - assert item["val"] == "***" |
| 269 | + assert item["val"] == "***" |
274 | 270 |
|
275 | | - def test_variables_list_hide_sensitive_without_show_values_fails(self): |
| 271 | + def test_variables_list_hide_sensitive_without_show_values_fails(self, mock_cli_api_client): |
276 | 272 | """--hide-sensitive without --show-values should fail.""" |
277 | 273 | args = self.parser.parse_args(["variables", "list", "--hide-sensitive"]) |
278 | 274 | with pytest.raises(SystemExit, match="--hide-sensitive can only be used with --show-values"): |
279 | 275 | variable_command.variables_list(args) |
| 276 | + mock_cli_api_client.variables.list.assert_not_called() |
280 | 277 |
|
281 | | - def test_variables_list_default_hides_values(self): |
| 278 | + def test_variables_list_default_hides_values(self, mock_cli_api_client, stdout_capture): |
282 | 279 | """By default, variables list should only show keys, not values.""" |
283 | | - Variable.set("test_key1", "test_value1") |
284 | | - Variable.set("test_key2", "test_value2") |
| 280 | + mock_cli_api_client.variables.list.return_value.variables = [ |
| 281 | + VariableResponse(key="test_key1", value="test_value1", is_encrypted=False), |
| 282 | + VariableResponse(key="test_key2", value="test_value2", is_encrypted=False), |
| 283 | + ] |
285 | 284 |
|
286 | 285 | args = self.parser.parse_args(["variables", "list", "--output", "json"]) |
287 | | - with redirect_stdout(StringIO()) as stdout_io: |
| 286 | + with stdout_capture as stdout: |
288 | 287 | variable_command.variables_list(args) |
289 | | - output = stdout_io.getvalue() |
290 | 288 |
|
291 | | - data = json.loads(output) |
292 | | - assert len(data) >= 2 |
| 289 | + data = json.loads(stdout.getvalue()) |
| 290 | + assert len(data) == 2 |
293 | 291 | for item in data: |
294 | | - if "test_key" in item["key"]: |
295 | | - assert "val" not in item |
| 292 | + assert "val" not in item |
296 | 293 |
|
297 | | - def test_variables_list_edge_cases(self): |
| 294 | + def test_variables_list_edge_cases(self, mock_cli_api_client, stdout_capture): |
298 | 295 | """Test variables list with None and empty values.""" |
299 | | - Variable.set("empty_var", "") |
300 | | - Variable.set("none_var", None) |
301 | | - Variable.set("normal_var", "normal_value") |
| 296 | + mock_cli_api_client.variables.list.return_value.variables = [ |
| 297 | + VariableResponse(key="empty_var", value="", is_encrypted=False), |
| 298 | + VariableResponse(key="none_str_var", value="None", is_encrypted=False), |
| 299 | + VariableResponse(key="none_var", value=None, is_encrypted=False), |
| 300 | + VariableResponse(key="normal_var", value="normal_value", is_encrypted=False), |
| 301 | + ] |
302 | 302 |
|
303 | 303 | args = self.parser.parse_args(["variables", "list", "--output", "json", "--show-values"]) |
304 | | - with redirect_stdout(StringIO()) as stdout_io: |
| 304 | + with stdout_capture as stdout: |
305 | 305 | variable_command.variables_list(args) |
306 | | - output = stdout_io.getvalue() |
307 | 306 |
|
308 | | - data = json.loads(output) |
| 307 | + data = json.loads(stdout.getvalue()) |
309 | 308 | key_value_map = {item["key"]: item["val"] for item in data} |
310 | 309 |
|
311 | 310 | assert key_value_map["empty_var"] == "" |
312 | | - assert key_value_map["none_var"] == "None" |
| 311 | + assert key_value_map["none_str_var"] == "None" |
| 312 | + assert key_value_map["none_var"] == "" |
313 | 313 | assert key_value_map["normal_var"] == "normal_value" |
314 | 314 |
|
315 | | - args = self.parser.parse_args( |
316 | | - ["variables", "list", "--output", "json", "--show-values", "--hide-sensitive"] |
317 | | - ) |
318 | | - with redirect_stdout(StringIO()) as stdout_io: |
319 | | - variable_command.variables_list(args) |
320 | | - output = stdout_io.getvalue() |
321 | | - |
322 | | - data = json.loads(output) |
323 | | - for item in data: |
324 | | - if item["key"] in ["empty_var", "none_var", "normal_var"]: |
325 | | - assert item["val"] == "***" |
326 | | - |
327 | 315 | def test_variables_delete(self): |
328 | 316 | """Test variable_delete command""" |
329 | 317 | variable_command.variables_set(self.parser.parse_args(["variables", "set", "foo", "bar"])) |
|
0 commit comments