Repository: Naruno/Naruno (language: python)

Issue:
tests: Adding unit test for safety_check function



Discussion / hints:

Fix patch:
diff --git a/decentra_network/lib/safety.py b/decentra_network/lib/safety.py
index b8035222..bd75bf44 100644
--- a/decentra_network/lib/safety.py
+++ b/decentra_network/lib/safety.py
@@ -4,34 +4,49 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at https://mozilla.org/MPL/2.0/.
-
 from decentra_network.lib.log import get_logger
 from decentra_network.lib.settings_system import the_settings
 
 logger = get_logger("Safety")
 
 
-def safety_check(interface=None, timeout=None):
+def safety_check(
+    interface=None,
+    timeout=None,
+    exit_on_error=True,
+    custom_pywall=None,
+    custom_debug_mode=None,
+):
     logger.info("Checking safety")
     try:
-        from pywall import pywall
+        the_import_string = "from pywall import pywall"
+        pywall_class = (exec(the_import_string)
+                        if custom_pywall is None else custom_pywall)
+        the_pywall = pywall() if custom_pywall is None else custom_pywall()
+        the_pywall.iface = the_pywall.iface if interface is None else interface
+        the_pywall.timeout = the_pywall.timeout if timeout is None else timeout
 
-        the_pywall = pywall()
-        if interface is not None:
-            the_pywall.iface = interface
-        if timeout is not None:
-            the_pywall.timeout = timeout
         control = the_pywall.control()
+
+        debug_mode = (the_settings()["debug_mode"]
+                      if custom_debug_mode is None else custom_debug_mode)
+
         if control is not None:
             if control:
                 logger.info("NOT Safe")
-                exit()
+                exit() if exit_on_error else None
+                return False
             else:
                 logger.info("Safe")
-        elif the_settings()["debug_mode"]:
-            logger.info("Control check is none but passing because of debug mode")
+                return True
+        elif debug_mode:
+            logger.info(
+                "Control check is none but passing because of debug mode")
+            return None
         else:
             logger.info("NOT Safe (Control check is None)")
-            exit()
+            exit() if exit_on_error else None
+            return False
     except ImportError:
         logger.info("Passing safety check (no pywall)")
+        return None


Test patch:
diff --git a/tests/unit_tests/test_lib.py b/tests/unit_tests/test_lib.py
index abbee79a..dded4728 100644
--- a/tests/unit_tests/test_lib.py
+++ b/tests/unit_tests/test_lib.py
@@ -30,10 +30,41 @@
                                              printcentertext, question_maker,
                                              quit_menu_maker,
                                              starting_text_centered)
+from decentra_network.lib.safety import safety_check
 from decentra_network.node.server.server import server
 from decentra_network.node.unl import Unl
 
 
+class pywall_none:
+
+    def __init__(self):
+        self.iface = "eth0"
+        self.timeout = 10
+
+    def control(self):
+        return None
+
+
+class pywall_true:
+
+    def __init__(self):
+        self.iface = "eth0"
+        self.timeout = 10
+
+    def control(self):
+        return True
+
+
+class pywall_false:
+
+    def __init__(self):
+        self.iface = "eth0"
+        self.timeout = 10
+
+    def control(self):
+        return False
+
+
 class Test_Lib(unittest.TestCase):
 
     @classmethod
@@ -449,5 +480,39 @@ def test_export_the_transactions(self):
 """
             self.assertEqual(content, expected_content)
 
+    def test_safety_check_no_module(self):
+        result = safety_check()
+        self.assertIsNone(result)
+
+    def test_safety_check_none_false(self):
+        result = safety_check(
+            custom_pywall=pywall_none,
+            custom_debug_mode=False,
+            exit_on_error=False,
+        )
+        self.assertFalse(result)
+
+    def test_safety_check_none_true(self):
+        result = safety_check(
+            custom_pywall=pywall_none,
+            custom_debug_mode=True,
+            exit_on_error=False,
+        )
+        self.assertIsNone(result)
+
+    def test_safety_check_true(self):
+        result = safety_check(
+            custom_pywall=pywall_true,
+            exit_on_error=False,
+        )
+        self.assertFalse(result)
+
+    def test_safety_check_false(self):
+        result = safety_check(
+            custom_pywall=pywall_false,
+            exit_on_error=False,
+        )
+        self.assertTrue(result)
+
 
 unittest.main(exit=False)