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')
Exemplo n.º 2
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)
Exemplo n.º 3
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)
Exemplo n.º 4
0
    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)
Exemplo n.º 5
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")
Exemplo n.º 6
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')
Exemplo n.º 7
0
    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, '')
Exemplo n.º 8
0
 def setUp(self):
     self.middleware = IPRestrictMiddleware()
     self.factory = RequestFactory()
Exemplo n.º 9
0
 def setUp(self):
     self.middleware = IPRestrictMiddleware(dummy_get_response)
     self.factory = RequestFactory()