def test_allowed(self): api_key = ApiKeyFactory.build(allow_locate=True, allow_transfer=True) assert api_key.allowed('locate') assert api_key.allowed('region') assert api_key.allowed('submit') assert api_key.allowed('transfer') assert api_key.allowed('unknown') is None assert not ApiKeyFactory.build(allow_locate=None).allowed('locate') assert not ApiKeyFactory.build(allow_locate=False).allowed('locate') assert not ApiKeyFactory.build(allow_transfer=None).allowed('transfer')
def test_allowed(self): api_key = ApiKeyFactory.build(allow_locate=True, allow_transfer=True) assert api_key.allowed('locate') assert api_key.allowed('region') assert api_key.allowed('submit') assert api_key.allowed('transfer') assert api_key.allowed('unknown') is None assert not ApiKeyFactory.build(allow_locate=None).allowed('locate') assert not ApiKeyFactory.build(allow_locate=False).allowed('locate') assert not ApiKeyFactory.build(allow_transfer=None).allowed('transfer')
def test_api_key_disallows(self): api_key = ApiKeyFactory.build(allow_fallback=False) cells = CellFactory.build_batch(2) wifis = WifiShardFactory.build_batch(2) query = self.model_query(cells=cells, wifis=wifis, api_key=api_key) self.check_should_search(query, False)
def setUp(self): super(TestSource, self).setUp() self.api_key = ApiKeyFactory.build(allow_fallback=True) self.fallback_model = DummyModel(lat=51.5366, lon=0.03989, radius=1500.0) self.fallback_result = { 'location': { 'lat': self.fallback_model.lat, 'lng': self.fallback_model.lon, }, 'accuracy': float(self.fallback_model.radius), 'fallback': 'lacf', } self.fallback_cached_result = floatjson.float_dumps({ 'lat': self.fallback_model.lat, 'lon': self.fallback_model.lon, 'accuracy': float(self.fallback_model.radius), 'fallback': 'lacf', })
def test_api_key_disallows(self): api_key = ApiKeyFactory.build(allow_fallback=False) cells = CellShardFactory.build_batch(2) wifis = WifiShardFactory.build_batch(2) query = self.model_query(cells=cells, wifis=wifis, api_key=api_key) self.check_should_search(query, False)
def test_no_log(self, geoip_db, stats): api_key = ApiKeyFactory.build(valid_key=None) self._make_query(geoip_db, stats, self._make_result(), api_key=api_key, api_type='locate') stats.check(total=0)
def test_no_api_key_shortname(self): api_key = ApiKeyFactory.build(shortname=None, log_locate=True) cell = CellFactory.build() self._make_query(api_key=api_key, cell=[cell]) self.check_stats(counter=[ ('locate.query', ['key:%s' % api_key.valid_key, 'region:none', 'geoip:false', 'cell:one', 'wifi:none']), ])
def test_no_api_key_shortname(self): api_key = ApiKeyFactory.build(shortname=None, log_locate=True) cell = CellShardFactory.build() self._make_query(api_key=api_key, cell=[cell]) self.check_stats(counter=[ ('locate.query', ['key:%s' % api_key.valid_key, 'region:none', 'geoip:false', 'cell:one', 'wifi:none']), ])
def setUpClass(cls): super(SourceTest, cls).setUpClass() cls.api_key = ApiKeyFactory.build(valid_key='key') cls.source = cls.TestSource( geoip_db=cls.geoip_db, raven_client=cls.raven_client, redis_client=cls.redis_client, stats_client=cls.stats_client, )
def test_store_sample(self): api_key = ApiKeyFactory.build(store_sample_locate=None, store_sample_submit=None) assert api_key.store_sample('locate') is False assert api_key.store_sample('submit') is False assert api_key.store_sample('region') is False assert api_key.store_sample('transfer') is False api_key = ApiKeyFactory.build(store_sample_locate=0, store_sample_submit=100) assert api_key.store_sample('locate') is False assert api_key.store_sample('submit') is True api_key = ApiKeyFactory.build(store_sample_locate=50) results = [] for i in range(20): results.append(api_key.store_sample('locate')) assert True in results assert False in results
def _setup_table_contents(conn): # Avoid import cycle from ichnaea.tests.factories import ApiKeyFactory conn.execute(text('DELETE FROM api_key')) conn.execute(text('DELETE FROM export_config')) key = ApiKeyFactory.build(valid_key='test') state = dict(key.__dict__) del state['_sa_instance_state'] conn.execute(key.__table__.insert().values(state))
def setUpClass(cls): super(SourceTest, cls).setUpClass() cls.api_key = ApiKeyFactory.build(shortname='key') cls.source = cls.TestSource( settings={'foo': '1'}, geoip_db=cls.geoip_db, raven_client=cls.raven_client, redis_client=cls.redis_client, stats_client=cls.stats_client, )
def _setup_table_contents(conn): # Avoid import cycle from ichnaea.tests.factories import ApiKeyFactory conn.execute(text('DELETE FROM api_key')) conn.execute(text('DELETE FROM export_config')) key = ApiKeyFactory.build(valid_key='test') state = dict(key.__dict__) del state['_sa_instance_state'] conn.execute(key.__table__.insert().values(state))
def setUp(self): super(BaseSourceTest, self).setUp() self.api_key = ApiKeyFactory.build(shortname='test') self.source = self.TestSource( settings=self.settings, geoip_db=self.geoip_db, raven_client=self.raven_client, redis_client=self.redis_client, stats_client=self.stats_client, )
def setUp(self): super(BaseSourceTest, self).setUp() self.api_key = ApiKeyFactory.build(shortname='test') self.source = self.TestSource( settings=self.settings, geoip_db=self.geoip_db, raven_client=self.raven_client, redis_client=self.redis_client, stats_client=self.stats_client, )
def setUpClass(cls): super(SourceTest, cls).setUpClass() cls.api_key = ApiKeyFactory.build(shortname='key', log=True) cls.source = cls.TestSource( settings={'foo': '1'}, geoip_db=cls.geoip_db, raven_client=cls.raven_client, redis_client=cls.redis_client, stats_client=cls.stats_client, )
def make_query(self, geoip_db, http_session, session, stats, **kw): api_key = kw.pop( 'api_key', ApiKeyFactory.build(valid_key='test', allow_fallback=True)) return Query(api_key=api_key, api_type=self.api_type, session=session, http_session=http_session, geoip_db=geoip_db, stats_client=stats, **kw)
def make_query(self, geoip_db, http_session, session, stats, **kw): api_key = kw.pop( 'api_key', ApiKeyFactory.build(valid_key='test', allow_fallback=True)) return Query( api_key=api_key, api_type=self.api_type, session=session, http_session=http_session, geoip_db=geoip_db, stats_client=stats, **kw)
def _setup_table_data(engine): # Avoid import cycle from ichnaea.tests.factories import ApiKeyFactory with engine.connect() as conn: with conn.begin() as trans: conn.execute(text("DELETE FROM api_key")) conn.execute(text("DELETE FROM export_config")) key = ApiKeyFactory.build(valid_key="test") state = dict(key.__dict__) del state["_sa_instance_state"] conn.execute(key.__table__.insert().values(state)) trans.commit()
def test_api_key_disallows(self, geoip_db, http_session, session, source, stats): api_key = ApiKeyFactory.build(allow_fallback=False) cells = CellShardFactory.build_batch(2) wifis = WifiShardFactory.build_batch(2) query = self.model_query(geoip_db, http_session, session, stats, cells=cells, wifis=wifis, api_key=api_key) self.check_should_search(source, query, False)
def _make_query(self, geoip_db, stats, source, results, api_key=None, api_type='locate', blue=(), cell=(), wifi=(), **kw): query = Query( api_key=api_key or ApiKeyFactory.build(valid_key='test'), api_type=api_type, blue=self.blue_model_query(blue), cell=self.cell_model_query(cell), wifi=self.wifi_model_query(wifi), geoip_db=geoip_db, stats_client=stats, **kw) query.emit_source_stats(source, results) return query
def _search(self, data_queues, geoip_db, raven, redis, stats, session, klass, **kw): query = Query( api_key=ApiKeyFactory.build(valid_key='test'), api_type='locate', session=session, stats_client=stats, **kw) searcher = klass( geoip_db=geoip_db, raven_client=raven, redis_client=redis, stats_client=stats, data_queues=data_queues, ) return searcher.search(query)
def test_rate_limit_allow(self, geoip_db, http_session, session, source, stats): api_key = ApiKeyFactory.build(allow_fallback=True) cell = CellShardFactory() with requests_mock.Mocker() as mock_request: mock_request.register_uri('POST', requests_mock.ANY, json=self.fallback_result) for _ in range(api_key.fallback_ratelimit): query = self.model_query(geoip_db, http_session, session, stats, cells=[cell]) results = source.search(query) self.check_model_results(results, [self.fallback_model])
def setUpClass(cls): super(BaseSourceTest, cls).setUpClass() cls.api_key = ApiKeyFactory.build(valid_key='test') bhutan = cls.geoip_data['Bhutan'] cls.bhutan_model = DummyModel( lat=bhutan['latitude'], lon=bhutan['longitude'], radius=bhutan['radius'], code=bhutan['region_code'], name=bhutan['region_name'], ip=bhutan['ip']) london = cls.geoip_data['London'] cls.london_model = DummyModel( lat=london['latitude'], lon=london['longitude'], radius=london['radius'], code=london['region_code'], name=london['region_name'], ip=london['ip'])
def setUpClass(cls): super(BaseSourceTest, cls).setUpClass() cls.api_key = ApiKeyFactory.build(valid_key='test') bhutan = cls.geoip_data['Bhutan'] cls.bhutan_model = DummyModel( lat=bhutan['latitude'], lon=bhutan['longitude'], radius=bhutan['radius'], code=bhutan['region_code'], name=bhutan['region_name'], ip=bhutan['ip']) london = cls.geoip_data['London'] cls.london_model = DummyModel( lat=london['latitude'], lon=london['longitude'], radius=london['radius'], code=london['region_code'], name=london['region_name'], ip=london['ip'])
def _make_query(self, geoip_db, stats, api_key=None, api_type='locate', blue=(), cell=(), wifi=(), **kw): query = Query(api_key=api_key or ApiKeyFactory.build(valid_key='test'), api_type=api_type, blue=self.blue_model_query(blue), cell=self.cell_model_query(cell), wifi=self.wifi_model_query(wifi), geoip_db=geoip_db, stats_client=stats, **kw) query.emit_query_stats() return query
def setUp(self): super(TestSource, self).setUp() self.api_key = ApiKeyFactory.build(allow_fallback=True) self.fallback_model = DummyModel( lat=51.5366, lon=0.03989, radius=1500.0) self.fallback_result = { 'location': { 'lat': self.fallback_model.lat, 'lng': self.fallback_model.lon, }, 'accuracy': float(self.fallback_model.radius), 'fallback': 'lacf', } self.fallback_cached_result = floatjson.float_dumps({ 'lat': self.fallback_model.lat, 'lon': self.fallback_model.lon, 'accuracy': float(self.fallback_model.radius), 'fallback': 'lacf', })
def test_rate_limit_blocks(self, geoip_db, http_session, redis, session, source, stats): api_key = ApiKeyFactory.build(allow_fallback=True) cell = CellShardFactory() with requests_mock.Mocker() as mock_request: mock_request.register_uri('POST', requests_mock.ANY, json=self.fallback_result) ratelimit_key = source._ratelimit_key( api_key.fallback_name, api_key.fallback_ratelimit_interval, ) redis.set(ratelimit_key, api_key.fallback_ratelimit) query = self.model_query(geoip_db, http_session, session, stats, cells=[cell]) results = source.search(query) self.check_model_results(results, None)
def _make_query(self, geoip_db, stats, **kw): return Query( api_key=ApiKeyFactory.build(valid_key='test'), geoip_db=geoip_db, stats_client=stats, **kw)
def test_api_key(self): api_key = ApiKeyFactory.build() query = Query(api_key=api_key) self.assertEqual(query.api_key.valid_key, api_key.valid_key) self.assertEqual(query.api_key, api_key)
def test_can_fallback(self): assert ApiKeyFactory.build(allow_fallback=True).can_fallback() assert not ApiKeyFactory.build(allow_fallback=False).can_fallback() assert not ApiKeyFactory.build(allow_fallback=None).can_fallback() assert not (ApiKeyFactory.build(allow_fallback=True, fallback_name=None).can_fallback()) assert not (ApiKeyFactory.build(allow_fallback=True, fallback_url=None).can_fallback()) assert not (ApiKeyFactory.build( allow_fallback=True, fallback_ratelimit=None).can_fallback()) assert (ApiKeyFactory.build(allow_fallback=True, fallback_ratelimit=0).can_fallback()) assert not (ApiKeyFactory.build( allow_fallback=True, fallback_ratelimit_interval=None).can_fallback()) assert not (ApiKeyFactory.build( allow_fallback=True, fallback_ratelimit_interval=0).can_fallback()) assert (ApiKeyFactory.build(allow_fallback=True, fallback_cache_expire=None).can_fallback()) assert (ApiKeyFactory.build(allow_fallback=True, fallback_cache_expire=0).can_fallback())
def test_no_log(self, geoip_db, stats): api_key = ApiKeyFactory.build(valid_key=None) self._make_query( geoip_db, stats, self._make_result(), api_key=api_key, api_type='locate') stats.check(total=0)
def test_api_key(self): api_key = ApiKeyFactory.build() query = Query(api_key=api_key) assert query.api_key.valid_key == api_key.valid_key assert query.api_key == api_key
def setUpClass(cls): super(QueryTest, cls).setUpClass() cls.api_key = ApiKeyFactory.build(shortname='key', log=True) cls.london = cls.geoip_data['London'] cls.london_ip = cls.london['ip']
def test_no_log(self): api_key = ApiKeyFactory.build(shortname='key', log_locate=False) self._make_query(self._make_result(), api_key=api_key, api_type='locate') self.check_stats(total=0)
def test_api_key(self): api_key = ApiKeyFactory.build() query = Query(api_key=api_key) assert query.api_key.valid_key == api_key.valid_key assert query.api_key == api_key
def test_no_log(self): api_key = ApiKeyFactory.build(shortname='key', log_locate=False) self._make_query(self._make_result(), api_key=api_key, api_type='locate') self.check_stats(total=0)
def setUp(self): super(SearcherTest, self).setUp() self.api_key = ApiKeyFactory.build(shortname='test') self.api_type = 'locate'
def setUp(self): super(SearcherTest, self).setUp() self.api_key = ApiKeyFactory.build(shortname='test') self.api_type = 'locate'
def setUpClass(cls): super(QueryTest, cls).setUpClass() cls.api_key = ApiKeyFactory.build(shortname='key') cls.london = cls.geoip_data['London'] cls.london_ip = cls.london['ip']
def test_api_key(self): api_key = ApiKeyFactory.build() query = Query(api_key=api_key) self.assertEqual(query.api_key.valid_key, api_key.valid_key) self.assertEqual(query.api_key, api_key)
def setUp(self): super(SearcherTest, self).setUp() self.api_key = ApiKeyFactory.build(valid_key='key') self.api_type = 'locate'
def test_no_log(self): api_key = ApiKeyFactory.build(valid_key=None) self._make_query(self._make_result(), api_key=api_key, api_type='locate') self.check_stats(total=0)
def test_no_log(self): api_key = ApiKeyFactory.build(shortname='key', log=False) self._make_query(api_key=api_key) self.check_stats(total=0)
def setUpClass(cls): super(QueryTest, cls).setUpClass() cls.api_key = ApiKeyFactory.build(valid_key='key') cls.london = cls.geoip_data['London'] cls.london_ip = cls.london['ip']
def test_can_fallback(self): assert ApiKeyFactory.build(allow_fallback=True).can_fallback() assert not ApiKeyFactory.build(allow_fallback=False).can_fallback() assert not ApiKeyFactory.build(allow_fallback=None).can_fallback() assert not (ApiKeyFactory.build( allow_fallback=True, fallback_name=None).can_fallback()) assert not (ApiKeyFactory.build( allow_fallback=True, fallback_url=None).can_fallback()) assert not (ApiKeyFactory.build( allow_fallback=True, fallback_ratelimit=None).can_fallback()) assert (ApiKeyFactory.build( allow_fallback=True, fallback_ratelimit=0).can_fallback()) assert not (ApiKeyFactory.build( allow_fallback=True, fallback_ratelimit_interval=None).can_fallback()) assert not (ApiKeyFactory.build( allow_fallback=True, fallback_ratelimit_interval=0).can_fallback()) assert (ApiKeyFactory.build( allow_fallback=True, fallback_cache_expire=None).can_fallback()) assert (ApiKeyFactory.build( allow_fallback=True, fallback_cache_expire=0).can_fallback())
def test_no_log(self): api_key = ApiKeyFactory.build(valid_key=None) self._make_query(self._make_result(), api_key=api_key, api_type='locate') self.check_stats(total=0)
def setUp(self): super(SearcherTest, self).setUp() self.api_key = ApiKeyFactory.build(valid_key='key') self.api_type = 'locate'