Document and test public hosting deployments
This commit is contained in:
1 parent
9e66510f7c
commit
4a955c7095
3 files changed
+317
-15
No files matched your search
@@ -0,0 +1,153 @@
|
||||
import gzip
|
||||
import json
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.test import SimpleTestCase, TestCase, override_settings
|
||||
from django.urls import reverse
|
||||
|
||||
from coulobre.dns_catalog import render_catalog
|
||||
from coulobre.mail_node import MailboxSpec, MailboxUsageRequest
|
||||
from coulobre.mail_queue import QUEUE_NAMES, summarize_queue
|
||||
from coulobre.web_node import WebSite
|
||||
from coulobre.web_node_usage import read_site_log, without_usage_logs
|
||||
|
||||
from .models import Customer, HostingAccount, HostingMailDomain
|
||||
|
||||
|
||||
class ServiceInspectionTests(SimpleTestCase):
|
||||
def test_postfix_queue_counts_only_envelopes_for_requested_domain(self):
|
||||
def entry(queue, sender, *recipients):
|
||||
return json.dumps(
|
||||
{
|
||||
"queue_name": queue,
|
||||
"sender": sender,
|
||||
"recipients": [{"address": item} for item in recipients],
|
||||
}
|
||||
)
|
||||
|
||||
data = [
|
||||
entry("deferred", "member@example.com", "other@elsewhere.test"),
|
||||
entry("active", "sender@elsewhere.test", "member@example.com"),
|
||||
entry("hold", "sender@notexample.com", "other@elsewhere.test"),
|
||||
entry("incoming", "", "other@example.com", "more@example.com"),
|
||||
]
|
||||
recap = summarize_queue(data, "example.com")
|
||||
self.assertEqual(
|
||||
recap,
|
||||
{
|
||||
"partial": False,
|
||||
"counts": dict(zip(QUEUE_NAMES, (1, 1, 0, 1, 0), strict=True)),
|
||||
},
|
||||
)
|
||||
|
||||
def test_rotated_access_logs_include_only_current_month(self):
|
||||
with TemporaryDirectory() as directory:
|
||||
logs = Path(directory)
|
||||
active = logs / "coulobre-9.access"
|
||||
active.write_bytes(b"20 4 200 GET /now\n")
|
||||
active.with_suffix(".access.1").write_bytes(
|
||||
b"19 5 200 GET /recent\n"
|
||||
)
|
||||
with gzip.open(active.with_suffix(".access.2.gz"), "wb") as old:
|
||||
old.write(b"7 50 200 GET /old\n14 12 200 GET /earlier\n")
|
||||
|
||||
def open_test_log(path):
|
||||
return path.open("rb"), path.name
|
||||
|
||||
with (
|
||||
patch("coulobre.web_node_usage.LOG_DIRECTORY", logs),
|
||||
patch("coulobre.web_node_usage.open_log", open_test_log),
|
||||
):
|
||||
usage, recent, partial = read_site_log(
|
||||
"coulobre-9",
|
||||
kind="access",
|
||||
cutoff=10,
|
||||
)
|
||||
self.assertEqual(usage, 21)
|
||||
self.assertEqual(recent[0], "20 4 200 GET /now")
|
||||
self.assertFalse(partial)
|
||||
|
||||
def test_vhosts_use_the_site_identity_for_log_paths(self):
|
||||
site = WebSite(2, "example.com", site_id=8)
|
||||
config = site.render("apache", server_name="alias.example.com")
|
||||
self.assertIn(
|
||||
'CustomLog "/var/log/apache2/coulobre-s8.access"', config
|
||||
)
|
||||
self.assertNotIn("CustomLog", without_usage_logs(config))
|
||||
self.assertIn(
|
||||
"ServerName alias.example.com", without_usage_logs(config)
|
||||
)
|
||||
|
||||
def test_catalog_has_stable_member_labels(self):
|
||||
old = render_catalog(1, {"example.com", "other.example"})
|
||||
new = render_catalog(2, {"example.com"})
|
||||
member = next(
|
||||
line
|
||||
for line in old.splitlines()
|
||||
if line.endswith("PTR example.com.")
|
||||
)
|
||||
self.assertIn(member, new.splitlines())
|
||||
self.assertIn('version IN TXT "2"', new)
|
||||
self.assertNotIn("PTR other.example.", new)
|
||||
|
||||
def test_mailbox_usage_request_requires_a_single_valid_mailbox(self):
|
||||
mailbox = MailboxSpec("example.com", "one", 5)
|
||||
from coulobre.mail_node import parse_mailbox_request
|
||||
|
||||
request = MailboxUsageRequest(mailbox)
|
||||
self.assertEqual(parse_mailbox_request(request.manifest()), request)
|
||||
broken = request.manifest()
|
||||
broken["mailbox"]["local_part"] = "*"
|
||||
with self.assertRaises(ValueError):
|
||||
parse_mailbox_request(broken)
|
||||
|
||||
|
||||
@override_settings(
|
||||
WEBSITE_USAGE=True,
|
||||
LOCAL_MAILBOX_PROVISIONING=True,
|
||||
REQUIRE_PRIVILEGED_MFA=False,
|
||||
)
|
||||
class PublicOperationsAccessTests(TestCase):
|
||||
def setUp(self):
|
||||
users = get_user_model().objects
|
||||
self.operator = users.create_superuser(
|
||||
"operator", "ops@example.com", "fixture"
|
||||
)
|
||||
self.customer = users.create_user("customer", password="fixture")
|
||||
self.other = users.create_user("other", password="fixture")
|
||||
self.account = HostingAccount.objects.create(
|
||||
name="Example account",
|
||||
primary_domain="example.com",
|
||||
customer=Customer.objects.create(user=self.customer),
|
||||
)
|
||||
Customer.objects.create(user=self.other)
|
||||
self.domain = HostingMailDomain.objects.create(
|
||||
domain="example.com", account=self.account
|
||||
)
|
||||
|
||||
def test_website_usage_is_visible_only_to_the_account_customer(self):
|
||||
url = reverse("panel:website_usage", args=[self.account.pk])
|
||||
self.client.force_login(self.customer)
|
||||
self.assertContains(self.client.get(url), "Usage for Example account")
|
||||
self.client.force_login(self.other)
|
||||
self.assertEqual(self.client.get(url).status_code, 404)
|
||||
self.assertEqual(self.client.post(url).status_code, 404)
|
||||
|
||||
def test_queue_is_visible_to_operators_only(self):
|
||||
url = reverse("panel:mail_queue", args=[self.domain.pk])
|
||||
self.client.force_login(self.customer)
|
||||
self.assertEqual(self.client.get(url).status_code, 403)
|
||||
self.assertEqual(self.client.post(url).status_code, 403)
|
||||
self.client.force_login(self.operator)
|
||||
self.assertContains(self.client.get(url), "Mail queue for example.com")
|
||||
with (
|
||||
patch(
|
||||
"coulobre.hosting.mail_queue_views.refresh_mail_queue.delay"
|
||||
) as send,
|
||||
self.captureOnCommitCallbacks(execute=True),
|
||||
):
|
||||
self.assertEqual(self.client.post(url).status_code, 302)
|
||||
send.assert_called_once()
|
||||
Reference in new issue
Block a user