Esempio n. 1
0
    def test_several_folders(self):
        """
        Searches for sources in several folders with
        valid and invalid proxy source classes
        """
        source_path = 'test/sample_proxysources/'
        test_data = [
            ('valid_metadata', 1, 3),
            ('no_metadata', 0, 0),
            ('incomplete_metadata', 0, 0),
            ('two_classes_one_file', 0, 0),
            ('no_inheritance', 0, 0),
        ]

        for name, valid_classes, valid_types in test_data:
            path = source_path + name
            os.environ['PROXY_PATH'] = path

            m = ProxySourceManager()
            if valid_classes:
                inst = m.instance(name)
                types = inst.metadata['type']
                for t in types:
                    self.assertEqual(len(m.proxies_per_type[t]), valid_classes)

            self.assertEqual(type(m.proxies_per_type), dict)
            self.assertEqual(len(m.proxies_per_type), valid_types)
Esempio n. 2
0
    def test_manager_instances_success(self):
        """
        Tests return of instance method when requesting a loaded source classes
        """
        source_name = 'valid_metadata'
        path = 'test/sample_proxysources/' + source_name
        os.environ['PROXY_PATH'] = path

        m = ProxySourceManager()
        inst = m.instance(source_name)
        self.assertEqual(inst.__class__.__name__, ValidMetadataSource.__name__)
Esempio n. 3
0
    def test_remove_invalid_source_valid_type(self):
        """
        Test source removal from manager memory with an invalid proxy source
        and a valid proxy type
        """
        path = 'test/sample_proxysources/valid_metadata'
        os.environ['PROXY_PATH'] = path
        types = ValidMetadataSource.metadata.get('type')

        m = ProxySourceManager()
        for t in types:
            m.remove_source(source_name='not_the_correct_name', proxy_type=t)
            self.assertEqual(len(m.proxies_per_type.get(t)), 1)
Esempio n. 4
0
    def test_remove_valid_source_valid_type(self):
        """
        Test source removal from manager memory, happy path
        """
        name = 'valid_metadata'
        path = 'test/sample_proxysources/valid_metadata'
        os.environ['PROXY_PATH'] = path
        types = ValidMetadataSource.metadata.get('type')

        m = ProxySourceManager()

        for t in types:
            m.remove_source(source_name=name, proxy_type=t)
            self.assertEqual(len(m.proxies_per_type.get(t)), 0)
Esempio n. 5
0
    def test_manager_instances_not_found(self):
        """
        Tests response when requesting the instance of a source class
        that isn't loaded
        """
        source_name = 'no_metadata'
        path = 'test/sample_proxysources/' + source_name
        os.environ['PROXY_PATH'] = path
        expected_stdout = 'The source doesn\'t exist'

        m = ProxySourceManager()
        with self.assertRaises(InvalidProxySource) as err:
            m.instance(source_name)
        self.assertEqual(expected_stdout, str(err.exception))
Esempio n. 6
0
    def test_remove_valid_source_invalid_type(self):
        """
        Test source removal from manager memory with an invalid proxy type and
        a valid source
        """
        name = 'valid_metadata'
        path = 'test/sample_proxysources/' + name
        os.environ['PROXY_PATH'] = path
        incorrect_type = 'invalid_proxy_type'
        correct_types = ValidMetadataSource.metadata.get('type')

        m = ProxySourceManager()
        m.remove_source(source_name=name, proxy_type=incorrect_type)
        for t in correct_types:
            self.assertEqual(len(m.proxies_per_type.get(t)), 1)
Esempio n. 7
0
import random
from fake_proxy.core.proxysourcemanager import ProxySourceManager
from fake_proxy.core.exceptions import EmptyResultsException
from fake_proxy.core.exceptions import ProxyTypeError

m = ProxySourceManager()


def proxy_sources(proxy_type=[]):
    """
    Returns a list with the names of the sources from where the library can
    fetch proxies
    : param proxy_type: string or list of strings with the type of proxy
    : return: list with the names of the proxy sources the library can handle
    """
    proxy_type = format_proxy_type(proxy_type)
    sources = {}
    for t in proxy_type:
        if t in m.proxies_per_type:
            sources[t] = m.proxies_per_type[t]
    return sources


def get(amount=1, proxy_type=[]):
    """
    Returns a certain amount of proxies of the given type. If no type is given,
    returns of random types.
    : param amount: integer, amount of proxies to request
    : param proxy_type: string or list of strings with types of
    proxies to return
    :return: list with proxies