def test_bad_request(self, mock_post): url = 'localhost' geoip = GeoIP(generate_settings(url=url)) mock_post.return_value = mock.Mock(status_code=404, json=lambda: None) ip = '3.3.3.3' result = geoip.lookup(ip) mock_post.assert_called_with('{0}/country.json'.format(url), timeout=0.2, data={'ip': ip}) eq_(result, 'restofworld')
def test_connection_error(self, mock_post): url = 'localhost' geoip = GeoIP(generate_settings(url=url)) mock_post.side_effect = requests.ConnectionError ip = '3.3.3.3' result = geoip.lookup(ip) mock_post.assert_called_with('{0}/country.json'.format(url), timeout=0.2, data={'ip': ip}) eq_(result, 'restofworld')
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['search_form'] = SearchForm() ip = self.kwargs['pk'] try: context['geoip'] = GeoIP().lookup(ip) except Exception as e: logger.error(e) try: context['domain'] = socket.gethostbyaddr(ip)[0] except Exception as e: logger.error(e) try: vt = VT() context['vt_ip'] = vt.getIPReport(ip) except Exception as e: logger.error(e) try: tm = ThreatMiner() context['tm_url'] = tm.getURIFromIP(ip) context['tm_sample'] = tm.getSamplesFromIP(ip) context['tm_report'] = tm.getReportFromIP(ip) except Exception as e: logger.error(e) context['bls'] = blacklist.objects.filter(Q(ip=ip)|Q(url__contains=ip)) count = context['bls'].count() if count > 0: context['bls_count'] = count context['events'] = Event.objects.filter(Q(info__icontains=ip)).order_by('-publish_timestamp') count = context['events'].count() if count > 0: context['events_count'] = count context['attributes'] = Attribute.objects.filter(Q(value__icontains=ip)).order_by('-timestamp') count = context['attributes'].count() if count > 0: context['attributes_count'] = count context['tws'] = tweet.objects.filter(Q(text__icontains=ip)).order_by('-datetime') count = context['tws'].count() if count > 0: context['tws_count'] = count context['exs'] = Exploit.objects.filter(Q(text__icontains=ip)).order_by('-datetime') count = context['exs'].count() if count > 0: context['exs_count'] = count return context
def test_lookup(self, mock_post): url = 'localhost' geoip = GeoIP(generate_settings(url=url)) mock_post.return_value = mock.Mock(status_code=200, json=lambda: { 'country_code': 'US', 'country_name': 'United States' }) ip = '1.1.1.1' result = geoip.lookup(ip) mock_post.assert_called_with('{0}/country.json'.format(url), timeout=0.2, data={'ip': ip}) eq_(result, 'us')
def test_private_ip(self, mock_post): url = 'localhost' geoip = GeoIP(generate_settings(url=url)) addrs = [ '127.0.0.1', '10.{0}.{1}.{2}'.format(randint(0, 255), randint(0, 255), randint(0, 255)), '192.168.{0}.{1}'.format(randint(0, 255), randint(0, 255)), '172.{0}.{1}.{2}'.format(randint(16, 31), randint(0, 255), randint(0, 255)) ] for ip in addrs: result = geoip.lookup(ip) assert not mock_post.called eq_(result, 'restofworld')
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['search_form'] = SearchForm() domain = self.kwargs['pk'] try: context['geoip'] = GeoIP().lookup(domain) except Exception as e: logger.error(e) try: context['ipaddress'] = socket.gethostbyname(domain) except Exception as e: logger.error(e) try: context['vt_domain'] = VT().getDomainReport(domain) except Exception as e: logger.error(e) context['bls'] = blacklist.objects.filter(Q(domain=domain)|Q(url__contains=domain)) count = context['bls'].count() if count > 0: context['bls_count'] = count context['events'] = Event.objects.filter(Q(info__icontains=domain)).order_by('-publish_timestamp') count = context['events'].count() if count > 0: context['events_count'] = count context['attributes'] = Attribute.objects.filter(Q(value__icontains=domain)).order_by('-timestamp') count = context['attributes'].count() if count > 0: context['attributes_count'] = count context['tws'] = tweet.objects.filter(Q(text__icontains=domain)).order_by('-datetime') count = context['tws'].count() if count > 0: context['tws_count'] = count context['exs'] = Exploit.objects.filter(Q(text__icontains=domain)).order_by('-datetime') count = context['exs'].count() if count > 0: context['exs_count'] = count return context
def __init__(self): self.geoip = GeoIP(settings)
def test_no_url(self, mock_post): geoip = GeoIP(generate_settings()) result = geoip.lookup('2.2.2.2') assert not mock_post.called eq_(result, 'restofworld')
def setUp(self): # Use GEOIP_NOOP to always return the default value. # This *should* be properly tested against a call to a GeoIP server. self.geoip = GeoIP(NOOP_Settings)