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

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

        self.nameserver_provider = Nameservers()
Example #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())
Example #3
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)
Example #4
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
Example #5
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)
Example #6
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
Example #7
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
Example #8
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
Example #9
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)
Example #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)
Example #11
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
Example #12
0
    def test_guess_and_set_use_whois_db(self) -> None:
        """
        Tests the method which let us guess and set the value of the
        :code`use_whois_db` attribute.
        """

        config_loader = ConfigLoader()
        config_loader.custom_config = {"cli_testing": {"whois_db": False}}

        config_loader.start()

        self.checker.guess_and_set_use_whois_db()

        expected = False
        actual = self.checker.use_whois_db

        self.assertEqual(expected, actual)

        del config_loader
Example #13
0
    def test_guess_and_set_use_extra_rules(self) -> None:
        """
        Tests the method which let us guess and set the value of the
        :code`use_extra_rules` attribute.
        """

        config_loader = ConfigLoader()
        config_loader.custom_config = {"lookup": {"special": False}}

        config_loader.start()

        self.checker.guess_and_set_use_extra_rules()

        expected = False
        actual = self.checker.use_extra_rules

        self.assertEqual(expected, actual)

        del config_loader
Example #14
0
    def test_guess_and_set_use_reputation_lookup(self) -> None:
        """
        Tests the method which let us guess and set the value of the
        :code`use_reputation_lookup` attribute.
        """

        config_loader = ConfigLoader()
        config_loader.custom_config = {"lookup": {"reputation": True}}

        config_loader.start()

        self.checker.guess_and_set_use_reputation_lookup()

        expected = True
        actual = self.checker.use_reputation_lookup

        self.assertEqual(expected, actual)

        del config_loader
Example #15
0
    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
Example #16
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
Example #17
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
Example #18
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
Example #19
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
Example #20
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
Example #21
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
Example #22
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
Example #23
0
    def setUp(self) -> None:
        """
        Setup everything needed for the tests.
        """

        self.config_loader = ConfigLoader()
Example #24
0
if not env_var_helper.set_name("PYFUNCEBLE_WORKERS_DATA_DIR").exists():
    raise RuntimeError(
        "Could not find PYFUNCEBLE_WORKERS_DATA_DIR environment variable.")

pyfunceble_webworker.storage.CONFIG_DIRECTORY = env_var_helper.get_value()

PyFunceble.storage.CONFIG_DIRECTORY = os.path.join(
    pyfunceble_webworker.storage.CONFIG_DIRECTORY,
    secrets.token_hex(8),
)

DirectoryHelper(PyFunceble.storage.CONFIG_DIRECTORY).create()
DirectoryHelper(pyfunceble_webworker.storage.CONFIG_DIRECTORY).create()

file_helper = FileHelper()
pyfunceble_config_loader = ConfigLoader()

if file_helper.set_path(
        os.path.join(
            pyfunceble_webworker.storage.CONFIG_DIRECTORY,
            assets_defaults.OVERWRITE_CONFIG_FILE,
        )).exists():
    local = DictHelper().from_yaml_file(file_helper.path)

    if local:
        pyfunceble_config_loader.custom_config = local
    else:
        pyfunceble_config_loader.custom_config = dict()
else:
    file_helper.write("")
Example #25
0
    def setUp(self) -> None:
        """
        Setups everything needed by the tests.
        """

        self.config_loader = ConfigLoader()

        self.tempfile = tempfile.NamedTemporaryFile()

        self.our_dataset = {
            "chrome": {
                "linux":
                "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 "
                "(KHTML, like Gecko) Chrome/77.0.3865.116 "
                "Safari/537.36 Edg/77.11.4.5118",
                "macosx":
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) "
                "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4364.0 "
                "Safari/537.36",
                "win10":
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4361.0 "
                "Safari/537.36",
            },
            "edge": {
                "linux":
                None,
                "macosx":
                None,
                "win10":
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 "
                "Safari/537.36 Edge/18.17763/5.9.7 (Linux;Android 10) "
                "ExoPlayerLib/2.9.6",
            },
            "firefox": {
                "linux":
                "Mozilla/5.0 (Linux x86_64; en-US) Gecko/20130401 "
                "Firefox/82.4",
                "macosx":
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0; "
                "en-US) Gecko/20100101 Firefox/74.7",
                "win10":
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) "
                "Gecko/20100101 Firefox/84.0/8mqDiPuL-36",
            },
            "ie": {
                "linux":
                None,
                "macosx":
                None,
                "win10":
                "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; "
                "Win64; x64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR "
                "2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Tablet "
                "PC 2.0; wbx 1.0.0; wbxapp 1.0.0)",
            },
            "opera": {
                "linux":
                "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 "
                "(KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 "
                "OPR/73.0.3856.284",
                "macosx":
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) "
                "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 "
                "Safari/537.36 OPR/72.0.3815.400",
                "win10":
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 "
                "Safari/537.36 OPR/73.0.3856.284 (Edition avira-2)",
            },
            "safari": {
                "linux":
                "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 "
                "(KHTML, like Gecko) Version/4.0 Chrome/70.0.3538.110 "
                "Safari/537.36 SputnikBrowser/1.2.5.158",
                "macosx":
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) "
                "AppleWebKit/600.8.9 (KHTML, like Gecko) Version/9.0.3 "
                "Safari/601.4.4",
                "win10":
                None,
            },
        }

        self.tempfile.write(json.dumps(self.our_dataset).encode())
        self.tempfile.seek(0)

        self.user_agent_dataset = UserAgentDataset()
        self.user_agent_dataset.source_file = self.tempfile.name

        self.get_content_patch = unittest.mock.patch.object(
            DatasetBase, "get_content")
        self.mock_get_content = self.get_content_patch.start()
        self.mock_get_content.return_value = copy.deepcopy(self.our_dataset)