Fix sitemap and add tests

This commit is contained in:
Joakim Hellsén 2024-03-16 19:04:26 +01:00
commit 832d3f1fd4
No known key found for this signature in database
GPG key ID: D196AE66FEBE1DC9
6 changed files with 21 additions and 9 deletions

View file

@ -13,8 +13,9 @@ BASE_DIR: Path = Path(__file__).resolve().parent.parent
SECRET_KEY: str = os.getenv("SECRET_KEY", default="") SECRET_KEY: str = os.getenv("SECRET_KEY", default="")
ADMINS: list[tuple[str, str]] = [("Joakim Hellsén", "django@feedvault.se")] ADMINS: list[tuple[str, str]] = [("Joakim Hellsén", "django@feedvault.se")]
ALLOWED_HOSTS: list[str] = [".feedvault.se", ".localhost", "127.0.0.1"] ALLOWED_HOSTS: list[str] = [".feedvault.se", ".localhost", "127.0.0.1"]
CSRF_COOKIE_DOMAIN = ".feedvault.se" if not DEBUG:
CSRF_TRUSTED_ORIGINS: list[str] = ["https://feedvault.se", "https://www.feedvault.se"] CSRF_COOKIE_DOMAIN = ".feedvault.se"
CSRF_TRUSTED_ORIGINS: list[str] = ["https://feedvault.se", "https://www.feedvault.se"]
TIME_ZONE = "Europe/Stockholm" TIME_ZONE = "Europe/Stockholm"
USE_TZ = True USE_TZ = True
USE_I18N = False USE_I18N = False

View file

@ -12,7 +12,7 @@ class StaticViewSitemap(Sitemap):
def items(self: StaticViewSitemap) -> list[str]: def items(self: StaticViewSitemap) -> list[str]:
"""Return all the items in the sitemap.""" """Return all the items in the sitemap."""
return ["feeds:index", "feeds:feeds", "feeds:domains"] return ["index", "feeds", "domains"]
def location(self: StaticViewSitemap, item: str) -> str: def location(self: StaticViewSitemap, item: str) -> str:
"""Return the location of the item.""" """Return the location of the item."""

View file

@ -85,8 +85,9 @@ class TestRobotsPage(TestCase):
"""Test if the robots page contains the expected content.""" """Test if the robots page contains the expected content."""
response: HttpResponse = self.client.get(reverse("robots")) response: HttpResponse = self.client.get(reverse("robots"))
assert ( assert (
response.content == b"User-agent: *\nDisallow: /add\nDisallow: /upload\nDisallow: /accounts/" response.content
), f"Expected b'User-agent: *\nDisallow: /add\nDisallow: /upload\nDisallow: /accounts/', got {response.content}" == b"User-agent: *\nDisallow: /add\nDisallow: /upload\nDisallow: /accounts/\n\nSitemap: https://feedvault.se/sitemap.xml"
), f"Expected b'User-agent: *\nDisallow: /add\nDisallow: /upload\nDisallow: /accounts/\n\nSitemap: https://feedvault.se/sitemap.xml', got {response.content}" # noqa: E501
class TestDomains(TestCase): class TestDomains(TestCase):
@ -189,3 +190,15 @@ class TestLogoutPage(TestCase):
response: HttpResponse = self.client.get(reverse("index")) response: HttpResponse = self.client.get(reverse("index"))
assert response.status_code == 200 assert response.status_code == 200
assert "testuser" not in response.content.decode("utf-8") assert "testuser" not in response.content.decode("utf-8")
class TestSitemap(TestCase):
def test_sitemap(self) -> None:
"""Test if the sitemap is accessible."""
response: HttpResponse = self.client.get(reverse("django.contrib.sitemaps.views.sitemap"))
assert response.status_code == 200, f"Expected 200, got {response.status_code}"
assert "urlset" in response.content.decode()
response2 = self.client.get("/sitemap.xml")
assert response2.status_code == 200, f"Expected 200, got {response2.status_code}"
assert "urlset" in response2.content.decode()

View file

@ -13,14 +13,12 @@ from feedvault.views import CustomLoginView, CustomLogoutView, ProfileView, Regi
app_name: str = "feedvault" app_name: str = "feedvault"
sitemaps = { sitemaps = {
"static": StaticViewSitemap, "static": StaticViewSitemap,
"feeds": GenericSitemap({"queryset": Feed.objects.all(), "date_field": "created_at"}), "feeds": GenericSitemap({"queryset": Feed.objects.all(), "date_field": "created_at"}),
"domains": GenericSitemap({"queryset": Domain.objects.all(), "date_field": "created_at"}), "domains": GenericSitemap({"queryset": Domain.objects.all(), "date_field": "created_at"}),
} }
urlpatterns: list[URLPattern] = [ urlpatterns: list[URLPattern] = [
path(route="", view=views.IndexView.as_view(), name="index"), path(route="", view=views.IndexView.as_view(), name="index"),
path(route="feed/<int:feed_id>/", view=views.FeedView.as_view(), name="feed"), path(route="feed/<int:feed_id>/", view=views.FeedView.as_view(), name="feed"),

View file

@ -254,7 +254,7 @@ class RobotsView(View):
def get(self, request: HttpRequest) -> HttpResponse: # noqa: ARG002 def get(self, request: HttpRequest) -> HttpResponse: # noqa: ARG002
"""Load the robots.txt file.""" """Load the robots.txt file."""
return HttpResponse( return HttpResponse(
content="User-agent: *\nDisallow: /add\nDisallow: /upload\nDisallow: /accounts/", content="User-agent: *\nDisallow: /add\nDisallow: /upload\nDisallow: /accounts/\n\nSitemap: https://feedvault.se/sitemap.xml",
content_type="text/plain", content_type="text/plain",
) )

View file

@ -7,7 +7,7 @@ readme = "README.md"
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "^3.12" python = "^3.12"
django = { extras = ["argon2"], version = "^5.0.3" } django = {extras = ["argon2"], version = "^5.0.3"}
python-dotenv = "^1.0.1" python-dotenv = "^1.0.1"
feedparser = "^6.0.11" feedparser = "^6.0.11"
gunicorn = "^21.2.0" gunicorn = "^21.2.0"