166 lines
6.1 KiB
Python
166 lines
6.1 KiB
Python
import json
|
|
from io import StringIO
|
|
from pathlib import Path
|
|
from tempfile import TemporaryDirectory
|
|
|
|
from django.core.management import CommandError, call_command
|
|
from django.test import TestCase
|
|
|
|
from .models import (
|
|
HostingAccount,
|
|
HostingDatabase,
|
|
HostingDnsZone,
|
|
HostingMailbox,
|
|
HostingMailDomain,
|
|
)
|
|
from .test_directadmin import make_tar
|
|
|
|
|
|
class DirectAdminImportPlanTests(TestCase):
|
|
def setUp(self):
|
|
directory = Path(self.enterContext(TemporaryDirectory()))
|
|
self.backup = directory / "user.admin.client.tar"
|
|
self.entries = [
|
|
("backup/client_site.sql", "database dump"),
|
|
("backup/client_Store.sql", ""),
|
|
("backup/client_store.sql", ""),
|
|
("backup/client_9.sql", ""),
|
|
("backup/other_wp.sql", ""),
|
|
("backup/example.net/email/passwd", "support:private-mail-hash:10\n"),
|
|
("backup/other.example/email/passwd", "billing:private-other-hash:10\n"),
|
|
("backup/example.net/example.net.db", "zone file"),
|
|
("domains/example.net/public_html/index.php", "website data"),
|
|
("imap/example.net/support/Maildir/cur/message", "mail data"),
|
|
]
|
|
|
|
def plan(self, entries=None, *, primary_domain=None):
|
|
if entries is None:
|
|
entries = [
|
|
("backup/user.conf", "username=client\ndomain=Example.Net.\n"),
|
|
*self.entries,
|
|
]
|
|
self.backup.write_bytes(make_tar(entries))
|
|
output = StringIO()
|
|
call_command(
|
|
"plan_directadmin_import",
|
|
self.backup,
|
|
primary_domain=primary_domain,
|
|
stdout=output,
|
|
)
|
|
return json.loads(output.getvalue())
|
|
|
|
def test_command_reports_account_requests_and_manual_work_without_writing(self):
|
|
plan = self.plan()
|
|
|
|
self.assertEqual(plan["state"], "ready")
|
|
self.assertEqual(
|
|
plan["account"], {"name": "client", "primary_domain": "example.net"}
|
|
)
|
|
self.assertEqual(plan["source_domains"], ["example.net", "other.example"])
|
|
self.assertEqual(
|
|
plan["database_requests"], [{"source": "client_site", "suffix": "site"}]
|
|
)
|
|
self.assertEqual(plan["mailbox_requests"], ["support@example.net"])
|
|
for entry in (
|
|
{
|
|
"kind": "database_name",
|
|
"sources": ["client_Store", "client_store"],
|
|
"reason": "duplicate_suffix",
|
|
},
|
|
{
|
|
"kind": "database_name",
|
|
"source": "client_9",
|
|
"reason": "unsupported_suffix",
|
|
},
|
|
{
|
|
"kind": "database_name",
|
|
"source": "other_wp",
|
|
"reason": "unsupported_suffix",
|
|
},
|
|
{"kind": "dns_zone", "domain": "example.net"},
|
|
{"kind": "website_files", "domain": "example.net", "count": 1},
|
|
{"kind": "mail_files", "domain": "example.net", "count": 1},
|
|
{
|
|
"kind": "additional_domain",
|
|
"domain": "other.example",
|
|
"mailboxes": ["billing@other.example"],
|
|
},
|
|
):
|
|
with self.subTest(entry=entry):
|
|
self.assertIn(entry, plan["manual_review"])
|
|
self.assertFalse(HostingAccount.objects.exists())
|
|
self.assertFalse(HostingDatabase.objects.exists())
|
|
self.assertFalse(HostingMailDomain.objects.exists())
|
|
self.assertFalse(HostingMailbox.objects.exists())
|
|
|
|
def test_existing_account_blocks_the_plan(self):
|
|
account = HostingAccount.objects.create(
|
|
name="Existing", primary_domain="example.net"
|
|
)
|
|
|
|
plan = self.plan()
|
|
|
|
self.assertEqual(plan["state"], "blocked")
|
|
self.assertEqual(
|
|
plan["conflicts"],
|
|
[
|
|
{
|
|
"kind": "hosting_account",
|
|
"domain": "example.net",
|
|
"account_id": account.pk,
|
|
}
|
|
],
|
|
)
|
|
self.assertEqual(HostingAccount.objects.count(), 1)
|
|
|
|
def test_existing_dns_and_mail_ownership_block_a_reused_domain(self):
|
|
owner = HostingAccount.objects.create(
|
|
name="Renamed", primary_domain="renamed.example"
|
|
)
|
|
HostingDnsZone.objects.create(
|
|
account=owner,
|
|
domain="example.net",
|
|
nameservers=["ns1.provider.example"],
|
|
responsible_name="hostmaster.provider.example",
|
|
)
|
|
HostingMailDomain.objects.create(account=owner, domain="example.net")
|
|
|
|
plan = self.plan()
|
|
|
|
self.assertEqual(plan["state"], "blocked")
|
|
self.assertEqual(
|
|
plan["conflicts"],
|
|
[
|
|
{"kind": "dns_zone", "domain": "example.net", "account_id": owner.pk},
|
|
{
|
|
"kind": "mail_domain",
|
|
"domain": "example.net",
|
|
"account_id": owner.pk,
|
|
},
|
|
],
|
|
)
|
|
|
|
def test_multiple_domains_without_a_default_require_a_selection(self):
|
|
entries = [("backup/user.conf", "username=client\n"), *self.entries]
|
|
|
|
blocked = self.plan(entries)
|
|
self.assertEqual(blocked["state"], "blocked")
|
|
self.assertIsNone(blocked["account"])
|
|
self.assertTrue(blocked["problems"])
|
|
|
|
selected = self.plan(entries, primary_domain="Other.Example.")
|
|
self.assertEqual(selected["state"], "ready")
|
|
self.assertEqual(selected["account"]["primary_domain"], "other.example")
|
|
self.assertEqual(selected["mailbox_requests"], ["billing@other.example"])
|
|
|
|
with self.assertRaisesRegex(CommandError, "absent from the backup"):
|
|
self.plan(entries, primary_domain="absent.example")
|
|
|
|
def test_single_discovered_domain_or_explicit_new_domain_can_be_used(self):
|
|
account = ("backup/user.conf", "username=client\n")
|
|
discovered = self.plan([account, ("backup/domains.list", "example.net\n")])
|
|
assigned = self.plan([account], primary_domain="new.example")
|
|
|
|
self.assertEqual(discovered["account"]["primary_domain"], "example.net")
|
|
self.assertEqual(assigned["account"]["primary_domain"], "new.example")
|