authentik.core.tests.test_error_views
Test the views wired up as django's handler400/403/404/500
1"""Test the views wired up as django's handler400/403/404/500""" 2 3from django.test import RequestFactory, TestCase 4 5from authentik.core.tests.utils import create_test_brand 6from authentik.core.views.error import ( 7 BadRequestView, 8 ForbiddenView, 9 NotFoundView, 10 ServerErrorView, 11) 12 13HANDLERS = ( 14 (BadRequestView, 400), 15 (ForbiddenView, 403), 16 (NotFoundView, 404), 17 (ServerErrorView, 500), 18) 19UNSAFE_METHODS = ("post", "put", "patch", "delete") 20 21 22class TestErrorViews(TestCase): 23 """The error handlers must answer with their own status code for every request 24 method, not just GET. 25 26 Django calls handler400/403/404/500 with the request that failed, whatever its 27 method. A handler that only implements `get` answers a failing POST with 405 28 Method Not Allowed and drops its own status code -- which made an error on a 29 POST-only API endpoint surface to clients as `405`.""" 30 31 def setUp(self): 32 self.factory = RequestFactory() 33 34 def test_get(self): 35 """A GET keeps returning the handler's own status code""" 36 for view, expected in HANDLERS: 37 with self.subTest(view=view.__name__): 38 response = view.as_view()(self.factory.get("/")) 39 self.assertEqual(response.status_code, expected) 40 41 def test_unsafe_methods(self): 42 """A POST/PUT/PATCH/DELETE must not be answered with 405""" 43 for view, expected in HANDLERS: 44 for method in UNSAFE_METHODS: 45 with self.subTest(view=view.__name__, method=method): 46 request = getattr(self.factory, method)("/") 47 response = view.as_view()(request) 48 self.assertEqual(response.status_code, expected) 49 self.assertNotIn("Allow", response) 50 51 52class TestErrorViewsRouting(TestCase): 53 """End-to-end: an unresolvable path must 404 regardless of request method. 54 55 The DRF router matches a detail route's id with `[^/.]+`, so an id containing a 56 dot never resolves and django raises Http404 before any view runs -- the 57 "invalid ID" case that reported 405 rather than a descriptive error.""" 58 59 def setUp(self): 60 create_test_brand() 61 62 def test_unresolvable_api_path(self): 63 """Every method gets 404, and the error page still renders""" 64 url = "/api/v3/providers/scim/1.5/sync/object/" 65 for method in ("get", *UNSAFE_METHODS): 66 with self.subTest(method=method): 67 response = getattr(self.client, method)(url) 68 self.assertEqual(response.status_code, 404) 69 self.assertTemplateUsed(response, "if/error.html") 70 71 72class TestErrorViewsThemedBackground(TestCase): 73 """The error page renders without the UI, so it resolves a themed default flow 74 background itself instead of sending the browser a %(theme)s placeholder.""" 75 76 def test_themed_background(self): 77 """The light variant is the default and the dark variant follows data-theme""" 78 create_test_brand( 79 branding_default_flow_background="/static/authentik/sources/kerberos/%(theme)s.png" 80 ) 81 response = self.client.get("/this-path-does-not-exist/") 82 self.assertEqual(response.status_code, 404) 83 self.assertTemplateUsed(response, "if/error.html") 84 content = response.content.decode() 85 self.assertNotIn("%(theme)s", content) 86 self.assertIn( 87 '<link rel="prefetch" href="/static/authentik/sources/kerberos/light.png"', content 88 ) 89 self.assertRegex( 90 content, 91 r":root \{\s*--ak-global--background-image: " 92 r'url\("/static/authentik/sources/kerberos/light.png"\);', 93 ) 94 self.assertRegex( 95 content, 96 r':root\[data-theme="dark"\] \{\s*--ak-global--background-image: ' 97 r'url\("/static/authentik/sources/kerberos/dark.png"\);', 98 )
23class TestErrorViews(TestCase): 24 """The error handlers must answer with their own status code for every request 25 method, not just GET. 26 27 Django calls handler400/403/404/500 with the request that failed, whatever its 28 method. A handler that only implements `get` answers a failing POST with 405 29 Method Not Allowed and drops its own status code -- which made an error on a 30 POST-only API endpoint surface to clients as `405`.""" 31 32 def setUp(self): 33 self.factory = RequestFactory() 34 35 def test_get(self): 36 """A GET keeps returning the handler's own status code""" 37 for view, expected in HANDLERS: 38 with self.subTest(view=view.__name__): 39 response = view.as_view()(self.factory.get("/")) 40 self.assertEqual(response.status_code, expected) 41 42 def test_unsafe_methods(self): 43 """A POST/PUT/PATCH/DELETE must not be answered with 405""" 44 for view, expected in HANDLERS: 45 for method in UNSAFE_METHODS: 46 with self.subTest(view=view.__name__, method=method): 47 request = getattr(self.factory, method)("/") 48 response = view.as_view()(request) 49 self.assertEqual(response.status_code, expected) 50 self.assertNotIn("Allow", response)
The error handlers must answer with their own status code for every request method, not just GET.
Django calls handler400/403/404/500 with the request that failed, whatever its
method. A handler that only implements get answers a failing POST with 405
Method Not Allowed and drops its own status code -- which made an error on a
POST-only API endpoint surface to clients as 405.
35 def test_get(self): 36 """A GET keeps returning the handler's own status code""" 37 for view, expected in HANDLERS: 38 with self.subTest(view=view.__name__): 39 response = view.as_view()(self.factory.get("/")) 40 self.assertEqual(response.status_code, expected)
A GET keeps returning the handler's own status code
42 def test_unsafe_methods(self): 43 """A POST/PUT/PATCH/DELETE must not be answered with 405""" 44 for view, expected in HANDLERS: 45 for method in UNSAFE_METHODS: 46 with self.subTest(view=view.__name__, method=method): 47 request = getattr(self.factory, method)("/") 48 response = view.as_view()(request) 49 self.assertEqual(response.status_code, expected) 50 self.assertNotIn("Allow", response)
A POST/PUT/PATCH/DELETE must not be answered with 405
53class TestErrorViewsRouting(TestCase): 54 """End-to-end: an unresolvable path must 404 regardless of request method. 55 56 The DRF router matches a detail route's id with `[^/.]+`, so an id containing a 57 dot never resolves and django raises Http404 before any view runs -- the 58 "invalid ID" case that reported 405 rather than a descriptive error.""" 59 60 def setUp(self): 61 create_test_brand() 62 63 def test_unresolvable_api_path(self): 64 """Every method gets 404, and the error page still renders""" 65 url = "/api/v3/providers/scim/1.5/sync/object/" 66 for method in ("get", *UNSAFE_METHODS): 67 with self.subTest(method=method): 68 response = getattr(self.client, method)(url) 69 self.assertEqual(response.status_code, 404) 70 self.assertTemplateUsed(response, "if/error.html")
End-to-end: an unresolvable path must 404 regardless of request method.
The DRF router matches a detail route's id with [^/.]+, so an id containing a
dot never resolves and django raises Http404 before any view runs -- the
"invalid ID" case that reported 405 rather than a descriptive error.
63 def test_unresolvable_api_path(self): 64 """Every method gets 404, and the error page still renders""" 65 url = "/api/v3/providers/scim/1.5/sync/object/" 66 for method in ("get", *UNSAFE_METHODS): 67 with self.subTest(method=method): 68 response = getattr(self.client, method)(url) 69 self.assertEqual(response.status_code, 404) 70 self.assertTemplateUsed(response, "if/error.html")
Every method gets 404, and the error page still renders
73class TestErrorViewsThemedBackground(TestCase): 74 """The error page renders without the UI, so it resolves a themed default flow 75 background itself instead of sending the browser a %(theme)s placeholder.""" 76 77 def test_themed_background(self): 78 """The light variant is the default and the dark variant follows data-theme""" 79 create_test_brand( 80 branding_default_flow_background="/static/authentik/sources/kerberos/%(theme)s.png" 81 ) 82 response = self.client.get("/this-path-does-not-exist/") 83 self.assertEqual(response.status_code, 404) 84 self.assertTemplateUsed(response, "if/error.html") 85 content = response.content.decode() 86 self.assertNotIn("%(theme)s", content) 87 self.assertIn( 88 '<link rel="prefetch" href="/static/authentik/sources/kerberos/light.png"', content 89 ) 90 self.assertRegex( 91 content, 92 r":root \{\s*--ak-global--background-image: " 93 r'url\("/static/authentik/sources/kerberos/light.png"\);', 94 ) 95 self.assertRegex( 96 content, 97 r':root\[data-theme="dark"\] \{\s*--ak-global--background-image: ' 98 r'url\("/static/authentik/sources/kerberos/dark.png"\);', 99 )
The error page renders without the UI, so it resolves a themed default flow background itself instead of sending the browser a %(theme)s placeholder.
77 def test_themed_background(self): 78 """The light variant is the default and the dark variant follows data-theme""" 79 create_test_brand( 80 branding_default_flow_background="/static/authentik/sources/kerberos/%(theme)s.png" 81 ) 82 response = self.client.get("/this-path-does-not-exist/") 83 self.assertEqual(response.status_code, 404) 84 self.assertTemplateUsed(response, "if/error.html") 85 content = response.content.decode() 86 self.assertNotIn("%(theme)s", content) 87 self.assertIn( 88 '<link rel="prefetch" href="/static/authentik/sources/kerberos/light.png"', content 89 ) 90 self.assertRegex( 91 content, 92 r":root \{\s*--ak-global--background-image: " 93 r'url\("/static/authentik/sources/kerberos/light.png"\);', 94 ) 95 self.assertRegex( 96 content, 97 r':root\[data-theme="dark"\] \{\s*--ak-global--background-image: ' 98 r'url\("/static/authentik/sources/kerberos/dark.png"\);', 99 )
The light variant is the default and the dark variant follows data-theme