Move well-known URLs into their own app

This commit is contained in:
Jake Howard 2022-08-22 20:33:52 +01:00
parent e74dbcc228
commit bfa4755871
Signed by: jake
GPG key ID: 57AFB45680EDD477
8 changed files with 106 additions and 85 deletions

View file

@ -37,35 +37,3 @@ class RobotsViewTestCase(SimpleTestCase):
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Disallow: /")
self.assertFalse(response.context["SEO_INDEX"])
class SecurityViewTestCase(TestCase):
url = reverse("securitytxt")
def test_accessible(self) -> None:
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.context["security_txt"],
"http://testserver/.well-known/security.txt",
)
class MatrixServerViewTestCase(SimpleTestCase):
url = reverse("matrix-server")
def test_accessible(self) -> None:
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "application/json")
self.assertTemplateUsed(response, "matrix-server.json")
class MatrixClientViewTestCase(SimpleTestCase):
url = reverse("matrix-client")
def test_accessible(self) -> None:
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "application/json")
self.assertTemplateUsed(response, "matrix-client.json")

View file

@ -1,13 +1,10 @@
from datetime import timedelta
from typing import Any
from django.http.response import HttpResponse
from django.urls import reverse
from django.utils import timezone
from django.views.defaults import ERROR_404_TEMPLATE_NAME
from django.views.generic import TemplateView
from website.contact.models import ContactPage
from website.home.models import HomePage
@ -36,31 +33,3 @@ class RobotsView(TemplateView):
context = super().get_context_data(**kwargs)
context["sitemap"] = self.request.build_absolute_uri(reverse("sitemap"))
return context
class SecurityView(TemplateView):
template_name = "security.txt"
content_type = "text/plain"
expires = timedelta(days=7)
def get_context_data(self, **kwargs: dict) -> dict:
context = super().get_context_data(**kwargs)
context["security_txt"] = self.request.build_absolute_uri(
reverse("securitytxt")
)
context["contact_page"] = ContactPage.objects.live().first()
context["expires"] = (
(timezone.now() + self.expires).replace(microsecond=0).isoformat()
)
return context
class MatrixServerView(TemplateView):
template_name = "matrix-server.json"
content_type = "application/json"
class MatrixClientView(TemplateView):
template_name = "matrix-client.json"
content_type = "application/json"

View file

@ -34,6 +34,7 @@ INSTALLED_APPS = [
"website.contact",
"website.spotify",
"website.utils",
"website.well_known",
"website.contrib.code_block",
"website.contrib.mermaid_block",
"website.contrib.unsplash",

View file

@ -7,13 +7,7 @@ from wagtail.contrib.sitemaps.views import sitemap
from wagtail.documents import urls as wagtaildocs_urls
from wagtail.images.views.serve import ServeView
from website.common.views import (
MatrixClientView,
MatrixServerView,
RobotsView,
SecurityView,
page_not_found,
)
from website.common.views import RobotsView, page_not_found
urlpatterns = [
path("admin/", include(wagtailadmin_urls)),
@ -22,6 +16,7 @@ urlpatterns = [
"code-block/",
include("website.contrib.code_block.urls"),
),
path(".well-known/", include("website.well_known.urls")),
re_path(
r"^images/([^/]*)/(\d*)/([^/]*)/[^/]*$",
ServeView.as_view(action="redirect"),
@ -29,21 +24,6 @@ urlpatterns = [
),
path("sitemap.xml", cache_page(60 * 60)(sitemap), name="sitemap"),
path("robots.txt", cache_page(60 * 60)(RobotsView.as_view()), name="robotstxt"),
path(
".well-known/security.txt",
cache_page(SecurityView.expires.total_seconds() / 2)(SecurityView.as_view()),
name="securitytxt",
),
path(
".well-known/matrix/server",
cache_page(60 * 60)(MatrixServerView.as_view()),
name="matrix-server",
),
path(
".well-known/matrix/client",
cache_page(60 * 60)(MatrixClientView.as_view()),
name="matrix-client",
),
path("404/", page_not_found, name="404"),
]

View file

View file

@ -0,0 +1,38 @@
from django.test import SimpleTestCase, TestCase
from django.urls import reverse
class SecurityViewTestCase(TestCase):
url = reverse("well-known:security-txt")
def test_accessible(self) -> None:
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.context["security_txt"],
"http://testserver/.well-known/security.txt",
)
def test_cache(self) -> None:
response = self.client.get(self.url)
self.assertEqual(response["Cache-Control"], "max-age=302400")
class MatrixServerViewTestCase(SimpleTestCase):
url = reverse("well-known:matrix-server")
def test_accessible(self) -> None:
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "application/json")
self.assertTemplateUsed(response, "matrix-server.json")
class MatrixClientViewTestCase(SimpleTestCase):
url = reverse("well-known:matrix-client")
def test_accessible(self) -> None:
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "application/json")
self.assertTemplateUsed(response, "matrix-client.json")

View file

@ -0,0 +1,23 @@
from django.urls import path
from . import views
app_name = "well-known"
urlpatterns = [
path(
"security.txt",
views.SecurityView.as_view(),
name="security-txt",
),
path(
"matrix/server",
views.MatrixServerView.as_view(),
name="matrix-server",
),
path(
"matrix/client",
views.MatrixClientView.as_view(),
name="matrix-client",
),
]

View file

@ -0,0 +1,42 @@
from datetime import timedelta
from django.http.request import HttpRequest
from django.http.response import HttpResponse
from django.utils import timezone
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django.views.generic import TemplateView
from website.contact.models import ContactPage
class SecurityView(TemplateView):
template_name = "security.txt"
content_type = "text/plain"
expires = timedelta(days=7)
@method_decorator(cache_page(int(expires.total_seconds() / 2)))
def dispatch(self, request: HttpRequest) -> HttpResponse:
return super().dispatch(request)
def get_context_data(self, **kwargs: dict) -> dict:
context = super().get_context_data(**kwargs)
context["security_txt"] = self.request.build_absolute_uri(self.request.path)
context["contact_page"] = ContactPage.objects.live().first()
context["expires"] = (
(timezone.now() + self.expires).replace(microsecond=0).isoformat()
)
return context
@method_decorator(cache_page(60 * 60), name="dispatch")
class MatrixServerView(TemplateView):
template_name = "matrix-server.json"
content_type = "application/json"
@method_decorator(cache_page(60 * 60), name="dispatch")
class MatrixClientView(TemplateView):
template_name = "matrix-client.json"
content_type = "application/json"