diff --git a/src/coulobre/hosting/templates/hosting/account_wordpress.html b/src/coulobre/hosting/templates/hosting/account_wordpress.html index cc66151..0be6611 100644 --- a/src/coulobre/hosting/templates/hosting/account_wordpress.html +++ b/src/coulobre/hosting/templates/hosting/account_wordpress.html @@ -76,7 +76,15 @@ - {% else %}—{% endif %} + {% endif %} + {% if staging %} +
+ {% csrf_token %} + + + +
+ {% endif %} {% endfor %} @@ -95,7 +103,7 @@ {% for job in jobs %} - {% if job.action == "add-admin" %}Add administrator{% elif job.action == "install" %}Install{% elif job.action == "check-updates" %}Check for updates{% elif job.action == "update" %}Update {{ job.details.update.slug }} to {{ job.details.update.version }}{% else %}Detect{% endif %} + {% if job.action == "add-admin" %}Add administrator{% elif job.action == "install" %}Install{% elif job.action == "check-updates" %}Check for updates{% elif job.action == "update" %}Update {{ job.details.update.slug }} to {{ job.details.update.version }}{% elif job.action == "stage" %}Refresh staging{% elif job.action == "stage-update" %}Test {{ job.details.update.slug }} {{ job.details.update.version }} on staging{% else %}Detect{% endif %} {{ job.requested_at }} {% if job.problem %}{{ job.problem }} @@ -107,6 +115,7 @@ {% if job.report.password_email == "accepted" %}
WordPress accepted the password setup email. {% elif job.report.password_email == "failed" %}
WordPress could not send the setup email. Check the website's mail settings and request a password reset. {% endif %} + {% if job.report.cleanup_pending %}
Staging was updated, but the old revision still needs cleanup on the web node.{% endif %} {% endfor %} @@ -115,6 +124,22 @@ {% endif %} + {% if version or staging_form.is_bound or staging %} +
+
+

WordPress staging

+ {% if staging %}Open staging{% endif %} +
+

Refresh the protected staging copy at staging.{{ site.domain }}. Changes to an existing staging copy are replaced. The live site is not changed.

+

Ask your server operator to prepare DNS, HTTPS and the staging login before the first refresh.

+
+ {% csrf_token %} + + {{ staging_form.as_div }} + +
+
+ {% endif %} {% if version or admin_form.is_bound %}

Add a WordPress administrator

@@ -138,6 +163,24 @@
{% endif %} +
+

Preview a WordPress import

+

Upload a ZIP or TAR export with the full WordPress installation in files/ and one database.sql at the archive root. Preview does not change any files or databases.

+
+ {% csrf_token %} + + {{ import_form.as_div }} + +
+ {% if import_summary %} +
+
Destination
{{ site.domain }}
+
Archive
{{ import_summary.format }}
+
Site files
{{ import_summary.file_count }} · {{ import_summary.site_bytes|filesizeformat }}
+
Database dump
{{ import_summary.sql_bytes|filesizeformat }}
+
+ {% endif %} +
{% else %}

Activate a website on this hosting account first.

{% endif %} diff --git a/src/coulobre/hosting/test_wordpress.py b/src/coulobre/hosting/test_wordpress.py index 5328e81..8861911 100644 --- a/src/coulobre/hosting/test_wordpress.py +++ b/src/coulobre/hosting/test_wordpress.py @@ -6,6 +6,7 @@ from uuid import uuid4 from zipfile import ZipFile from django.contrib.auth import get_user_model +from django.core.files.uploadedfile import SimpleUploadedFile from django.test import SimpleTestCase, TestCase, override_settings from django.urls import reverse from django.utils import timezone @@ -24,12 +25,15 @@ from coulobre.wordpress_updates import WordPressUpdate from .models import ( Customer, + DatabaseProvisioning, HostingAccount, + HostingDatabase, WebActivation, WebProvisioningPlan, WordPressJob, ) from .tasks import dispatch_web_activations, run_wordpress_job +from .test_wordpress_import import export_zip class InstallRequestTests(SimpleTestCase): @@ -240,6 +244,92 @@ class WordPressWorkflowTests(TestCase): self.assertEqual(next_job.state, "failed") self.assertEqual(self.client.get(self.url).status_code, 404) + def test_import_preview_reports_destination_without_a_write(self): + response = self.client.post( + self.url, + { + "site": self.account.primary_domain, + "action": "import-preview", + "archive": SimpleUploadedFile( + "wordpress.zip", export_zip().getvalue() + ), + }, + ) + self.assertContains(response, "Import preview") + self.assertContains(response, self.account.primary_domain) + self.assertEqual(response.context["import_summary"]["sql_bytes"], 9) + self.assertFalse(self.account.wordpress_jobs.exists()) + self.assertFalse(self.account.databases.exists()) + + @patch("coulobre.hosting.node_client.apply_on_local_node") + def test_stage_refresh_and_test_update_use_separate_revision(self, apply): + database = HostingDatabase.objects.create( + account=self.account, suffix="wordpress" + ) + DatabaseProvisioning.objects.create( + database=database, finished_at=timezone.now() + ) + data = self.data | { + "action": "stage", + "database_id": database.pk, + "confirmed": "", + } + self.assertContains( + self.client.post(self.url, data), "This field is required" + ) + self.assertFalse(self.account.wordpress_jobs.exists()) + self.assertEqual( + self.client.post(self.url, data | {"confirmed": "on"}).status_code, + 302, + ) + stage = self.account.wordpress_jobs.get() + candidate = stage.details["candidate"] + self.assertEqual( + candidate["domain"], f"staging.{self.account.primary_domain}" + ) + report = { + "wp_version": "7.0", + "staging_domain": candidate["domain"], + "staging_revision": candidate["revision"], + } + apply.return_value = report + run_wordpress_job.run(stage.pk) + stage.refresh_from_db() + self.assertEqual(stage.state, "configured") + self.assertContains(self.client.get(self.url), "Open staging") + + selection = { + "kind": "core", + "slug": "wordpress", + "installed": "7.0", + "version": "7.1", + } + self.account.wordpress_jobs.create( + action="check-updates", + actor=self.customer.user, + site=stage.site, + finished_at=timezone.now(), + report={"version": "7.0", "updates": [selection]}, + ) + self.assertContains(self.client.get(self.url), "Test on staging") + response = self.client.post( + self.url, + data + | { + "action": "stage-update", + "selection": "core:wordpress:7.0:7.1", + }, + ) + self.assertEqual(response.status_code, 302) + update = self.account.wordpress_jobs.latest("pk") + self.assertEqual(update.details["staging"], candidate) + self.assertFalse(self.account.backup_jobs.exists()) + apply.return_value = report | {"wp_version": "7.1"} + run_wordpress_job.run(update.pk) + update.refresh_from_db() + self.assertEqual(update.state, "configured") + self.assertEqual(update.report["version"], "7.1") + def test_site_selection_stays_within_the_account(self): response = self.client.post( self.url, self.data | {"site": "outside.test"} diff --git a/src/coulobre/hosting/test_wordpress_import.py b/src/coulobre/hosting/test_wordpress_import.py new file mode 100644 index 0000000..cc32f81 --- /dev/null +++ b/src/coulobre/hosting/test_wordpress_import.py @@ -0,0 +1,94 @@ +import stat +from io import BytesIO +from tarfile import TarFile, TarInfo +from unittest.mock import patch +from zipfile import ZipFile, ZipInfo + +from django.test import SimpleTestCase + +from coulobre.wordpress_import import REQUIRED_FILES, inspect_export +from coulobre.wordpress_staging_updates import package_url +from coulobre.wordpress_updates import WordPressUpdate + + +def export_zip(*extra): + buffer = BytesIO() + with ZipFile(buffer, "w") as archive: + for path in sorted(REQUIRED_FILES): + archive.writestr(path, b" 1024 + or any(len(part.encode("utf-8")) > 255 for part in path.parts) + ): + raise ValueError("The archive contains an unsafe path.") + if path.parts[0] != "files" and name != "database.sql": + raise ValueError( + "The archive root must contain only files/ and database.sql." + ) + return name + + +def zip_entries(archive): + for member in archive.infolist(): + kind = stat.S_IFMT(member.external_attr >> 16) + is_directory = member.is_dir() + if ( + kind not in (0, stat.S_IFREG, stat.S_IFDIR) + or kind == stat.S_IFDIR + and not is_directory + or kind == stat.S_IFREG + and is_directory + or is_directory + and member.file_size != 0 + ): + raise ValueError("Use only regular files and directories.") + if member.flag_bits & 1: + raise ValueError("Use an archive without password protection.") + yield member.orig_filename, is_directory, member.file_size + + +def tar_entries(archive): + for member in archive: + if not (member.isdir() or member.isreg()) or member.issparse(): + raise ValueError("Use only regular files and directories.") + if member.isdir() and member.size != 0: + raise ValueError("The TAR has an invalid directory entry.") + yield member.name, member.isdir(), member.size + + +def inspect_entries(entries, archive_type): + paths = {} + total = sql = files = 0 + for name, directory, size in entries: + name = archive_name(name) + if name in paths: + raise ValueError(f"The archive lists {name} more than once.") + if len(paths) >= MAX_ARCHIVE_ENTRIES: + raise ValueError("The archive has too many entries.") + if size < 0 or total + size > MAX_EXPANDED_BYTES: + raise ValueError("The expanded archive exceeds 4 GiB.") + total += size + paths[name] = directory + if name == "files" and not directory: + raise ValueError("files must be a directory.") + if name == "database.sql": + if directory or size == 0: + raise ValueError("Provide a nonempty database.sql file.") + sql = size + elif not directory: + files += 1 + + if not sql: + raise ValueError("Add database.sql at the root of the archive.") + if any(paths.get(name, True) for name in REQUIRED_FILES): + raise ValueError("Include the full WordPress installation in files/.") + for name in paths: + for parent in PurePosixPath(name).parents: + if paths.get(parent.as_posix()) is False: + raise ValueError("A file cannot also contain other entries.") + return { + "format": archive_type, + "file_count": files, + "site_bytes": total - sql, + "sql_bytes": sql, + } + + +def inspect_export(source): + source.seek(0, 2) + if not 0 < source.tell() <= MAX_ARCHIVE_BYTES: + raise ValueError("The upload must be no larger than 2 GiB.") + source.seek(0) + try: + zip_format = is_zipfile(source) + source.seek(0) + if zip_format: + with ZipFile(source) as archive: + return inspect_entries(zip_entries(archive), "ZIP") + # Iterate so entry and size limits apply during inspection. + with TarFile.open(fileobj=source, mode="r|*") as archive: + return inspect_entries(tar_entries(archive), "TAR") + except (OSError, EOFError, BadZipFile, TarError, UnicodeError) as error: + raise ValueError("Upload a readable ZIP or TAR archive.") from error + finally: + source.seek(0)