예제 #1
0
    def test_should_not_provide_location_if_malformed_wifi(self):
        wifi = WifiFactory.build()
        malformed_wifi = WifiFactory.build()
        malformed_wifi.key = 'abcd'

        query = self.model_query(wifis=[wifi, malformed_wifi])
        self.check_should_locate(query, False)
예제 #2
0
    def test_blacklist(self):
        utcnow = util.utcnow()

        bad_wifi = WifiFactory.build()
        good_wifi = WifiFactory.build()
        black = WifiBlacklist(time=utcnow, count=1, key=bad_wifi.key)
        self.session.add(black)
        self.session.flush()

        obs = dict(lat=good_wifi.lat, lon=good_wifi.lon)
        entries = [
            {'key': good_wifi.key},
            {'key': good_wifi.key},
            {'key': bad_wifi.key},
        ]
        for entry in entries:
            entry.update(obs)

        result = insert_measures_wifi.delay(entries)
        self.assertEqual(result.get(), 2)

        self.assertEqual(self.data_queue.size(), 2)
        update_wifi.delay().get()

        wifis = self.session.query(Wifi).all()
        self.assertEqual(len(wifis), 1)
        self._compare_sets([w.key for w in wifis], [good_wifi.key])

        self.check_statcounter(StatKey.wifi, 2)
        self.check_statcounter(StatKey.unique_wifi, 1)
예제 #3
0
    def test_max_min_range_update(self):
        wifi = WifiFactory(range=100, total_measures=4)
        wifi_lat = wifi.lat
        wifi_lon = wifi.lon
        wifi.max_lat = wifi.lat + 0.001
        wifi.min_lat = wifi.lat - 0.001
        wifi.max_lon = wifi.lon + 0.001
        wifi.min_lon = wifi.lon - 0.001

        observations = [
            WifiObservation(lat=wifi.lat + 0.002, lon=wifi.lon - 0.004,
                            key=wifi.key),
            WifiObservation(lat=wifi.lat - 0.002, lon=wifi.lon + 0.01,
                            key=wifi.key),
        ]
        self.data_queue.enqueue(observations)
        self.session.commit()

        self.assertEqual(update_wifi.delay().get(), (1, 0))

        wifis = self.session.query(Wifi).all()
        self.assertEqual(len(wifis), 1)
        wifi = wifis[0]

        self.assertAlmostEqual(wifi.lat, wifi_lat)
        self.assertAlmostEqual(wifi.max_lat, wifi_lat + 0.002)
        self.assertAlmostEqual(wifi.min_lat, wifi_lat - 0.002)
        self.assertAlmostEqual(wifi.lon, wifi_lon + 0.001)
        self.assertAlmostEqual(wifi.max_lon, wifi_lon + 0.01)
        self.assertAlmostEqual(wifi.min_lon, wifi_lon - 0.004)
        self.assertEqual(wifi.range, 662)
        self.assertEqual(wifi.total_measures, 6)
예제 #4
0
    def add_reports(self, num=1, blue_factor=0, cell_factor=1, wifi_factor=2,
                    api_key='test', email=None, ip=None, nickname=None,
                    blue_key=None, cell_mcc=None, wifi_key=None, lat=None):
        reports = []
        for i in range(num):
            pos = CellFactory.build()
            report = {
                'timestamp': time.time() * 1000.0,
                'position': {},
                'bluetoothBeacons': [],
                'cellTowers': [],
                'wifiAccessPoints': [],
            }
            report['position']['latitude'] = lat or pos.lat
            report['position']['longitude'] = pos.lon
            report['position']['accuracy'] = 17 + i

            blues = WifiFactory.build_batch(blue_factor,
                                            lat=pos.lat, lon=pos.lon)
            for blue in blues:
                blue_data = {
                    'macAddress': blue_key or blue.key,
                    'signalStrength': -100 + i,
                }
                report['bluetoothBeacons'].append(blue_data)

            cells = CellFactory.build_batch(cell_factor,
                                            lat=pos.lat, lon=pos.lon)
            for cell in cells:
                cell_data = {
                    'radioType': cell.radio.name,
                    'mobileCountryCode': cell_mcc or cell.mcc,
                    'mobileNetworkCode': cell.mnc,
                    'locationAreaCode': cell.lac,
                    'cellId': cell.cid,
                    'primaryScramblingCode': cell.psc,
                    'signalStrength': -110 + i,
                }
                report['cellTowers'].append(cell_data)

            wifis = WifiFactory.build_batch(wifi_factor,
                                            lat=pos.lat, lon=pos.lon)
            for wifi in wifis:
                wifi_data = {
                    'macAddress': wifi_key or wifi.key,
                    'signalStrength': -90 + i,
                }
                report['wifiAccessPoints'].append(wifi_data)

            reports.append(report)

        queue_reports.delay(reports=reports, api_key=api_key,
                            email=email, ip=ip, nickname=nickname).get()
        return reports
예제 #5
0
파일: tests.py 프로젝트: thebent/ichnaea
    def test_ok_wifi(self):
        session = self.session
        wifis = WifiFactory.create_batch(4)
        new_wifi = WifiFactory()
        session.flush()
        res = self.app.post_json('/v1/geosubmit?key=test', {
            "items": [
                {
                    "latitude":
                    wifis[0].lat,
                    "longitude":
                    wifis[0].lon,
                    "wifiAccessPoints": [
                        {
                            "macAddress": wifis[0].key
                        },
                        {
                            "macAddress": wifis[1].key
                        },
                        {
                            "macAddress": wifis[2].key
                        },
                        {
                            "macAddress": wifis[3].key
                        },
                        {
                            "macAddress": new_wifi.key
                        },
                    ]
                },
            ]
        },
                                 status=200)

        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.json, {})

        # Check that new wifi exists
        query = session.query(Wifi)
        count = query.filter(Wifi.key == new_wifi.key).count()
        self.assertEquals(count, 1)

        # check that WifiObservation records are created
        self.assertEquals(session.query(WifiObservation).count(), 5)

        self.check_stats(counter=[
            'items.api_log.test.uploaded.batches',
            'items.api_log.test.uploaded.reports',
            'items.api_log.test.uploaded.wifi_observations',
            'items.uploaded.wifi_observations',
        ],
                         timer=['items.api_log.test.uploaded.batch_size'])
예제 #6
0
    def test_wifi_prefer_larger_cluster_over_high_signal(self):
        wifi = WifiFactory()
        wifis = WifiFactory.create_batch(3, lat=wifi.lat, lon=wifi.lon)
        wifis2 = WifiFactory.create_batch(3, lat=wifi.lat + 1.0, lon=wifi.lon)
        self.session.flush()

        query = self.model_query(wifis=[wifi] + wifis + wifis2)
        for entry in query.wifi[:-3]:
            entry['signal'] = -80
        for entry in query.wifi[-3:]:
            entry['signal'] = -70
        location = self.provider.locate(query)
        self.check_model_location(location, wifi)
예제 #7
0
    def test_wifi(self):
        wifi = WifiFactory.build()
        self._post([{
            'latitude': wifi.lat,
            'longitude': wifi.lon,
            'wifiAccessPoints': [{
                'macAddress': wifi.key,
                'age': 3,
                'channel': 5,
                'frequency': 2437,
                'radioType': '802.11n',
                'signalStrength': -90,
                'signalToNoiseRatio': 5,
                'xtra_field': 3,
            }]},
        ])

        self._assert_queue_size(1)
        item = self.queue.dequeue(self.queue.queue_key())[0]
        self.assertEqual(item['metadata']['api_key'], None)
        report = item['report']
        self.assertTrue('timestamp' in report)
        position = report['position']
        self.assertEqual(position['latitude'], wifi.lat)
        self.assertEqual(position['longitude'], wifi.lon)
        wifis = item['report']['wifiAccessPoints']
        self.assertEqual(len(wifis), 1)
        self.assertEqual(wifis[0]['macAddress'], wifi.key)
        self.assertEqual(wifis[0]['age'], 3),
        self.assertEqual(wifis[0]['channel'], 5),
        self.assertEqual(wifis[0]['frequency'], 2437),
        self.assertEqual(wifis[0]['radioType'], '802.11n')
        self.assertEqual(wifis[0]['signalStrength'], -90),
        self.assertEqual(wifis[0]['signalToNoiseRatio'], 5),
        self.assertFalse('xtra_field' in wifis[0])
예제 #8
0
 def test_geoip_fallback(self):
     london = self.geoip_data['London']
     wifis = WifiFactory.build_batch(4)
     res = self.app.post_json(
         '%s?key=test' % self.url, {
             "wifiAccessPoints": [
                 {
                     "macAddress": wifis[0].key
                 },
                 {
                     "macAddress": wifis[1].key
                 },
                 {
                     "macAddress": wifis[2].key
                 },
                 {
                     "macAddress": wifis[3].key
                 },
             ]
         },
         extra_environ={'HTTP_X_FORWARDED_FOR': london['ip']},
         status=200)
     self.assertEqual(res.content_type, 'application/json')
     self.assertEqual(
         res.json, {
             "location": {
                 "lat": london['latitude'],
                 "lng": london['longitude']
             },
             "accuracy": london['accuracy']
         })
예제 #9
0
파일: tests.py 프로젝트: thebent/ichnaea
    def test_log_unknown_api_key(self):
        wifis = WifiFactory.create_batch(2)
        self.app.post_json('/v1/geosubmit?key=invalidkey', {
            "items": [
                {
                    "latitude":
                    wifis[0].lat,
                    "longitude":
                    wifis[0].lon,
                    "wifiAccessPoints": [
                        {
                            "macAddress": wifis[0].key
                        },
                        {
                            "macAddress": wifis[1].key
                        },
                    ]
                },
            ]
        },
                           status=200)

        self.check_stats(counter=[
            'geosubmit.unknown_api_key', ('geosubmit.api_key.invalidkey', 0)
        ])
예제 #10
0
    def test_wifi(self):
        wifi = WifiFactory.build()
        self._post([{
            'lat': wifi.lat,
            'lon': wifi.lon,
            'accuracy': 17,
            'wifi': [{'key': wifi.key.upper(),
                      'frequency': 2437,
                      'signal': -70,
                      'signalToNoiseRatio': 5,
                      }]
        }])

        self._assert_queue_size(1)
        item = self.queue.dequeue(self.queue.queue_key())[0]
        self.assertEqual(item['metadata']['api_key'], None)
        report = item['report']
        position = report['position']
        self.assertEqual(position['latitude'], wifi.lat)
        self.assertEqual(position['longitude'], wifi.lon)
        self.assertEqual(position['accuracy'], 17)
        self.assertFalse('altitude' in position)
        self.assertFalse('altitudeAccuracy' in position)
        wifis = report['wifiAccessPoints']
        self.assertEqual(wifis[0]['macAddress'], wifi.key.upper())
        self.assertFalse('channel' in wifis[0])
        self.assertEqual(wifis[0]['frequency'], 2437)
        self.assertEqual(wifis[0]['signalStrength'], -70)
        self.assertEqual(wifis[0]['signalToNoiseRatio'], 5)
예제 #11
0
 def test_minimal(self):
     schema = SubmitV1Schema()
     wifi = WifiFactory.build()
     data = schema.deserialize(
         {'items': [{'lat': wifi.lat, 'lon': wifi.lon, 'wifi': [{}]}]})
     self.assertTrue('items' in data)
     self.assertEqual(len(data['items']), 1)
예제 #12
0
파일: tests.py 프로젝트: aliendb/ichnaea
    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, {'error': {
                'errors': [{
                    'domain': 'geolocation',
                    'reason': 'notFound',
                    'message': 'Not found',
                }],
                'code': 404,
                'message': 'Not found'
            }}
        )

        # 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'],
            timer=[self.metric_url])
예제 #13
0
파일: tests.py 프로젝트: thebent/ichnaea
    def test_email_header(self):
        nickname = 'World Tr\xc3\xa4veler'
        email = 'world_tr\xc3\[email protected]'
        session = self.session
        wifis = WifiFactory.create_batch(2)
        self.app.post_json('/v1/geosubmit?key=test', {
            "items": [
                {
                    "latitude":
                    wifis[0].lat,
                    "longitude":
                    wifis[0].lon,
                    "wifiAccessPoints": [
                        {
                            "macAddress": wifis[0].key
                        },
                        {
                            "macAddress": wifis[1].key
                        },
                    ]
                },
            ]
        },
                           headers={
                               'X-Nickname': nickname,
                               'X-Email': email,
                           },
                           status=200)

        session = self.session
        result = session.query(User).all()
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].email, email.decode('utf-8'))
예제 #14
0
    def test_no_call_made_when_not_allowed_for_apikey(self):
        cells = CellFactory.build_batch(2)
        wifis = WifiFactory.build_batch(2)
        self.provider.api_key.allow_fallback = False

        query = self.model_query(cells=cells, wifis=wifis)
        self.check_should_locate(query, False)
예제 #15
0
파일: tests.py 프로젝트: awoland/ichnaea
    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, {"error": {
                "errors": [{
                    "domain": "geolocation",
                    "reason": "notFound",
                    "message": "Not found",
                }],
                "code": 404,
                "message": "Not found"
            }}
        )

        # 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'],
            timer=[self.metric_url])
        self.check_raven(total=0)
예제 #16
0
파일: tests.py 프로젝트: thebent/ichnaea
 def test_duplicated_wifi_observations(self):
     session = self.session
     wifi = WifiFactory.build()
     self.app.post_json('/v1/geosubmit?key=test', {
         "items": [
             {
                 "latitude":
                 wifi.lat,
                 "longitude":
                 wifi.lon,
                 "wifiAccessPoints": [
                     {
                         "macAddress": wifi.key,
                         "signalStrength": -92
                     },
                     {
                         "macAddress": wifi.key,
                         "signalStrength": -77
                     },
                 ]
             },
         ]
     },
                        status=200)
     self.assertEquals(session.query(WifiObservation).count(), 1)
예제 #17
0
    def setUp(self):
        super(TestFallbackProvider, self).setUp()

        self.provider.api_key.allow_fallback = True

        self.response_location = {
            'location': {
                'lat': 51.5366,
                'lng': 0.03989,
            },
            'accuracy': 1500,
            'fallback': 'lacf',
        }

        self.cells = []
        for cell in CellFactory.build_batch(2):
            self.cells.append({
                'radio': cell.radio,
                'mcc': cell.mcc,
                'mnc': cell.mnc,
                'lac': cell.lac,
                'cid': cell.cid,
                'signal': -70,
            })
        self.cells[0]['ta'] = 1

        self.wifis = []
        for wifi in WifiFactory.build_batch(2):
            self.wifis.append({
                'key': wifi.key,
                'signal': -77,
            })
        self.wifis[0]['channel'] = 6
        self.wifis[0]['frequency'] = 2437
        self.wifis[0]['snr'] = 13
예제 #18
0
    def add_reports(self, number=3, api_key='test', email=None):
        reports = []
        for i in range(number):
            report = {
                'timestamp': time.time() * 1000.0,
                'position': {},
                'cellTowers': [],
                'wifiAccessPoints': [],
            }
            cell = CellFactory.build()
            report['position']['latitude'] = cell.lat
            report['position']['longitude'] = cell.lon
            report['position']['accuracy'] = 17 + i
            cell_data = {
                'radioType': cell.radio.name,
                'mobileCountryCode': cell.mcc,
                'mobileNetworkCode': cell.mnc,
                'locationAreaCode': cell.lac,
                'cellId': cell.cid,
                'primaryScramblingCode': cell.psc,
                'signalStrength': -110 + i,
            }
            report['cellTowers'].append(cell_data)
            wifis = WifiFactory.build_batch(2, lat=cell.lat, lon=cell.lon)
            for wifi in wifis:
                wifi_data = {
                    'macAddress': wifi.key,
                    'signalStrength': -90 + i,
                }
                report['wifiAccessPoints'].append(wifi_data)
            reports.append(report)

        queue_reports.delay(
            reports=reports, api_key=api_key, email=email).get()
        return reports
예제 #19
0
    def test_database_error(self):
        london = self.geoip_data['London']
        self.session.execute(text('drop table wifi;'))
        self.session.execute(text('drop table cell;'))
        cell = CellFactory.build()
        wifis = WifiFactory.build_batch(2)

        res = self.app.post_json(
            '/v1/geolocate?key=test', {
                'cellTowers': [{
                    'radioType': cell.radio.name,
                    'mobileCountryCode': cell.mcc,
                    'mobileNetworkCode': cell.mnc,
                    'locationAreaCode': cell.lac,
                    'cellId': cell.cid},
                ],
                'wifiAccessPoints': [
                    {'macAddress': wifis[0].key},
                    {'macAddress': wifis[1].key},
                ]},
            extra_environ={'HTTP_X_FORWARDED_FOR': london['ip']},
            status=200)

        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.json, {'location': {'lat': london['latitude'],
                                                 'lng': london['longitude']},
                                    'accuracy': london['accuracy']})

        self.check_stats(
            timer=['request.v1.geolocate'],
            counter=[
                'request.v1.geolocate.200',
                'geolocate.geoip_hit',
            ])
        self.check_raven([('ProgrammingError', 2)])
예제 #20
0
    def add_reports(self, number=3, api_key='test', email=None):
        reports = []
        for i in range(number):
            report = {
                'timestamp': time.time() * 1000.0,
                'position': {},
                'cellTowers': [],
                'wifiAccessPoints': [],
            }
            cell = CellFactory.build()
            report['position']['latitude'] = cell.lat
            report['position']['longitude'] = cell.lon
            report['position']['accuracy'] = 17 + i
            cell_data = {
                'radioType': cell.radio.name,
                'mobileCountryCode': cell.mcc,
                'mobileNetworkCode': cell.mnc,
                'locationAreaCode': cell.lac,
                'cellId': cell.cid,
                'primaryScramblingCode': cell.psc,
                'signalStrength': -110 + i,
            }
            report['cellTowers'].append(cell_data)
            wifis = WifiFactory.build_batch(2, lat=cell.lat, lon=cell.lon)
            for wifi in wifis:
                wifi_data = {
                    'macAddress': wifi.key,
                    'signalStrength': -90 + i,
                }
                report['wifiAccessPoints'].append(wifi_data)
            reports.append(report)

        queue_reports.delay(reports=reports, api_key=api_key,
                            email=email).get()
        return reports
예제 #21
0
    def test_bluetooth(self):
        wifi = WifiFactory.build()
        self._post([{
            'position': {
                'latitude': wifi.lat,
                'longitude': wifi.lon,
            },
            'bluetoothBeacons': [{
                'macAddress': wifi.key,
                'name': 'my-beacon',
                'age': 3,
                'signalStrength': -90,
                'xtra_field': 4,
            }],
            'wifiAccessPoints': [{
                'signalStrength': -52,
            }]},
        ])

        self._assert_queue_size(1)
        item = self.queue.dequeue(self.queue.queue_key())[0]
        report = item['report']
        self.assertTrue('timestamp' in report)
        position = report['position']
        self.assertEqual(position['latitude'], wifi.lat)
        self.assertEqual(position['longitude'], wifi.lon)
        blues = report['bluetoothBeacons']
        self.assertEqual(len(blues), 1)
        self.assertEqual(blues[0]['macAddress'], wifi.key)
        self.assertEqual(blues[0]['age'], 3),
        self.assertEqual(blues[0]['name'], 'my-beacon'),
        self.assertEqual(blues[0]['signalStrength'], -90),
        self.assertFalse('xtra_field' in blues[0])
        wifis = report['wifiAccessPoints']
        self.assertEqual(len(wifis), 1)
예제 #22
0
파일: tests.py 프로젝트: aliendb/ichnaea
 def test_empty_wifi_entry(self):
     schema = ReportV1Schema()
     wifi = WifiFactory.build()
     request = self._make_request(
         '{"lat": %s, "lon": %s, "wifi": [{}]}' % (wifi.lat, wifi.lon))
     data, errors = preprocess_request(request, schema, None)
     self.assertFalse(errors)
예제 #23
0
파일: tests.py 프로젝트: awoland/ichnaea
    def test_email_header(self):
        nickname = 'World Tr\xc3\xa4veler'
        email = 'world_tr\xc3\[email protected]'
        session = self.session
        wifis = WifiFactory.create_batch(2)
        self.app.post_json(
            '/v1/geosubmit?key=test',
            {"items": [{
                "latitude": wifis[0].lat,
                "longitude": wifis[0].lon,
                "wifiAccessPoints": [
                    {"macAddress": wifis[0].key},
                    {"macAddress": wifis[1].key},
                ]},
            ]},
            headers={
                'X-Nickname': nickname,
                'X-Email': email,
            },
            status=200)

        session = self.session
        result = session.query(User).all()
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].email, email.decode('utf-8'))
예제 #24
0
    def test_wifi_too_few_candidates(self):
        wifis = WifiFactory.create_batch(2)
        self.session.flush()

        query = self.model_query(wifis=[wifis[0]])
        location = self.provider.locate(query)
        self.check_model_location(location, None)
예제 #25
0
    def test_should_not_provide_location_if_non_geoip_location_found(self):
        wifis = WifiFactory.build_batch(2)
        internal_pos = Position(
            source=DataSource.Internal, lat=1.0, lon=1.0, accuracy=1.0)

        query = self.model_query(wifis=wifis)
        self.check_should_locate(query, False, location=internal_pos)
예제 #26
0
파일: base.py 프로젝트: SOFTowaha/ichnaea
    def test_fallback_used_when_geoip_also_present(self):
        cells = CellFactory.build_batch(2, radio=Radio.wcdma)
        wifis = WifiFactory.build_batch(3)
        api_key = ApiKey.getkey(self.session, 'test')
        api_key.allow_fallback = True
        self.session.flush()

        with requests_mock.Mocker() as mock:
            response_location = {
                'location': {
                    'lat': 1.0,
                    'lng': 1.0,
                },
                'accuracy': 100,
            }
            mock.register_uri(
                'POST', requests_mock.ANY, json=response_location)

            query = self.model_query(cells=cells, wifis=wifis)
            res = self._call(body=query, ip=self.test_ip)

            send_json = mock.request_history[0].json()
            self.assertEqual(len(send_json['cellTowers']), 2)
            self.assertEqual(len(send_json['wifiAccessPoints']), 3)

        self.check_model_response(res, None, lat=1.0, lon=1.0, accuracy=100)

        self.check_stats(
            timer=[self.metric_url],
            counter=[self.metric + '.api_key.test',
                     self.metric + '.fallback_hit',
                     self.metric_url + '.200',
                     self.metric + '.api_log.test.fallback_hit'],
        )
예제 #27
0
    def test_wifi_too_few_matches(self):
        wifis = WifiFactory.create_batch(3)
        wifis[0].lat = None
        self.session.flush()

        query = self.model_query(wifis=wifis[:2])
        location = self.provider.locate(query)
        self.check_model_location(location, None, used=True)
예제 #28
0
    def test_wifi_similar_bssids_but_enough_found_clusters(self):
        wifi = WifiFactory(key='00000000001f')
        wifi2 = WifiFactory(key='000000000024',
                            lat=wifi.lat + 0.00004, lon=wifi.lon + 0.00004)
        other_wifi = [
            WifiFactory.build(key='000000000020'),
            WifiFactory.build(key='000000000021'),
            WifiFactory.build(key='000000000022'),
            WifiFactory.build(key='000000000023'),
        ]
        self.session.flush()

        query = self.model_query(wifis=[wifi, wifi2] + other_wifi)
        location = self.provider.locate(query)
        self.check_model_location(
            location, wifi,
            lat=wifi.lat + 0.00002, lon=wifi.lon + 0.00002)
예제 #29
0
파일: tests.py 프로젝트: SOFTowaha/ichnaea
 def test_error(self):
     wifi = WifiFactory.build()
     res = self.app.post_json(
         '/v1/submit',
         [{'lat': wifi.lat, 'lon': wifi.lon, 'cell': []}],
         status=400)
     self.assertEqual(res.json, ParseError.json_body())
     self.check_raven(['ParseError'])
예제 #30
0
파일: tests.py 프로젝트: aliendb/ichnaea
 def test_minimal(self):
     schema = SubmitV1Schema()
     wifi = WifiFactory.build()
     request = self._make_request(
         '{"items": [{"lat": %s, "lon": %s}]}' % (wifi.lat, wifi.lon))
     data, errors = preprocess_request(request, schema, None)
     self.assertFalse(errors)
     self.assertTrue('items' in data)
     self.assertEqual(len(data['items']), 1)
예제 #31
0
 def test_error(self):
     wifi = WifiFactory.build()
     self._post([{
         'latitude': wifi.lat,
         'longitude': wifi.lon,
         'wifiAccessPoints': [{
             'macAddress': 10,
         }],
     }], status=400)
     self._assert_queue_size(0)
예제 #32
0
파일: tests.py 프로젝트: aliendb/ichnaea
 def test_error(self):
     wifi = WifiFactory.build()
     res = self.app.post_json(
         '/v1/submit',
         [{'lat': wifi.lat, 'lon': wifi.lon, 'cell': []}],
         status=400)
     self.assertEqual(res.content_type, 'application/json')
     self.assertTrue('errors' in res.json)
     self.assertFalse('status' in res.json)
     self.check_raven(['JSONError'])
예제 #33
0
    def test_should_provide_location_if_only_geoip_location_found(self):
        london = self.london_model
        wifis = WifiFactory.build_batch(2)
        geoip_pos = Position(
            source=DataSource.GeoIP,
            lat=london.lat,
            lon=london.lon,
            accuracy=london.range)

        query = self.model_query(wifis=wifis, geoip=london.ip)
        self.check_should_locate(query, True, location=geoip_pos)
예제 #34
0
파일: tests.py 프로젝트: SOFTowaha/ichnaea
 def test_wifi(self):
     wifis = WifiFactory.build_batch(2)
     query = self.model_query(wifis=wifis)
     res = self._call(
         body=query,
         ip='127.0.0.1',
         status=404)
     self.check_response(res, 'not_found')
     self.check_db_calls(rw=0, ro=0)
     self.check_stats(
         counter=[self.metric + '.miss'])
예제 #35
0
파일: tests.py 프로젝트: thebent/ichnaea
 def test_many_errors(self):
     wifi = WifiFactory.build()
     wifis = [{'wrong_key': 'ab'} for i in range(100)]
     res = self.app.post_json(
         '/v1/submit',
         {'items': [{'lat': wifi.lat, 'lon': wifi.lon, 'wifi': wifis}]},
         status=400)
     self.assertEqual(res.content_type, 'application/json')
     self.assertTrue('errors' in res.json)
     self.assertTrue(len(res.json['errors']) < 10)
     self.check_raven(['JSONError'])
예제 #36
0
 def test_error_missing_latlon(self):
     wifi = WifiFactory.build()
     self._post([
         {'lat': wifi.lat,
          'lon': wifi.lon,
          'accuracy': 17,
          'wifi': [{'key': wifi.key}],
          },
         {'wifi': [{'key': wifi.key}],
          'accuracy': 16},
     ])
     self._assert_queue_size(2)
예제 #37
0
 def test_ok_wifi(self):
     wifi = WifiFactory()
     offset = 0.0001
     wifis = [
         wifi,
         WifiFactory(lat=wifi.lat + offset),
         WifiFactory(lat=wifi.lat + offset * 2),
         WifiFactory(lat=None, lon=None),
     ]
     self.session.flush()
     res = self.app.post_json('%s?key=test' % self.url, {
         "wifiAccessPoints": [
             {
                 "macAddress": wifis[0].key
             },
             {
                 "macAddress": wifis[1].key
             },
             {
                 "macAddress": wifis[2].key
             },
             {
                 "macAddress": wifis[3].key
             },
         ]
     },
                              status=200)
     self.check_stats(counter=[
         self.metric + '.api_key.test', self.metric +
         '.api_log.test.wifi_hit'
     ])
     self.assertEqual(res.content_type, 'application/json')
     self.assertEqual(
         res.json, {
             "location": {
                 "lat": wifi.lat + offset,
                 "lng": wifi.lon
             },
             "accuracy": wifi.range
         })
예제 #38
0
    def test_unique_wifi_histogram(self):
        session = self.session
        today = util.utcnow()
        yesterday = (today - timedelta(1))
        two_days = (today - timedelta(2))
        long_ago = (today - timedelta(3))

        WifiFactory(key="ab1234567890", created=long_ago)
        WifiFactory(key="bc1234567890", created=two_days)
        WifiFactory(key="cd1234567890", created=yesterday)
        WifiFactory(key="de1234567890", created=yesterday)
        WifiFactory(key="ef1234567890", created=today)
        session.flush()

        result = unique_wifi_histogram.delay(ago=3)
        added = result.get()
        self.assertEqual(added, 1)

        stats = session.query(Stat).order_by(Stat.time).all()
        self.assertEqual(len(stats), 1)
        self.assertEqual(stats[0].key, StatKey.unique_wifi)
        self.assertEqual(stats[0].time, long_ago.date())
        self.assertEqual(stats[0].value, 1)

        # fill up newer dates
        unique_wifi_histogram.delay(ago=2).get()
        unique_wifi_histogram.delay(ago=1).get()
        unique_wifi_histogram.delay(ago=0).get()

        # test duplicate execution
        unique_wifi_histogram.delay(ago=1).get()

        stats = session.query(Stat.time, Stat.value).order_by(Stat.time).all()
        self.assertEqual(len(stats), 4)
        self.assertEqual(dict(stats), {
                         long_ago.date(): 1,
                         two_days.date(): 2,
                         yesterday.date(): 4,
                         today.date(): 5})
예제 #39
0
    def test_database_error(self):
        london = self.geoip_data['London']
        session = self.session
        stmt = text("drop table wifi;")
        session.execute(stmt)
        stmt = text("drop table cell;")
        session.execute(stmt)
        cell = CellFactory.build()
        wifis = WifiFactory.build_batch(2)

        res = self.app.post_json(
            '/v1/geolocate?key=test', {
                "cellTowers": [
                    {
                        "radioType": cell.radio.name,
                        "mobileCountryCode": cell.mcc,
                        "mobileNetworkCode": cell.mnc,
                        "locationAreaCode": cell.lac,
                        "cellId": cell.cid
                    },
                ],
                "wifiAccessPoints": [
                    {
                        "macAddress": wifis[0].key
                    },
                    {
                        "macAddress": wifis[1].key
                    },
                ]
            },
            extra_environ={'HTTP_X_FORWARDED_FOR': london['ip']},
            status=200)

        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(
            res.json, {
                "location": {
                    "lat": london['latitude'],
                    "lng": london['longitude']
                },
                "accuracy": london['accuracy']
            })

        self.check_stats(timer=['request.v1.geolocate'],
                         counter=[
                             'request.v1.geolocate.200',
                             'geolocate.geoip_hit',
                         ])
        self.check_raven([('ProgrammingError', 2)])
예제 #40
0
 def test_many_errors(self):
     wifi = WifiFactory.build()
     wifis = [{'wrong_key': 'ab'} for i in range(100)]
     res = self.app.post_json(
         '/v1/submit',
         {'items': [{
             'lat': wifi.lat,
             'lon': wifi.lon,
             'wifi': wifis
         }]},
         status=400)
     self.assertEqual(res.content_type, 'application/json')
     self.assertTrue('errors' in res.json)
     self.assertTrue(len(res.json['errors']) < 10)
     self.check_raven(['JSONError'])
예제 #41
0
파일: tests.py 프로젝트: thebent/ichnaea
 def test_invalid_latitude(self):
     session = self.session
     wifi = WifiFactory.build()
     self.app.post_json('/v1/geosubmit?key=test', {
         "items": [
             {
                 "latitude": 12345.0,
                 "longitude": wifi.lon,
                 "wifiAccessPoints": [{
                     "macAddress": wifi.key,
                 }]
             },
         ]
     },
                        status=200)
     self.assertEquals(session.query(WifiObservation).count(), 0)
예제 #42
0
파일: tests.py 프로젝트: thebent/ichnaea
    def test_batches(self):
        session = self.session
        batch_size = 110
        wifis = WifiFactory.create_batch(batch_size)
        items = [{
            "latitude": wifis[i].lat,
            "longitude": wifis[i].lon + (i / 10000.0),
            "wifiAccessPoints": [{
                "macAddress": wifis[i].key
            }]
        } for i in range(batch_size)]

        # let's add a bad one, this will just be skipped
        items.append({'lat': 10, 'lon': 10, 'whatever': 'xx'})
        self.app.post_json('/v1/geosubmit?key=test', {"items": items},
                           status=200)

        result = session.query(WifiObservation).all()
        self.assertEqual(len(result), batch_size)
예제 #43
0
파일: tests.py 프로젝트: thebent/ichnaea
    def test_ok_no_existing_wifi(self):
        session = self.session
        wifi = WifiFactory.build()

        res = self.app.post_json('/v1/geosubmit?key=test', {
            "items": [
                {
                    "latitude":
                    wifi.lat,
                    "longitude":
                    wifi.lon,
                    "wifiAccessPoints": [{
                        "macAddress": wifi.key,
                        "age": 3,
                        "channel": 6,
                        "frequency": 2437,
                        "signalToNoiseRatio": 13,
                        "signalStrength": -77,
                    }]
                },
            ]
        },
                                 status=200)

        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.json, {})

        # Check that wifi exists
        query = session.query(Wifi)
        count = query.filter(Wifi.key == wifi.key).count()
        self.assertEquals(count, 1)

        # check that WifiObservation records are created
        result = session.query(WifiObservation).all()
        self.assertEquals(len(result), 1)
        obs = result[0]
        self.assertEqual(obs.lat, wifi.lat)
        self.assertEqual(obs.lon, wifi.lon)
        self.assertEqual(obs.key, wifi.key)
        self.assertEqual(obs.channel, 6)
        self.assertEqual(obs.signal, -77)
        self.assertEqual(obs.snr, 13)
예제 #44
0
파일: tests.py 프로젝트: thebent/ichnaea
 def test_invalid_float(self):
     wifi = WifiFactory.build()
     self.app.post_json('/v1/geosubmit?key=test', {
         "items": [
             {
                 "latitude": wifi.lat,
                 "longitude": wifi.lon,
                 "accuracy": float('+nan'),
                 "altitude": float('-inf'),
                 "wifiAccessPoints": [{
                     "macAddress": wifi.key,
                 }]
             },
         ]
     },
                        status=200)
     obs = self.session.query(WifiObservation).all()
     self.assertEqual(len(obs), 1)
     self.assertFalse(obs[0].accuracy)
     self.assertFalse(obs[0].altitude)
예제 #45
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, {
                "error": {
                    "errors": [{
                        "domain": "geolocation",
                        "reason": "notFound",
                        "message": "Not found",
                    }],
                    "code":
                    404,
                    "message":
                    "Not found"
                }
            })

        # 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'
        ],
                         timer=[self.metric_url])
        self.check_raven(total=0)