예제 #1
0
    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'])
예제 #2
0
파일: base.py 프로젝트: AmmarNoman/ichnaea
 def check_response(self,
                    data_queues,
                    response,
                    status,
                    fallback=None,
                    details=None):
     assert response.content_type == "application/json"
     assert response.headers["Access-Control-Allow-Origin"] == "*"
     assert response.headers["Access-Control-Max-Age"] == "2592000"
     if status == "ok":
         body = dict(response.json)
         if fallback:
             assert body["fallback"] == fallback
             del body["fallback"]
         assert body == self.ip_response
     elif status == "invalid_key":
         assert response.json == InvalidAPIKey().json_body()
     elif status == "not_found":
         assert response.json == LocationNotFound().json_body()
     elif status == "parse_error":
         assert response.json == ParseError(details).json_body()
     elif status == "limit_exceeded":
         assert response.json == DailyLimitExceeded().json_body()
     if status != "ok":
         self.check_queue(data_queues, 0)
예제 #3
0
    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']),
            ])
예제 #4
0
    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())
예제 #5
0
    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']),
            ])
예제 #6
0
 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())
예제 #7
0
    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)
예제 #8
0
    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'])
예제 #9
0
    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'])
예제 #10
0
    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']),
        ])
예제 #11
0
    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']),
        ])
예제 #12
0
    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])
예제 #13
0
    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']),
        ])
예제 #14
0
    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)])
        metricsmock.assert_incr_once("locate.fallback.lookup",
                                     tags=[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']),
        ])
예제 #16
0
    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'])
예제 #17
0
 def fallback_not_found(self):
     return LocationNotFound().json_body()
예제 #18
0
 def fallback_not_found(self):
     return LocationNotFound.json_body()