Skip to content

Commit 8104a7e

Browse files
authored
Merge pull request #40 from ipinfo/silvano/eng-517-add-plus-bundle-support-in-ipinfodjango-library
Add support for Plus bundle
2 parents 350811a + 515f6e6 commit 8104a7e

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

ipinfo_django/middleware.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,23 @@ def __init__(self, get_response):
119119
ipinfo_token = getattr(settings, "IPINFO_TOKEN", None)
120120
ipinfo_settings = getattr(settings, "IPINFO_SETTINGS", {})
121121
self.ipinfo = ipinfo.getHandlerAsyncCore(ipinfo_token, **ipinfo_settings)
122+
123+
124+
class IPinfoPlusMiddleware(IPinfoMiddleware):
125+
def __init__(self, get_response):
126+
super().__init__(get_response=get_response)
127+
128+
ipinfo_token = getattr(settings, "IPINFO_TOKEN", None)
129+
ipinfo_settings = getattr(settings, "IPINFO_SETTINGS", {})
130+
self.ipinfo = ipinfo.getHandlerPlus(ipinfo_token, **ipinfo_settings)
131+
132+
133+
class IPinfoAsyncPlusMiddleware(IPinfoAsyncMiddleware):
134+
sync_capable = False
135+
async_capable = True
136+
137+
def __init__(self, get_response):
138+
super().__init__(get_response=get_response)
139+
ipinfo_token = getattr(settings, "IPINFO_TOKEN", None)
140+
ipinfo_settings = getattr(settings, "IPINFO_SETTINGS", {})
141+
self.ipinfo = ipinfo.getHandlerAsyncPlus(ipinfo_token, **ipinfo_settings)

tests/conftest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,17 @@ def ipinfo_async_core_middleware(settings):
4141
settings.MIDDLEWARE = [
4242
"ipinfo_django.middleware.IPinfoAsyncCoreMiddleware",
4343
]
44+
45+
46+
@pytest.fixture
47+
def ipinfo_plus_middleware(settings):
48+
settings.MIDDLEWARE = [
49+
"ipinfo_django.middleware.IPinfoPlusMiddleware",
50+
]
51+
52+
53+
@pytest.fixture
54+
def ipinfo_async_plus_middleware(settings):
55+
settings.MIDDLEWARE = [
56+
"ipinfo_django.middleware.IPinfoAsyncPlusMiddleware",
57+
]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from http import HTTPStatus
2+
from unittest import mock
3+
4+
import pytest
5+
from ipinfo.details import Details
6+
7+
8+
@pytest.mark.asyncio
9+
async def test_middleware_appends_ip_info(async_client, ipinfo_async_plus_middleware):
10+
with mock.patch("ipinfo.AsyncHandlerPlus.getDetails") as mocked_getDetails:
11+
mocked_getDetails.return_value = Details({"ip": "127.0.0.1"})
12+
res = await async_client.get("/test_view/")
13+
assert res.status_code == HTTPStatus.OK
14+
assert b"For testing: 127.0.0.1" in res.content
15+
16+
17+
@pytest.mark.asyncio
18+
async def test_middleware_filters(async_client, ipinfo_async_plus_middleware):
19+
res = await async_client.get("/test_view/", USER_AGENT="some bot")
20+
assert res.status_code == HTTPStatus.OK
21+
assert b"Request filtered." in res.content
22+
23+
24+
@pytest.mark.asyncio
25+
async def test_middleware_behind_proxy(async_client, ipinfo_async_plus_middleware):
26+
with mock.patch("ipinfo.AsyncHandlerPlus.getDetails") as mocked_getDetails:
27+
mocked_getDetails.return_value = Details({"ip": "93.44.186.197"})
28+
res = await async_client.get("/test_view/", X_FORWARDED_FOR="93.44.186.197")
29+
30+
mocked_getDetails.assert_called_once_with("93.44.186.197")
31+
assert res.status_code == HTTPStatus.OK
32+
assert b"For testing: 93.44.186.197" in res.content
33+
34+
35+
@pytest.mark.asyncio
36+
async def test_middleware_not_behind_proxy(async_client, ipinfo_async_plus_middleware):
37+
with mock.patch("ipinfo.AsyncHandlerPlus.getDetails") as mocked_getDetails:
38+
mocked_getDetails.return_value = Details({"ip": "127.0.0.1"})
39+
res = await async_client.get("/test_view/")
40+
41+
mocked_getDetails.assert_called_once_with("127.0.0.1")
42+
assert res.status_code == HTTPStatus.OK
43+
assert b"For testing: 127.0.0.1" in res.content

tests/test_plus_middleware.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from http import HTTPStatus
2+
from unittest import mock
3+
4+
from ipinfo.details import Details
5+
6+
7+
def test_middleware_appends_ip_info(client, ipinfo_plus_middleware):
8+
with mock.patch("ipinfo.HandlerPlus.getDetails") as mocked_getDetails:
9+
mocked_getDetails.return_value = Details({"ip": "127.0.0.1"})
10+
res = client.get("/test_view/")
11+
assert res.status_code == HTTPStatus.OK
12+
assert b"For testing: 127.0.0.1" in res.content
13+
14+
15+
def test_middleware_filters(client, ipinfo_plus_middleware):
16+
res = client.get("/test_view/", HTTP_USER_AGENT="some bot")
17+
assert res.status_code == HTTPStatus.OK
18+
assert b"Request filtered." in res.content
19+
20+
21+
def test_middleware_behind_proxy(client, ipinfo_plus_middleware):
22+
with mock.patch("ipinfo.HandlerPlus.getDetails") as mocked_getDetails:
23+
mocked_getDetails.return_value = Details({"ip": "93.44.186.197"})
24+
res = client.get("/test_view/", HTTP_X_FORWARDED_FOR="93.44.186.197")
25+
26+
mocked_getDetails.assert_called_once_with("93.44.186.197")
27+
assert res.status_code == HTTPStatus.OK
28+
assert b"For testing: 93.44.186.197" in res.content
29+
30+
31+
def test_middleware_not_behind_proxy(client, ipinfo_plus_middleware):
32+
with mock.patch("ipinfo.HandlerPlus.getDetails") as mocked_getDetails:
33+
mocked_getDetails.return_value = Details({"ip": "127.0.0.1"})
34+
res = client.get("/test_view/")
35+
36+
mocked_getDetails.assert_called_once_with("127.0.0.1")
37+
assert res.status_code == HTTPStatus.OK
38+
assert b"For testing: 127.0.0.1" in res.content

0 commit comments

Comments
 (0)