def multi_proxy_creation_test():
    """
    2 bad and one good proxy, the good one should be created
    """
    config = [
        {
            'id': 'proxy_id',
            'class': 'jormungandr.realtime_schedule.timeo.Timeo',
            'args': {
                'service_url': 'http://custom_url.com',
                'timezone': 'Europe/Paris',
                'service_args': {
                    'serviceID': 'custom_id',
                    'EntityID': 'custom_entity',
                    'Media': 'custom_media'
                }
            }
        },
        {
            'id': 'wrong',
            'class': 'wrong_class',
            'args': {}
        },
        {
            # one with no id, shouldn't be created
            'class': 'wrong_class',
            'args': {}
        }
    ]
    manager = RealtimeProxyManager(config)
    assert manager.get('proxy_id') is not None
    assert manager.get('wrong') is None
def realtime_proxy_creation_test():
    """
    simple timeo proxy creation
    """
    config = [{
        'id': 'proxy_id',
        'class': 'jormungandr.realtime_schedule.timeo.Timeo',
        'args': {
            'timezone': 'Europe/Paris',
            'service_url': 'http://custom_url.com',
            'service_args': {
                'serviceID': 'custom_id',
                'EntityID': 'custom_entity',
                'Media': 'custom_media'
            }
        }
    }]

    manager = RealtimeProxyManager(config)

    timeo = manager.get('proxy_id')

    assert timeo
    timeo.service_url = 'http://custom_url.com'
    timeo.service_args = {
        'serviceID': 'custom_id',
        'EntityID': 'custom_entity',
        'Media': 'custom_media'
    }
def wrong_argument_test():
    """
    test with a timeo proxy, but without the mandatory param 'service_url'
    No error is returned, but the proxy is not created
    """
    config = [
        {
            'id': 'proxy_id',
            'class': 'jormungandr.realtime_schedule.timeo.Timeo',
            'args': {
                'timezone': 'Europe/Paris',
                'service_args': {'serviceID': 'custom_id', 'EntityID': 'custom_entity', 'Media': 'custom_media'},
            },
        }
    ]

    manager = RealtimeProxyManager(config)
    assert manager.get('proxy_id') is None
def wrong_realtime_proxy_class_test():
    """
    test with a unknown class
    No error is returned, but the proxy is not created
    """
    config = [
        {
            'id': 'proxy_id',
            'class': 'wrong_class.bob',
            'args': {
                'service_url': 'http://custom_url.com',
                'service_args': {'serviceID': 'custom_id', 'EntityID': 'custom_entity', 'Media': 'custom_media'},
            },
        }
    ]

    manager = RealtimeProxyManager(config)
    assert manager.get('proxy_id') is None
def proxy_only_database_test():
    def provider():
        json = {
            "navitia_service": 'realtime_proxies',
            "klass": 'jormungandr.realtime_schedule.siri.Siri',
            "args": {
                "requestor_ref": "AA_Siri_TAO",
                "service_url": "http://siri.com",
                "timeout": 1
            },
        }
        return [ExternalService(id='Siri_TAO', json=json)]

    manager = RealtimeProxyManager([],
                                   MockInstance(),
                                   providers_getter=provider)
    assert len(manager._realtime_proxies) == 0
    assert len(manager._realtime_proxies_legacy) == 0
    assert len(manager.get_all_realtime_proxies()) == 0

    manager.update_config()

    assert len(manager._realtime_proxies_legacy) == 0

    assert len(manager._realtime_proxies) == 1
    assert 'Siri_TAO' in manager._realtime_proxies
    assert len(manager.get_all_realtime_proxies()) == 1
def multi_proxy_conf_file_and_database_test():
    manager = RealtimeProxyManager(
        All_realtime_proxies,
        MockInstance(),
        providers_getter=mock_get_realtime_proxies_from_db)
    assert len(manager._realtime_proxies) == 0
    assert len(manager._realtime_proxies_legacy) == 5
    for rt_proxy in [
            "clever_age_TBC", "SytralRT", "Timeo_Amelys", "Siri_TAO",
            "siri_stif"
    ]:
        assert rt_proxy in manager._realtime_proxies_legacy

    manager.update_config()

    assert len(manager._realtime_proxies) == 5
    for rt_proxy in [
            "clever_age_TBC", "SytralRT", "Timeo_Amelys", "Siri_TAO",
            "siri_stif"
    ]:
        assert rt_proxy in manager._realtime_proxies

    assert len(manager._realtime_proxies_legacy) == 0
def proxy_conf_file_and_database_same_id_test():
    def provider():
        json = {
            "navitia_service": 'realtime_proxies',
            "klass": 'jormungandr.realtime_schedule.sytral.Sytral',
            "args": {
                "service_url": "http://sytralrt.com",
                "timeout": 10,
                "object_id_tag": "source"
            },
        }
        return [ExternalService(id='SytralRT', json=json)]

    config = [{
        "object_id_tag": "source",
        "class": "jormungandr.realtime_schedule.sytral.Sytral",
        "args": {
            "service_url": "http://sytralrt.com",
            "timeout": 10
        },
        "id": "SytralRT",
    }]
    manager = RealtimeProxyManager(config,
                                   MockInstance(),
                                   providers_getter=provider)
    assert len(manager._realtime_proxies) == 0
    assert len(manager._realtime_proxies_legacy) == 1
    assert 'SytralRT' in manager._realtime_proxies_legacy

    manager.update_config()

    assert len(manager._realtime_proxies) == 1
    assert 'SytralRT' in manager._realtime_proxies
    assert len(manager._realtime_proxies_legacy) == 0

    assert len(manager.get_all_realtime_proxies()) == 1
def wrong_timezone_test():
    """
    test with a timeo proxy, but with a wrong timezone
    an error should be raised
    """
    config = [
        {
            'id': 'proxy_id',
            'class': 'jormungandr.realtime_schedule.timeo.Timeo',
            'args': {
                'timezone': 'bobette',
                'service_url': 'http://custom_url.com',
                'service_args': {'serviceID': 'custom_id', 'EntityID': 'custom_entity', 'Media': 'custom_media'},
            },
        }
    ]

    with pytest.raises(pytz.UnknownTimeZoneError):
        RealtimeProxyManager(config)  # should raise an Exception
def update_proxy_database_test():
    def provider():
        json = {
            "navitia_service": 'realtime_proxies',
            "klass": 'jormungandr.realtime_schedule.siri.Siri',
            "args": {
                "requestor_ref": "AA_Siri_TAO",
                "service_url": "http://siri.com",
                "timeout": 1
            },
        }
        external_service = ExternalService(id='Siri_TAO', json=json)
        external_service.created_at = datetime.datetime.utcnow(
        ) - datetime.timedelta(hours=2)
        return [external_service]

    manager = RealtimeProxyManager([],
                                   MockInstance(),
                                   providers_getter=provider,
                                   update_interval=-1)
    assert len(manager._realtime_proxies) == 0
    assert len(manager._realtime_proxies_legacy) == 0
    assert len(manager.get_all_realtime_proxies()) == 0

    manager.update_config()

    assert len(manager._realtime_proxies_legacy) == 0

    assert len(manager._realtime_proxies) == 1
    assert 'Siri_TAO' in manager._realtime_proxies
    assert manager._realtime_proxies[
        "Siri_TAO"].service_url == 'http://siri.com'
    assert manager._realtime_proxies["Siri_TAO"].timeout == 1
    assert len(manager.get_all_realtime_proxies()) == 1

    # Update Url and timeout values
    def update_provider():
        json = {
            "navitia_service": 'realtime_proxies',
            "klass": 'jormungandr.realtime_schedule.siri.Siri',
            "args": {
                "requestor_ref": "AA_Siri_TAO",
                "service_url": "http://new_url_siri.com",
                "timeout": 10
            },
        }
        external_service = ExternalService(id='Siri_TAO', json=json)
        external_service.updated_at = datetime.datetime.utcnow()
        return [external_service]

    manager._realtime_proxies_getter = update_provider
    manager.update_config()

    assert len(manager._realtime_proxies_legacy) == 0

    assert len(manager._realtime_proxies) == 1
    assert 'Siri_TAO' in manager._realtime_proxies
    assert manager._realtime_proxies[
        "Siri_TAO"].service_url == 'http://new_url_siri.com'
    assert manager._realtime_proxies["Siri_TAO"].timeout == 10
    assert len(manager.get_all_realtime_proxies()) == 1