feat(static-sites): add scoped Bunny purge gateway
All checks were successful
Test / test (push) Successful in 12s
All checks were successful
Test / test (push) Successful in 12s
This commit is contained in:
parent
d40d2837f5
commit
86964aa187
4 changed files with 123 additions and 7 deletions
|
|
@ -4,6 +4,7 @@ from pathlib import Path
|
|||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
MODULE_PATH = Path(__file__).resolve().parents[1] / "static_site.py"
|
||||
SPEC = importlib.util.spec_from_file_location("static_site_deploy", MODULE_PATH)
|
||||
|
|
@ -17,6 +18,7 @@ bunny_plan = static_site.bunny_plan
|
|||
collect_files = static_site.collect_files
|
||||
manifest_for = static_site.manifest_for
|
||||
parse_manifest = static_site.parse_manifest
|
||||
purge_bunny_gateway = static_site.purge_bunny_gateway
|
||||
|
||||
|
||||
def write_site(root: Path) -> None:
|
||||
|
|
@ -77,6 +79,64 @@ class StaticSiteTests(unittest.TestCase):
|
|||
remote = manifest_for(files)["files"]
|
||||
self.assertEqual(bunny_plan(files, remote), ([], []))
|
||||
|
||||
def test_gateway_requires_a_plain_https_origin(self) -> None:
|
||||
invalid = (
|
||||
"http://purge.example",
|
||||
"https://purge.example/path",
|
||||
"https://user@purge.example",
|
||||
"https://purge.example?token=secret",
|
||||
)
|
||||
for endpoint in invalid:
|
||||
with self.subTest(endpoint=endpoint):
|
||||
with self.assertRaisesRegex(DeployError, "HTTPS origin"):
|
||||
purge_bunny_gateway(endpoint, "example-org", "password")
|
||||
|
||||
@mock.patch.object(static_site.urllib.request, "urlopen")
|
||||
def test_gateway_reuses_storage_password(self, urlopen: mock.Mock) -> None:
|
||||
urlopen.return_value.__enter__.return_value = object()
|
||||
purge_bunny_gateway(
|
||||
"https://purge.example", "example-org", "storage-password"
|
||||
)
|
||||
request = urlopen.call_args.args[0]
|
||||
self.assertEqual(
|
||||
request.full_url,
|
||||
"https://purge.example/v1/purge/example-org",
|
||||
)
|
||||
self.assertEqual(request.method, "POST")
|
||||
self.assertEqual(
|
||||
request.get_header("Authorization"), "Bearer storage-password"
|
||||
)
|
||||
|
||||
@mock.patch.object(static_site, "purge_bunny_gateway")
|
||||
@mock.patch.object(static_site, "BunnyStorage")
|
||||
def test_unchanged_deployment_retries_gateway_purge(
|
||||
self, storage_class: mock.Mock, purge_gateway: mock.Mock
|
||||
) -> None:
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
write_site(root)
|
||||
files = collect_files(root)
|
||||
storage_class.return_value.get_manifest.return_value = json.dumps(
|
||||
manifest_for(files)
|
||||
).encode()
|
||||
args = static_site.argparse.Namespace(
|
||||
directory=root,
|
||||
zone="example-org",
|
||||
password="storage-password",
|
||||
endpoint="storage.bunnycdn.com",
|
||||
purge_endpoint="https://purge.example",
|
||||
pull_zone_id="",
|
||||
api_key="",
|
||||
dry_run=False,
|
||||
)
|
||||
result = static_site.deploy_bunny(args)
|
||||
|
||||
self.assertFalse(result["changed"])
|
||||
self.assertTrue(result["purged"])
|
||||
purge_gateway.assert_called_once_with(
|
||||
"https://purge.example", "example-org", "storage-password"
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
|||
Loading…
Reference in a new issue