Files
coulobre/src/coulobre/hosting/test_customer_restore.py
T

74 lines
2.6 KiB
Python

from unittest.mock import patch
from uuid import uuid4
from django.contrib.auth import get_user_model
from django.test import TestCase, override_settings
from django.urls import reverse
from django.utils import timezone
from coulobre.account_backup import AccountBackup
from coulobre.web_node import WebSite
from .models import (
AccountBackupJob,
ComponentRestoreJob,
Customer,
HostingAccount,
HostingDatabase,
)
from .tasks import restore_backup_component
@override_settings(LOCAL_ACCOUNT_BACKUPS=True, LOCAL_DATABASE_RESTORE=True)
class CustomerRestoreTests(TestCase):
def test_restore_requires_its_customers_backup_and_current_assignment(
self,
):
users = get_user_model().objects
customer = Customer.objects.create(user=users.create_user("customer"))
other = Customer.objects.create(user=users.create_user("other"))
account = HostingAccount.objects.create(
name="Shop", primary_domain="shop.example.test", customer=customer
)
database = HostingDatabase.objects.create(
account=account, suffix="cms"
)
archive = AccountBackup(
WebSite(account.pk, account.primary_domain), (database.spec,)
)
backup = AccountBackupJob.objects.create(
account=account,
manifest=archive.manifest(),
finished_at=timezone.now(),
export_customer_id=customer.pk,
)
url = reverse(
"panel:customer_restore", args=[account.pk, backup.pk, "database"]
)
self.client.force_login(other.user)
self.assertEqual(self.client.get(url).status_code, 404)
self.client.force_login(customer.user)
data = {"restore_id": str(uuid4()), "resource": database.suffix}
self.assertContains(
self.client.post(url, data | {"action": "restore"}),
"Review the destination before restoring.",
)
self.assertContains(
self.client.post(url, data | {"action": "preview"}),
database.spec.name,
)
self.assertFalse(ComponentRestoreJob.objects.exists())
self.assertRedirects(
self.client.post(url, data | {"action": "restore"}),
reverse("panel:customer_backups", args=[account.pk]),
)
job = ComponentRestoreJob.objects.get()
HostingAccount.objects.filter(pk=account.pk).update(customer=other)
with patch(
"coulobre.hosting.tasks.restore_local_component"
) as restore:
restore_backup_component(job.pk)
restore.assert_not_called()
job.refresh_from_db()
self.assertEqual(job.state, "failed")