def setUp(self) -> None: """ Setups everything needed for the tests. """ self.config_loader = ConfigLoader() self.config_loader.start() self.nameserver_provider = Nameservers()
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())
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)
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
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)
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
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
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
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)
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
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)
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 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
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
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
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
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
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
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
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
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
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
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)
def setUp(self) -> None: """ Setup everything needed for the tests. """ self.config_loader = ConfigLoader()
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())
class TestUserAgentDataset(unittest.TestCase): """ Tests the user agent dataset interaction. """ 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) def tearDown(self) -> None: """ Destroys everything needed by the tests. """ self.get_content_patch.stop() del self.mock_get_content del self.tempfile del self.our_dataset del self.config_loader del self.user_agent_dataset def test_contains(self) -> None: """ Tests of the method which let us check if a given browser is into the dataset. """ given = "chrome" expected = True actual = given in self.user_agent_dataset self.assertEqual(expected, actual) def test_not_contains(self) -> None: """ Tests the method which let us check if a given platform is into the dataset. """ given = "vivaldi" expected = False actual = given in self.user_agent_dataset self.assertEqual(expected, actual) def test_get_all_from_browser(self) -> None: """ Tests the way to get every information of a given browser. """ given = "chrome" expected = copy.deepcopy(self.our_dataset[given]) actual = self.user_agent_dataset[given] self.assertEqual(expected, actual) def test_get_all_from_unknown_browser(self) -> None: """ Tests the way to get every information of a given browser. In this test, we try to get from something that does not exists. """ given = "vivaldi" expected = dict() # pylint: disable=use-dict-literal actual = self.user_agent_dataset[given] self.assertEqual(expected, actual) def test_is_supported_browser(self) -> None: """ Tests the method which let us check if a browser is supported. """ given = "chrome" expected = True actual = self.user_agent_dataset.is_supported_browser(given) self.assertEqual(expected, actual) def test_is_not_supported_browser(self) -> None: """ Tests teh method which let us check if a browser is supported for the case that the given browser is not supported. """ given = "vivaldi" expected = False actual = self.user_agent_dataset.is_supported_browser(given) self.assertEqual(expected, actual) def test_is_supported_browser_not_str(self) -> None: """ Tests the method which let us check if a browser is supported for the case the given browser not is not a string. """ given = ["Hello", "World"] self.assertRaises( TypeError, lambda: self.user_agent_dataset.is_supported_browser(given)) def test_is_supported(self) -> None: """ Tests the method which let us check if a browser and platform is supported. """ given_browser = "chrome" given_platform = "linux" expected = True actual = self.user_agent_dataset.is_supported(given_browser, given_platform) self.assertEqual(expected, actual) def test_is_supported_not_str_browser(self) -> None: """ Tests the method which let us check if a browser and platform is supported. In this test, we check the case that the given browser is not a string. """ given_browser = ["Hello", "World"] given_platform = "linux" self.assertRaises( TypeError, lambda: self.user_agent_dataset.is_supported( given_browser, given_platform), ) def test_is_supported_not_str_platform(self) -> None: """ Tests the method which let us check if a browser and platform is supported. In this test, we check the case that the given platform is not a string. """ given_browser = "chrome" given_platform = ["Hello", "Worl"] self.assertRaises( TypeError, lambda: self.user_agent_dataset.is_supported( given_browser, given_platform), ) def test_is_supported_unknown_browser(self) -> None: """ Tests the method which let us check if a browser and platform is supported. In this test, we check the case that the given browser is not known. """ given_browser = "vivaldi" given_platform = "linux" expected = False actual = self.user_agent_dataset.is_supported(given_browser, given_platform) self.assertEqual(expected, actual) def test_is_supported_unknown_platform(self) -> None: """ Tests the method which let us check if a browser and platform is supported. In this test, we check the case that the given platform is not known. """ given_browser = "chrome" given_platform = "bsd" expected = False actual = self.user_agent_dataset.is_supported(given_browser, given_platform) self.assertEqual(expected, actual) def test_is_supported_unknown_platform_and_browser(self) -> None: """ Tests the method which let us check if a browser and platform is supported. In this test, we check the case that the given platform and browser are not known. """ given_browser = "vivaldi" given_platform = "bsd" expected = False actual = self.user_agent_dataset.is_supported(given_browser, given_platform) self.assertEqual(expected, actual) def test_set_prefered(self) -> None: """ Tests the method which let us set our prefered browser and platform. """ given_browser = "chrome" given_platform = "win10" actual = self.user_agent_dataset.set_prefered(given_browser, given_platform) self.assertIsInstance(actual, UserAgentDataset) expected_platform = "win10" expected_browser = "chrome" actual_platform = self.user_agent_dataset.prefered_platform actual_browser = self.user_agent_dataset.prefered_browser self.assertEqual(expected_platform, actual_platform) self.assertEqual(expected_browser, actual_browser) def test_set_prefered_not_supported(self) -> None: """ Tests the method which let us set our prefered browser and platform. In this test, we check that an exception is correctly raised when the platform or browser is not supported. """ given_browser = "vivaldi" given_platform = "win10" self.assertRaises( ValueError, lambda: self.user_agent_dataset.set_prefered( given_browser, given_platform), ) given_browser = "vivaldi" given_platform = "bsd" self.assertRaises( ValueError, lambda: self.user_agent_dataset.set_prefered( given_browser, given_platform), ) def test_get_latest(self) -> None: """ Tests the method which let us get the latest user agent known. """ given_browser = "chrome" given_platform = "win10" expected = self.our_dataset[given_browser][given_platform] self.user_agent_dataset.set_prefered(given_browser, given_platform) actual = self.user_agent_dataset.get_latest() self.assertEqual(expected, actual) def test_get_latest_from_config(self) -> None: """ Tests the method which let us get the latest user agent known based on the settings from the configuration file. """ given_browser = "firefox" given_platform = "win10" self.config_loader.custom_config = { "user_agent": { "platform": given_platform, "browser": given_browser } } self.config_loader.start() expected = self.our_dataset[given_browser][given_platform] actual = self.user_agent_dataset.get_latest() self.assertEqual(expected, actual) def test_get_latest_from_config_custom(self) -> None: """ Tests the method which let us get the latest user agent known based on the settings from the configuration file. """ self.config_loader.custom_config = { "user_agent": { "custom": "Hello, World!" } } self.config_loader.start() expected = "Hello, World!" actual = self.user_agent_dataset.get_latest() self.assertEqual(expected, actual)
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)
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!")
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("")