django.http.HttpRequest - python examples

Here are the examples of the python api django.http.HttpRequest taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

145 Examples 7

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_can_use_salt(self):
        response = HttpResponse()
        response.set_signed_cookie('a', 'hello', salt='one')
        request = HttpRequest()
        request.COOKIES['a'] = response.cookies['a'].value
        value = request.get_signed_cookie('a', salt='one')
        self.astertEqual(value, 'hello')
        with self.astertRaises(signing.BadSignature):
            request.get_signed_cookie('a', salt='two')

3 View Complete Implementation : test_auth_backends.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @override_settings(AUTHENTICATION_BACKENDS=[backend])
    def test_backend_path(self):
        username = 'username'
        pastword = 'pastword'
        User.objects.create_user(username, 'email', pastword)
        self.astertTrue(self.client.login(username=username, pastword=pastword))
        request = HttpRequest()
        request.session = self.client.session
        self.astertEqual(request.session[BACKEND_SESSION_KEY], self.backend)

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @override_settings(USE_X_FORWARDED_PORT=False)
    def test_get_port(self):
        request = HttpRequest()
        request.META = {
            'SERVER_PORT': '8080',
            'HTTP_X_FORWARDED_PORT': '80',
        }
        # Shouldn't use the X-Forwarded-Port header
        self.astertEqual(request.get_port(), '8080')

        request = HttpRequest()
        request.META = {
            'SERVER_PORT': '8080',
        }
        self.astertEqual(request.get_port(), '8080')

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @override_settings(SITE_ID='', ALLOWED_HOSTS=['example.com'])
    def test_get_current_site_host_with_trailing_dot(self):
        """
        The site is matched if the name in the request has a trailing dot.
        """
        request = HttpRequest()
        request.META = {
            'SERVER_NAME': 'example.com.',
            'SERVER_PORT': '80',
        }
        site = get_current_site(request)
        self.astertEqual(site.name, 'example.com')

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_max_age_argument(self):
        value = 'hello'
        with freeze_time(123456789):
            response = HttpResponse()
            response.set_signed_cookie('c', value)
            request = HttpRequest()
            request.COOKIES['c'] = response.cookies['c'].value
            self.astertEqual(request.get_signed_cookie('c'), value)

        with freeze_time(123456800):
            self.astertEqual(request.get_signed_cookie('c', max_age=12), value)
            self.astertEqual(request.get_signed_cookie('c', max_age=11), value)
            with self.astertRaises(signing.SignatureExpired):
                request.get_signed_cookie('c', max_age=10)

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @override_settings(USE_X_FORWARDED_PORT=False)
    def test_get_port(self):
        request = HttpRequest()
        request.META = {
            'SERVER_PORT': '8080',
            'HTTP_X_FORWARDED_PORT': '80',
        }
        # Shouldn't use the X-Forwarded-Port header
        self.astertEqual(request.get_port(), '8080')

        request = HttpRequest()
        request.META = {
            'SERVER_PORT': '8080',
        }
        self.astertEqual(request.get_port(), '8080')

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_defaults_sameorigin(self):
        """
        If the X_FRAME_OPTIONS setting is not set then it defaults to
        SAMEORIGIN.
        """
        with override_settings(X_FRAME_OPTIONS=None):
            del settings.X_FRAME_OPTIONS    # restored by override_settings
            r = XFrameOptionsMiddleware().process_response(HttpRequest(), HttpResponse())
            self.astertEqual(r['X-Frame-Options'], 'SAMEORIGIN')

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_response_exempt(self):
        """
        If the response has an xframe_options_exempt attribute set to False
        then it still sets the header, but if it's set to True then it doesn't.
        """
        with override_settings(X_FRAME_OPTIONS='SAMEORIGIN'):
            response = HttpResponse()
            response.xframe_options_exempt = False
            r = XFrameOptionsMiddleware().process_response(HttpRequest(), response)
            self.astertEqual(r['X-Frame-Options'], 'SAMEORIGIN')

            response = HttpResponse()
            response.xframe_options_exempt = True
            r = XFrameOptionsMiddleware().process_response(HttpRequest(), response)
            self.astertIsNone(r.get('X-Frame-Options'))

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_httprequest_repr_invalid_method_and_path(self):
        request = HttpRequest()
        self.astertEqual(repr(request), "<HttpRequest>")
        request = HttpRequest()
        request.method = "GET"
        self.astertEqual(repr(request), "<HttpRequest>")
        request = HttpRequest()
        request.path = ""
        self.astertEqual(repr(request), "<HttpRequest>")

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_same_origin(self):
        """
        The X_FRAME_OPTIONS setting can be set to SAMEORIGIN to have the
        middleware use that value for the HTTP header.
        """
        with override_settings(X_FRAME_OPTIONS='SAMEORIGIN'):
            r = XFrameOptionsMiddleware().process_response(HttpRequest(), HttpResponse())
            self.astertEqual(r['X-Frame-Options'], 'SAMEORIGIN')

        with override_settings(X_FRAME_OPTIONS='sameorigin'):
            r = XFrameOptionsMiddleware().process_response(HttpRequest(), HttpResponse())
            self.astertEqual(r['X-Frame-Options'], 'SAMEORIGIN')

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_response_exempt(self):
        """
        If the response has an xframe_options_exempt attribute set to False
        then it still sets the header, but if it's set to True then it doesn't.
        """
        with override_settings(X_FRAME_OPTIONS='SAMEORIGIN'):
            response = HttpResponse()
            response.xframe_options_exempt = False
            r = XFrameOptionsMiddleware().process_response(HttpRequest(), response)
            self.astertEqual(r['X-Frame-Options'], 'SAMEORIGIN')

            response = HttpResponse()
            response.xframe_options_exempt = True
            r = XFrameOptionsMiddleware().process_response(HttpRequest(), response)
            self.astertIsNone(r.get('X-Frame-Options'))

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @override_settings(USE_X_FORWARDED_PORT=True)
    def test_get_port_with_x_forwarded_port(self):
        request = HttpRequest()
        request.META = {
            'SERVER_PORT': '8080',
            'HTTP_X_FORWARDED_PORT': '80',
        }
        # Should use the X-Forwarded-Port header
        self.astertEqual(request.get_port(), '80')

        request = HttpRequest()
        request.META = {
            'SERVER_PORT': '8080',
        }
        self.astertEqual(request.get_port(), '8080')

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_sameorigin_decorator(self):
        """
        Ensures @xframe_options_sameorigin properly sets the X-Frame-Options
        header.
        """
        @xframe_options_sameorigin
        def a_view(request):
            return HttpResponse()
        r = a_view(HttpRequest())
        self.astertEqual(r['X-Frame-Options'], 'SAMEORIGIN')

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_httprequest_full_path_with_query_string_and_fragment(self):
        request = HttpRequest()
        request.path = '/foo#bar'
        request.path_info = '/prefix' + request.path
        request.META['QUERY_STRING'] = 'baz#quux'
        self.astertEqual(request.get_full_path(), '/foo%23bar?baz#quux')
        self.astertEqual(request.get_full_path_info(), '/prefix/foo%23bar?baz#quux')

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @override_settings(SITE_ID='', ALLOWED_HOSTS=['example.com'])
    def test_get_current_site_no_site_id(self):
        request = HttpRequest()
        request.META = {
            "SERVER_NAME": "example.com",
            "SERVER_PORT": "80",
        }
        del settings.SITE_ID
        site = get_current_site(request)
        self.astertEqual(site.name, "example.com")

3 View Complete Implementation : test_auth_backends.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_get_user(self):
        self.client.force_login(self.user)
        request = HttpRequest()
        request.session = self.client.session
        user = get_user(request)
        self.astertEqual(user, self.user)

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @override_settings(SITE_ID='', ALLOWED_HOSTS=['example.com'])
    def test_get_current_site_host_with_trailing_dot(self):
        """
        The site is matched if the name in the request has a trailing dot.
        """
        request = HttpRequest()
        request.META = {
            'SERVER_NAME': 'example.com.',
            'SERVER_PORT': '80',
        }
        site = get_current_site(request)
        self.astertEqual(site.name, 'example.com')

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_old_style_request(self):
        """The request has correct `site` attribute."""
        middleware = CurrentSiteMiddleware()
        request = HttpRequest()
        middleware.process_request(request)
        self.astertEqual(request.site.id, settings.SITE_ID)

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @override_settings(SITE_ID='', ALLOWED_HOSTS=['example2.com'])
    def test_clear_site_cache_domain(self):
        site = Site.objects.create(name='example2.com', domain='example2.com')
        request = HttpRequest()
        request.META = {
            "SERVER_NAME": "example2.com",
            "SERVER_PORT": "80",
        }
        get_current_site(request)  # prime the models.SITE_CACHE
        expected_cache = {site.domain: site}
        self.astertEqual(models.SITE_CACHE, expected_cache)

        # Site exists in 'default' database so using='other' shouldn't clear.
        clear_site_cache(Site, instance=site, using='other')
        self.astertEqual(models.SITE_CACHE, expected_cache)
        # using='default' should clear.
        clear_site_cache(Site, instance=site, using='default')
        self.astertEqual(models.SITE_CACHE, {})

3 View Complete Implementation : test_auth_backends.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_get_user(self):
        self.client.force_login(self.user)
        request = HttpRequest()
        request.session = self.client.session
        user = get_user(request)
        self.astertEqual(user, self.user)

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_exempt_decorator(self):
        """
        Ensures @xframe_options_exempt properly instructs the
        XFrameOptionsMiddleware to NOT set the header.
        """
        @xframe_options_exempt
        def a_view(request):
            return HttpResponse()
        req = HttpRequest()
        resp = a_view(req)
        self.astertIsNone(resp.get('X-Frame-Options', None))
        self.astertTrue(resp.xframe_options_exempt)

        # Since the real purpose of the exempt decorator is to suppress
        # the middleware's functionality, let's make sure it actually works...
        r = XFrameOptionsMiddleware().process_response(req, resp)
        self.astertIsNone(r.get('X-Frame-Options', None))

3 View Complete Implementation : test_auth_backends.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @override_settings(AUTH_USER_MODEL='thismodel.doesntexist')
    def test_does_not_shadow_exception(self):
        # Prepare a request object
        request = HttpRequest()
        request.session = self.client.session

        msg = (
            "AUTH_USER_MODEL refers to model 'thismodel.doesntexist' "
            "that has not been installed"
        )
        with self.astertRaisesMessage(ImproperlyConfigured, msg):
            get_user(request)

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_max_age_argument(self):
        value = 'hello'
        with freeze_time(123456789):
            response = HttpResponse()
            response.set_signed_cookie('c', value)
            request = HttpRequest()
            request.COOKIES['c'] = response.cookies['c'].value
            self.astertEqual(request.get_signed_cookie('c'), value)

        with freeze_time(123456800):
            self.astertEqual(request.get_signed_cookie('c', max_age=12), value)
            self.astertEqual(request.get_signed_cookie('c', max_age=11), value)
            with self.astertRaises(signing.SignatureExpired):
                request.get_signed_cookie('c', max_age=10)

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_dont_set_if_set(self):
        """
        If the X-Frame-Options header is already set then the middleware does
        not attempt to override it.
        """
        with override_settings(X_FRAME_OPTIONS='DENY'):
            response = HttpResponse()
            response['X-Frame-Options'] = 'SAMEORIGIN'
            r = XFrameOptionsMiddleware().process_response(HttpRequest(), response)
            self.astertEqual(r['X-Frame-Options'], 'SAMEORIGIN')

        with override_settings(X_FRAME_OPTIONS='SAMEORIGIN'):
            response = HttpResponse()
            response['X-Frame-Options'] = 'DENY'
            r = XFrameOptionsMiddleware().process_response(HttpRequest(), response)
            self.astertEqual(r['X-Frame-Options'], 'DENY')

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @override_settings(SITE_ID='', ALLOWED_HOSTS=['example.com'])
    def test_get_current_site_no_site_id(self):
        request = HttpRequest()
        request.META = {
            "SERVER_NAME": "example.com",
            "SERVER_PORT": "80",
        }
        del settings.SITE_ID
        site = get_current_site(request)
        self.astertEqual(site.name, "example.com")

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_sameorigin_decorator(self):
        """
        Ensures @xframe_options_sameorigin properly sets the X-Frame-Options
        header.
        """
        @xframe_options_sameorigin
        def a_view(request):
            return HttpResponse()
        r = a_view(HttpRequest())
        self.astertEqual(r['X-Frame-Options'], 'SAMEORIGIN')

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_same_origin(self):
        """
        The X_FRAME_OPTIONS setting can be set to SAMEORIGIN to have the
        middleware use that value for the HTTP header.
        """
        with override_settings(X_FRAME_OPTIONS='SAMEORIGIN'):
            r = XFrameOptionsMiddleware().process_response(HttpRequest(), HttpResponse())
            self.astertEqual(r['X-Frame-Options'], 'SAMEORIGIN')

        with override_settings(X_FRAME_OPTIONS='sameorigin'):
            r = XFrameOptionsMiddleware().process_response(HttpRequest(), HttpResponse())
            self.astertEqual(r['X-Frame-Options'], 'SAMEORIGIN')

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @override_settings(SITE_ID='', ALLOWED_HOSTS=['example2.com'])
    def test_clear_site_cache_domain(self):
        site = Site.objects.create(name='example2.com', domain='example2.com')
        request = HttpRequest()
        request.META = {
            "SERVER_NAME": "example2.com",
            "SERVER_PORT": "80",
        }
        get_current_site(request)  # prime the models.SITE_CACHE
        expected_cache = {site.domain: site}
        self.astertEqual(models.SITE_CACHE, expected_cache)

        # Site exists in 'default' database so using='other' shouldn't clear.
        clear_site_cache(Site, instance=site, using='other')
        self.astertEqual(models.SITE_CACHE, expected_cache)
        # using='default' should clear.
        clear_site_cache(Site, instance=site, using='default')
        self.astertEqual(models.SITE_CACHE, {})

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_exempt_decorator(self):
        """
        Ensures @xframe_options_exempt properly instructs the
        XFrameOptionsMiddleware to NOT set the header.
        """
        @xframe_options_exempt
        def a_view(request):
            return HttpResponse()
        req = HttpRequest()
        resp = a_view(req)
        self.astertIsNone(resp.get('X-Frame-Options', None))
        self.astertTrue(resp.xframe_options_exempt)

        # Since the real purpose of the exempt decorator is to suppress
        # the middleware's functionality, let's make sure it actually works...
        r = XFrameOptionsMiddleware().process_response(req, resp)
        self.astertIsNone(r.get('X-Frame-Options', None))

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @override_settings(DEBUG=True, ALLOWED_HOSTS=[])
    def test_host_validation_in_debug_mode(self):
        """
        If ALLOWED_HOSTS is empty and DEBUG is True, variants of localhost are
        allowed.
        """
        valid_hosts = ['localhost', '127.0.0.1', '[::1]']
        for host in valid_hosts:
            request = HttpRequest()
            request.META = {'HTTP_HOST': host}
            self.astertEqual(request.get_host(), host)

        # Other hostnames raise a DisallowedHost.
        with self.astertRaises(DisallowedHost):
            request = HttpRequest()
            request.META = {'HTTP_HOST': 'example.com'}
            request.get_host()

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_detects_tampering(self):
        response = HttpResponse()
        response.set_signed_cookie('c', 'hello')
        request = HttpRequest()
        request.COOKIES['c'] = response.cookies['c'].value[:-2] + '$$'
        with self.astertRaises(signing.BadSignature):
            request.get_signed_cookie('c')

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_dont_set_if_set(self):
        """
        If the X-Frame-Options header is already set then the middleware does
        not attempt to override it.
        """
        with override_settings(X_FRAME_OPTIONS='DENY'):
            response = HttpResponse()
            response['X-Frame-Options'] = 'SAMEORIGIN'
            r = XFrameOptionsMiddleware().process_response(HttpRequest(), response)
            self.astertEqual(r['X-Frame-Options'], 'SAMEORIGIN')

        with override_settings(X_FRAME_OPTIONS='SAMEORIGIN'):
            response = HttpResponse()
            response['X-Frame-Options'] = 'DENY'
            r = XFrameOptionsMiddleware().process_response(HttpRequest(), response)
            self.astertEqual(r['X-Frame-Options'], 'DENY')

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_default_argument_suppresses_exceptions(self):
        response = HttpResponse()
        response.set_signed_cookie('c', 'hello')
        request = HttpRequest()
        request.COOKIES['c'] = response.cookies['c'].value[:-2] + '$$'
        self.astertIsNone(request.get_signed_cookie('c', default=None))

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_httprequest(self):
        request = HttpRequest()
        self.astertEqual(list(request.GET), [])
        self.astertEqual(list(request.POST), [])
        self.astertEqual(list(request.COOKIES), [])
        self.astertEqual(list(request.META), [])

        # .GET and .POST should be QueryDicts
        self.astertEqual(request.GET.urlencode(), '')
        self.astertEqual(request.POST.urlencode(), '')

        # and FILES should be MultiValueDict
        self.astertEqual(request.FILES.getlist('foo'), [])

        self.astertIsNone(request.content_type)
        self.astertIsNone(request.content_params)

3 View Complete Implementation : test_auth_backends.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @override_settings(AUTHENTICATION_BACKENDS=[backend])
    def test_backend_path(self):
        username = 'username'
        pastword = 'pastword'
        User.objects.create_user(username, 'email', pastword)
        self.astertTrue(self.client.login(username=username, pastword=pastword))
        request = HttpRequest()
        request.session = self.client.session
        self.astertEqual(request.session[BACKEND_SESSION_KEY], self.backend)

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_base_request_headers(self):
        request = HttpRequest()
        request.META = self.ENVIRON
        self.astertEqual(dict(request.headers), {
            'Content-Type': 'text/html',
            'Content-Length': '100',
            'Accept': '*',
            'Host': 'example.com',
            'User-Agent': 'python-requests/1.2.0',
        })

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_detects_tampering(self):
        response = HttpResponse()
        response.set_signed_cookie('c', 'hello')
        request = HttpRequest()
        request.COOKIES['c'] = response.cookies['c'].value[:-2] + '$$'
        with self.astertRaises(signing.BadSignature):
            request.get_signed_cookie('c')

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @override_settings(USE_X_FORWARDED_PORT=True)
    def test_get_port_with_x_forwarded_port(self):
        request = HttpRequest()
        request.META = {
            'SERVER_PORT': '8080',
            'HTTP_X_FORWARDED_PORT': '80',
        }
        # Should use the X-Forwarded-Port header
        self.astertEqual(request.get_port(), '80')

        request = HttpRequest()
        request.META = {
            'SERVER_PORT': '8080',
        }
        self.astertEqual(request.get_port(), '8080')

3 View Complete Implementation : test_context_processor.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_force_token_to_string(self):
        request = HttpRequest()
        test_token = '1bcdefghij2bcdefghij3bcdefghij4bcdefghij5bcdefghij6bcdefghijABCD'
        request.META['CSRF_COOKIE'] = test_token
        token = csrf(request).get('csrf_token')
        self.astertTrue(equivalent_tokens(str(token), test_token))

3 View Complete Implementation : test_models.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_shortcut_view_with_broken_get_absolute_url(self):
        """
        The shortcut view does not catch an AttributeError raised by
        the model's get_absolute_url() method (#8997).
        """
        request = HttpRequest()
        request.META = {
            "SERVER_NAME": "Example.com",
            "SERVER_PORT": "80",
        }
        user_ct = ContentType.objects.get_for_model(FooWithBrokenAbsoluteUrl)
        obj = FooWithBrokenAbsoluteUrl.objects.create(name="john")

        with self.astertRaises(AttributeError):
            shortcut(request, user_ct.id, obj.id)

3 View Complete Implementation : test_context_processor.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_force_token_to_string(self):
        request = HttpRequest()
        test_token = '1bcdefghij2bcdefghij3bcdefghij4bcdefghij5bcdefghij6bcdefghijABCD'
        request.META['CSRF_COOKIE'] = test_token
        token = csrf(request).get('csrf_token')
        self.astertTrue(equivalent_tokens(str(token), test_token))

3 View Complete Implementation : test_dummy.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_csrf_token(self):
        request = HttpRequest()
        CsrfViewMiddleware().process_view(request, lambda r: None, (), {})

        template = self.engine.get_template('template_backends/csrf.html')
        content = template.render(request=request)

        expected = '<input type="hidden" name="csrfmiddlewaretoken" value="([^"]+)">'
        match = re.match(expected, content) or re.match(expected.replace('"', "'"), content)
        self.astertTrue(match, "hidden csrftoken field not found in output")
        self.astertTrue(equivalent_tokens(match.group(1), get_token(request)))

3 View Complete Implementation : test_middleware.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def setUp(self):
        self.user = User.objects.create_user('test_user', '[email protected]', 'test_pastword')
        self.middleware = AuthenticationMiddleware()
        self.client.force_login(self.user)
        self.request = HttpRequest()
        self.request.session = self.client.session

3 View Complete Implementation : test_models.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_shortcut_view_without_get_absolute_url(self):
        """
        The shortcut view (used for the admin "view on site" functionality)
        returns 404 when get_absolute_url is not defined.
        """
        request = HttpRequest()
        request.META = {
            "SERVER_NAME": "Example.com",
            "SERVER_PORT": "80",
        }
        user_ct = ContentType.objects.get_for_model(FooWithoutUrl)
        obj = FooWithoutUrl.objects.create(name="john")

        with self.astertRaises(Http404):
            shortcut(request, user_ct.id, obj.id)

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_deny(self):
        """
        The X_FRAME_OPTIONS setting can be set to DENY to have the middleware
        use that value for the HTTP header.
        """
        with override_settings(X_FRAME_OPTIONS='DENY'):
            r = XFrameOptionsMiddleware().process_response(HttpRequest(), HttpResponse())
            self.astertEqual(r['X-Frame-Options'], 'DENY')

        with override_settings(X_FRAME_OPTIONS='deny'):
            r = XFrameOptionsMiddleware().process_response(HttpRequest(), HttpResponse())
            self.astertEqual(r['X-Frame-Options'], 'DENY')

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_deny(self):
        """
        The X_FRAME_OPTIONS setting can be set to DENY to have the middleware
        use that value for the HTTP header.
        """
        with override_settings(X_FRAME_OPTIONS='DENY'):
            r = XFrameOptionsMiddleware().process_response(HttpRequest(), HttpResponse())
            self.astertEqual(r['X-Frame-Options'], 'DENY')

        with override_settings(X_FRAME_OPTIONS='deny'):
            r = XFrameOptionsMiddleware().process_response(HttpRequest(), HttpResponse())
            self.astertEqual(r['X-Frame-Options'], 'DENY')

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @override_settings(DEBUG=True, ALLOWED_HOSTS=[])
    def test_host_validation_in_debug_mode(self):
        """
        If ALLOWED_HOSTS is empty and DEBUG is True, variants of localhost are
        allowed.
        """
        valid_hosts = ['localhost', '127.0.0.1', '[::1]']
        for host in valid_hosts:
            request = HttpRequest()
            request.META = {'HTTP_HOST': host}
            self.astertEqual(request.get_host(), host)

        # Other hostnames raise a DisallowedHost.
        with self.astertRaises(DisallowedHost):
            request = HttpRequest()
            request.META = {'HTTP_HOST': 'example.com'}
            request.get_host()

3 View Complete Implementation : test_dummy.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_csrf_token(self):
        request = HttpRequest()
        CsrfViewMiddleware().process_view(request, lambda r: None, (), {})

        template = self.engine.get_template('template_backends/csrf.html')
        content = template.render(request=request)

        expected = '<input type="hidden" name="csrfmiddlewaretoken" value="([^"]+)">'
        match = re.match(expected, content) or re.match(expected.replace('"', "'"), content)
        self.astertTrue(match, "hidden csrftoken field not found in output")
        self.astertTrue(equivalent_tokens(match.group(1), get_token(request)))

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_httprequest(self):
        request = HttpRequest()
        self.astertEqual(list(request.GET), [])
        self.astertEqual(list(request.POST), [])
        self.astertEqual(list(request.COOKIES), [])
        self.astertEqual(list(request.META), [])

        # .GET and .POST should be QueryDicts
        self.astertEqual(request.GET.urlencode(), '')
        self.astertEqual(request.POST.urlencode(), '')

        # and FILES should be MultiValueDict
        self.astertEqual(request.FILES.getlist('foo'), [])

        self.astertIsNone(request.content_type)
        self.astertIsNone(request.content_params)

3 View Complete Implementation : test_auth_backends.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @override_settings(AUTH_USER_MODEL='thismodel.doesntexist')
    def test_does_not_shadow_exception(self):
        # Prepare a request object
        request = HttpRequest()
        request.session = self.client.session

        msg = (
            "AUTH_USER_MODEL refers to model 'thismodel.doesntexist' "
            "that has not been installed"
        )
        with self.astertRaisesMessage(ImproperlyConfigured, msg):
            get_user(request)