Beispiel #1
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)
Beispiel #2
0
    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'))
Beispiel #3
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)
Beispiel #4
0
    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'))
Beispiel #5
0
    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)
        ])
Beispiel #6
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)
Beispiel #7
0
    def test_wifi_ignore_outlier(self):
        wifi = WifiFactory()
        wifis = WifiFactory.create_batch(3, lat=wifi.lat, lon=wifi.lon)
        wifis[0].lat = wifi.lat + 0.0001
        wifis[1].lat = wifi.lat + 0.0002
        wifis[2].lat = wifi.lat + 1.0
        self.session.flush()

        query = self.model_query(wifis=[wifi] + wifis)
        location = self.provider.locate(query)
        self.check_model_location(
            location, wifi, lat=wifi.lat + 0.0001)
Beispiel #8
0
    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'])
Beispiel #9
0
    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)
Beispiel #10
0
    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)])
    def test_update_wifi(self):
        wifis = WifiFactory.create_batch(2)
        obs = []
        obs_factory = WifiObservationFactory
        # first wifi
        wifi = wifis[0]
        wifi.total_measures = 3
        lat1, lon1 = (wifi.lat, wifi.lon)
        obs.extend([
            obs_factory(lat=wifi.lat,
                        lon=wifi.lon, key=wifi.key),
            obs_factory(lat=wifi.lat + 0.002,
                        lon=wifi.lon + 0.003, key=wifi.key),
            obs_factory(lat=wifi.lat + 0.004,
                        lon=wifi.lon + 0.006, key=wifi.key),
        ])
        wifi.lat = None
        wifi.lon = None
        # second wifi
        wifi = wifis[1]
        wifi.total_measures = 2
        wifi.lat += 1.0
        wifi.lon += 1.0
        lat2, lon2 = (wifi.lat, wifi.lon)
        obs.extend([
            obs_factory(lat=wifi.lat + 0.002,
                        lon=wifi.lon + 0.004, key=wifi.key),
            obs_factory(lat=wifi.lat + 0.002,
                        lon=wifi.lon + 0.004, key=wifi.key),
        ])
        self.data_queue.enqueue(obs)
        self.session.commit()

        result = update_wifi.delay()
        self.assertEqual(result.get(), (2, 0))
        self.check_stats(
            timer=['task.data.update_wifi'],
        )

        found = dict(self.session.query(Wifi.key, Wifi).all())
        self.assertEqual(set(found.keys()), set([wifis[0].key, wifis[1].key]))
        self.assertAlmostEqual(found[wifis[0].key].lat, lat1 + 0.002)
        self.assertAlmostEqual(found[wifis[0].key].lon, lon1 + 0.003)
        self.assertAlmostEqual(found[wifis[1].key].lat, lat2 + 0.001)
        self.assertAlmostEqual(found[wifis[1].key].lon, lon2 + 0.002)
Beispiel #12
0
    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)
    def test_remove_wifi(self):
        wifis = WifiFactory.create_batch(5)
        wifi_keys = [wifi.key for wifi in wifis]
        self.session.flush()

        result = remove_wifi.delay(wifi_keys[:2])
        self.assertEqual(result.get(), 2)

        wifis = self.session.query(Wifi).all()
        self.assertEqual(len(wifis), 3)

        result = remove_wifi.delay(wifi_keys)
        self.assertEqual(result.get(), 3)

        result = remove_wifi.delay(wifi_keys)
        self.assertEqual(result.get(), 0)

        wifis = self.session.query(Wifi).all()
        self.assertEqual(len(wifis), 0)
Beispiel #14
0
    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'])
    def test_blacklist_moving_wifis(self):
        now = util.utcnow()
        obs = []
        obs_factory = WifiObservationFactory
        moving = set()
        wifis = WifiFactory.create_batch(4)
        wifis.append(WifiFactory.build())
        # a wifi with an entry but no prior position
        wifi = wifis[0]
        wifi.total_measures = 0
        obs.extend([
            obs_factory(lat=wifi.lat + 0.001,
                        lon=wifi.lon + 0.001, key=wifi.key),
            obs_factory(lat=wifi.lat + 0.002,
                        lon=wifi.lon + 0.005, key=wifi.key),
            obs_factory(lat=wifi.lat + 0.003,
                        lon=wifi.lon + 0.009, key=wifi.key),
        ])
        wifi.lat = None
        wifi.lon = None
        # a wifi with a prior known position
        wifi = wifis[1]
        wifi.total_measures = 1
        wifi.lat += 1.0
        wifi.lon += 1.0
        obs.extend([
            obs_factory(lat=wifi.lat + 0.01,
                        lon=wifi.lon, key=wifi.key),
            obs_factory(lat=wifi.lat + 0.07,
                        lon=wifi.lon, key=wifi.key),
        ])
        moving.add(wifi.hashkey())
        # a wifi with a very different prior position
        wifi = wifis[2]
        wifi.total_measures = 1
        obs.extend([
            obs_factory(lat=wifi.lat + 2.0,
                        lon=wifi.lon + 2.0, key=wifi.key),
            obs_factory(lat=wifi.lat - 4.0,
                        lon=wifi.lon + 2.0, key=wifi.key),
        ])
        moving.add(wifi.hashkey())
        # another wifi with a prior known position (and negative lat)
        wifi = wifis[3]
        wifi.total_measures = 1
        wifi.lat *= -1.0
        obs.extend([
            obs_factory(lat=wifi.lat - 0.1,
                        lon=wifi.lon, key=wifi.key),
            obs_factory(lat=wifi.lat - 0.16,
                        lon=wifi.lon, key=wifi.key),
        ])
        moving.add(wifi.hashkey())
        # an already blacklisted wifi
        wifi = wifis[4]
        WifiBlacklistFactory(key=wifi.key, time=now, count=1)
        obs.extend([
            obs_factory(lat=wifi.lat,
                        lon=wifi.lon, key=wifi.key),
            obs_factory(lat=wifi.lat + 0.1,
                        lon=wifi.lon, key=wifi.key),
        ])
        moving.add(wifi.hashkey())

        self.data_queue.enqueue(obs)
        self.session.commit()

        result = update_wifi.delay()
        self.assertEqual(result.get(), (4, 3))

        black = self.session.query(WifiBlacklist).all()
        self.assertEqual(set([b.hashkey() for b in black]), moving)

        # test duplicate call
        result = update_wifi.delay()
        self.assertEqual(result.get(), (0, 0))

        self.check_stats(
            timer=[
                # We made duplicate calls
                ('task.data.update_wifi', 2),
                # One of those would've scheduled a remove_wifi task
                ('task.data.remove_wifi', 1)
            ])