Пример #1
0
    def test_guess_and_set_timeout(self) -> None:
        """
        Tests the method which let us guess the timeout to use.
        """

        config_loader = ConfigLoader()
        config_loader.set_custom_config({"lookup": {"timeout": 10.0}}).start()

        self.resolver_provider.guess_and_set_timeout()

        expected = 10.0
        actual = self.resolver_provider.get_timeout()

        self.assertEqual(expected, actual)
Пример #2
0
    def test_guess_and_set_delay_less_than_zero(self) -> None:
        """
        Tests the method which let us guess and set the delay between each
        queries.

        In this test, we check the case that the given delay is set to a value
        less than 0.
        """

        config_loader = ConfigLoader()
        config_loader.set_custom_config({"dns": {"delay": -3.0}}).start()

        # pylint: disable=unnecessary-lambda
        self.assertRaises(ValueError, lambda: self.query_tool.guess_and_set_delay())
Пример #3
0
    def test_guess_and_set_follow_nameserver_order(self) -> None:
        """
        Tests the method which let us guess and set the order of the server.
        """

        config_loader = ConfigLoader()
        config_loader.set_custom_config({"dns": {"follow_server_order": False}}).start()

        self.query_tool.guess_and_set_follow_nameserver_order()

        expected = False
        actual = self.query_tool.follow_nameserver_order

        self.assertEqual(expected, actual)

        del config_loader
Пример #4
0
    def test_guess_and_set_preferred_protocol_none(self) -> None:
        """
        Tests the method which let us guess and set the preferred protocol.

        In this test, we check the case that the given protocol is set to None.
        """

        config_loader = ConfigLoader()
        config_loader.set_custom_config({"dns": {"protocol": None}}).start()

        self.query_tool.guess_and_set_preferred_protocol()

        expected = self.query_tool.STD_PROTOCOL
        actual = self.query_tool.preferred_protocol

        self.assertEqual(expected, actual)
Пример #5
0
    def test_guess_and_set_preferred_protocol(self) -> None:
        """
        Tests the method which let us guess and set the preferred protocol.
        """

        config_loader = ConfigLoader()
        config_loader.set_custom_config({"dns": {"protocol": "HTTPS"}}).start()

        self.query_tool.guess_and_set_preferred_protocol()

        expected = "HTTPS"
        actual = self.query_tool.preferred_protocol

        self.assertEqual(expected, actual)

        del config_loader
Пример #6
0
    def test_guess_and_set_delay_none(self) -> None:
        """
        Tests the method which let us guess and set the delay between each
        queries.

        In this test, we check the case that the given delay is set to None.
        """

        config_loader = ConfigLoader()
        config_loader.set_custom_config({"dns": {"delay": None}}).start()

        self.query_tool.guess_and_set_delay()

        expected = self.query_tool.STD_DELAY
        actual = self.query_tool.delay

        self.assertEqual(expected, actual)
Пример #7
0
    def test_guess_and_set_trust_server(self) -> None:
        """
        Tests the method which let us guess and set the trust flag from the
        configuration file.
        """

        config_loader = ConfigLoader()
        config_loader.set_custom_config({"dns": {"trust_server": True}}).start()

        self.query_tool.guess_and_set_trust_server()

        expected = True
        actual = self.query_tool.trust_server

        self.assertEqual(expected, actual)

        del config_loader
Пример #8
0
    def test_guess_and_set_timeout(self) -> None:
        """
        Tests the method which let us guess and set the timeout from the
        configuration file.
        """

        config_loader = ConfigLoader()
        config_loader.set_custom_config({"lookup": {"timeout": 10.0}}).start()

        self.query_tool.guess_and_set_timeout()

        expected = 10.0
        actual = self.query_tool.query_timeout

        self.assertEqual(expected, actual)

        del config_loader
Пример #9
0
    def test_guess_and_set_follow_nameserver_order_none(self) -> None:
        """
        Tests the method which let us guess and set the order of the server.

        In this test, we check the case that the given value is set to None.
        """

        config_loader = ConfigLoader()
        config_loader.set_custom_config({"dns": {"follow_server_order": None}}).start()

        self.query_tool.guess_and_set_follow_nameserver_order()

        expected = self.query_tool.STD_FOLLOW_NAMESERVER_ORDER
        actual = self.query_tool.follow_nameserver_order

        self.assertEqual(expected, actual)

        del config_loader
Пример #10
0
    def test_guess_and_set_use_collection(self) -> None:
        """
        Tests the method that let us guess and set the collection from the
        configuration.
        """

        config_loader = ConfigLoader()
        config_loader.set_custom_config({
            "lookup": {
                "collection": True
            }
        }).start()

        self.checker.guess_and_set_use_collection()
        actual = self.checker.use_collection
        expected = True

        self.assertEqual(expected, actual)
Пример #11
0
    def test_guess_and_set_timeout_none(self) -> None:
        """
        Tests the method which let us guess and set the timeout from the
        configuration file.

        In this test, we check the case the None is given.
        """

        config_loader = ConfigLoader()
        config_loader.set_custom_config({"lookup": {"timeout": None}}).start()

        self.query_tool.guess_and_set_timeout()

        expected = self.query_tool.STD_TIMEOUT
        actual = self.query_tool.query_timeout

        self.assertEqual(expected, actual)

        del config_loader
Пример #12
0
    def test_guess_and_set_verify_certificate(self) -> None:
        """
        Tests the method which let us guess and set the certificate verification
        attribute from the configuration file.
        """

        config_loader = ConfigLoader()
        config_loader.set_custom_config({
            "verify_ssl_certificate": True
        }).start()

        self.query_tool.guess_and_set_verify_certificate()

        expected = True
        actual = self.query_tool.verify_certificate

        self.assertEqual(expected, actual)

        del config_loader
Пример #13
0
    def test_guess_and_set_trust_server_none(self) -> None:
        """
        Tests the method which let us guess and set the trust flag from the
        configuration file.

        In this case, we test the case that None or implicitly a non boolean
        value is given.
        """

        config_loader = ConfigLoader()
        config_loader.set_custom_config({"dns": {"trust_server": None}}).start()

        self.query_tool.guess_and_set_trust_server()

        expected = self.query_tool.STD_TRUST_SERVER
        actual = self.query_tool.trust_server

        self.assertEqual(expected, actual)

        del config_loader
Пример #14
0
    def test_guess_and_set_preferred_status_origin_not_str(self) -> None:
        """
        Tests the method which let us guess and set the preferred status origin.
        """

        config_loader = ConfigLoader()
        config_loader.set_custom_config({
            "collection": {
                "preferred_status_origin": None
            }
        }).start()

        self.query_tool.guess_and_set_preferred_status_origin()

        expected = "frequent"
        actual = self.query_tool.preferred_status_origin

        self.assertEqual(expected, actual)

        del config_loader
Пример #15
0
    def test_guess_and_set_url_base_not_str(self) -> None:
        """
        Tests the method which let us guess and set the URL base.
        """

        config_loader = ConfigLoader()
        config_loader.set_custom_config({
            "collection": {
                "url_base": False
            }
        }).start()

        self.query_tool.guess_and_set_url_base()

        expected = "http://localhost:8001"
        actual = self.query_tool.url_base

        self.assertEqual(expected, actual)

        del config_loader
Пример #16
0
    def test_guess_and_set_url_base(self) -> None:
        """
        Tests the method which let us guess and set the URL base.
        """

        config_loader = ConfigLoader()
        config_loader.set_custom_config({
            "collection": {
                "url_base": "https://example.org:8443"
            }
        }).start()

        self.query_tool.guess_and_set_url_base()

        expected = "https://example.org:8443"
        actual = self.query_tool.url_base

        self.assertEqual(expected, actual)

        del config_loader
Пример #17
0
    def test_guess_and_set_use_collection_not_boolean(self) -> None:
        """
        Tests the method that let us guess and set the collection from the
        configuration.

        In this case, we test the case that the given value is not a boolean.
        """

        config_loader = ConfigLoader()
        config_loader.set_custom_config({
            "lookup": {
                "collection": None
            }
        }).start()

        self.checker.guess_and_set_use_collection()
        actual = self.checker.use_collection
        expected = False

        self.assertEqual(expected, actual)

        del config_loader
Пример #18
0
class TestConfigLoader(unittest.TestCase):
    """
    Provides the test of our configuration loader.
    """
    def setUp(self) -> None:
        """
        Setups everything needed by the tests.
        """

        self.our_config = Box(copy.deepcopy(pyf_test_dataset.DEFAULT_CONFIG))

        self.default_config_file = tempfile.NamedTemporaryFile(delete=False)
        self.overwrite_config_file = tempfile.NamedTemporaryFile(delete=False)
        self.config_file = tempfile.NamedTemporaryFile(delete=False)

        self.merge_upstream = False

        self.config_loader = ConfigLoader()

        self.config_loader.merge_upstream = self.merge_upstream
        self.config_loader.path_to_default_config = self.default_config_file.name
        self.config_loader.path_to_overwrite_config = self.overwrite_config_file.name
        self.config_loader.path_to_config = self.config_file.name

    def tearDown(self) -> None:
        """
        Destroys everything initiated by the tests.
        """

        self.config_file.close()
        self.default_config_file.close()
        self.overwrite_config_file.close()

        os.unlink(self.config_file.name)
        os.unlink(self.default_config_file.name)
        os.unlink(self.overwrite_config_file.name)

        del self.our_config
        del self.default_config_file
        del self.config_file
        del self.merge_upstream
        del self.config_loader

    def test_is_already_loader(self) -> None:
        """
        Tests the method which let us know if the configuration was already
        loaded.
        """

        PyFunceble.storage.CONFIGURATION = self.our_config

        expected = True
        actual = self.config_loader.is_already_loaded()

        self.assertEqual(expected, actual)

    def test_is_not_already_loaded(self) -> None:
        """
        Tests the method which let us know if the configuration was already
        loaded for the case that it was not loaded yet.
        """

        expected = False
        actual = self.config_loader.is_already_loaded()

        self.assertEqual(expected, actual)

    def test_set_custom_config(self) -> None:
        """
        Tests the method which let us set the custom configuration to work with.
        """

        given = {"hello": "world"}
        expected = {"hello": "world"}

        self.config_loader.custom_config = given
        actual = self.config_loader.custom_config

        self.assertEqual(expected, actual)

    def test_set_custom_config_config_already_loaded(self) -> None:
        """
        Tests the method which let us set the custom configuration to work with.

        In this case, we want the loader to reload (itself). So we basically
        tests that the reload occurs.
        """

        self.default_config_file.write(
            yaml.dump(self.our_config.to_dict()).encode())
        self.default_config_file.seek(0)

        self.config_file.write(yaml.dump(self.our_config).encode())
        self.config_file.seek(0)

        PyFunceble.storage.CONFIGURATION = self.our_config

        given = {"hello": "world"}
        expected = {"hello": "world"}

        self.config_loader.custom_config = given
        actual = self.config_loader.custom_config

        self.assertEqual(expected, actual)

        expected = "world"
        actual = PyFunceble.storage.CONFIGURATION["hello"]

        self.assertEqual(expected, actual)

    def test_set_custom_config_config_already_loaded_merging_active(
            self) -> None:
        """
        Tests the method which let us set the custom configuration to work with.

        In this case, we want the loader to reload (itself). So we basically
        tests that the reload occurs. And that nothing has changed if the
        merging is authorized
        """

        self.default_config_file.write(
            yaml.dump(self.our_config.to_dict()).encode())
        self.default_config_file.seek(0)

        self.config_file.write(yaml.dump(self.our_config).encode())
        self.config_file.seek(0)

        self.config_loader.merge_upstream = True

        PyFunceble.storage.CONFIGURATION = copy.deepcopy(self.our_config)
        PyFunceble.storage.CONFIGURATION["cli_testing"]["display_mode"][
            "dots"] = True

        given = {"hello": "world"}
        expected = {"hello": "world"}

        self.config_loader.custom_config = given
        actual = self.config_loader.custom_config

        self.assertEqual(expected, actual)

        expected = "world"
        actual = PyFunceble.storage.CONFIGURATION["hello"]

        self.assertEqual(expected, actual)

        self.config_file.seek(0)

        expected = copy.deepcopy(self.our_config.to_dict())
        actual = yaml.safe_load(self.config_file)

        self.assertEqual(expected, actual)

    def test_set_custom_config_not_dict(self) -> None:
        """
        Tests the method which let us set the custom configuration to work with
        for the case that the given custom configuration is not a
        :py:class:`dict`.
        """

        given = "Hello, World!"

        self.assertRaises(TypeError,
                          lambda: self.config_loader.set_custom_config(given))

    def test_set_custom_config_previously_given(self) -> None:
        """
        Tests the method which let us set the custom configuration to work with
        for the case that the given custom configuration is not a
        :py:class:`dict`.
        """

        given = {"Hello": "World!"}
        given2 = {"World!": "Hello"}

        self.config_loader.set_custom_config(given)
        self.config_loader.set_custom_config(given2)

        expected = {"Hello": "World!", "World!": "Hello"}
        actual = self.config_loader.custom_config

        self.assertEqual(expected, actual)

    def test_set_merge_upstream(self) -> None:
        """
        Tests the method which let us authorize the merging of upstream inside
        the local one.
        """

        given = True
        expected = True

        self.config_loader.merge_upstream = given

        actual = self.config_loader.merge_upstream

        self.assertEqual(expected, actual)

    def test_set_merge_upstream_return(self) -> None:
        """
        Tests the method which let us authorize the merging of upstream inside
        the local one.

        In this case, we just want to be sure that the response is correct.
        """

        given = True

        actual = self.config_loader.set_merge_upstream(given)

        self.assertIsInstance(actual, ConfigLoader)

    def test_set_merge_upstream_not_bool(self) -> None:
        """
        Tests the method which let us authorize the merging of upstream inside
        the local one for the case that the given value is not a boolean.
        """

        given = "Hello, World!"

        self.assertRaises(
            TypeError,
            lambda: self.config_loader.set_merge_upstream(given),
        )

    def test_get_config_file_content(self) -> None:
        """
        Tests the method which let us get the content of the configuration file.
        """

        self.config_file.write(yaml.dump(self.our_config.to_dict()).encode())
        self.config_file.seek(0)

        expected = self.our_config.to_dict()

        actual = self.config_loader.get_config_file_content()

        self.assertEqual(expected, actual)

    def test_get_config_file_content_yaml_issue(self) -> None:
        """
        Tests the method which let us get the content of the configuration file.

        This case try to reproduce the issue we met because of my inattention.
        """

        self.default_config_file.write(
            yaml.dump(self.our_config.to_dict()).encode())
        self.default_config_file.seek(0)

        self.config_file.write(yaml.dump(self.our_config).encode())
        self.config_file.seek(0)

        expected = self.our_config.to_dict()

        actual = self.config_loader.get_config_file_content()

        self.assertEqual(expected, actual)

    def test_get_config_file_already_exists(self) -> None:
        """
        Tests the method which let us get the content of the configuration file
        for the case that the configuration was already loaded.
        """

        self.config_file.write(yaml.dump(self.our_config.to_dict()).encode())
        self.config_file.seek(0)

        PyFunceble.storage.CONFIGURATION = Box({"hello": "world"})

        expected = self.our_config.to_dict()
        actual = self.config_loader.get_config_file_content()

        self.assertEqual(expected, actual)

    def test_get_config_file_but_empty(self) -> None:
        """
        Tests the method which let us get the content of the configuration file
        for the case it is empty.
        """

        self.default_config_file.write(
            yaml.dump(self.our_config.to_dict()).encode())
        self.default_config_file.seek(0)

        self.config_file.write("".encode())
        self.config_file.seek(0)

        expected = self.our_config
        actual = self.config_loader.get_config_file_content()

        self.assertEqual(expected, actual)

    def test_get_configured_value_not_loaded(self) -> None:
        """
        Tests the method which let us get the configured value.

        In this test, we check for the case that configuration is not
        loaded yet.
        """

        given = "cli_testing.display_mode.colour"

        self.assertRaises(
            RuntimeError,
            lambda: self.config_loader.get_configured_value(given))

    def test_get_configured_value_not_found(self) -> None:
        """
        Tests the method which let us get the configured value.

        In this test, we check for the case that the wanted index does not
        exists.
        """

        PyFunceble.storage.CONFIGURATION = self.our_config

        given = "hello_world_hello"

        self.assertRaises(
            ValueError, lambda: self.config_loader.get_configured_value(given))

    def test_get_configured_value(self) -> None:
        """
        Tests the method which let us get the configured value.

        In this test, we check for the case that configuration is not
        loaded yet.
        """

        self.config_loader.set_custom_config(self.our_config).start()

        given = "cli_testing.testing_mode.syntax"
        actual = self.config_loader.get_configured_value(given)

        self.assertIsInstance(actual, bool)

    def test_start_and_destroy(self) -> None:
        """
        Tests the methods which loads, put everything together and destroy
        the configuration.
        """

        self.config_file.write(yaml.dump(self.our_config.to_dict()).encode())
        self.config_file.seek(0)

        given_custom = {"this_is_a_custom": "test"}
        self.config_loader.set_custom_config(given_custom)

        response = self.config_loader.start()

        self.assertIsInstance(response, ConfigLoader)

        expected_with_custom = dict(copy.deepcopy(self.our_config.to_dict()),
                                    **given_custom)
        self.assertEqual(
            expected_with_custom,
            PyFunceble.storage.CONFIGURATION,
        )

        expected_indexes = ["http_codes", "links"]

        for index in expected_indexes:
            # Tests if correctly stored.
            expected = copy.deepcopy(self.our_config[index])
            actual = getattr(PyFunceble.storage, index.upper())

            self.assertEqual(expected, actual)

        response = self.config_loader.destroy()

        self.assertIsInstance(response, ConfigLoader)

        expected_custom = dict()  # pylint: disable=use-dict-literal
        actual = self.config_loader.custom_config

        self.assertEqual(expected_custom, actual)

        expected_indexes = ["http_codes", "links"]
        expected = dict()  # pylint: disable=use-dict-literal

        for index in expected_indexes:
            actual = getattr(PyFunceble.storage, index.upper())

            self.assertEqual(expected, actual)

    def test_conditional_switch_autocontinue_ci_active(self) -> None:
        """
        Tests the method which let us switch some of our values based on
        some assumption.

        In this test, we check the the autocontinue is getting activated if we
        are under CI.
        """

        given = copy.deepcopy(self.our_config)
        given["cli_testing"]["ci"]["active"] = True
        given["cli_testing"]["autocontinue"] = False

        expected = copy.deepcopy(given)
        expected["cli_testing"]["autocontinue"] = True

        actual = self.config_loader.conditional_switch(given)

        self.assertEqual(expected, actual)

    def test_conditional_switch_autocontinue_ci_not_active(self) -> None:
        """
        Tests the method which let us switch some of our values based on
        some assumption.

        In this test, we check the the autocontinue is getting activated if we
        are under CI.
        """

        given = copy.deepcopy(self.our_config)
        given["cli_testing"]["ci"]["active"] = False
        given["cli_testing"]["autocontinue"] = False

        expected = copy.deepcopy(given)

        actual = self.config_loader.conditional_switch(given)

        self.assertEqual(expected, actual)

    def test_overwite_file_found(self) -> None:
        """
        Tests the loading of the configuration file for the case the an
        overwrite file is given.
        """

        given = {"Hello": "World!"}

        self.overwrite_config_file.write(yaml.dump(given).encode())
        self.overwrite_config_file.seek(0)

        self.config_file.write(yaml.dump(self.our_config.to_dict()).encode())
        self.config_file.seek(0)

        response = self.config_loader.start()

        self.assertIsInstance(response, ConfigLoader)

        expected_with_overwrite = dict(
            copy.deepcopy(self.our_config.to_dict()), **given)
        self.assertEqual(
            expected_with_overwrite,
            PyFunceble.storage.CONFIGURATION,
        )

        self.assertTrue("Hello" in PyFunceble.storage.CONFIGURATION)

    def test_overwite_file_found_but_empty(self) -> None:
        """
        Tests the loading of the configuration file for the case the an
        overwrite file is given but is empty.
        """

        given = {}

        self.overwrite_config_file.write(yaml.dump(given).encode())
        self.overwrite_config_file.seek(0)

        self.config_file.write(yaml.dump(self.our_config.to_dict()).encode())
        self.config_file.seek(0)

        response = self.config_loader.start()

        self.assertIsInstance(response, ConfigLoader)

        expected = self.our_config.to_dict()

        self.assertEqual(
            expected,
            PyFunceble.storage.CONFIGURATION,
        )

    def test_overwite_file_found_but_overwrote_by_custom(self) -> None:
        """
        Tests the loading of the configuration file for the case the an
        overwrite file is given, but the custom configuration is also given.

        When the custom configuration is given, it overwrites everything,
        including the overwrites file.
        """

        given = {"Hello": "World"}

        self.overwrite_config_file.write(yaml.dump(given).encode())
        self.overwrite_config_file.seek(0)

        given_custom = {"Hello": "Funilrys!"}
        self.config_loader.set_custom_config(given_custom)

        self.config_file.write(yaml.dump(self.our_config.to_dict()).encode())
        self.config_file.seek(0)

        response = self.config_loader.start()

        self.assertIsInstance(response, ConfigLoader)

        expected_with_custom = dict(copy.deepcopy(self.our_config.to_dict()),
                                    **given_custom)
        self.assertEqual(
            expected_with_custom,
            PyFunceble.storage.CONFIGURATION,
        )

        self.assertTrue(
            PyFunceble.storage.CONFIGURATION["Hello"] == "Funilrys!")
Пример #19
0
class TestCheckerWhoisUtils(unittest.TestCase):
    """
    Tests of the WHOIS utilities.
    """

    def setUp(self) -> None:
        """
        Setup everything needed for the tests.
        """

        self.config_loader = ConfigLoader()

    def tearDown(self) -> None:
        """
        Destroys everything needed for the tests.
        """

        del self.config_loader

    def test_get_whois_dataset_obj_no_config(self) -> None:
        """
        Tests of the function which let us get a new WHOIS dataset object.
        """

        self.assertRaises(RuntimeError, lambda: whois.get_whois_dataset_object())

    def test_get_whois_dataset_obj_csv(self) -> None:
        """
        Tests of the function which let us get a new WHOIS dataset object.

        In this case, we check the case that the CSV was declared.
        """

        self.config_loader.set_custom_config(
            {"cli_testing": {"db_type": "csv"}}
        ).start()

        expected = CSVWhoisDataset
        actual = whois.get_whois_dataset_object()

        self.assertIsInstance(actual, expected)

    def test_get_whois_dataset_obj_mariadb(self) -> None:
        """
        Tests of the function which let us get a new WHOIS dataset object.

        In this case, we check the case that the MariaDb was declared.
        """

        self.config_loader.set_custom_config(
            {"cli_testing": {"db_type": "mariadb"}}
        ).start()

        expected = MariaDBWhoisDataset
        actual = whois.get_whois_dataset_object()

        self.assertIsInstance(actual, expected)

    def test_get_whois_dataset_obj_mysql(self) -> None:
        """
        Tests of the function which let us get a new WHOIS dataset object.

        In this case, we check the case that the MySQL was declared.
        """

        self.config_loader.set_custom_config(
            {"cli_testing": {"db_type": "mysql"}}
        ).start()

        expected = MySQLWhoisDataset
        actual = whois.get_whois_dataset_object()

        self.assertIsInstance(actual, expected)

    def test_get_whois_dataset_obj_unknown(self) -> None:
        """
        Tests of the function which let us get a new WHOIS dataset object.

        In this case, we check the case that an unknown db type was declared.
        """

        self.config_loader.set_custom_config(
            {"cli_testing": {"db_type": "hello"}}
        ).start()

        self.assertRaises(ValueError, lambda: whois.get_whois_dataset_object())
Пример #20
0
class TestNameserver(unittest.TestCase):
    """
    Tests our nameserver "manager".
    """

    def setUp(self) -> None:
        """
        Setups everything needed for the tests.
        """

        self.config_loader = ConfigLoader()
        self.config_loader.start()

        self.nameserver_provider = Nameservers()

    def tearDown(self) -> None:
        """
        Destroys everything previously initiated.
        """

        del self.config_loader

    @staticmethod
    def fake_resolve_response(data: str) -> object:
        """
        Provides a fake resolve response to use.
        """

        return dataclasses.make_dataclass(
            "FakeResponse", [("address", str, dataclasses.field(default=data))]
        )

    @staticmethod
    def fake_resolver(_: str, rqtype: str):
        """
        Provides a fake resolution.
        """

        if rqtype == "A":
            return [
                TestNameserver.fake_resolve_response("192.168.1.1"),
                TestNameserver.fake_resolve_response("10.47.91.9"),
            ]

        return [
            TestNameserver.fake_resolve_response("fe80::6b01:9045:a42a:fb5f"),
            TestNameserver.fake_resolve_response("fe80::6b01:9049:a42a:fb5f"),
        ]

    def test_split_nameserver_from_port(self) -> None:
        """
        Tests the method which let us split the nameserver from the port.
        """

        given = "example.org:45"

        expected = ("example.org", 45)
        actual = self.nameserver_provider.split_nameserver_from_port(given)

        self.assertEqual(expected, actual)

    def test_split_nameserver_from_port_no_port(self) -> None:
        """
        Tests the method which let us split the nameserver from the port for the
        case that no port is given.
        """

        given = "example.org"

        expected = ("example.org", 53)
        actual = self.nameserver_provider.split_nameserver_from_port(given)

        self.assertEqual(expected, actual)

    def test_split_nameserver_from_port_ipv6_no_port(self) -> None:
        """
        Tests the method which let us split the nameserver from the port for the
        case that an IPv6 is given and no port.
        """

        given = pyf_test_dataset.VALID_IPV6[0]

        expected = (pyf_test_dataset.VALID_IPV6[0], 53)
        actual = self.nameserver_provider.split_nameserver_from_port(given)

        self.assertEqual(expected, actual)

    def test_split_nameserver_from_port_ipv6_with_port(self) -> None:
        """
        Tests the method which let us split the nameserver from the port for the
        case that an IPv6 is given and no port.
        """

        given = f"{pyf_test_dataset.VALID_IPV6[1]}:55"

        expected = (pyf_test_dataset.VALID_IPV6[1], 55)
        actual = self.nameserver_provider.split_nameserver_from_port(given)

        self.assertEqual(expected, actual)

    @unittest.mock.patch.object(dns.resolver.Resolver, "resolve")
    def test_get_ip_from_nameserver(self, resolver_patch) -> None:
        """
        Tests the method which let us get the IP from a nameserver.
        """

        resolver_patch.side_effect = self.fake_resolver

        given = "example.org"

        expected = [
            "192.168.1.1",
            "10.47.91.9",
            "fe80::6b01:9045:a42a:fb5f",
            "fe80::6b01:9049:a42a:fb5f",
        ]
        actual = self.nameserver_provider.get_ip_from_nameserver(given)

        self.assertEqual(expected, actual)

    @unittest.mock.patch.object(dns.resolver.Resolver, "resolve")
    def test_get_ip_from_nameserver_exceptions(self, resolver_patch) -> None:
        """
        Tests the method which let us get the IP from a nameserver for the
        case that only exceptions are given back.
        """

        resolver_patch.side_effect = dns.exception.DNSException("This is a test :-)")

        given = "example.org"

        expected = []
        actual = self.nameserver_provider.get_ip_from_nameserver(given)

        self.assertEqual(expected, actual)

    def test_get_ip_from_nameserver_not_valid_domain(self) -> None:
        """
        Tests the method which let us get the IP from a nameserver for the
        case that a private domain is given.
        """

        given = "example.funilrys"

        expected = ["example.funilrys"]
        actual = self.nameserver_provider.get_ip_from_nameserver(given)

        self.assertEqual(expected, actual)

    @unittest.mock.patch.object(dns.resolver.Resolver, "resolve")
    def test_set_nameservers(self, resolver_patch) -> None:
        """
        Tests the method which let us set the nameserver to work with.
        """

        resolver_patch.side_effect = self.fake_resolver

        given = ["example.org:53"]

        expected_nameserver = [
            "192.168.1.1",
            "10.47.91.9",
            "fe80::6b01:9045:a42a:fb5f",
            "fe80::6b01:9049:a42a:fb5f",
        ]
        expected_nameserver_port = {
            "192.168.1.1": 53,
            "10.47.91.9": 53,
            "fe80::6b01:9045:a42a:fb5f": 53,
            "fe80::6b01:9049:a42a:fb5f": 53,
        }

        self.nameserver_provider.set_nameservers(given)

        actual = self.nameserver_provider.get_nameservers()
        actual_nameserver_port = self.nameserver_provider.get_nameserver_ports()

        self.assertEqual(expected_nameserver, actual)
        self.assertEqual(expected_nameserver_port, actual_nameserver_port)

    @unittest.mock.patch.object(dns.resolver.Resolver, "resolve")
    def test_set_nameservers_through_init(self, resolver_patch) -> None:
        """
        Tests the method which let us set the nameserver from the contributor
        method.
        """

        resolver_patch.side_effect = self.fake_resolver

        given = ["example.org:53"]

        expected_nameserver = [
            "192.168.1.1",
            "10.47.91.9",
            "fe80::6b01:9045:a42a:fb5f",
            "fe80::6b01:9049:a42a:fb5f",
        ]
        expected_nameserver_port = {
            "192.168.1.1": 53,
            "10.47.91.9": 53,
            "fe80::6b01:9045:a42a:fb5f": 53,
            "fe80::6b01:9049:a42a:fb5f": 53,
        }

        nameserver_obj = Nameservers(nameserver=given)

        actual = nameserver_obj.get_nameservers()
        actual_nameserver_port = nameserver_obj.get_nameserver_ports()

        self.assertEqual(expected_nameserver, actual)
        self.assertEqual(expected_nameserver_port, actual_nameserver_port)

    def test_set_nameservers_https(self) -> None:
        """
        Tests the method which let us set the nameserver to work with for the
        case that a URL is given.
        """

        given = ["https://example.org/dns-query", "https://example.net/dns-query"]

        expected_nameserver = [
            "https://example.org/dns-query",
            "https://example.net/dns-query",
        ]
        expected_nameserver_port = {
            "https://example.org/dns-query": 443,
            "https://example.net/dns-query": 443,
        }

        self.nameserver_provider.protocol = "HTTPS"
        self.nameserver_provider.set_nameservers(given)

        actual = self.nameserver_provider.get_nameservers()
        actual_nameserver_port = self.nameserver_provider.get_nameserver_ports()

        self.assertEqual(expected_nameserver, actual)
        self.assertEqual(expected_nameserver_port, actual_nameserver_port)

    def test_set_nameservers_https_no_scheme(self) -> None:
        """
        Tests the method which let us set the nameserver to work with for the
        case that a URL is given.

        Now, we test for the case that no scheme is provided.
        """

        given = ["example.org/dns-query", "example.net/dns-query"]

        expected_nameserver = [
            "https://example.org/dns-query",
            "https://example.net/dns-query",
        ]
        expected_nameserver_port = {
            "https://example.org/dns-query": 443,
            "https://example.net/dns-query": 443,
        }

        self.nameserver_provider.protocol = "HTTPS"
        self.nameserver_provider.set_nameservers(given)

        actual = self.nameserver_provider.get_nameservers()
        actual_nameserver_port = self.nameserver_provider.get_nameserver_ports()

        self.assertEqual(expected_nameserver, actual)
        self.assertEqual(expected_nameserver_port, actual_nameserver_port)

    def test_set_nameservers_https_no_scheme_and_path(self) -> None:
        """
        Tests the method which let us set the nameserver to work with for the
        case that a URL is given.

        Now, we test for the case that no scheme and explicit path is provided.
        """

        given = ["example.org", "example.net"]

        expected_nameserver = [
            "https://example.org",
            "https://example.net",
        ]
        expected_nameserver_port = {
            "https://example.org": 443,
            "https://example.net": 443,
        }

        self.nameserver_provider.protocol = "HTTPS"
        self.nameserver_provider.set_nameservers(given)

        actual = self.nameserver_provider.get_nameservers()
        actual_nameserver_port = self.nameserver_provider.get_nameserver_ports()

        self.assertEqual(expected_nameserver, actual)
        self.assertEqual(expected_nameserver_port, actual_nameserver_port)

    def test_set_nameservers_not_list(self) -> None:
        """
        Tests the method which let us set the nameserver to work with for the
        case that the given value is not a list.
        """

        given = "Hello, World!"

        self.assertRaises(
            TypeError, lambda: self.nameserver_provider.set_nameservers(given)
        )

    def test_set_nameservers_empty_list(self) -> None:
        """
        Tests the method which let us set the nameserver to work with for the
        case that the given value is an empty list.
        """

        given = []

        self.assertRaises(ValueError, lambda: Nameservers().set_nameservers(given))

    @unittest.mock.patch.object(dns.resolver.Resolver, "resolve")
    def test_guess_and_set_nameservers(self, resolver_patch) -> None:
        """
        Tests the method which let us guess the nameserver to use.
        """

        resolver_patch.side_effect = self.fake_resolver

        given = ["example.org:53"]

        expected_nameserver = [
            "192.168.1.1",
            "10.47.91.9",
            "fe80::6b01:9045:a42a:fb5f",
            "fe80::6b01:9049:a42a:fb5f",
        ]
        expected_nameserver_port = {
            "192.168.1.1": 53,
            "10.47.91.9": 53,
            "fe80::6b01:9045:a42a:fb5f": 53,
            "fe80::6b01:9049:a42a:fb5f": 53,
        }

        self.config_loader.set_custom_config({"dns": {"server": given}}).start()

        self.nameserver_provider.guess_and_set_nameservers()

        actual = self.nameserver_provider.get_nameservers()
        actual_nameserver_port = self.nameserver_provider.get_nameserver_ports()

        self.assertEqual(expected_nameserver, actual)
        self.assertEqual(expected_nameserver_port, actual_nameserver_port)

        self.config_loader.set_custom_config({"dns": {"server": given[0]}}).start()

        self.nameserver_provider.guess_and_set_nameservers()

        actual = self.nameserver_provider.get_nameservers()
        actual_nameserver_port = self.nameserver_provider.get_nameserver_ports()

        self.assertEqual(expected_nameserver, actual)
        self.assertEqual(expected_nameserver_port, actual_nameserver_port)