Skip to content

Commit

Permalink
fixing more lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pratibhavermasalesforce committed Dec 27, 2023
1 parent c0e65db commit 18eb060
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
11 changes: 5 additions & 6 deletions metadeploy/multitenancy/iprestrict_middleware.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from metadeploy.api.models import SiteProfile
from . import current_site_id
from ipaddress import ip_network, ip_address
from django.conf import settings
from django.http import HttpResponseForbidden
from ipaddress import ip_network, ip_address
from metadeploy.api.models import SiteProfile
from . import current_site_id

ADMIN_URL = f"/{settings.ADMIN_URL}/".replace("//", "/")


class IPRestrictMiddleware:
def getSiteProfile(self):
def get_site_profile(self):
profile = SiteProfile.objects.filter(site=current_site_id()).first()
return profile

Expand All @@ -17,7 +17,7 @@ def __init__(self, get_response):

def __call__(self, request):
client_ip = request.META.get('REMOTE_ADDR', None)
profile = self.getSiteProfile()
profile = self.get_site_profile()
is_admin_url = request.path.startswith(ADMIN_URL)
has_ip_allowlist = (
hasattr(profile, "allowed_ip_addresses") and profile.allowed_ip_addresses
Expand All @@ -42,4 +42,3 @@ def validate_ip(self, target_ip, allowed_ips):
except ValueError:
continue
return False

14 changes: 7 additions & 7 deletions metadeploy/multitenancy/tests/test_iprestrict_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class IPRestrictionMiddlewaretest(TestCase):
def setUp(self):
self.factory = RequestFactory()

@patch('metadeploy.multitenancy.iprestrict_middleware.IPRestrictMiddleware.getSiteProfile')
@patch('metadeploy.multitenancy.iprestrict_middleware.IPRestrictMiddleware.get_site_profile')
def test_ip_restrict_middleware_with_matching_allowed_client_ip(self, mock_site_profile_get):
request = self.factory.get('/test')
request.META["REMOTE_ADDR"] = "127.0.0.1"
Expand All @@ -28,7 +28,7 @@ def test_ip_restrict_middleware_with_matching_allowed_client_ip(self, mock_site_
response = IPRestrictMiddleware(lambda x: x)(request)
assert response == request

@patch('metadeploy.multitenancy.iprestrict_middleware.IPRestrictMiddleware.getSiteProfile')
@patch('metadeploy.multitenancy.iprestrict_middleware.IPRestrictMiddleware.get_site_profile')
def test_ip_restrict_middleware_with_value_error_client_ip(self, mock_site_profile_get):
request = self.factory.get('/test')
request.META["REMOTE_ADDR"] = "127.0.0.1"
Expand All @@ -46,7 +46,7 @@ def test_ip_restrict_middleware_with_value_error_client_ip(self, mock_site_profi
response = IPRestrictMiddleware(lambda x: x)(request)
assert response == request

@patch('metadeploy.multitenancy.iprestrict_middleware.IPRestrictMiddleware.getSiteProfile')
@patch('metadeploy.multitenancy.iprestrict_middleware.IPRestrictMiddleware.get_site_profile')
def test_ip_restrict_middleware_with_matching_allowed_client_ip_cidr_format(self, mock_site_profile_get):
request = self.factory.get('/test')
request.META["REMOTE_ADDR"] = "127.0.0.1"
Expand All @@ -64,7 +64,7 @@ def test_ip_restrict_middleware_with_matching_allowed_client_ip_cidr_format(self
response = IPRestrictMiddleware(lambda x: x)(request)
assert response == request

@patch('metadeploy.multitenancy.iprestrict_middleware.IPRestrictMiddleware.getSiteProfile')
@patch('metadeploy.multitenancy.iprestrict_middleware.IPRestrictMiddleware.get_site_profile')
def test_ip_restrict_middleware_without_matching_allowed_client_ip(self, mock_site_profile_get):
request = self.factory.get('/test')
request.META["REMOTE_ADDR"] = "127.0.0.2"
Expand All @@ -82,7 +82,7 @@ def test_ip_restrict_middleware_without_matching_allowed_client_ip(self, mock_si
response = IPRestrictMiddleware(lambda x: x)(request)
assert response.status_code == 403

@patch('metadeploy.multitenancy.iprestrict_middleware.IPRestrictMiddleware.getSiteProfile')
@patch('metadeploy.multitenancy.iprestrict_middleware.IPRestrictMiddleware.get_site_profile')
def test_ip_restrict_middleware_without_allowed_list(self, mock_site_profile_get):
request = self.factory.get('/test')
request.META["REMOTE_ADDR"] = "127.0.0.1"
Expand All @@ -96,7 +96,7 @@ def test_ip_restrict_middleware_without_allowed_list(self, mock_site_profile_get
response = IPRestrictMiddleware(lambda x: x)(request)
assert response == request

@patch('metadeploy.multitenancy.iprestrict_middleware.IPRestrictMiddleware.getSiteProfile')
@patch('metadeploy.multitenancy.iprestrict_middleware.IPRestrictMiddleware.get_site_profile')
def test_ip_restrict_middleware_with_allowed_list_none(self, mock_site_profile_get):
request = self.factory.get('/test')
request.META["REMOTE_ADDR"] = "127.0.0.1"
Expand All @@ -111,7 +111,7 @@ def test_ip_restrict_middleware_with_allowed_list_none(self, mock_site_profile_g
response = IPRestrictMiddleware(lambda x: x)(request)
assert response == request

@patch('metadeploy.multitenancy.iprestrict_middleware.IPRestrictMiddleware.getSiteProfile')
@patch('metadeploy.multitenancy.iprestrict_middleware.IPRestrictMiddleware.get_site_profile')
def test_ip_restrict_middleware_without_matching_allowed_client_ip_admin_page(self, mock_site_profile_get):
request = self.factory.get('/admin/sites/site/1/change/')
request.META["REMOTE_ADDR"] = "127.0.0.2"
Expand Down

0 comments on commit 18eb060

Please sign in to comment.