|
| 1 | +import sys |
| 2 | +from unittest import TestCase |
| 3 | +from unittest.mock import ANY, Mock, create_autospec, patch |
| 4 | +from robot.running.model import TestSuite |
| 5 | +from oxygen.oxygen import OxygenCLI |
| 6 | +from ..helpers import RESOURCES_PATH |
| 7 | + |
| 8 | + |
| 9 | +class TestOxygenZapCLI(TestCase): |
| 10 | + ZAP_XML = str(RESOURCES_PATH / "zap" / "zap.xml") |
| 11 | + |
| 12 | + def setUp(self): |
| 13 | + self.cli = OxygenCLI() |
| 14 | + self.handler = self.cli._handlers["oxygen.zap"] |
| 15 | + self.expected_suite = create_autospec(TestSuite) |
| 16 | + self.mock = Mock() |
| 17 | + self.mock.running.build_suite = Mock(return_value=self.expected_suite) |
| 18 | + |
| 19 | + def test_cli(self): |
| 20 | + self.assertEqual( |
| 21 | + self.handler.cli(), |
| 22 | + { |
| 23 | + ("--accepted-risk-level",): { |
| 24 | + "help": "Set accepted risk level", |
| 25 | + "type": int, |
| 26 | + }, |
| 27 | + ("--required-confidence-level",): { |
| 28 | + "help": "Set required confidence level", |
| 29 | + "type": int, |
| 30 | + }, |
| 31 | + ("result_file",): {}, |
| 32 | + }, |
| 33 | + ) |
| 34 | + |
| 35 | + @patch("oxygen.oxygen.RobotInterface") |
| 36 | + def test_cli_run(self, mock_robot_iface): |
| 37 | + mock_robot_iface.return_value = self.mock |
| 38 | + |
| 39 | + cmd_args = f"oxygen oxygen.zap {self.ZAP_XML}" |
| 40 | + with patch.object(sys, "argv", cmd_args.split()): |
| 41 | + self.cli.run() |
| 42 | + |
| 43 | + self.assertEqual(self.handler._config["accepted_risk_level"], 2) |
| 44 | + self.assertEqual(self.handler._config["required_confidence_level"], 1) |
| 45 | + |
| 46 | + self.mock.running.build_suite.assert_called_once() |
| 47 | + |
| 48 | + self.expected_suite.run.assert_called_once_with( |
| 49 | + output=str(RESOURCES_PATH / "zap" / "zap_robot_output.xml"), |
| 50 | + log=None, |
| 51 | + report=None, |
| 52 | + stdout=ANY, |
| 53 | + ) |
| 54 | + |
| 55 | + @patch("oxygen.oxygen.RobotInterface") |
| 56 | + def test_cli_run_with_levels(self, mock_robot_iface): |
| 57 | + mock_robot_iface.return_value = self.mock |
| 58 | + |
| 59 | + cmd_args = ( |
| 60 | + f"oxygen oxygen.zap {self.ZAP_XML} --accepted-risk-level 3" |
| 61 | + " --required-confidence-level 3" |
| 62 | + ) |
| 63 | + with patch.object(sys, "argv", cmd_args.split()): |
| 64 | + self.cli.run() |
| 65 | + |
| 66 | + self.assertEqual(self.handler._config["accepted_risk_level"], 3) |
| 67 | + self.assertEqual(self.handler._config["required_confidence_level"], 3) |
| 68 | + |
| 69 | + @patch("oxygen.oxygen.RobotInterface") |
| 70 | + def test_cli_run_with_accepted_risk_level(self, mock_robot_iface): |
| 71 | + mock_robot_iface.return_value = self.mock |
| 72 | + |
| 73 | + cmd_args = f"oxygen oxygen.zap {self.ZAP_XML} --accepted-risk-level 3" |
| 74 | + with patch.object(sys, "argv", cmd_args.split()): |
| 75 | + self.cli.run() |
| 76 | + |
| 77 | + self.assertEqual(self.handler._config["accepted_risk_level"], 3) |
| 78 | + self.assertEqual(self.handler._config["required_confidence_level"], 1) |
| 79 | + |
| 80 | + @patch("oxygen.oxygen.RobotInterface") |
| 81 | + def test_cli_run_with_required_confidence_level(self, mock_robot_iface): |
| 82 | + mock_robot_iface.return_value = self.mock |
| 83 | + |
| 84 | + cmd_args = f"oxygen oxygen.zap {self.ZAP_XML} " "--required-confidence-level 3" |
| 85 | + with patch.object(sys, "argv", cmd_args.split()): |
| 86 | + self.cli.run() |
| 87 | + |
| 88 | + self.assertEqual(self.handler._config["accepted_risk_level"], 2) |
| 89 | + self.assertEqual(self.handler._config["required_confidence_level"], 3) |
0 commit comments