Repository: migros/fotoobo (language: python)

Issue:
Add tests for fgt backup CLI help



Discussion / hints:

Fix patch:
diff --git a/fotoobo/fortinet/fortigate.py b/fotoobo/fortinet/fortigate.py
index 0efe606..b3875db 100644
--- a/fotoobo/fortinet/fortigate.py
+++ b/fotoobo/fortinet/fortigate.py
@@ -70,10 +70,13 @@ def api(  # pylint: disable=too-many-arguments
 
     def backup(self, timeout: int = 10) -> str:
         """
-        Get the configuration backup from a FortiGate
+        Get the configuration backup from a FortiGate.
+
+        Args:
+            timeout (int): Timeout in sec to wait for the response
 
         Returns:
-            str: configuration backup as text
+            str: Configuration backup as text
         """
         data = self.api(
             "get", "monitor/system/config/backup", params={"scope": "global"}, timeout=timeout


Test patch:
diff --git a/tests/cli/fgt/test_cli_fgt_main.py b/tests/cli/fgt/test_cli_fgt_main.py
index 8928df1..a264ecd 100644
--- a/tests/cli/fgt/test_cli_fgt_main.py
+++ b/tests/cli/fgt/test_cli_fgt_main.py
@@ -1,6 +1,10 @@
 """
 Testing the cli app
 """
+from pathlib import Path
+from unittest.mock import MagicMock
+
+from _pytest.monkeypatch import MonkeyPatch
 from typer.testing import CliRunner
 
 from fotoobo.cli.main import app
@@ -17,3 +21,47 @@ def test_cli_app_fgt_help() -> None:
     assert not arguments
     assert options == {"-h", "--help"}
     assert set(commands) == {"backup", "monitor", "get", "config"}
+
+
+def test_cli_app_fgt_backup_help() -> None:
+    """Test cli help for fgt backup"""
+    result = runner.invoke(app, ["-c", "tests/fotoobo.yaml", "fgt", "backup", "-h"])
+    assert result.exit_code == 0
+    arguments, options, commands = parse_help_output(result.stdout)
+    assert set(arguments) == {"host"}
+    assert options == {"--backup-dir", "-b", "--ftp", "-f", "--smtp", "-s", "-h", "--help"}
+    assert not commands
+
+
+def test_cli_app_fgt_backup_single(monkeypatch: MonkeyPatch, temp_dir: str) -> None:
+    """Test cli fgt backup with no FortiGate given so it backups all"""
+    monkeypatch.setattr(
+        "fotoobo.fortinet.fortigate.FortiGate.backup",
+        MagicMock(return_value="#config-version\ntest-1234"),
+    )
+    Path(temp_dir / Path("test_fgt_1.conf")).unlink(missing_ok=True)
+    Path(temp_dir / Path("test_fgt_2.conf")).unlink(missing_ok=True)
+    assert not Path.exists(Path(temp_dir / Path("test_fgt_1.conf")))
+    assert not Path.exists(Path(temp_dir / Path("test_fgt_2.conf")))
+    result = runner.invoke(
+        app, ["-c", "tests/fotoobo.yaml", "fgt", "backup", "test_fgt_1", "-b", temp_dir]
+    )
+    assert result.exit_code == 0
+    assert Path.exists(Path(temp_dir / Path("test_fgt_1.conf")))
+    assert not Path.exists(Path(temp_dir / Path("test_fgt_2.conf")))
+
+
+def test_cli_app_fgt_backup_all(monkeypatch: MonkeyPatch, temp_dir: str) -> None:
+    """Test cli fgt backup with no FortiGate given so it backups all"""
+    monkeypatch.setattr(
+        "fotoobo.fortinet.fortigate.FortiGate.backup",
+        MagicMock(return_value="#config-version\ntest-1234"),
+    )
+    Path(temp_dir / Path("test_fgt_1.conf")).unlink(missing_ok=True)
+    Path(temp_dir / Path("test_fgt_2.conf")).unlink(missing_ok=True)
+    assert not Path.exists(Path(temp_dir / Path("test_fgt_1.conf")))
+    assert not Path.exists(Path(temp_dir / Path("test_fgt_2.conf")))
+    result = runner.invoke(app, ["-c", "tests/fotoobo.yaml", "fgt", "backup", "-b", temp_dir])
+    assert result.exit_code == 0
+    assert Path.exists(Path(temp_dir / Path("test_fgt_1.conf")))
+    assert Path.exists(Path(temp_dir / Path("test_fgt_2.conf")))
diff --git a/tests/fortinet/test_fortigate.py b/tests/fortinet/test_fortigate.py
index cce7f64..707f27d 100644
--- a/tests/fortinet/test_fortigate.py
+++ b/tests/fortinet/test_fortigate.py
@@ -34,7 +34,7 @@ def test_api(monkeypatch: MonkeyPatch) -> None:
         )
 
     @staticmethod
-    def test_get_backup(monkeypatch: MonkeyPatch) -> None:
+    def test_backup(monkeypatch: MonkeyPatch) -> None:
         """Test the FortiGate backup method"""
         monkeypatch.setattr(
             "fotoobo.fortinet.fortigate.FortiGate.api",
diff --git a/tests/utils/fgt/test_backup.py b/tests/utils/fgt/test_backup.py
index 182ba67..b9d50ad 100644
--- a/tests/utils/fgt/test_backup.py
+++ b/tests/utils/fgt/test_backup.py
@@ -10,7 +10,7 @@
 from fotoobo.utils.fgt import backup
 
 
-def test_backup_no_backup_dir(monkeypatch: MonkeyPatch) -> None:
+def test_backup_no_backup_dir(monkeypatch: MonkeyPatch, temp_dir: str) -> None:
     """Test fgt backup without backup_dir in config"""
     monkeypatch.setattr("fotoobo.utils.fgt.main.config.inventory_file", "tests/data/inventory.yaml")
     monkeypatch.setattr("fotoobo.utils.fgt.main.create_dir", MagicMock(return_value=True))
@@ -20,7 +20,7 @@ def test_backup_no_backup_dir(monkeypatch: MonkeyPatch) -> None:
     )
     monkeypatch.setattr("fotoobo.utils.fgt.main.os.path.isfile", MagicMock(return_value=True))
     monkeypatch.setattr("fotoobo.utils.fgt.main.os.remove", MagicMock(return_value=True))
-    backup("test_fgt_1")
+    backup("test_fgt_1", temp_dir)
     assert True
 
 
@@ -28,7 +28,7 @@ def test_backup_all(monkeypatch: MonkeyPatch, temp_dir: str) -> None:
     """Test fgt backup with no 'hosts' so all FortiGates are backed up"""
     monkeypatch.setattr("fotoobo.utils.fgt.main.create_dir", MagicMock(return_value=True))
     monkeypatch.setattr("fotoobo.utils.fgt.main.config.inventory_file", "tests/data/inventory.yaml")
-    backup("")
+    backup("", temp_dir)
     assert True
 
 
@@ -42,7 +42,7 @@ def test_backup_single(monkeypatch: MonkeyPatch, temp_dir: str) -> None:
     )
     monkeypatch.setattr("fotoobo.utils.fgt.main.os.path.isfile", MagicMock(return_value=True))
     monkeypatch.setattr("fotoobo.utils.fgt.main.os.remove", MagicMock(return_value=True))
-    backup("test_fgt_1")
+    backup("test_fgt_1", temp_dir)
     assert True
 
 
@@ -56,7 +56,7 @@ def test_backup_single_with_invalid_config(monkeypatch: MonkeyPatch, temp_dir: s
     )
     monkeypatch.setattr("fotoobo.utils.fgt.main.os.path.isfile", MagicMock(return_value=True))
     monkeypatch.setattr("fotoobo.utils.fgt.main.os.remove", MagicMock(return_value=True))
-    backup("test_fgt_1")
+    backup("test_fgt_1", temp_dir)
     assert True
 
 
@@ -70,5 +70,5 @@ def test_backup_api_error(monkeypatch: MonkeyPatch, temp_dir: str) -> None:
         "fotoobo.fortinet.fortigate.FortiGate.backup",
         MagicMock(side_effect=APIError("dummy error")),
     )
-    backup("test_fgt_1")
+    backup("test_fgt_1", temp_dir)
     assert True