224 lines
8.8 KiB
Python
224 lines
8.8 KiB
Python
from unittest.mock import patch
|
|
|
|
from django.contrib.auth import get_user_model
|
|
from django.contrib.auth.models import Permission
|
|
from django.test import TestCase, override_settings
|
|
from django.urls import reverse
|
|
from django.utils import timezone
|
|
|
|
from coulobre.mail_node import MailboxSpec
|
|
|
|
from .models import (
|
|
Customer,
|
|
HostingAccount,
|
|
HostingMailbox,
|
|
HostingMailDomain,
|
|
MailboxProvisioning,
|
|
Reseller,
|
|
)
|
|
from .tasks import dispatch_mailbox_provisioning, provision_mailbox
|
|
|
|
|
|
@override_settings(LOCAL_MAILBOX_PROVISIONING=True)
|
|
class MailboxProvisioningTests(TestCase):
|
|
@classmethod
|
|
def setUpTestData(cls):
|
|
users = get_user_model().objects
|
|
cls.operator = users.create_user("operator")
|
|
cls.customer = users.create_user("customer")
|
|
cls.reseller = users.create_user("reseller")
|
|
cls.permissions = Permission.objects.filter(
|
|
content_type__app_label="hosting",
|
|
codename__in=(
|
|
"view_hostingaccount",
|
|
"view_hostingmailbox",
|
|
"add_mailboxprovisioning",
|
|
),
|
|
)
|
|
for user in (cls.operator, cls.customer, cls.reseller):
|
|
user.user_permissions.add(*cls.permissions)
|
|
cls.account = HostingAccount.objects.create(
|
|
name="Mail hosting",
|
|
primary_domain="site.example",
|
|
customer=Customer.objects.create(user=cls.customer),
|
|
reseller=Reseller.objects.create(user=cls.reseller),
|
|
)
|
|
domain = HostingMailDomain.objects.create(
|
|
account=cls.account, domain="site.example"
|
|
)
|
|
cls.mailbox = HostingMailbox.objects.create(
|
|
mail_domain=domain, local_part="support"
|
|
)
|
|
|
|
def setUp(self):
|
|
self.client.force_login(self.operator)
|
|
self.detail = self.account.get_absolute_url()
|
|
self.url = reverse("panel:mailbox_provision", args=[self.mailbox.pk])
|
|
|
|
def test_local_worker_provisions_the_requested_address_once(self):
|
|
self.assertContains(self.client.get(self.detail), "Provision mailbox")
|
|
self.assertEqual(self.client.get(self.url).status_code, 405)
|
|
for _ in range(2):
|
|
self.assertRedirects(self.client.post(self.url), self.detail)
|
|
job = MailboxProvisioning.objects.get(mailbox=self.mailbox)
|
|
self.assertContains(self.client.get(self.detail), "Queued")
|
|
with patch.object(provision_mailbox, "apply_async") as send:
|
|
dispatch_mailbox_provisioning.run()
|
|
dispatch_mailbox_provisioning.run()
|
|
send.assert_called_once_with(
|
|
args=[job.pk], queue="coulobre-mailbox-provisioning", retry=False
|
|
)
|
|
|
|
HostingAccount.objects.filter(pk=self.account.pk).update(
|
|
primary_domain="new.example"
|
|
)
|
|
with patch("coulobre.hosting.tasks.provision_local_mailbox") as apply:
|
|
provision_mailbox.run(job.pk)
|
|
provision_mailbox.run(job.pk)
|
|
apply.assert_called_once_with(MailboxSpec("site.example", "support"))
|
|
self.assertContains(self.client.get(self.detail), "Configured")
|
|
|
|
def test_customer_reseller_and_viewer_cannot_provision(self):
|
|
viewer = get_user_model().objects.create_user("viewer")
|
|
viewer.user_permissions.add(
|
|
*self.permissions.exclude(codename="add_mailboxprovisioning")
|
|
)
|
|
for user in (self.customer, self.reseller, viewer):
|
|
with self.subTest(user=user):
|
|
self.client.force_login(user)
|
|
self.assertNotContains(
|
|
self.client.get(self.detail), "Provision mailbox"
|
|
)
|
|
self.assertEqual(self.client.post(self.url).status_code, 403)
|
|
self.assertFalse(MailboxProvisioning.objects.exists())
|
|
|
|
@override_settings(LOCAL_MAILBOX_PROVISIONING=False)
|
|
def test_disabled_provisioning_leaves_manifest_downloads_available(self):
|
|
self.assertNotContains(
|
|
self.client.get(self.detail), "Provision mailbox"
|
|
)
|
|
self.assertEqual(self.client.post(self.url).status_code, 404)
|
|
manifest_url = reverse(
|
|
"panel:mailbox_manifest", args=[self.mailbox.pk]
|
|
)
|
|
self.assertEqual(
|
|
self.client.get(manifest_url).json(), self.mailbox.spec.manifest()
|
|
)
|
|
|
|
def test_failed_provisioning_can_be_retried(self):
|
|
self.assertRedirects(self.client.post(self.url), self.detail)
|
|
job = MailboxProvisioning.objects.get(mailbox=self.mailbox)
|
|
with patch(
|
|
"coulobre.hosting.tasks.provision_local_mailbox",
|
|
side_effect=OSError,
|
|
):
|
|
provision_mailbox.run(job.pk)
|
|
detail = self.client.get(self.detail)
|
|
self.assertContains(detail, "Retry provisioning")
|
|
self.assertContains(
|
|
detail, "Could not complete the request to the mail node"
|
|
)
|
|
self.assertRedirects(self.client.post(self.url), self.detail)
|
|
job.refresh_from_db()
|
|
self.assertEqual(job.state, "queued")
|
|
self.assertFalse(job.problem)
|
|
|
|
def allow_quota_changes(self, user):
|
|
user.user_permissions.add(
|
|
Permission.objects.get(
|
|
content_type__app_label="hosting",
|
|
codename="change_hostingmailbox",
|
|
)
|
|
)
|
|
|
|
def test_quota_edit_queues_an_update_for_a_configured_mailbox(self):
|
|
self.allow_quota_changes(self.operator)
|
|
job = MailboxProvisioning.objects.create(
|
|
mailbox=self.mailbox, finished_at=timezone.now()
|
|
)
|
|
quota_url = reverse("panel:mailbox_quota", args=[self.mailbox.pk])
|
|
self.assertContains(
|
|
self.client.get(self.detail), "Change storage limit"
|
|
)
|
|
self.assertRedirects(
|
|
self.client.post(quota_url, {"quota_mib": 384}), self.detail
|
|
)
|
|
|
|
self.mailbox.refresh_from_db()
|
|
self.assertEqual(self.mailbox.quota_mib, 384)
|
|
job.refresh_from_db()
|
|
self.assertEqual(job.state, "queued")
|
|
self.assertTrue(job.quota_update)
|
|
with (
|
|
patch(
|
|
"coulobre.hosting.tasks.update_local_mailbox_quota"
|
|
) as update,
|
|
patch("coulobre.hosting.tasks.provision_local_mailbox") as install,
|
|
):
|
|
provision_mailbox.run(job.pk)
|
|
update.assert_called_once_with(
|
|
MailboxSpec("site.example", "support", 384)
|
|
)
|
|
install.assert_not_called()
|
|
job.refresh_from_db()
|
|
self.assertEqual(job.state, "configured")
|
|
|
|
def test_failed_quota_update_keeps_the_target_for_retry(self):
|
|
self.allow_quota_changes(self.operator)
|
|
job = MailboxProvisioning.objects.create(
|
|
mailbox=self.mailbox, finished_at=timezone.now()
|
|
)
|
|
quota_url = reverse("panel:mailbox_quota", args=[self.mailbox.pk])
|
|
self.client.post(quota_url, {"quota_mib": 384})
|
|
with patch(
|
|
"coulobre.hosting.tasks.update_local_mailbox_quota",
|
|
side_effect=OSError,
|
|
):
|
|
provision_mailbox.run(job.pk)
|
|
|
|
blocked = self.client.post(quota_url, {"quota_mib": 128})
|
|
self.assertContains(
|
|
blocked, "Complete the pending or failed mailbox request first."
|
|
)
|
|
self.assertEqual(
|
|
HostingMailbox.objects.get(pk=self.mailbox.pk).quota_mib, 384
|
|
)
|
|
self.assertRedirects(self.client.post(self.url), self.detail)
|
|
with patch(
|
|
"coulobre.hosting.tasks.update_local_mailbox_quota"
|
|
) as update:
|
|
provision_mailbox.run(job.pk)
|
|
update.assert_called_once_with(
|
|
MailboxSpec("site.example", "support", 384)
|
|
)
|
|
|
|
@override_settings(LOCAL_MAILBOX_PROVISIONING=False)
|
|
def test_separate_mail_node_quota_edit_is_saved_in_the_manifest(self):
|
|
self.allow_quota_changes(self.operator)
|
|
quota_url = reverse("panel:mailbox_quota", args=[self.mailbox.pk])
|
|
manifest_url = reverse(
|
|
"panel:mailbox_manifest", args=[self.mailbox.pk]
|
|
)
|
|
self.assertRedirects(
|
|
self.client.post(quota_url, {"quota_mib": 256}), self.detail
|
|
)
|
|
self.assertEqual(
|
|
self.client.get(manifest_url).json()["quota_mib"], 256
|
|
)
|
|
self.assertRedirects(
|
|
self.client.post(quota_url, {"quota_mib": ""}), self.detail
|
|
)
|
|
self.assertEqual(self.client.get(manifest_url).json()["version"], 1)
|
|
self.assertFalse(MailboxProvisioning.objects.exists())
|
|
|
|
def test_reseller_cannot_edit_quota_even_with_mailbox_permissions(self):
|
|
self.allow_quota_changes(self.reseller)
|
|
self.client.force_login(self.reseller)
|
|
quota_url = reverse("panel:mailbox_quota", args=[self.mailbox.pk])
|
|
self.assertEqual(
|
|
self.client.post(quota_url, {"quota_mib": 128}).status_code, 403
|
|
)
|
|
self.assertNotContains(
|
|
self.client.get(self.detail), "Change storage limit"
|
|
)
|