Пример #1
0
    def test_invalid_combination_format_and_show_last_comment_configfile(self):
        cli_args = argparse.Namespace(insecure=False, cacert=None)
        config = {
            "arguments": {
                "format": "oneline",
                "show_last_comment": 0
            }
        }
        with self.assertRaises(ValueError) as context:
            get_arguments(
                cli_arguments=cli_args,
                config=config,
                choices=self.choices,
            )
            msg = "No format should be specified when selecting email output"

            self.assertTrue(msg in str(context.exception))

        config = {
            "arguments": {
                "format": "indented",
                "show_last_comment": 0
            }
        }
        with self.assertRaises(ValueError) as context:
            get_arguments(
                cli_arguments=cli_args,
                config=config,
                choices=self.choices,
            )
            msg = "No format should be specified when selecting email output"

            self.assertTrue(msg in str(context.exception))
Пример #2
0
    def test_invalid_combination_format_and_email_config_file(self):
        cli_args = argparse.Namespace(insecure=False, cacert=None)

        config = {"arguments": {"email": "some@email", "format": "json"}}
        with self.assertRaises(ValueError) as context:
            get_arguments(cli_arguments=cli_args, config=config)
            msg = "No format should be specified when selecting email output"

            self.assertTrue(msg in str(context.exception))
Пример #3
0
    def test_invalid_combination_format_and_irc_command_line(self):
        cli_args = argparse.Namespace(format="json",
                                      irc=True,
                                      insecure=False,
                                      cacert=None)
        with self.assertRaises(ValueError) as context:
            get_arguments(cli_arguments=cli_args, config={})
            msg = "No format should be specified when selecting irc output"

            self.assertTrue(msg in str(context.exception))
Пример #4
0
    def test_irc_functionality_without_irc_configuration(self):
        cli_args = argparse.Namespace(cacert=None, insecure=False)
        config = {"arguments": {"irc": "#channel1"}}

        with self.assertRaises(ValueError) as context:
            get_arguments(cli_args, config)
            msg = "Missing irc configuration. " \
                  "Check examples/sampleinput_irc.yaml " \
                  "for correct configuration."

            self.assertTrue(msg in str(context.exception))
Пример #5
0
 def test_args_cacert_with_insecure(self):
     cli_args = argparse.Namespace(
         cacert=None,
         debug=False,
         format=None,
         insecure=False,
         reverse=False,
         age=None,
     )
     config = self.config["test6"]
     with self.assertRaises(ValueError) as context:
         get_arguments(cli_args, config)
         msg = "Certificate file can't be used with insecure flag"
         self.assertTrue(msg in str(context.exception),
                         msg=context.exception)
Пример #6
0
    def test_grouped_args_is_none(self):
        cli_args = argparse.Namespace(
            cacert=None,
            debug=True,
            format=None,
            insecure=False,
            reverse=True,
            duration=None,
            state=None,
            value=None,
        )

        config = self.config["test4"]
        arguments = get_arguments(
            cli_args, config, self.choices
        )
        # Only 'state' and 'duration' is given in config, but 'value' is not.
        # So value of all grouped arguments should be None

        group_arguments = (
            arguments.get("state") is None
            and arguments.get("duration") is None
            and arguments.get("value") is None
        )

        self.assertTrue(group_arguments)
Пример #7
0
    def test_args_ca_certi_invalid_path_from_command_line(self):
        cli_args = argparse.Namespace(
            cacert="~/review-rot/",
            debug=False,
            format=None,
            insecure=False,
            reverse=False,
            age=None,
        )

        config = self.config["test5"]
        with self.assertRaises(IOError) as context:
            get_arguments(cli_args, config)
            msg = "No certificate file found "
            self.assertTrue(msg in str(context.exception),
                            msg=context.exception)
Пример #8
0
    def test_args_from_command_line_except_format(self):
        cli_args = argparse.Namespace(
            cacert=None,
            debug=True,
            format=None,
            insecure=False,
            reverse=True,
            age=None,
            sort=None,
        )

        config = self.config["test3"]
        config_args = self.config["test3"]["arguments"]
        arguments = get_arguments(cli_args, config)
        # All arguments must contains values from cli arguments except 'format'
        # It should be from config file

        debug_result = arguments.get("debug") is True
        format_result = arguments.get("format") == config_args.get("format")
        ssl_result = arguments.get("ssl_verify") is True
        reverse_result = arguments.get("reverse") is True
        age = arguments.get('age') is None
        sort_result = arguments.get('sort') is None

        self.assertTrue(debug_result and reverse_result and format_result
                        and ssl_result and age and sort_result)
Пример #9
0
    def test_email_argument_in_config(self):
        cli_args = argparse.Namespace(cacert=None, insecure=False)
        email_input = (
            "[email protected], [email protected],"
            "       [email protected],[email protected]"
        )
        config = {
            "arguments": {
                "email": email_input
            },
            "mailer": {
                "sender": "*****@*****.**",
                "server": "smtp.example.com",
            }
        }

        arguments = get_arguments(
            cli_args, config, self.choices
        )

        self.assertEqual(
            arguments.get("email"),
            [
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**",
            ],
        )
Пример #10
0
    def test_irc_argument_in_config(self):
        cli_args = argparse.Namespace(cacert=None, insecure=False)
        irc_channels = (
            "#channel1, #channel2,"
            "       #channel3,#channel4"
        )
        config = {
            "arguments": {
                "irc": irc_channels
            },
            "irc": {
                "server": "irc.example.com",
                "port": 12345,
            }
        }

        arguments = get_arguments(
            cli_args, config, self.choices
        )

        self.assertEqual(
            arguments.get("irc"),
            [
                "#channel1",
                "#channel2",
                "#channel3",
                "#channel4",
            ],
        )
Пример #11
0
    def test_args_from_command_line_except_format(self):
        cli_args = argparse.Namespace(cacert=None,
                                      debug=True,
                                      format=None,
                                      insecure=False,
                                      reverse=True,
                                      duration=None,
                                      state=None,
                                      value=None)

        config_args = self.config['test3']['arguments']
        arguments = get_arguments(cli_args, config_args, self.choices)
        # All arguments must contains values from cli arguments except 'format'
        # It should be from config file

        debug_result = arguments.get('debug') is True
        format_result = arguments.get('format') == config_args.get('format')
        ssl_result = arguments.get('ssl_verify') is None
        reverse_result = arguments.get('reverse') is True
        group_arguments = arguments.get('state') is None and \
            arguments.get('duration') is None and \
            arguments.get('value') is None

        self.assertTrue(debug_result and reverse_result and format_result
                        and ssl_result and group_arguments)
Пример #12
0
    def test_grouped_args_from_config(self):
        cli_args = argparse.Namespace(
            cacert=None,
            debug=True,
            format=None,
            insecure=False,
            reverse=True,
            duration=None,
            state=None,
            value=None,
        )

        config = self.config["test5"]
        config_args = self.config["test5"]["arguments"]
        arguments = get_arguments(
            cli_args, config, self.choices
        )
        # Arguments 'state', 'duration' and 'value' are given in config. So
        # value of all grouped arguments should taken from config file. Grouped
        # argument's (state', 'duration', 'value) values given by CLI are None
        group_arguments = (
            arguments.get("state") == config_args.get("state")
            and arguments.get("duration") == config_args.get("duration")
            and arguments.get("value") == config_args.get("value")
        )

        self.assertTrue(group_arguments)
Пример #13
0
    def test_args_from_config_with_insecure(self):
        cli_args = argparse.Namespace(
            cacert=None,
            debug=False,
            format=None,
            insecure=False,
            reverse=False,
            age=None,
            sort=None,
        )

        config = self.config["test1"]
        config_args = self.config["test1"]["arguments"]
        arguments = get_arguments(cli_args, config)
        # arguments must contains values from config arguments

        debug_result = arguments.get("debug") == config_args.get("debug")
        format_result = arguments.get("format") == config_args.get("format")
        ssl_result = arguments.get("ssl_verify") != config_args.get("insecure")
        reverse_result = arguments.get("reverse") == config_args.get("reverse")
        age_result = arguments.get('age') == config_args.get('age')
        sort_result = arguments.get("sort") == config_args.get("sort")

        self.assertTrue(debug_result and reverse_result and format_result
                        and age_result and ssl_result and sort_result)
Пример #14
0
    def test_args_from_command_line(self):
        cli_args = argparse.Namespace(
            cacert=None,
            debug=True,
            format="json",
            insecure=True,
            reverse=True,
            age=None,
            sort='updated',
        )

        config = self.config["test2"]
        arguments = get_arguments(cli_args, config)
        # arguments must contains values from cli arguments

        debug_result = arguments.get("debug") == vars(cli_args).get("debug")
        format_result = arguments.get("format") == vars(cli_args).get("format")
        ssl_result = arguments.get("ssl_verify") is False
        reverse_result = arguments.get("reverse") == vars(cli_args).get(
            "reverse")
        age = arguments.get('age') is None
        sort_result = arguments.get("sort") == vars(cli_args).get('sort')

        self.assertTrue(debug_result and reverse_result and format_result
                        and ssl_result and age and sort_result)
Пример #15
0
    def test_args_from_command_line(self):
        cli_args = argparse.Namespace(cacert=None,
                                      debug=True,
                                      format='json',
                                      insecure=True,
                                      reverse=True,
                                      duration=None,
                                      state=None,
                                      value=None)

        confi_args = self.config['test2']['arguments']
        arguments = get_arguments(cli_args, confi_args, self.choices)
        # arguments must contains values from cli arguments

        debug_result = arguments.get('debug') == vars(cli_args).get('debug')
        format_result = arguments.get('format') == vars(cli_args).get('format')
        ssl_result = arguments.get('ssl_verify') is False
        reverse_result = arguments.get('reverse') == vars(cli_args).get(
            'reverse')
        group_arguments = arguments.get('state') is None and \
            arguments.get('duration') is None and \
            arguments.get('value') is None

        self.assertTrue(debug_result and reverse_result and format_result
                        and ssl_result and group_arguments)
Пример #16
0
    def test_grouped_args_from_command_line(self):
        cli_args = argparse.Namespace(
            cacert=None,
            debug=True,
            format=None,
            insecure=False,
            reverse=True,
            duration=10,
            state="older",
            value="m",
        )

        config = self.config["test6"]
        arguments = get_arguments(
            cli_args, config, choices=self.choices
        )
        # Arguments 'state', 'duration' and 'value' is given in config. Grouped
        # argument (state', 'duration', 'value) values are also given as CLI
        # arguments. So value of all grouped arguments should taken from CLI

        group_arguments = (
            arguments.get("state") == vars(cli_args).get("state")
            and arguments.get("duration") == vars(cli_args).get("duration")
            and arguments.get("value") == vars(cli_args).get("value")
        )

        self.assertTrue(group_arguments)
Пример #17
0
    def test_email_functionality_without_mailer_configuration(self):
        cli_args = argparse.Namespace(cacert=None, insecure=False)
        config = {
            "arguments": {
                "email": "*****@*****.**"
            }
        }

        with self.assertRaises(ValueError) as context:
            get_arguments(
                cli_args, config, choices=self.choices
            )
            msg = "Missing mailer configuration. " \
                  "Check examples/sampleinput_email.yaml " \
                  "for correct configuration."

            self.assertTrue(msg in str(context.exception))
Пример #18
0
    def test_invalid_combination_format_and_irc_config_file(self):
        cli_args = argparse.Namespace(insecure=False, cacert=None)

        config = {
            "arguments": {
                "irc": "#channel1",
                "format": "json"
            },
            "irc": {
                "server": "irc.example.com",
                "port": 12345,
            }
        }
        with self.assertRaises(ValueError) as context:
            get_arguments(cli_arguments=cli_args, config=config)
            msg = "No format should be specified when selecting irc output"

            self.assertTrue(msg in str(context.exception))
Пример #19
0
 def test_args_cacert_with_insecure(self):
     cli_args = argparse.Namespace(cacert=None, debug=False, format=None,
                                   insecure=False, reverse=False,
                                   duration=None, state=None, value=None)
     config_args = self.config['test9']['arguments']
     with self.assertRaises(ValueError) as context:
         arguments = get_arguments(cli_args, config_args, self.choices)
         msg = "Certificate file can't be used with insecure flag"
         self.assertTrue(msg in str(context.exception), msg=context.exception)
Пример #20
0
    def test_args_ca_certi_invalid_path_from_config(self):
        cli_args = argparse.Namespace(cacert=None, debug=False, format=None,
                                      insecure=False, reverse=False,
                                      duration=None, state=None, value=None)

        config_args = self.config['test7']['arguments']
        with self.assertRaises(IOError) as context:
            arguments = get_arguments(cli_args, config_args, self.choices)
            msg = "No certificate file found "
            self.assertTrue(msg in str(context.exception), msg=context.exception)
Пример #21
0
    def test_invalid_combination_format_and_show_last_comment_command_line(
            self):

        cli_args = argparse.Namespace(format="oneline",
                                      show_last_comment=0,
                                      insecure=False,
                                      cacert=None)
        with self.assertRaises(ValueError) as context:
            get_arguments(cli_arguments=cli_args, config={})
            msg = "No format should be specified when selecting email output"

            self.assertTrue(msg in str(context.exception))

        cli_args = argparse.Namespace(format="indented",
                                      show_last_comment=0,
                                      insecure=False,
                                      cacert=None)
        with self.assertRaises(ValueError) as context:
            get_arguments(cli_arguments=cli_args, config={})
            msg = "No format should be specified when selecting email output"

            self.assertTrue(msg in str(context.exception))
Пример #22
0
    def test_age_argument_in_config(self):

        now = datetime.datetime.now()
        expected_date = (now - relativedelta(days=5, hours=4))

        cli_args = argparse.Namespace(cacert=None, insecure=False)
        config = {"arguments": {"age": "older 5d 4h"}}

        arguments = get_arguments(cli_args, config)
        self.assertEqual(arguments.get('age').state, 'older')
        self.assertEqual(
            arguments.get('age').date.replace(second=0, microsecond=0),
            expected_date.replace(second=0, microsecond=0))
Пример #23
0
    def test_arg_cacert_from_cli(self, mocked_exists):
        """Ensure that cacert value from cli is used in ssl_verify
        argument."""

        cli_args = argparse.Namespace(
            cacert='ca.crt',
            insecure=False,
        )

        config_args = {}

        arguments = get_arguments(cli_args, config_args)

        self.assertEqual(arguments.get('ssl_verify'), 'ca.crt')
        assert mocked_exists.called
Пример #24
0
    def test_grouped_args_from_command_line(self):
        cli_args = argparse.Namespace(cacert=None, debug=True, format=None,
                                      insecure=False, reverse=True,
                                      duration=10, state='older', value='m')

        config_args = self.config['test6']['arguments']
        arguments = get_arguments(cli_args, config_args, self.choices)
        # Arguments 'state', 'duration' and 'value' is given in config. Grouped
        # argument (state', 'duration', 'value) values are also given as CLI
        # arguments. So value of all grouped arguments should taken from CLI

        group_arguments = arguments.get('state') == vars(cli_args).get('state')\
            and arguments.get('duration') == vars(cli_args).get('duration')\
            and arguments.get('value') == vars(cli_args).get('value')

        self.assertTrue(group_arguments)
Пример #25
0
    def test_args_ssl_verify_is_false(self):
        cli_args = argparse.Namespace(cacert=None,
                                      debug=False,
                                      format=None,
                                      insecure=False,
                                      reverse=False,
                                      duration=None,
                                      state=None,
                                      value=None)
        config_args = self.config['test9']['arguments']
        arguments = get_arguments(cli_args, config_args, self.choices)
        # Arguments 'cacert' and 'insecure' is given in config.
        # Since insecure is True, 'cacert' will be ignored and
        # ssl_verify will be False

        ssl_result = arguments.get('ssl_verify') is False
        self.assertTrue(ssl_result)
Пример #26
0
    def test_args_ca_certi_path_from_config(self):
        cli_args = argparse.Namespace(cacert=None,
                                      debug=False,
                                      format=None,
                                      insecure=False,
                                      reverse=False,
                                      duration=None,
                                      state=None,
                                      value=None)

        config_args = self.config['test7']['arguments']
        arguments = get_arguments(cli_args, config_args, self.choices)
        # Arguments 'cacert' and 'insecure' is given in config.
        # Since insecure is False, ssl_verify should have CA Certificate path
        cacert_value = self.config['test7']['arguments'].get('cacert')
        ssl_result = arguments.get('ssl_verify') == \
            expanduser(expandvars(cacert_value))
        self.assertTrue(ssl_result)
Пример #27
0
    def test_args_ca_certi_path_from_command_line(self):
        cli_args = argparse.Namespace(cacert="~/review-rot/",
                                      debug=False,
                                      format=None,
                                      insecure=False,
                                      reverse=False,
                                      duration=None,
                                      state=None,
                                      value=None)

        config_args = self.config['test8']['arguments']
        arguments = get_arguments(cli_args, config_args, self.choices)
        # Arguments 'cacert' is given in config.
        # But 'cacert' is also provided as CLI arguments. So CLI arguments has
        # higher precedence. Since insecure is False, ssl_verify should have
        # CA Certificate path

        ssl_result = arguments.get('ssl_verify') == \
            expanduser(expandvars(vars(cli_args).get('cacert')))
        self.assertTrue(ssl_result)
Пример #28
0
    def test_args_from_command_line(self):
        cli_args = argparse.Namespace(
            cacert=None,
            debug=True,
            format="json",
            insecure=True,
            reverse=True,
            duration=None,
            state=None,
            value=None,
        )

        config = self.config["test2"]
        arguments = get_arguments(
            cli_args, config, self.choices
        )
        # arguments must contains values from cli arguments

        debug_result = arguments.get("debug") == vars(cli_args).get("debug")
        format_result = arguments.get("format") == vars(cli_args).get("format")
        ssl_result = arguments.get("ssl_verify") is False
        reverse_result = arguments.get("reverse") == vars(cli_args).get(
            "reverse"
        )
        group_arguments = (
            arguments.get("state") is None
            and arguments.get("duration") is None
            and arguments.get("value") is None
        )

        self.assertTrue(
            debug_result
            and reverse_result
            and format_result
            and ssl_result
            and group_arguments
        )
Пример #29
0
    def test_args_from_command_line_except_format(self):
        cli_args = argparse.Namespace(
            cacert=None,
            debug=True,
            format=None,
            insecure=False,
            reverse=True,
            duration=None,
            state=None,
            value=None,
        )

        config = self.config["test3"]
        config_args = self.config["test3"]["arguments"]
        arguments = get_arguments(
            cli_args, config, self.choices
        )
        # All arguments must contains values from cli arguments except 'format'
        # It should be from config file

        debug_result = arguments.get("debug") is True
        format_result = arguments.get("format") == config_args.get("format")
        ssl_result = arguments.get("ssl_verify") is True
        reverse_result = arguments.get("reverse") is True
        group_arguments = (
            arguments.get("state") is None
            and arguments.get("duration") is None
            and arguments.get("value") is None
        )

        self.assertTrue(
            debug_result
            and reverse_result
            and format_result
            and ssl_result
            and group_arguments
        )
Пример #30
0
    def test_args_from_config(self):
        cli_args = argparse.Namespace(
            cacert=None,
            debug=False,
            format=None,
            insecure=False,
            reverse=False,
            duration=None,
            state=None,
            value=None,
        )

        config = self.config["test1"]
        config_args = self.config["test1"]["arguments"]
        arguments = get_arguments(
            cli_args, config, self.choices
        )
        # arguments must contains values from config arguments

        debug_result = arguments.get("debug") == config_args.get("debug")
        format_result = arguments.get("format") == config_args.get("format")
        ssl_result = arguments.get("ssl_verify") != config_args.get("insecure")
        reverse_result = arguments.get("reverse") == config_args.get("reverse")
        group_arguments = (
            arguments.get("state") is None
            and arguments.get("duration") is None
            and arguments.get("value") is None
        )

        self.assertTrue(
            debug_result
            and reverse_result
            and format_result
            and ssl_result
            and group_arguments
        )