test django views in djangoviewsdefaults py nonexistenturls nonexistenturl this is in urls py othernonexistenturl this not in urls py requestfactory requestfactory classmethod def setuptestdatacls objects createnameboris article objects create titleold article slugoldarticle datecreateddatetime datetime2001 1 1 21 22 23 article objects create titlecurrent article slugcurrentarticle datecreateddatetime datetime2007 9 17 21 22 23 article objects create titlefuture article slugfuturearticle datecreateddatetime datetime3000 1 1 21 22 23 cls urlarticle urlarticle objects create titleold article slugoldarticle datecreateddatetime datetime2001 1 1 21 22 23 siteid1 domaintestserver nametestserver save def testpagenotfoundself a 404 status is returned by the pagenotfound view for url in self nonexistenturls response self client geturl self assertequalresponse statuscode 404 self assertinbh1not foundh1 response content self assertin bpthe requested resource was not found on this server p response content overridesettings templates backend django template backends django djangotemplates options loaders django template loaders locmem loader 404 html csrftoken def testcsrftokenin404self see ticket 14565 for url in self nonexistenturls response self client geturl self assertnotequalresponse content bnotprovided self assertnotequalresponse content b def testservererrorself the servererror view raises a 500 status response self client getservererror self assertcontainsresponse bh1server error 500h1 statuscode500 def testbadrequestself request self requestfactory get response badrequestrequest exception self assertcontainsresponse bh1bad request 400h1 statuscode400 overridesettings templates backend django template backends django djangotemplates options loaders django template loaders locmem loader 404 html this is a test template for a 404 error path requestpath exception exception 500 html this is a test template for a 500 error def testcustomtemplatesself response self client getservererror self assertcontainsresponse test template for a 500 error statuscode500 response self client getnosuchurl self assertcontainsresponse path nosuchurl statuscode404 self assertcontainsresponse exception resolver404 statuscode404 response self client gettechnical404 self assertcontains response exception testing technical 404 statuscode404 def testgetabsoluteurlattributesself a model can set attributes on the getabsoluteurl method self asserttrue getattrurlarticle getabsoluteurl purge false the attributes of the original getabsoluteurl must be added article urlarticle objects getpkself urlarticle pk self asserttrue getattrarticle getabsoluteurl purge false the attributes of the original getabsoluteurl must be added def testcustomtemplateswrongself request self requestfactory get with self assertraisestemplatedoesnotexist badrequestrequest exception templatenamenonexistent with self assertraisestemplatedoesnotexist permissiondeniedrequest exception templatenamenonexistent with self assertraisestemplatedoesnotexist pagenotfoundrequest http404 templatenamenonexistent with self assertraisestemplatedoesnotexist servererrorrequest templatenamenonexistent def testerrorpagesself request self requestfactory get for response title in badrequestrequest exception bbad request 400 permissiondeniedrequest exception b403 forbidden pagenotfoundrequest http404 bnot found servererrorrequest bserver error 500 with self subtesttitletitle self assertinb doctype html response content self assertinb html langen response content self assertinbhead response content self assertinbtitlestitle title response content self assertinbbody response content test django views in django views defaults py this is in urls py this not in urls py the 404 page should have the csrf_token available in the context see ticket 14565 404 html and 500 html templates are picked by their respective handler default error views should raise templatedoesnotexist when passed a template that doesn t exist
import datetime

from django.contrib.sites.models import Site
from django.http import Http404
from django.template import TemplateDoesNotExist
from django.test import RequestFactory, TestCase
from django.test.utils import override_settings
from django.views.defaults import (
    bad_request,
    page_not_found,
    permission_denied,
    server_error,
)

from ..models import Article, Author, UrlArticle


@override_settings(ROOT_URLCONF="view_tests.urls")
class DefaultsTests(TestCase):
    

    nonexistent_urls = [
        "/nonexistent_url/",  
        "/other_nonexistent_url/",  
    ]
    request_factory = RequestFactory()

    @classmethod
    def setUpTestData(cls):
        author = Author.objects.create(name="Boris")
        Article.objects.create(
            title="Old Article",
            slug="old_article",
            author=author,
            date_created=datetime.datetime(2001, 1, 1, 21, 22, 23),
        )
        Article.objects.create(
            title="Current Article",
            slug="current_article",
            author=author,
            date_created=datetime.datetime(2007, 9, 17, 21, 22, 23),
        )
        Article.objects.create(
            title="Future Article",
            slug="future_article",
            author=author,
            date_created=datetime.datetime(3000, 1, 1, 21, 22, 23),
        )
        cls.urlarticle = UrlArticle.objects.create(
            title="Old Article",
            slug="old_article",
            author=author,
            date_created=datetime.datetime(2001, 1, 1, 21, 22, 23),
        )
        Site(id=1, domain="testserver", name="testserver").save()

    def test_page_not_found(self):
        "A 404 status is returned by the page_not_found view"
        for url in self.nonexistent_urls:
            response = self.client.get(url)
            self.assertEqual(response.status_code, 404)
        self.assertIn(b"<h1>Not Found</h1>", response.content)
        self.assertIn(
            b"<p>The requested resource was not found on this server.</p>",
            response.content,
        )

    @override_settings(
        TEMPLATES=[
            {
                "BACKEND": "django.template.backends.django.DjangoTemplates",
                "OPTIONS": {
                    "loaders": [
                        (
                            "django.template.loaders.locmem.Loader",
                            {
                                "404.html": "{{ csrf_token }}",
                            },
                        ),
                    ],
                },
            }
        ]
    )
    def test_csrf_token_in_404(self):
        
        
        for url in self.nonexistent_urls:
            response = self.client.get(url)
            self.assertNotEqual(response.content, b"NOTPROVIDED")
            self.assertNotEqual(response.content, b"")

    def test_server_error(self):
        "The server_error view raises a 500 status"
        response = self.client.get("/server_error/")
        self.assertContains(response, b"<h1>Server Error (500)</h1>", status_code=500)

    def test_bad_request(self):
        request = self.request_factory.get("/")
        response = bad_request(request, Exception())
        self.assertContains(response, b"<h1>Bad Request (400)</h1>", status_code=400)

    @override_settings(
        TEMPLATES=[
            {
                "BACKEND": "django.template.backends.django.DjangoTemplates",
                "OPTIONS": {
                    "loaders": [
                        (
                            "django.template.loaders.locmem.Loader",
                            {
                                "404.html": (
                                    "This is a test template for a 404 error "
                                    "(path: {{ request_path }}, "
                                    "exception: {{ exception }})."
                                ),
                                "500.html": "This is a test template for a 500 error.",
                            },
                        ),
                    ],
                },
            }
        ]
    )
    def test_custom_templates(self):
        
        response = self.client.get("/server_error/")
        self.assertContains(response, "test template for a 500 error", status_code=500)
        response = self.client.get("/no_such_url/")
        self.assertContains(response, "path: /no_such_url/", status_code=404)
        self.assertContains(response, "exception: Resolver404", status_code=404)
        response = self.client.get("/technical404/")
        self.assertContains(
            response, "exception: Testing technical 404.", status_code=404
        )

    def test_get_absolute_url_attributes(self):
        "A model can set attributes on the get_absolute_url method"
        self.assertTrue(
            getattr(UrlArticle.get_absolute_url, "purge", False),
            "The attributes of the original get_absolute_url must be added.",
        )
        article = UrlArticle.objects.get(pk=self.urlarticle.pk)
        self.assertTrue(
            getattr(article.get_absolute_url, "purge", False),
            "The attributes of the original get_absolute_url must be added.",
        )

    def test_custom_templates_wrong(self):
        
        request = self.request_factory.get("/")

        with self.assertRaises(TemplateDoesNotExist):
            bad_request(request, Exception(), template_name="nonexistent")

        with self.assertRaises(TemplateDoesNotExist):
            permission_denied(request, Exception(), template_name="nonexistent")

        with self.assertRaises(TemplateDoesNotExist):
            page_not_found(request, Http404(), template_name="nonexistent")

        with self.assertRaises(TemplateDoesNotExist):
            server_error(request, template_name="nonexistent")

    def test_error_pages(self):
        request = self.request_factory.get("/")
        for response, title in (
            (bad_request(request, Exception()), b"Bad Request (400)"),
            (permission_denied(request, Exception()), b"403 Forbidden"),
            (page_not_found(request, Http404()), b"Not Found"),
            (server_error(request), b"Server Error (500)"),
        ):
            with self.subTest(title=title):
                self.assertIn(b"<!doctype html>", response.content)
                self.assertIn(b'<html lang="en">', response.content)
                self.assertIn(b"<head>", response.content)
                self.assertIn(b"<title>%s</title>" % title, response.content)
                self.assertIn(b"<body>", response.content)