Пример #1
0
def _1_wipe_test_db_tables(pytestconfig):
    """
    Wipe all tables before each test function
    """
    # Only run this if we are not running in CI mode
    if not pytestconfig.getoption("--ci-mode"):
        # Connect to the database
        config_local = Config()
        database = Database(
            config_local["db_host"],
            config_local["db_port"],
            config_local["db_name"],
            config_local["db_user"],
            config_local["db_password"],
        )
        db_session_local = database.session()

        # Wipe the database tables and reset the autoincrement counters
        meta = database.model.metadata
        for table in meta.tables.keys():
            db_session_local.execute(
                f"TRUNCATE TABLE {table.lower()} RESTART IDENTITY CASCADE;")

        # Commit the changes
        db_session_local.commit()
        db_session_local.close()
Пример #2
0
    def test_getting_from_env_none_raise_exception():
        # Delete the current values for testing
        for _, value in ENV_VARS.items():
            del os.environ[value]

        with pytest.raises(ConfigInitError):
            Config()
Пример #3
0
 def test_should_not_change_values_by_initiated_dict():
     base = {"eggs": 42, "spam": "ham"}
     my_dict = Config(base)
     base["eggs"] = 123
     assert my_dict.eggs == 42
     assert my_dict["eggs"] == 42
     assert my_dict.spam == "ham"
     assert my_dict["spam"] == "ham"
Пример #4
0
def tor_proxy():
    """
    Connect to the Tor container and return connection details
    """
    config_local = Config()
    tor_launcher = TorLauncher(config_local)
    proxy = (tor_launcher.ip_address, tor_launcher.socks_port)
    yield proxy
    tor_launcher.close()
Пример #5
0
def __http_proxies(tor_proxy):
    """
    Meant to be only used by `insert_domains_fetchers_relays_proxies` in order to
    prevent repeated calls to the external proxy API
    """
    config_local = Config()
    return get_random_http_proxy(config=config_local,
                                 tor_proxy=tor_proxy,
                                 country="US",
                                 multiple=True)
Пример #6
0
 def test_del_attr():
     my_dict = Config()
     my_dict["test"] = 123
     my_dict["python"] = 42
     del my_dict["test"]
     del my_dict.python
     with pytest.raises(KeyError):
         my_dict["test"]
     with pytest.raises(AttributeError):
         my_dict.python
Пример #7
0
    def test_repr(self):
        my_dict = Config()

        # Create and populate a regular dictionary
        real_dict = {}
        for key, _ in ENV_VARS.items():
            real_dict[key] = self.env_var_default_value
        real_dict["version"] = __version__

        assert repr(my_dict) == repr(real_dict)
Пример #8
0
    def __init__(self, verbose: Optional[bool] = False) -> None:
        """
        Initializes the submodules

        :param verbose: Verbose output option, defaults to False
        :type verbose: bool, optional
        """
        # Private class attributes
        self.__logger = logging.getLogger(__name__)
        self.__node_id: str = str(node_id())

        try:
            self.__config: Config = Config()

        except ConfigInitError:
            self.__logger.warning(
                "Could not initialize CAPTCHA Monitor since some configuration values are missing, exitting"
            )
            sys.exit(1)

        # Try connecting to the database 3 times
        for _ in range(3):
            try:
                self.__database: Database = Database(
                    self.__config["db_host"],
                    self.__config["db_port"],
                    self.__config["db_name"],
                    self.__config["db_user"],
                    self.__config["db_password"],
                    verbose,
                )
                break
            except DatabaseInitError:
                self.__logger.warning(
                    "Could not connect to the database, retrying")
                time.sleep(3)

        # Check if database connection was made
        if not hasattr_private(self, "__database"):
            self.__logger.warning(
                "Could not initialize CAPTCHA Monitor since I couldn't connect to the database, exitting"
            )
            sys.exit(1)

        # Obtain the session from database module
        self.__db_session = self.__database.session()

        try:
            if self.__db_session.query(MetaData).count == 0:
                insert_fixtures(self.__db_session, self.__config,
                                "metadata.json")
        except IntegrityError:
            # We can skip if we are inserting the same values again by mistake
            pass
Пример #9
0
def insert_domains_fetchers_relays_proxies(db_session, __http_proxies):
    """
    Insert fetchers before each test function
    """
    config_local = Config()

    relays = ["A53C46F5B157DD83366D45A8E99A244934A14C46"]
    domains = [
        "check.torproject.org",
        "duckduckgo.com",
        "stupid.urlextension",
        "api.ipify.org",
    ]
    proxy_country = "US"
    http_proxies = __http_proxies

    UpdateFetchers(config=config_local, db_session=db_session)

    for relay in relays:
        db_session.add(
            Relay(
                fingerprint=relay,
                ipv4_address="127.0.0.1",
                ipv4_exiting_allowed=True,
                ipv6_exiting_allowed=False,
            ))

    for domain in domains:
        db_session.add(
            Domain(
                domain=domain,
                supports_http=True,
                supports_https=True,
                supports_ftp=False,
                supports_ipv4=True,
                supports_ipv6=False,
                requires_multiple_requests=True,
            ))

    for proxy in http_proxies:
        db_session.add(
            Proxy(
                host=proxy[0],
                port=proxy[1],
                country=proxy_country,
                google_pass=False,
                anonymity="N",
                incoming_ip_different_from_outgoing_ip=False,
                ssl=True,
            ))

    db_session.commit()
Пример #10
0
def db_session():
    config_local = Config()
    database = Database(
        config_local["db_host"],
        config_local["db_port"],
        config_local["db_name"],
        config_local["db_user"],
        config_local["db_password"],
    )
    db_session_local = database.session()
    insert_fixtures(db_session_local, config_local, "metadata.json")
    yield db_session_local
    db_session_local.close()
Пример #11
0
def http_proxy(request, tor_proxy):
    """
    Find and return random HTTP proxies
    """
    config_local = Config()
    if hasattr(request, "param"):
        country = request.param.get("country", None)
        multiple = request.param.get("multiple", False)
        return get_random_http_proxy(
            config=config_local,
            tor_proxy=tor_proxy,
            country=country,
            multiple=multiple,
        )
    return get_random_http_proxy(config=config_local, tor_proxy=tor_proxy)
Пример #12
0
 def test_should_init_with_one_dict():
     my_dict = Config({"eggs": 42, "spam": "ham"})
     assert my_dict.eggs == 42
     assert my_dict["eggs"] == 42
     assert my_dict.spam == "ham"
     assert my_dict["spam"] == "ham"
Пример #13
0
 def test_getting_from_env(self):
     my_dict = Config()
     for key, _ in ENV_VARS.items():
         assert my_dict[key] == self.env_var_default_value
Пример #14
0
 def test_len_should_work_like_in_dict():
     my_dict = Config()
     my_dict["test"] = 123
     my_dict.python = 42
     assert len(my_dict) == 3 + len(ENV_VARS)
Пример #15
0
 def test_in_should_work_like_in_dict():
     my_dict = Config()
     my_dict["test"] = 123
     assert "test" in my_dict
     assert "bla" not in my_dict
Пример #16
0
def config():
    config_local = Config()
    return config_local
Пример #17
0
 def test_set_item():
     my_dict = Config()
     my_dict["test"] = 123
     assert my_dict["test"] == 123
     assert my_dict.test == 123