Esempio n. 1
0
    def to_python(cls, data):
        data = data.copy()

        ident = data.pop('id', None)
        if ident is not None:
            ident = trim(six.text_type(ident), 128)

        try:
            email = trim(validate_email(data.pop('email', None), False),
                         MAX_EMAIL_FIELD_LENGTH)
        except ValueError:
            raise InterfaceValidationError("Invalid value for 'email'")

        username = data.pop('username', None)
        if username is not None:
            username = trim(six.text_type(username), 128)

        name = data.pop('name', None)
        if name is not None:
            name = trim(six.text_type(name), 128)

        try:
            ip_address = validate_ip(data.pop('ip_address', None), False)
        except ValueError:
            raise InterfaceValidationError("Invalid value for 'ip_address'")

        geo = data.pop('geo', None)
        if not geo and ip_address:
            geo = Geo.from_ip_address(ip_address)
        elif geo:
            geo = Geo.to_python(geo)

        extra_data = data.pop('data', None)
        if not isinstance(extra_data, dict):
            extra_data = {}
        extra_data.update(data)

        # TODO(dcramer): patch in fix to deal w/ old data but not allow new
        # if not (ident or email or username or ip_address):
        #     raise ValueError('No identifying value')

        kwargs = {
            'id': ident,
            'email': email,
            'username': username,
            'ip_address': ip_address,
            'name': name,
            'geo': geo,
            'data': trim_dict(extra_data)
        }

        return cls(**kwargs)
Esempio n. 2
0
    def to_python(cls, data):
        data = data.copy()

        ident = data.pop('id', None)
        if ident is not None:
            ident = trim(six.text_type(ident), 128)

        try:
            email = trim(validate_email(data.pop('email', None), False), MAX_EMAIL_FIELD_LENGTH)
        except ValueError:
            raise InterfaceValidationError("Invalid value for 'email'")

        username = data.pop('username', None)
        if username is not None:
            username = trim(six.text_type(username), 128)

        name = data.pop('name', None)
        if name is not None:
            name = trim(six.text_type(name), 128)

        try:
            ip_address = validate_ip(data.pop('ip_address', None), False)
        except ValueError:
            raise InterfaceValidationError("Invalid value for 'ip_address'")

        geo = data.pop('geo', None)
        if not geo and ip_address:
            geo = Geo.from_ip_address(ip_address)
        elif geo:
            geo = Geo.to_python(geo)

        extra_data = data.pop('data', None)
        if not isinstance(extra_data, dict):
            extra_data = {}
        extra_data.update(data)

        # TODO(dcramer): patch in fix to deal w/ old data but not allow new
        # if not (ident or email or username or ip_address):
        #     raise ValueError('No identifying value')

        kwargs = {
            'id': ident,
            'email': email,
            'username': username,
            'ip_address': ip_address,
            'name': name,
            'geo': geo,
            'data': trim_dict(extra_data)
        }

        return cls(**kwargs)
Esempio n. 3
0
 def to_python(cls, data):
     data = data.copy()
     for key in ("id", "email", "username", "ip_address", "name", "geo",
                 "data"):
         data.setdefault(key, None)
     if data["geo"] is not None:
         data["geo"] = Geo.to_python(data["geo"])
     return cls(**data)
Esempio n. 4
0
    def to_python(cls, data, **kwargs):
        data = data.copy()
        for key in ("id", "email", "username", "ip_address", "name", "geo",
                    "data"):
            data.setdefault(key, None)
        if data["geo"] is not None:
            data["geo"] = Geo.to_python_subpath(data, ["geo"], **kwargs)

        return super().to_python(data, **kwargs)
Esempio n. 5
0
 def test_serialize_behavior(self):
     assert Geo.to_python({
         'country_code': 'US',
         'city': 'San Francisco',
         'region': 'CA',
     }).to_json() == {
         'country_code': 'US',
         'city': 'San Francisco',
         'region': 'CA',
     }
Esempio n. 6
0
 def to_python(cls, data):
     data = data.copy()
     for key in (
         'id',
         'email',
         'username',
         'ip_address',
         'name',
         'geo',
         'data',
     ):
         data.setdefault(key, None)
     if data['geo'] is not None:
         data['geo'] = Geo.to_python(data['geo'])
     return cls(**data)
Esempio n. 7
0
    def test_iso_8859_1_country_code(self, geo_by_addr_mock):
        # https://github.com/maxmind/geoip-api-python/releases/tag/v1.3.2
        # Previously GeoIP.country_names was populated from GeoIP_country_name in
        # the libGeoIP C API. Some versions of the libGeoIP include non-ASCII
        # ISO-8859-1 characters in these names, causing encoding errors under Python

        geo_by_addr_mock.return_value = {
            'city': 'San Francisco',
            'country_code': '\xc5lborg',
            'region': 'CA',
        }

        assert Geo.from_ip_address('192.168.0.1').to_json() == {
            'city': u'San Francisco',
            'country_code': u'\xc5lborg',
            'region': u'CA'
        }
Esempio n. 8
0
def test_iso_8859_1_country_code(geo_by_addr_mock):
    # https://github.com/maxmind/geoip-api-python/releases/tag/v1.3.2
    # Previously GeoIP.country_names was populated from GeoIP_country_name in
    # the libGeoIP C API. Some versions of the libGeoIP include non-ASCII
    # ISO-8859-1 characters in these names, causing encoding errors under Python

    geo_by_addr_mock.return_value = {
        "city": "San Francisco",
        "country_code": "\xc5lborg",
        "region": "CA",
    }

    assert Geo.from_ip_address("192.168.0.1").to_json() == {
        "city": u"San Francisco",
        "country_code": u"\xc5lborg",
        "region": u"CA",
    }
Esempio n. 9
0
def test_does_geo_from_ip(from_ip_address_mock):
    from sentry.interfaces.geo import Geo

    geo = {
        'city': 'San Francisco',
        'country_code': 'US',
        'region': 'CA',
    }
    from_ip_address_mock.return_value = Geo.to_python(geo)

    manager = EventManager(
        make_event(**{
            'user': {
                'ip_address': '192.168.0.1',
            },
        }))

    manager.normalize()
    data = manager.get_data()
    assert data['user']['ip_address'] == '192.168.0.1'
    assert data['user']['geo'] == geo
Esempio n. 10
0
    def test_from_ip_address(self, geo_by_addr_mock):
        geo_by_addr_mock.return_value = {
            'area_code': 415,
            'city': 'San Francisco',
            'country_code': 'US',
            'country_code3': 'USA',
            'country_name': 'United States',
            'dma_code': 807,
            'latitude': 37.79570007324219,
            'longitude': -122.4208984375,
            'metro_code': 807,
            'postal_code': '94109',
            'region': 'CA',
            'region_name': 'California',
            'time_zone': 'America/Los_Angeles'
        }

        assert Geo.from_ip_address('192.168.0.1').to_json() == {
            'country_code': 'US',
            'city': 'San Francisco',
            'region': 'CA',
        }
Esempio n. 11
0
def test_from_ip_address(geo_by_addr_mock):
    geo_by_addr_mock.return_value = {
        "area_code": 415,
        "city": "San Francisco",
        "country_code": "US",
        "country_code3": "USA",
        "country_name": "United States",
        "dma_code": 807,
        "latitude": 37.79570007324219,
        "longitude": -122.4208984375,
        "metro_code": 807,
        "postal_code": "94109",
        "region": "CA",
        "region_name": "California",
        "time_zone": "America/Los_Angeles",
    }

    assert Geo.from_ip_address("192.168.0.1").to_json() == {
        "country_code": "US",
        "city": "San Francisco",
        "region": "CA",
    }
Esempio n. 12
0
def test_does_geo_from_ip(from_ip_address_mock):
    from sentry.interfaces.geo import Geo

    geo = {
        'city': 'San Francisco',
        'country_code': 'US',
        'region': 'CA',
    }
    from_ip_address_mock.return_value = Geo.to_python(geo)

    manager = EventManager(
        make_event(
            **{
                'user': {
                    'ip_address': '192.168.0.1',
                },
            }
        )
    )

    manager.normalize()
    data = manager.get_data()
    assert data['user']['ip_address'] == '192.168.0.1'
    assert data['user']['geo'] == geo
Esempio n. 13
0
 def test_path(self):
     assert Geo().get_path() == 'geo'
Esempio n. 14
0
    def to_python(cls, data, rust_renormalized=RUST_RENORMALIZED_DEFAULT):
        if rust_renormalized:
            data = data.copy()
            for key in (
                    'id',
                    'email',
                    'username',
                    'ip_address',
                    'name',
                    'geo',
                    'data',
            ):
                data.setdefault(key, None)
            if data['geo'] is not None:
                data['geo'] = Geo.to_python(data['geo'])
            return cls(**data)

        data = data.copy()

        ident = data.pop('id', None)
        if ident is not None:
            ident = trim(six.text_type(ident), 128)

        email = data.pop('email', None)
        if not isinstance(email, six.string_types):
            email = None
        email = trim(email, MAX_EMAIL_FIELD_LENGTH)

        username = data.pop('username', None)
        if username is not None:
            username = trim(six.text_type(username), 128)

        name = data.pop('name', None)
        if name is not None:
            name = trim(six.text_type(name), 128)

        try:
            ip_address = validate_ip(data.pop('ip_address', None), False)
        except ValueError:
            ip_address = None

        geo = data.pop('geo', None)
        if not geo and ip_address:
            geo = Geo.from_ip_address(ip_address)
        elif geo:
            geo = Geo.to_python(geo)

        extra_data = data.pop('data', None)
        if not isinstance(extra_data, dict):
            extra_data = {}
        extra_data.update(data)

        # TODO(dcramer): patch in fix to deal w/ old data but not allow new
        # if not (ident or email or username or ip_address):
        #     raise ValueError('No identifying value')

        kwargs = {
            'id': ident,
            'email': email,
            'username': username,
            'ip_address': ip_address,
            'name': name,
            'geo': geo,
            'data': trim_dict(extra_data)
        }

        return cls(**kwargs)
Esempio n. 15
0
 def test_null_values(self):
     sink = {}
     assert Geo.to_python({}).to_json() == sink
     assert Geo.to_python({"country_code": None}).to_json() == sink
     assert Geo.to_python({"city": None}).to_json() == sink
     assert Geo.to_python({"region": None}).to_json() == sink
Esempio n. 16
0
    def to_python(cls, data, rust_renormalized=RUST_RENORMALIZED_DEFAULT):
        if rust_renormalized:
            data = data.copy()
            for key in (
                'id',
                'email',
                'username',
                'ip_address',
                'name',
                'geo',
                'data',
            ):
                data.setdefault(key, None)
            if data['geo'] is not None:
                data['geo'] = Geo.to_python(data['geo'])
            return cls(**data)

        data = data.copy()

        ident = data.pop('id', None)
        if ident is not None:
            ident = trim(six.text_type(ident), 128)

        email = data.pop('email', None)
        if not isinstance(email, six.string_types):
            email = None
        email = trim(email, MAX_EMAIL_FIELD_LENGTH)

        username = data.pop('username', None)
        if username is not None:
            username = trim(six.text_type(username), 128)

        name = data.pop('name', None)
        if name is not None:
            name = trim(six.text_type(name), 128)

        try:
            ip_address = validate_ip(data.pop('ip_address', None), False)
        except ValueError:
            ip_address = None

        geo = data.pop('geo', None)
        if not geo and ip_address:
            geo = Geo.from_ip_address(ip_address)
        elif geo:
            geo = Geo.to_python(geo)

        extra_data = data.pop('data', None)
        if not isinstance(extra_data, dict):
            extra_data = {}
        extra_data.update(data)

        # TODO(dcramer): patch in fix to deal w/ old data but not allow new
        # if not (ident or email or username or ip_address):
        #     raise ValueError('No identifying value')

        kwargs = {
            'id': ident,
            'email': email,
            'username': username,
            'ip_address': ip_address,
            'name': name,
            'geo': geo,
            'data': trim_dict(extra_data)
        }

        return cls(**kwargs)