def test_site_http_method_filter(self):
        """"""
        request = self.request_factory.request()
        request.path_info = u'/admin/sites/site/'
        request.site = get_current_site(request)
        user = request.user = self.user_model_cls.objects.get(**{self.user_model_cls.USERNAME_FIELD: TEST_USER_NAME})
        request.session = self.SessionStore(hashlib.md5(smart_bytes(getattr(user, user.USERNAME_FIELD))).hexdigest())
        request.META['HTTP_ACCEPT'] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
        middleware = FirewallMiddleware()

        site_rules = SiteRoutingRules.objects.get(site=request.site)
        site_rules.allowed_method = ['GET', 'HEAD']
        site_rules.save()
        request.method = 'POST'
        response = middleware.process_request(request)
        # Should not be None
        self.assertIsNotNone(response)
        self.assertEqual(response.status_code, 405)
        self.assertIn(request.method, smart_text(response.content))

        HACS_SITE_CACHE.clear()
        request.META['HTTP_ACCEPT'] = "application/json, text/javascript, */*; q=0.01"
        response = middleware.process_request(request)
        self.assertEqual(405, response.status_code)
        self.assertIsInstance(response, JsonResponse)
        self.assertEqual(json.loads(smart_text(response.content))['meta']['status'], 405)

        HACS_SITE_CACHE.clear()
        request.method = 'GET'
        response = middleware.process_request(request)
        # Should be None
        self.assertIsNone(response)
Ejemplo n.º 2
0
    def setUp(self):

        super(TestDynamicRouteMiddlewareFromBrowser, self).setUp()
        self.request_factory = RequestFactory()
        clean_lru()
        HACS_SITE_CACHE.clear()
        self._refactor_fixture()
    def setUp(self):

        super(TestMiddlewareFunction, self).setUp()
        self.request_factory = RequestFactory()
        HACS_SITE_CACHE.clear()
        # Let's clear the lru cache, as this function wrapped by lru_cache decorator
        clean_lru()
    def test_site_in_maintenance_mode(self):
        """
        :return:
        """
        request = self.request_factory.request()
        request.path_info = u'/admin/'
        request.site = get_current_site(request)
        user = request.user = self.user_model_cls.objects.get(**{self.user_model_cls.USERNAME_FIELD: TEST_USER_NAME})
        request.session = self.SessionStore(hashlib.md5(smart_bytes(getattr(user, user.USERNAME_FIELD))).hexdigest())
        middleware = FirewallMiddleware()
        site_rules = SiteRoutingRules.objects.get(site=request.site)
        site_rules.maintenance_mode = True
        site_rules.save()

        request.META['HTTP_ACCEPT'] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
        response = middleware.process_request(request)
        self.assertEqual(503, response.status_code)
        self.assertIn('503', smart_text(response.content))

        request.META['HTTP_ACCEPT'] = "application/json, text/javascript, */*; q=0.01"
        response = middleware.process_request(request)
        self.assertEqual(503, response.status_code)
        self.assertIsInstance(response, JsonResponse)
        self.assertEqual(json.loads(smart_text(response.content))['meta']['status'], 503)

        # Make sure no maintenance mode restriction
        clean_all_lru_caches()
        HACS_SITE_CACHE.clear()
        site_rules.maintenance_mode = False
        site_rules.save()
        response = middleware.process_request(request)
        self.assertIsNone(response)
Ejemplo n.º 5
0
    def setUp(self):

        super(TestMiddlewareFunction, self).setUp()
        self.request_factory = RequestFactory()
        HACS_SITE_CACHE.clear()
        # Let's clear the lru cache, as this function wrapped by lru_cache decorator
        clean_lru()
    def setUp(self):

        super(TestDynamicRouteMiddlewareFromBrowser, self).setUp()
        self.request_factory = RequestFactory()
        clean_lru()
        HACS_SITE_CACHE.clear()
        self.user_model_cls = get_user_model()
        self._refactor_fixture()
Ejemplo n.º 7
0
    def tearDown(self):
        super(TestDynamicRouteMiddleware, self).tearDown()
        for root, dirs, files in os.walk(settings.HACS_GENERATED_URLCONF_DIR):
            for file_name in files:
                if file_name.startswith('hacs'):
                    os.unlink(os.path.join(root, file_name))

        HACS_SITE_CACHE.clear()
    def tearDown(self):
        super(TestDynamicRouteMiddleware, self).tearDown()
        for root, dirs, files in os.walk(settings.HACS_GENERATED_URLCONF_DIR):
            for file_name in files:
                if file_name.startswith('hacs'):
                    os.unlink(os.path.join(root, file_name))

        HACS_SITE_CACHE.clear()
        HACS_ACCESS_CONTROL_LOCAL.__release_local__()
Ejemplo n.º 9
0
 def setUp(self):
     super(TestFirewallMiddlewareFromBrowser, self).setUp()
     self.request_factory = RequestFactory()
     self._refactor_fixture()
     engine = import_module(settings.SESSION_ENGINE)
     self.SessionStore = engine.SessionStore
     self.cache = caches[getattr(settings, 'HACS_CACHE_SETTING_NAME', HACS_CACHE_SETTING_NAME)]
     # Let's clear the lru cache, as this function wrapped by lru_cache decorator
     clean_lru()
     HACS_SITE_CACHE.clear()
    def tearDown(self):

        super(TestDynamicRouteMiddlewareFromBrowser, self).tearDown()
        for root, dirs, files in os.walk(settings.HACS_GENERATED_URLCONF_DIR):
            for file_name in files:
                if file_name.startswith('hacs'):
                    os.unlink(os.path.join(root, file_name))

        HACS_SITE_CACHE.clear()
        HACS_ACCESS_CONTROL_LOCAL.__release_local__()
 def setUp(self):
     super(TestFirewallMiddlewareFromBrowser, self).setUp()
     self.user_model_cls = get_user_model()
     self.request_factory = RequestFactory()
     self._refactor_fixture()
     engine = import_module(settings.SESSION_ENGINE)
     self.SessionStore = engine.SessionStore
     self.cache = caches[getattr(settings, 'HACS_CACHE_SETTING_NAME', HACS_CACHE_SETTING_NAME)]
     # Let's clear the lru cache, as this function wrapped by lru_cache decorator
     clean_lru()
     HACS_SITE_CACHE.clear()
Ejemplo n.º 12
0
    def tearDown(self):
        """
        :return:
        """
        super(TestSelect2ContentTypesViewFromBrowser, self).tearDown()
        for root, dirs, files in os.walk(settings.HACS_GENERATED_URLCONF_DIR):
            for file_name in files:
                if file_name.startswith('hacs'):
                    os.unlink(os.path.join(root, file_name))

        HACS_SITE_CACHE.clear()
        self.cache.clear()
    def tearDown(self):

        super(TestMiddlewareFunctionException, self).tearDown()
        HACS_SITE_CACHE.clear()
        HACS_ACCESS_CONTROL_LOCAL.__release_local__()
Ejemplo n.º 14
0
 def setUp(self):
     """"""
     super(TestSelect2ContentTypesViewFromBrowser, self).setUp()
     self.cache = caches[getattr(settings, 'HACS_CACHE_SETTING_NAME', HACS_CACHE_SETTING_NAME)]
     self.fix_password()
     HACS_SITE_CACHE.clear()
    def test_site_uri_blacklist_whitelist_filter(self):
        """
        :return:
        """
        request = self.request_factory.request()
        request.urlconf = None
        request.path_info = u'/admin/sites/site/'
        request.site = get_current_site(request)
        user = request.user = self.user_model_cls.objects.get(**{self.user_model_cls.USERNAME_FIELD: TEST_USER_NAME})
        request.session = self.SessionStore(hashlib.md5(smart_bytes(getattr(user, user.USERNAME_FIELD))).hexdigest())
        middleware = FirewallMiddleware()
        ##
        # Site Level URI Filter Test Start
        ##
        # BlackList
        site_rules = SiteRoutingRules.objects.get(site=request.site)
        site_rules.blacklisted_uri = "^a[a-zA-Z0-9/]+/sites/"
        site_rules.save()
        request.META['HTTP_ACCEPT'] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
        response = middleware.process_request(request)

        self.assertIsNotNone(response)
        self.assertEqual(response.status_code, 503)
        self.assertIn("503", smart_text(response.content))

        request.META['HTTP_ACCEPT'] = "application/json, text/javascript, */*; q=0.01"
        response = middleware.process_request(request)
        self.assertEqual(503, response.status_code)
        self.assertIsInstance(response, JsonResponse)
        self.assertEqual(json.loads(smart_text(response.content))['meta']['status'], 503)

        clean_all_lru_caches()
        HACS_SITE_CACHE.clear()
        site_rules.blacklisted_uri = None
        site_rules.save()
        response = middleware.process_request(request)
        self.assertIsNone(response)

        # Whitelist
        clean_all_lru_caches()
        HACS_SITE_CACHE.clear()
        site_rules = SiteRoutingRules.objects.get(site=request.site)
        site_rules.whitelisted_uri = "^a[a-zA-Z0-9/]+/sites/"
        site_rules.save()
        request.META['HTTP_ACCEPT'] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
        response = middleware.process_request(request)
        # pass filter, so should be None
        self.assertIsNone(response)

        clean_all_lru_caches()
        HACS_SITE_CACHE.clear()
        site_rules.whitelisted_uri = "^f[a-zA-Z0-9/]+/sites/"
        site_rules.save()

        response = middleware.process_request(request)
        self.assertIsNotNone(response)
        self.assertEqual(response.status_code, 503)
        self.assertIn("503", smart_text(response.content))

        request.META['HTTP_ACCEPT'] = "application/json, text/javascript, */*; q=0.01"
        response = middleware.process_request(request)
        self.assertEqual(503, response.status_code)
        self.assertIsInstance(response, JsonResponse)
        self.assertEqual(json.loads(smart_text(response.content))['meta']['status'], 503)
    def setUp(self):

        super(TestDynamicRouteMiddleware, self).setUp()
        self.request_factory = RequestFactory()
        clean_lru()
        HACS_SITE_CACHE.clear()
Ejemplo n.º 17
0
    def tearDown(self):

        super(TestMiddlewareFunctionException, self).tearDown()
        HACS_SITE_CACHE.clear()
Ejemplo n.º 18
0
    def setUp(self):

        super(TestDynamicRouteMiddleware, self).setUp()
        self.request_factory = RequestFactory()
        clean_lru()
        HACS_SITE_CACHE.clear()