def test_init_table_when_user_is_not_root(mocker): table_name = "filter" mocked_user = mocker.patch("connord.iptables.user.is_root") mocked_user.return_value = False from connord import iptables try: iptables.init_table(table_name) assert False except PermissionError as error: assert str(error) == "Function 'init_table' needs root access." mocked_user.assert_called_once()
def test_init_table_when_table_is_valid_ipv4(mocker): table_name = "filter" mocked_user = mocker.patch("connord.iptables.user.is_root") mocked_user.return_value = True from connord import iptables table_mock = mocker.Mock() mocked_table = mocker.patch.object(iptables, "Table", table_mock) mocker.patch.object(iptables.Table, "ALL", ["filter"]) iptables.init_table(table_name) mocked_user.assert_called_once() mocked_table.assert_called_once_with("filter")
def test_init_table_when_table_is_invalid_ipv4(mocker): table_name = "invalid" mocked_user = mocker.patch("connord.iptables.user.is_root") mocked_user.return_value = True from connord import iptables table_mock = mocker.Mock() mocked_table = mocker.patch.object(iptables, "Table", table_mock) mocker.patch.object(iptables.Table, "ALL", ["filter"]) try: iptables.init_table(table_name) assert False except TypeError as error: assert str(error) == "Error: 'invalid' is not a valid table." mocked_user.assert_called_once() mocked_table.assert_not_called()