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
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
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)
def test_init(): data = {'foo': 'bar'} details = Details(data) assert details.details == data
def test_all(): data = {'foo': 'bar', 'ham': 'eggs'} details = Details(data) assert details.all == data
def test_getattr_fail(): data = {'foo': 'bar'} details = Details(data) with pytest.raises(Exception): details.blah
def test_getattr_success(): data = {'foo': 'bar'} details = Details(data) assert details.foo == data['foo']
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
def test_init(): data = {"foo": "bar"} details = Details(data) assert details.details == data
def test_all(): data = {"foo": "bar", "ham": "eggs"} details = Details(data) assert details.all == data
def test_getattr_fail(): data = {"foo": "bar"} details = Details(data) with pytest.raises(Exception): details.blah
def test_getattr_success(): data = {"foo": "bar"} details = Details(data) assert details.foo == data["foo"]