def test_single_proxy(self):
        self.middleware = IPRestrictMiddleware()
        request = self.factory.get('',
                                   REMOTE_ADDR=PROXY,
                                   HTTP_X_FORWARDED_FOR=LOCAL_IP)

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEquals(client_ip, LOCAL_IP)
    def test_ignore_bad_forwarded_addr(self):
        self.middleware = IPRestrictMiddleware()
        request = self.factory.get('',
                                   REMOTE_ADDR=LOCAL_IP,
                                   HTTP_X_FORWARDED_FOR="unknown,1.2.3.4")

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEquals(client_ip, '1.2.3.4')
class MiddlewareExtractClientIpTest(TestCase):
    def setUp(self):
        self.middleware = IPRestrictMiddleware()
        self.factory = RequestFactory()

    def test_remote_addr_only(self):
        self.middleware = IPRestrictMiddleware()
        request = self.factory.get('', REMOTE_ADDR=LOCAL_IP)

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEquals(client_ip, LOCAL_IP)

    def test_remote_addr_empty(self):
        self.middleware = IPRestrictMiddleware()
        request = self.factory.get('', REMOTE_ADDR='')

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEquals(client_ip, '')

    @override_settings(IPRESTRICT_TRUSTED_PROXIES=(PROXY, ))
    def test_single_proxy(self):
        self.middleware = IPRestrictMiddleware()
        request = self.factory.get('',
                                   REMOTE_ADDR=PROXY,
                                   HTTP_X_FORWARDED_FOR=LOCAL_IP)

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEquals(client_ip, LOCAL_IP)

    @override_settings(IPRESTRICT_TRUSTED_PROXIES=(PROXY, '2.2.2.2', '4.4.4.4')
                       )
    def test_multiple_proxies_one_not_trusted(self):
        self.middleware = IPRestrictMiddleware()
        proxies = ['2.2.2.2', '3.3.3.3', '4.4.4.4']
        request = self.factory.get('',
                                   REMOTE_ADDR=PROXY,
                                   HTTP_X_FORWARDED_FOR=', '.join([LOCAL_IP] +
                                                                  proxies))

        try:
            client_ip = self.middleware.extract_client_ip(request)
        except exceptions.PermissionDenied:
            pass
        else:
            self.fail('Should raise PermissionDenied exception')

    @override_settings(IPRESTRICT_TRUSTED_PROXIES=(PROXY, '2.2.2.2', '3.3.3.3',
                                                   '4.4.4.4'))
    def test_multiple_proxies_all_trusted(self):
        self.middleware = IPRestrictMiddleware()
        proxies = ['2.2.2.2', '3.3.3.3', '4.4.4.4']
        request = self.factory.get('',
                                   REMOTE_ADDR=PROXY,
                                   HTTP_X_FORWARDED_FOR=', '.join([LOCAL_IP] +
                                                                  proxies))

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEquals(client_ip, LOCAL_IP)
示例#4
0
    def test_trust_all_proxies_on(self):
        self.middleware = IPRestrictMiddleware(dummy_get_response)
        proxies = ["1.1.1.1", "2.2.2.2", "3.3.3.3", "4.4.4.4"]
        request = self.factory.get(
            "", REMOTE_ADDR=PROXY, HTTP_X_FORWARDED_FOR=", ".join([LOCAL_IP] + proxies)
        )

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEqual(client_ip, LOCAL_IP)
    def test_trust_all_proxies_on(self):
        self.middleware = IPRestrictMiddleware()
        proxies = ['1.1.1.1', '2.2.2.2', '3.3.3.3', '4.4.4.4']
        request = self.factory.get('',
                                   REMOTE_ADDR=PROXY,
                                   HTTP_X_FORWARDED_FOR=', '.join([LOCAL_IP] +
                                                                  proxies))

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEquals(client_ip, LOCAL_IP)
示例#6
0
    def test_multiple_proxies_one_not_trusted(self):
        self.middleware = IPRestrictMiddleware(dummy_get_response)
        proxies = ["2.2.2.2", "3.3.3.3", "4.4.4.4"]
        request = self.factory.get(
            "", REMOTE_ADDR=PROXY, HTTP_X_FORWARDED_FOR=", ".join([LOCAL_IP] + proxies)
        )

        try:
            _ = self.middleware.extract_client_ip(request)
        except exceptions.PermissionDenied:
            pass
        else:
            self.fail("Should raise PermissionDenied exception")
示例#7
0
class MiddlewareExtractClientIpTest(TestCase):
    def setUp(self):
        self.middleware = IPRestrictMiddleware()
        self.factory = RequestFactory()
 
    def test_remote_addr_only(self):
        self.middleware = IPRestrictMiddleware()
        request = self.factory.get('', REMOTE_ADDR=LOCAL_IP)

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEquals(client_ip, LOCAL_IP)

    def test_remote_addr_empty(self):
        self.middleware = IPRestrictMiddleware()
        request = self.factory.get('', REMOTE_ADDR='')

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEquals(client_ip, '')

    @override_settings(IPRESTRICT_TRUSTED_PROXIES=(PROXY,))
    def test_single_proxy(self):
        self.middleware = IPRestrictMiddleware()
        request = self.factory.get('', REMOTE_ADDR=PROXY, HTTP_X_FORWARDED_FOR = LOCAL_IP)

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEquals(client_ip, LOCAL_IP)

    @override_settings(IPRESTRICT_TRUSTED_PROXIES=(PROXY,'2.2.2.2','4.4.4.4'))
    def test_multiple_proxies_one_not_trusted(self):
        self.middleware = IPRestrictMiddleware()
        proxies = ['2.2.2.2', '3.3.3.3', '4.4.4.4']
        request = self.factory.get('', REMOTE_ADDR=PROXY, 
           HTTP_X_FORWARDED_FOR = ', '.join([LOCAL_IP] + proxies))
        
        try:
            client_ip = self.middleware.extract_client_ip(request)
        except exceptions.PermissionDenied:
            pass
        else:
            self.fail('Should raise PermissionDenied exception')

    @override_settings(IPRESTRICT_TRUSTED_PROXIES=(PROXY,'2.2.2.2','3.3.3.3', '4.4.4.4'))
    def test_multiple_proxies_all_trusted(self):
        self.middleware = IPRestrictMiddleware()
        proxies = ['2.2.2.2', '3.3.3.3', '4.4.4.4']
        request = self.factory.get('', REMOTE_ADDR=PROXY, 
           HTTP_X_FORWARDED_FOR = ', '.join([LOCAL_IP] + proxies))
        
        client_ip = self.middleware.extract_client_ip(request)
        self.assertEquals(client_ip, LOCAL_IP)
    def test_multiple_proxies_one_not_trusted(self):
        self.middleware = IPRestrictMiddleware()
        proxies = ['2.2.2.2', '3.3.3.3', '4.4.4.4']
        request = self.factory.get('',
                                   REMOTE_ADDR=PROXY,
                                   HTTP_X_FORWARDED_FOR=', '.join([LOCAL_IP] +
                                                                  proxies))

        try:
            client_ip = self.middleware.extract_client_ip(request)
        except exceptions.PermissionDenied:
            pass
        else:
            self.fail('Should raise PermissionDenied exception')
示例#9
0
 def test_multiple_proxies_all_trusted(self):
     self.middleware = IPRestrictMiddleware()
     proxies = ['2.2.2.2', '3.3.3.3', '4.4.4.4']
     request = self.factory.get('', REMOTE_ADDR=PROXY, 
        HTTP_X_FORWARDED_FOR = ', '.join([LOCAL_IP] + proxies))
     
     client_ip = self.middleware.extract_client_ip(request)
     self.assertEquals(client_ip, LOCAL_IP)
示例#10
0
 def test_multiple_proxies_one_not_trusted(self):
     self.middleware = IPRestrictMiddleware()
     proxies = ['2.2.2.2', '3.3.3.3', '4.4.4.4']
     request = self.factory.get('', REMOTE_ADDR=PROXY, 
        HTTP_X_FORWARDED_FOR = ', '.join([LOCAL_IP] + proxies))
     
     try:
         client_ip = self.middleware.extract_client_ip(request)
     except exceptions.PermissionDenied:
         pass
     else:
         self.fail('Should raise PermissionDenied exception')
示例#11
0
    def test_remote_addr_only(self):
        self.middleware = IPRestrictMiddleware()
        request = self.factory.get('', REMOTE_ADDR=LOCAL_IP)

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEquals(client_ip, LOCAL_IP)
示例#12
0
 def setUp(self):
     self.middleware = IPRestrictMiddleware()
     self.factory = RequestFactory()
示例#13
0
    def test_single_proxy(self):
        self.middleware = IPRestrictMiddleware()
        request = self.factory.get('', REMOTE_ADDR=PROXY, HTTP_X_FORWARDED_FOR = LOCAL_IP)

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEquals(client_ip, LOCAL_IP)
    def test_remote_addr_empty(self):
        self.middleware = IPRestrictMiddleware()
        request = self.factory.get('', REMOTE_ADDR='')

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEquals(client_ip, '')
 def setUp(self):
     self.middleware = IPRestrictMiddleware()
     self.factory = RequestFactory()
示例#16
0
 def setUp(self):
     self.middleware = IPRestrictMiddleware(dummy_get_response)
     self.factory = RequestFactory()
示例#17
0
class MiddlewareExtractClientIpTest(TestCase):
    def setUp(self):
        self.middleware = IPRestrictMiddleware(dummy_get_response)
        self.factory = RequestFactory()

    def test_remote_addr_only(self):
        request = self.factory.get("", REMOTE_ADDR=LOCAL_IP)

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEqual(client_ip, LOCAL_IP)

    def test_remote_addr_empty(self):
        request = self.factory.get("", REMOTE_ADDR="")

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEqual(client_ip, "")

    @override_settings(IPRESTRICT_TRUSTED_PROXIES=(PROXY,))
    def test_single_proxy(self):
        self.middleware = IPRestrictMiddleware(dummy_get_response)
        request = self.factory.get("", REMOTE_ADDR=PROXY, HTTP_X_FORWARDED_FOR=LOCAL_IP)

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEqual(client_ip, LOCAL_IP)

    @override_settings(IPRESTRICT_TRUSTED_PROXIES=(PROXY, "2.2.2.2", "4.4.4.4"))
    def test_multiple_proxies_one_not_trusted(self):
        self.middleware = IPRestrictMiddleware(dummy_get_response)
        proxies = ["2.2.2.2", "3.3.3.3", "4.4.4.4"]
        request = self.factory.get(
            "", REMOTE_ADDR=PROXY, HTTP_X_FORWARDED_FOR=", ".join([LOCAL_IP] + proxies)
        )

        try:
            _ = self.middleware.extract_client_ip(request)
        except exceptions.PermissionDenied:
            pass
        else:
            self.fail("Should raise PermissionDenied exception")

    @override_settings(
        IPRESTRICT_TRUSTED_PROXIES=(PROXY, "2.2.2.2", "3.3.3.3", "4.4.4.4")
    )
    def test_multiple_proxies_all_trusted(self):
        self.middleware = IPRestrictMiddleware(dummy_get_response)
        proxies = ["2.2.2.2", "3.3.3.3", "4.4.4.4"]
        request = self.factory.get(
            "", REMOTE_ADDR=PROXY, HTTP_X_FORWARDED_FOR=", ".join([LOCAL_IP] + proxies)
        )

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEqual(client_ip, LOCAL_IP)

    @override_settings(
        IPRESTRICT_TRUSTED_PROXIES=(PROXY,), IPRESTRICT_TRUST_ALL_PROXIES=True
    )
    def test_trust_all_proxies_on(self):
        self.middleware = IPRestrictMiddleware(dummy_get_response)
        proxies = ["1.1.1.1", "2.2.2.2", "3.3.3.3", "4.4.4.4"]
        request = self.factory.get(
            "", REMOTE_ADDR=PROXY, HTTP_X_FORWARDED_FOR=", ".join([LOCAL_IP] + proxies)
        )

        client_ip = self.middleware.extract_client_ip(request)
        self.assertEqual(client_ip, LOCAL_IP)