def test_cellarea_not_found_when_lacf_disabled(self): cell = CellAreaFactory() self.session.flush() res = self.app.post_json( '%s?key=test' % self.url, { 'radioType': cell.radio.name, 'cellTowers': [{ 'mobileCountryCode': cell.mcc, 'mobileNetworkCode': cell.mnc, 'locationAreaCode': cell.lac, 'signalStrength': -70, 'timingAdvance': 1}, ], 'fallbacks': { 'lacf': 0, }, }, status=404) self.check_stats( counter=[self.metric_url + '.404', self.metric + '.api_key.test'] ) self.assertEqual(res.content_type, 'application/json') self.assertEqual(res.json, LocationNotFound.json_body())
def test_empty_result_from_fallback_cached(self): cell = CellFactory.build() with requests_mock.Mocker() as mock_request: mock_request.register_uri( 'POST', requests_mock.ANY, json=LocationNotFound.json_body(), status_code=404 ) query = self.model_query(cells=[cell]) location = self.provider.locate(query) self.check_model_location(location, None) self.assertEqual(mock_request.call_count, 1) self.check_stats( counter=[ 'm.fallback.lookup_status.404', 'm.fallback.cache.miss', ], timer=['m.fallback.lookup']) query = self.model_query(cells=[cell]) location = self.provider.locate(query) self.check_model_location(location, None) self.assertEqual(mock_request.call_count, 1) self.check_stats( counter=[ 'm.fallback.lookup_status.404', 'm.fallback.cache.hit', ], timer=['m.fallback.lookup'])
def test_cache_empty_result(self): cell = CellFactory.build() with requests_mock.Mocker() as mock_request: mock_request.register_uri( 'POST', requests_mock.ANY, json=LocationNotFound.json_body(), status_code=404 ) query = self.model_query(cells=[cell]) result = self.source.search(query) self.check_model_result(result, None) self.assertEqual(mock_request.call_count, 1) self.check_stats(counter=[ ('locate.fallback.cache', ['status:miss']), ('locate.fallback.lookup', ['status:404']), ]) query = self.model_query(cells=[cell]) result = self.source.search(query) self.check_model_result(result, None) self.assertEqual(mock_request.call_count, 1) self.check_stats(counter=[ ('locate.fallback.cache', ['status:hit']), ('locate.fallback.lookup', ['status:404']), ])
def test_cache_empty_result(self): cell = CellShardFactory.build() with requests_mock.Mocker() as mock_request: mock_request.register_uri('POST', requests_mock.ANY, json=LocationNotFound.json_body(), status_code=404) query = self.model_query(cells=[cell]) results = self.source.search(query) self.check_model_results(results, None) self.assertEqual(mock_request.call_count, 1) self.check_stats(counter=[ ('locate.fallback.cache', ['status:miss']), ('locate.fallback.lookup', ['fallback_name:fall', 'status:404']), ]) query = self.model_query(cells=[cell]) results = self.source.search(query) self.check_model_results(results, None) self.assertEqual(mock_request.call_count, 1) self.check_stats(counter=[ ('locate.fallback.cache', ['status:hit']), ('locate.fallback.lookup', ['fallback_name:fall', 'status:404']), ])
def _check_geoip_result(self, result, status=200): self.assertEqual(result.content_type, 'application/json') self.assertEqual(result.charset, 'UTF-8') if status == 200: self.assertEqual(result.json, {'country_name': 'United Kingdom', 'country_code': 'GB'}) elif status == 400: self.assertEqual(result.json, InvalidAPIKey.json_body()) elif status == 404: self.assertEqual(result.json, LocationNotFound.json_body())
def test_exception_rendering(self): # make sure all the sub-classing returns what we expect error = LocationNotFound() self.assertEqual(str(error), '<LocationNotFound>: 404') response = Request.blank('/').get_response(error) self.assertEqual(response.status_code, 404) self.assertEqual(response.content_type, 'application/json') self.assertEqual(response.json, LocationNotFound.json_body()) self.assertTrue('404' in response.body) self.assertTrue('notFound' in response.body)
def test_404_response_returns_empty_location(self): cell = CellFactory.build() with requests_mock.Mocker() as mock_request: mock_request.register_uri( 'POST', requests_mock.ANY, json=LocationNotFound.json_body(), status_code=404) query = self.model_query(cells=[cell]) location = self.provider.locate(query) self.check_model_location(location, None) self.check_raven([('HTTPError', 0)]) self.check_stats(counter=['m.fallback.lookup_status.404'])
def test_404_response_returns_empty_location(self): with requests_mock.Mocker() as mock_request: mock_request.register_uri( 'POST', requests_mock.ANY, json=LocationNotFound.json_body(), status_code=404) location = self.provider.locate({ 'cell': self.cells, 'wifi': self.wifis, }) self.assertFalse(location.found()) self.check_raven([('HTTPError', 0)]) self.check_stats(counter=['m.fallback.lookup_status.404'])
def test_404_response(self): cell = CellFactory.build() with requests_mock.Mocker() as mock_request: mock_request.register_uri( 'POST', requests_mock.ANY, json=LocationNotFound.json_body(), status_code=404) query = self.model_query(cells=[cell]) result = self.source.search(query) self.check_model_result(result, None) self.check_raven([('HTTPError', 0)]) self.check_stats(counter=[ ('locate.fallback.lookup', ['status:404']), ])
def test_404_response(self): cell = CellShardFactory.build() with requests_mock.Mocker() as mock_request: mock_request.register_uri('POST', requests_mock.ANY, json=LocationNotFound.json_body(), status_code=404) query = self.model_query(cells=[cell]) results = self.source.search(query) self.check_model_results(results, None) self.check_raven([('HTTPError', 0)]) self.check_stats(counter=[ ('locate.fallback.lookup', ['fallback_name:fall', 'status:404']), ])
def test_wifi_not_found(self): wifis = WifiFactory.build_batch(2) res = self.app.post_json( '%s?key=test' % self.url, { 'wifiAccessPoints': [ {'macAddress': wifis[0].key}, {'macAddress': wifis[1].key}, ]}, status=404) self.assertEqual(res.content_type, 'application/json') self.assertEqual(res.json, LocationNotFound.json_body()) # Make sure to get two counters, a timer, and no traceback self.check_stats( counter=[self.metric + '.api_key.test', self.metric + '.api_log.test.wifi_miss', (self.metric_url + '.404', 1)], timer=[self.metric_url])
def test_404_response(self, geoip_db, http_session, raven, session, source, stats): cell = CellShardFactory.build() with requests_mock.Mocker() as mock_request: mock_request.register_uri( 'POST', requests_mock.ANY, json=LocationNotFound.json_body(), status_code=404) query = self.model_query( geoip_db, http_session, session, stats, cells=[cell]) results = source.search(query) self.check_model_results(results, None) raven.check([('HTTPError', 0)]) stats.check(counter=[ ('locate.fallback.lookup', [self.fallback_tag, 'status:404']), ])
def test_404_response(self, geoip_db, http_session, raven, session, source, stats): cell = CellShardFactory.build() with requests_mock.Mocker() as mock_request: mock_request.register_uri('POST', requests_mock.ANY, json=LocationNotFound.json_body(), status_code=404) query = self.model_query(geoip_db, http_session, session, stats, cells=[cell]) results = source.search(query) self.check_model_results(results, None) raven.check([('HTTPError', 0)]) stats.check(counter=[ ('locate.fallback.lookup', [self.fallback_tag, 'status:404']), ])
def test_empty_result_from_fallback_cached(self): with requests_mock.Mocker() as mock_request: mock_request.register_uri( 'POST', requests_mock.ANY, json=LocationNotFound.json_body(), status_code=404 ) location = self.provider.locate({ 'cell': self.cells[:1], 'wifi': [], }) self.assertFalse(location.found()) self.assertEqual(mock_request.call_count, 1) self.check_stats( counter=[ 'm.fallback.lookup_status.404', 'm.fallback.cache.miss', ], timer=['m.fallback.lookup']) location = self.provider.locate({ 'cell': self.cells[:1], 'wifi': [], }) self.assertFalse(location.found()) self.assertEqual(mock_request.call_count, 1) self.check_stats( counter=[ 'm.fallback.lookup_status.404', 'm.fallback.cache.hit', ], timer=['m.fallback.lookup'])
def test_404_response( self, geoip_db, http_session, raven, session, source, metricsmock ): cell = CellShardFactory.build() with requests_mock.Mocker() as mock_request: mock_request.register_uri( "POST", requests_mock.ANY, json=LocationNotFound.json_body(), status_code=404, ) query = self.model_query(geoip_db, http_session, session, cells=[cell]) results = source.search(query) self.check_model_results(results, None) raven.check([("HTTPError", 0)]) assert metricsmock.has_record( "incr", "locate.fallback.lookup", value=1, tags=[self.fallback_tag, "status:404"], )
def fallback_not_found(self): return LocationNotFound.json_body()