Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions ipinfo_django/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,23 @@ def __init__(self, get_response):
ipinfo_token = getattr(settings, "IPINFO_TOKEN", None)
ipinfo_settings = getattr(settings, "IPINFO_SETTINGS", {})
self.ipinfo = ipinfo.getHandlerAsyncCore(ipinfo_token, **ipinfo_settings)


class IPinfoPlusMiddleware(IPinfoMiddleware):
def __init__(self, get_response):
super().__init__(get_response=get_response)

ipinfo_token = getattr(settings, "IPINFO_TOKEN", None)
ipinfo_settings = getattr(settings, "IPINFO_SETTINGS", {})
self.ipinfo = ipinfo.getHandlerPlus(ipinfo_token, **ipinfo_settings)


class IPinfoAsyncPlusMiddleware(IPinfoAsyncMiddleware):
sync_capable = False
async_capable = True

def __init__(self, get_response):
super().__init__(get_response=get_response)
ipinfo_token = getattr(settings, "IPINFO_TOKEN", None)
ipinfo_settings = getattr(settings, "IPINFO_SETTINGS", {})
self.ipinfo = ipinfo.getHandlerAsyncPlus(ipinfo_token, **ipinfo_settings)
14 changes: 14 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,17 @@ def ipinfo_async_core_middleware(settings):
settings.MIDDLEWARE = [
"ipinfo_django.middleware.IPinfoAsyncCoreMiddleware",
]


@pytest.fixture
def ipinfo_plus_middleware(settings):
settings.MIDDLEWARE = [
"ipinfo_django.middleware.IPinfoPlusMiddleware",
]


@pytest.fixture
def ipinfo_async_plus_middleware(settings):
settings.MIDDLEWARE = [
"ipinfo_django.middleware.IPinfoAsyncPlusMiddleware",
]
43 changes: 43 additions & 0 deletions tests/test_async_plus_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from http import HTTPStatus
from unittest import mock

import pytest
from ipinfo.details import Details


@pytest.mark.asyncio
async def test_middleware_appends_ip_info(async_client, ipinfo_async_plus_middleware):
with mock.patch("ipinfo.AsyncHandlerPlus.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


@pytest.mark.asyncio
async def test_middleware_filters(async_client, ipinfo_async_plus_middleware):
res = await async_client.get("/test_view/", USER_AGENT="some bot")
assert res.status_code == HTTPStatus.OK
assert b"Request filtered." in res.content


@pytest.mark.asyncio
async def test_middleware_behind_proxy(async_client, ipinfo_async_plus_middleware):
with mock.patch("ipinfo.AsyncHandlerPlus.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


@pytest.mark.asyncio
async def test_middleware_not_behind_proxy(async_client, ipinfo_async_plus_middleware):
with mock.patch("ipinfo.AsyncHandlerPlus.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
38 changes: 38 additions & 0 deletions tests/test_plus_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from http import HTTPStatus
from unittest import mock

from ipinfo.details import Details


def test_middleware_appends_ip_info(client, ipinfo_plus_middleware):
with mock.patch("ipinfo.HandlerPlus.getDetails") as mocked_getDetails:
mocked_getDetails.return_value = Details({"ip": "127.0.0.1"})
res = client.get("/test_view/")
assert res.status_code == HTTPStatus.OK
assert b"For testing: 127.0.0.1" in res.content


def test_middleware_filters(client, ipinfo_plus_middleware):
res = client.get("/test_view/", HTTP_USER_AGENT="some bot")
assert res.status_code == HTTPStatus.OK
assert b"Request filtered." in res.content


def test_middleware_behind_proxy(client, ipinfo_plus_middleware):
with mock.patch("ipinfo.HandlerPlus.getDetails") as mocked_getDetails:
mocked_getDetails.return_value = Details({"ip": "93.44.186.197"})
res = client.get("/test_view/", HTTP_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 test_middleware_not_behind_proxy(client, ipinfo_plus_middleware):
with mock.patch("ipinfo.HandlerPlus.getDetails") as mocked_getDetails:
mocked_getDetails.return_value = Details({"ip": "127.0.0.1"})
res = 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