Files
coulobre/src/coulobre/hosting/test_update_views.py
T
parkel dbac22b0ce
Coulobre Panel CI / Checks (Python 3.12) (pull_request) Canceled after 0s
Coulobre Panel CI / Checks (Python 3.14) (pull_request) Canceled after 0s
Show published release notes in the panel changelog
2026-11-24 10:14:04 +01:00

110 lines
4.2 KiB
Python

from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.test import TestCase
from django.urls import reverse
from coulobre.release_updates import RELEASES_URL, Release, UpdateError
class UpdateViewTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.admin = get_user_model().objects.create_superuser("operator")
def test_catalog_renders_markdown_and_escapes_untrusted_content(self):
checksum = "a" * 64
notes = f"""## Changes
- **Upgrade** from the previous release.
- Follow the [deployment guide](https://example.test/docs#upgrade).
```text
{checksum} coulobre_panel-0.11.1rc1.tar.gz
```
<script>Should stay visible as text.</script>
[Unsafe](javascript&#58;invalid) [Local action](/updates/)
![Remote image](https://example.test/image.png)
"""
release = Release("0.11.1rc1", notes, True, checksum)
self.client.force_login(self.admin)
cache.clear()
self.addCleanup(cache.clear)
path = "coulobre.hosting.update_views"
with (
patch(f"{path}.SOCKET", **{"is_socket.return_value": True}),
patch(f"{path}.inspect_linked_nodes", return_value={"nodes": []}),
patch(f"{path}.available_releases", return_value=[release]),
):
response = self.client.get(reverse("panel:updates"))
self.assertContains(response, "<h2>Changes</h2>", html=True)
self.assertContains(response, "<strong>Upgrade</strong>", html=True)
self.assertContains(response, '<pre><code class="language-text">')
self.assertContains(response, f"{checksum} coulobre_panel-")
self.assertContains(
response, 'href="https://example.test/docs#upgrade"'
)
self.assertContains(response, "&lt;script&gt;")
self.assertNotContains(response, "<script>Should stay")
self.assertNotContains(response, 'href="javascript:')
self.assertNotContains(response, 'href="/updates/">Local action')
self.assertNotContains(response, '<img src="https://example.test/')
def test_changelog_renders_and_caches_public_notes_without_updater(self):
self.client.force_login(self.admin)
cache.clear()
self.addCleanup(cache.clear)
url = reverse("panel:changelog")
path = "coulobre.release_updates.read_json"
with patch(
path,
return_value=[
{
"tag_name": "v0.13.0-rc.2",
"draft": False,
"published_at": "2026-11-24T09:59:53+01:00",
"body": "## Changes\n\n<script>Untrusted</script>",
},
{
"tag_name": "v0.12.0",
"draft": False,
"body": "Previous release",
},
],
) as catalog:
response = self.client.get(url)
cached_response = self.client.get(url)
catalog.assert_called_once()
self.assertContains(response, "<h2>Changes</h2>", html=True)
self.assertContains(response, "0.13.0rc2 · Prerelease")
self.assertContains(response, ">24 Nov 2026</time>")
self.assertContains(response, "&lt;script&gt;Untrusted")
self.assertNotContains(response, "<script>Untrusted")
self.assertContains(response, 'aria-current="page">Changelog')
self.assertContains(response, f'href="{RELEASES_URL}/tag/v0.12.0"')
self.assertContains(cached_response, "Previous release")
self.assertContains(
self.client.get(reverse("panel:installation")),
f'href="{url}">Changelog',
)
def test_changelog_links_to_gitea_when_the_catalog_is_unavailable(self):
self.client.force_login(self.admin)
cache.clear()
self.addCleanup(cache.clear)
with patch(
"coulobre.hosting.update_views.changelog",
side_effect=UpdateError("Could not read releases."),
):
response = self.client.get(reverse("panel:changelog"))
self.assertContains(
response, "The release history is temporarily unavailable"
)
self.assertContains(response, f'href="{RELEASES_URL}"')