Ejemplo n.º 1
0
 def test_country_ok(self, mock):
     mock.get(self.base_uri + 'country/1.2.3.4',
              json=self.country,
              status_code=200,
              headers={'Content-Type': self._content_type('country')})
     country = self.client.country('1.2.3.4')
     self.assertEqual(type(country), geoip2.models.Country,
                      'return value of client.country')
     self.assertEqual(country.continent.geoname_id, 42,
                      'continent geoname_id is 42')
     self.assertEqual(country.continent.code, 'NA', 'continent code is NA')
     self.assertEqual(country.continent.name, 'North America',
                      'continent name is North America')
     self.assertEqual(country.country.geoname_id, 1,
                      'country geoname_id is 1')
     self.assertIs(country.country.is_in_european_union, False,
                   'country is_in_european_union is False')
     self.assertEqual(country.country.iso_code, 'US',
                      'country iso_code is US')
     self.assertEqual(country.country.names,
                      {'en': 'United States of America'}, 'country names')
     self.assertEqual(country.country.name, 'United States of America',
                      'country name is United States of America')
     self.assertEqual(country.maxmind.queries_remaining, 11,
                      'queries_remaining is 11')
     self.assertIs(country.registered_country.is_in_european_union, True,
                   'registered_country is_in_european_union is True')
     self.assertEqual(country.traits.network,
                      compat_ip_network('1.2.3.0/24'), 'network')
     self.assertEqual(country.raw, self.country, 'raw response is correct')
Ejemplo n.º 2
0
 def test_city_ok(self, mock):
     mock.get(self.base_uri + 'city/' + '1.2.3.4',
              json=self.country,
              status_code=200,
              headers={'Content-Type': self._content_type('city')})
     city = self.client.city('1.2.3.4')
     self.assertEqual(type(city), geoip2.models.City,
                      'return value of client.city')
     self.assertEqual(city.traits.network, compat_ip_network('1.2.3.0/24'),
                      'network')
Ejemplo n.º 3
0
 def test_city_ok(self, mock):
     mock.get(
         self.base_uri + "city/" + "1.2.3.4",
         json=self.country,
         status_code=200,
         headers={"Content-Type": self._content_type("city")},
     )
     city = self.client.city("1.2.3.4")
     self.assertEqual(type(city), geoip2.models.City,
                      "return value of client.city")
     self.assertEqual(city.traits.network, compat_ip_network("1.2.3.0/24"),
                      "network")
Ejemplo n.º 4
0
 def test_insights_ok(self, mock):
     mock.get(self.base_uri + 'insights/1.2.3.4',
              json=self.insights,
              status_code=200,
              headers={'Content-Type': self._content_type('country')})
     insights = self.client.insights('1.2.3.4')
     self.assertEqual(type(insights), geoip2.models.Insights,
                      'return value of client.insights')
     self.assertEqual(insights.traits.network,
                      compat_ip_network('1.2.3.0/24'), 'network')
     self.assertEqual(insights.traits.static_ip_score, 1.3,
                      'static_ip_score is 1.3')
     self.assertEqual(insights.traits.user_count, 2, 'user_count is 2')
Ejemplo n.º 5
0
 def test_insights_ok(self, mock):
     mock.get(
         self.base_uri + "insights/1.2.3.4",
         json=self.insights,
         status_code=200,
         headers={"Content-Type": self._content_type("country")},
     )
     insights = self.client.insights("1.2.3.4")
     self.assertEqual(type(insights), geoip2.models.Insights,
                      "return value of client.insights")
     self.assertEqual(insights.traits.network,
                      compat_ip_network("1.2.3.0/24"), "network")
     self.assertEqual(insights.traits.static_ip_score, 1.3,
                      "static_ip_score is 1.3")
     self.assertEqual(insights.traits.user_count, 2, "user_count is 2")
Ejemplo n.º 6
0
    def network(self):
        """The network for the record"""
        network = self._network
        if isinstance(network, (ipaddress.IPv4Network, ipaddress.IPv6Network)):
            return network

        if network is None:
            ip_address = self.ip_address
            prefix_len = self._prefix_len
            if ip_address is None or prefix_len is None:
                return None
            network = "{}/{}".format(ip_address, prefix_len)
        network = compat_ip_network(network, False)
        self._network = network
        return network
Ejemplo n.º 7
0
    def network(self):
        """The network for the record"""
        # This code is duplicated for performance reasons
        # pylint: disable=duplicate-code
        network = self._network
        if isinstance(network, (ipaddress.IPv4Network, ipaddress.IPv6Network)):
            return network

        ip_address = self.ip_address
        prefix_len = self._prefix_len
        if ip_address is None or prefix_len is None:
            return None
        network = compat_ip_network("{}/{}".format(ip_address, prefix_len),
                                    False)
        self._network = network
        return network
Ejemplo n.º 8
0
 def test_country_ok(self, mock):
     mock.get(
         self.base_uri + "country/1.2.3.4",
         json=self.country,
         status_code=200,
         headers={"Content-Type": self._content_type("country")},
     )
     country = self.client.country("1.2.3.4")
     self.assertEqual(type(country), geoip2.models.Country,
                      "return value of client.country")
     self.assertEqual(country.continent.geoname_id, 42,
                      "continent geoname_id is 42")
     self.assertEqual(country.continent.code, "NA", "continent code is NA")
     self.assertEqual(country.continent.name, "North America",
                      "continent name is North America")
     self.assertEqual(country.country.geoname_id, 1,
                      "country geoname_id is 1")
     self.assertIs(
         country.country.is_in_european_union,
         False,
         "country is_in_european_union is False",
     )
     self.assertEqual(country.country.iso_code, "US",
                      "country iso_code is US")
     self.assertEqual(country.country.names,
                      {"en": "United States of America"}, "country names")
     self.assertEqual(
         country.country.name,
         "United States of America",
         "country name is United States of America",
     )
     self.assertEqual(country.maxmind.queries_remaining, 11,
                      "queries_remaining is 11")
     self.assertIs(
         country.registered_country.is_in_european_union,
         True,
         "registered_country is_in_european_union is True",
     )
     self.assertEqual(country.traits.network,
                      compat_ip_network("1.2.3.0/24"), "network")
     self.assertEqual(country.raw, self.country, "raw response is correct")