Beispiel #1
0
async def test_middleware_not_behind_proxy(async_client):
    with mock.patch("ipinfo.AsyncHandler.getDetails") as mocked_getDetails:
        mocked_getDetails.return_value = Details({"ip": "127.0.0.1"})
        res = await async_client.get("/test_view/")

        mocked_getDetails.assert_called_once_with("127.0.0.1")
        assert res.status_code == HTTPStatus.OK
        assert b"For testing: 127.0.0.1" in res.content
Beispiel #2
0
async def test_middleware_behind_proxy(async_client):
    with mock.patch("ipinfo.AsyncHandler.getDetails") as mocked_getDetails:
        mocked_getDetails.return_value = Details({"ip": "93.44.186.197"})
        res = await async_client.get("/test_view/",
                                     X_FORWARDED_FOR="93.44.186.197")

        mocked_getDetails.assert_called_once_with("93.44.186.197")
        assert res.status_code == HTTPStatus.OK
        assert b"For testing: 93.44.186.197" in res.content
Beispiel #3
0
def mocked_ipinfo_call():
    """Mock the result of the IPInfo call.
    This call returns a custom class called Details
    Import it here and fill the class with mock data"""
    from ipinfo.details import Details

    raw_details = {
        'ip': u'8.8.8.8',
        'hostname': 'google-public-dns-a.google.com',
        'city': 'Mountain View',
        'region': 'California',
        'country': 'US',
        'loc': '37.3860,-122.0840',
        'postal': '94035',
        'phone': '650',
        'org': 'AS15169 Google LLC',
        'country_name': 'United States',
        'latitude': '37.3860',
        'longitude': '-122.0840'
    }

    #if sys.version_info < (3, 0):
    raw_details['ip_address'] = ipaddress.ip_address(raw_details.get('ip'))
    return Details(raw_details)
Beispiel #4
0
def test_init():
    data = {'foo': 'bar'}
    details = Details(data)
    assert details.details == data
Beispiel #5
0
def test_all():
    data = {'foo': 'bar', 'ham': 'eggs'}
    details = Details(data)
    assert details.all == data
Beispiel #6
0
def test_getattr_fail():
    data = {'foo': 'bar'}
    details = Details(data)
    with pytest.raises(Exception):
        details.blah
Beispiel #7
0
def test_getattr_success():
    data = {'foo': 'bar'}
    details = Details(data)
    assert details.foo == data['foo']
Beispiel #8
0
async def test_middleware_appends_ip_info(async_client):
    with mock.patch("ipinfo.AsyncHandler.getDetails") as mocked_getDetails:
        mocked_getDetails.return_value = Details({"ip": "127.0.0.1"})
        res = await async_client.get("/test_view/")
        assert res.status_code == HTTPStatus.OK
        assert b"For testing: 127.0.0.1" in res.content
Beispiel #9
0
def test_init():
    data = {"foo": "bar"}
    details = Details(data)
    assert details.details == data
Beispiel #10
0
def test_all():
    data = {"foo": "bar", "ham": "eggs"}
    details = Details(data)
    assert details.all == data
Beispiel #11
0
def test_getattr_fail():
    data = {"foo": "bar"}
    details = Details(data)
    with pytest.raises(Exception):
        details.blah
Beispiel #12
0
def test_getattr_success():
    data = {"foo": "bar"}
    details = Details(data)
    assert details.foo == data["foo"]