Esempio n. 1
0
def test_json_serialization():
    handler = Handler()
    fake_details = {
        "asn": {
            "asn": "AS20001",
            "domain": "twcable.com",
            "name": "Time Warner Cable Internet LLC",
            "route": "104.172.0.0/14",
            "type": "isp",
        },
        "city": "Los Angeles",
        "company": {
            "domain": "twcable.com",
            "name": "Time Warner Cable Internet LLC",
            "type": "isp",
        },
        "country": "US",
        "country_name": "United States",
        "hostname": "cpe-104-175-221-247.socal.res.rr.com",
        "ip": "104.175.221.247",
        "loc": "34.0293,-118.3570",
        "latitude": "34.0293",
        "longitude": "-118.3570",
        "phone": "323",
        "postal": "90016",
        "region": "California",
    }

    handler._requestDetails = lambda x: fake_details

    details = handler.getDetails(fake_details["ip"])
    assert isinstance(details, Details)
    assert json.dumps(details.all)
Esempio n. 2
0
def test_headers():
    token = "mytesttoken"
    handler = Handler(token)
    headers = handler._get_headers()

    assert "user-agent" in headers
    assert "accept" in headers
    assert "authorization" in headers
Esempio n. 3
0
def test_headers():
    token = 'mytesttoken'
    handler = Handler(token)
    headers = handler._get_headers()

    assert 'user-agent' in headers
    assert 'accept' in headers
    assert 'authorization' in headers
Esempio n. 4
0
def test_get_details():
    token = os.environ.get("IPINFO_TOKEN", "")
    handler = Handler(token)
    details = handler.getDetails("8.8.8.8")
    assert isinstance(details, Details)
    assert details.ip == "8.8.8.8"
    assert details.hostname == "dns.google"
    assert details.city == "Mountain View"
    assert details.region == "California"
    assert details.country == "US"
    assert details.country_name == "United States"
    assert details.loc == "37.4056,-122.0775"
    assert details.latitude == "37.4056"
    assert details.longitude == "-122.0775"
    assert details.postal == "94043"
    assert details.timezone == "America/Los_Angeles"
    if token:
        asn = details.asn
        assert asn["asn"] == "AS15169"
        assert asn["name"] == "Google LLC"
        assert asn["domain"] == "google.com"
        assert asn["route"] == "8.8.8.0/24"
        assert asn["type"] == "business"

        company = details.company
        assert company["name"] == "Google LLC"
        assert company["domain"] == "google.com"
        assert company["type"] == "business"

        privacy = details.privacy
        assert privacy["vpn"] == False
        assert privacy["proxy"] == False
        assert privacy["tor"] == False
        assert privacy["relay"] == False
        assert privacy["hosting"] == False
        assert privacy["service"] == ""

        abuse = details.abuse
        assert (abuse["address"] ==
                "US, CA, Mountain View, 1600 Amphitheatre Parkway, 94043")
        assert abuse["country"] == "US"
        assert abuse["email"] == "*****@*****.**"
        assert abuse["name"] == "Abuse"
        assert abuse["network"] == "8.8.8.0/24"
        assert abuse["phone"] == "+1-650-253-0000"

        domains = details.domains
        assert domains["ip"] == "8.8.8.8"
        # NOTE: actual number changes too much
        assert "total" in domains
        assert len(domains["domains"]) == 5
Esempio n. 5
0
def test_get_details():
    handler = Handler()
    fake_details = {"country": "US", "ip": "127.0.0.1", "loc": "12.34,56.78"}

    handler._requestDetails = lambda x: fake_details

    details = handler.getDetails(fake_details["ip"])
    assert isinstance(details, Details)
    assert details.country == fake_details["country"]
    assert details.country_name == "United States"
    assert details.ip == fake_details["ip"]
    assert details.loc == fake_details["loc"]
    assert details.longitude == "56.78"
    assert details.latitude == "12.34"
Esempio n. 6
0
def test_get_details():
    handler = Handler()
    fake_details = {'country': 'US', 'ip': '127.0.0.1', 'loc': '12.34,56.78'}

    handler._requestDetails = lambda x: fake_details

    details = handler.getDetails(fake_details['ip'])
    assert isinstance(details, Details)
    assert details.country == fake_details['country']
    assert details.country_name == 'United States'
    assert details.ip == fake_details['ip']
    assert isinstance(details.ip_address, ipaddress.IPv4Address)
    assert details.loc == fake_details['loc']
    assert details.longitude == '56.78'
    assert details.latitude == '12.34'
Esempio n. 7
0
def _prepare_batch_test():
    """Helper for preparing batch test cases."""
    token = os.environ.get("IPINFO_TOKEN", "")
    if not token:
        pytest.skip("token required for batch tests")
    handler = Handler(token)
    return handler, token, _batch_ip_addrs
Esempio n. 8
0
def test_get_batch_details(n):
    token = os.environ.get("IPINFO_TOKEN", "")
    if not token:
        pytest.skip("token required for batch tests")
    handler = Handler(token)
    ips = ["1.1.1.1", "8.8.8.8", "9.9.9.9"]
    details = handler.getBatchDetails(ips)

    for ip in ips:
        assert ip in details
        d = details[ip]
        assert d["ip"] == ip
        assert d["country"] == "US"
        assert d["country_name"] == "United States"
        if token:
            assert "asn" in d
            assert "company" in d
            assert "privacy" in d
            assert "abuse" in d
            assert "domains" in d
Esempio n. 9
0
    def test_success(self, circuits_app, ipinfo_query_ip, expected_results):
        """ Test calling with sample values for the parameters """
        # Import ipinfo here to avoid py2 failure on test setup
        from ipinfo.handler import Handler
        import ipinfo

        function_params = {"ipinfo_query_ip": ipinfo_query_ip}
        # Patch the init of Handler class to prevent outside calls
        with patch.object(Handler, "__init__",
                          lambda access_token: None) as mocked_handler:

            with patch.object(ipinfo, "getHandler", lambda token: Handler()):
                # Patch getDetails with a mock of the return data
                with patch.object(Handler, "getDetails") as mock_get_details:
                    mock_get_details.return_value = mocked_ipinfo_call()
                    results = call_fn_ipinfo_query_ip_address_function(
                        circuits_app, function_params)
                    assert (results["success"])
Esempio n. 10
0
def test_init():
    token = "mytesttoken"
    handler = Handler(token)
    assert handler.access_token == token
    assert isinstance(handler.cache, DefaultCache)
    assert "US" in handler.countries
Esempio n. 11
0
def test_get_map():
    handler = Handler()
    mapUrl = handler.getMap(open("tests/map-ips.txt").read().splitlines())
    print(f"got URL={mapUrl}")