Files
amnezia-web-panel/tests/test_static_cache.py
T
hell 9ff488de42 fix: three follow-ups from the second review round
- CachedStaticFiles matched the substring `v=`, so `?rev=1` looked
  fingerprinted and earned the 180-day immutable lifetime. It parses the
  query now and looks for a parameter actually named `v`.
- saveRenameProtocol() restored the button label from a server-rendered
  `'{{ _("save") }}'` string literal; a locale whose translation carries a
  quote would break the whole inline script. Uses the runtime helper and
  textContent instead.
- The add-user modal kept the "password optional" hint up after the role
  changed to one that requires a password. It is hidden with the label now.
2026-09-10 00:17:21 +07:00

42 lines
1.4 KiB
Python

"""Only fingerprinted static URLs may be cached forever.
`static_version()` appends ?v=<mtime> to the assets whose URL is built in a
template, but plenty of them are referenced without it - the favicon, the
icons, qrcode.min.js, searchable-select.js, the vendored CodeMirror bundles
and ReDoc. Those keep the same URL across deploys, so an `immutable` lifetime
would pin the old file in the browser for half a year.
"""
import unittest
from fastapi.testclient import TestClient
import app as panel
class StaticCacheTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.client = TestClient(panel.app)
def test_fingerprinted_asset_is_immutable(self):
res = self.client.get('/static/css/style.css?v=12345')
self.assertEqual(res.status_code, 200)
self.assertIn('immutable', res.headers['cache-control'])
def test_plain_asset_is_revalidated(self):
res = self.client.get('/static/js/qrcode.min.js')
self.assertEqual(res.status_code, 200)
cache_control = res.headers['cache-control']
self.assertNotIn('immutable', cache_control)
self.assertIn('must-revalidate', cache_control)
def test_another_query_is_not_mistaken_for_a_version(self):
res = self.client.get('/static/js/qrcode.min.js?rev=1')
self.assertEqual(res.status_code, 200)
self.assertNotIn('immutable', res.headers['cache-control'])
if __name__ == '__main__':
unittest.main()