Let resellers onboard customers and provision hosting resources #101

Merged
parkel merged 2 commits from feature/reseller-customer-onboarding into main 2026-11-22 11:53:10 +01:00
2 changed files with 303 additions and 0 deletions
Showing only changes of commit 98bf8e127b - Show all commits

No files matched your search

+25
View File
@@ -1971,6 +1971,31 @@ limit and retain their assigned plans. Operators can assign and move
accounts from the hosting account form. Use the audit history to review
changes to reseller policy and account assignments made through the panel.
### Onboard customers and delegate provisioning
A reseller can open an unassigned hosting account in their own portfolio and
select **Add customer login**. Enter a new username and password, and add an
email address to support customer password recovery. Share the initial
credentials with the customer through a trusted channel. An existing
customer created by the same reseller can be added with **Link existing
customer**. The operator controls changes to existing assignments and to
customer sign-in status. The reseller's **Customers** page lists their own
logins.
Enable **Manage databases and mailboxes** on the reseller policy to let that
reseller request and provision both resources for their hosting accounts.
New requests use the account's existing database and mailbox limits and
mailbox storage quota. A deployment must enable the relevant provisioner
before the reseller can queue work. The panel shows provisioning status,
while service credentials and deployment manifests follow their existing
permissions. Both local and separate mail receivers use the same panel
permission.
The operator's reseller detail page records customer creation and linking,
resource requests, provisioning attempts queued or retried, and changes to
the reseller policy. Customer sign-in and operator support access use the
same workflows as operator-created accounts.
## Suspend and resume hosting services
Open the hosting account and use **Hosting service access**. This requires
@@ -0,0 +1,278 @@
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 .models import (
Customer,
DatabaseProvisioning,
HostingAccount,
HostingDatabase,
HostingMailbox,
HostingMailDomain,
HostingPlan,
MailboxProvisioning,
Reseller,
ResellerAuditEvent,
)
from .reseller_onboarding import request_reseller_resource
PASSWORD = "fixture-password-for-customer"
Action = ResellerAuditEvent.Action
@override_settings(
LOCAL_DATABASE_PROVISIONING=True,
LOCAL_MAILBOX_PROVISIONING=True,
)
class ResellerOnboardingTests(TestCase):
@classmethod
def setUpTestData(cls):
users = get_user_model().objects
cls.operator = users.create_superuser("operator")
cls.alice = Reseller.objects.create(
user=users.create_user("alice"), manage_resources=True
)
cls.bob = Reseller.objects.create(
user=users.create_user("bob"), manage_resources=True
)
cls.plan = HostingPlan.objects.create(
name="Small",
database_limit=1,
mailbox_limit=1,
mailbox_quota_mib=50,
)
cls.account = HostingAccount.objects.create(
name="Alice account",
primary_domain="alice.test",
reseller=cls.alice,
hosting_plan=cls.plan,
)
cls.second = HostingAccount.objects.create(
name="Second Alice account",
primary_domain="second.test",
reseller=cls.alice,
hosting_plan=cls.plan,
)
cls.other = HostingAccount.objects.create(
name="Bob account",
primary_domain="bob.test",
reseller=cls.bob,
hosting_plan=cls.plan,
)
cls.other_customer = Customer.objects.create(
user=users.create_user("bob-customer"), reseller=cls.bob
)
cls.operator_customer = Customer.objects.create(
user=users.create_user("operator-customer")
)
def setUp(self):
self.client.force_login(self.alice.user)
def post_customer(self, account, **overrides):
return self.client.post(
reverse("panel:reseller_customer_create", args=[account.pk]),
{
"username": "new-customer",
"email": "customer@example.test",
"password1": PASSWORD,
"password2": PASSWORD,
**overrides,
},
)
def test_create_and_link_a_customer_to_owned_accounts(self):
self.assertContains(
self.client.get(self.account.get_absolute_url()),
"Add customer login",
)
self.assertRedirects(
self.post_customer(self.account, reseller=self.bob.pk),
self.account.get_absolute_url(),
)
customer = Customer.objects.get(user__username="new-customer")
self.assertEqual(customer.reseller_id, self.alice.pk)
self.assertTrue(customer.user.check_password(PASSWORD))
self.assertEqual(customer.user.email, "customer@example.test")
self.assertFalse(customer.user.is_staff)
self.account.refresh_from_db()
self.assertEqual(self.account.customer_id, customer.pk)
url = reverse("panel:reseller_customer_link", args=[self.second.pk])
form = self.client.get(url)
self.assertContains(form, "new-customer")
self.assertNotContains(form, "bob-customer")
self.assertNotContains(form, "operator-customer")
for outsider in (self.other_customer, self.operator_customer):
response = self.client.post(url, {"customer": outsider.pk})
self.assertIn("customer", response.context["form"].errors)
self.assertRedirects(
self.client.post(url, {"customer": customer.pk}),
self.second.get_absolute_url(),
)
self.assertEqual(
set(customer.hostingaccount_set.values_list("pk", flat=True)),
{self.account.pk, self.second.pk},
)
self.assertEqual(self.post_customer(self.account).status_code, 404)
events = self.alice.audit_events.order_by("pk")
self.assertEqual(
list(
events.values_list(
"action", "actor_username", "account_domain"
)
),
[
(Action.CUSTOMER_CREATE, "alice", "alice.test"),
(Action.CUSTOMER_LINK, "alice", "second.test"),
],
)
self.assertEqual(events.first().changes, {"customer": "new-customer"})
self.client.force_login(customer.user)
self.assertContains(
self.client.get(self.second.get_absolute_url()), self.second.name
)
self.assertEqual(
self.client.get(self.other.get_absolute_url()).status_code, 404
)
self.client.force_login(self.bob.user)
self.assertNotContains(
self.client.get(reverse("panel:customers")), "new-customer"
)
self.assertContains(
self.client.get(reverse("panel:customers")), "bob-customer"
)
def test_reseller_cannot_create_a_customer_for_another_reseller(self):
self.assertEqual(self.post_customer(self.other).status_code, 404)
self.assertFalse(
Customer.objects.filter(user__username="new-customer").exists()
)
self.alice.user.user_permissions.set(
Permission.objects.filter(content_type__app_label="hosting")
)
url = reverse("panel:customer_access", args=[self.other_customer.pk])
self.assertEqual(
self.client.post(url, {"access": "suspend"}).status_code, 403
)
self.client.force_login(self.operator)
self.assertContains(
self.client.get(reverse("panel:customers")), "bob-customer"
)
self.assertRedirects(
self.client.post(url, {"access": "suspend"}),
reverse("panel:customers"),
)
def test_delegated_resource_management_preserves_limits_and_audits_jobs(
self,
):
database_url = reverse("panel:database_create", args=[self.account.pk])
mailbox_url = reverse("panel:mailbox_create", args=[self.account.pk])
detail = self.account.get_absolute_url()
self.assertRedirects(
self.client.post(database_url, {"suffix": "shop"}), detail
)
self.assertRedirects(
self.client.post(mailbox_url, {"local_part": "info"}), detail
)
self.assertContains(
self.client.post(database_url, {"suffix": "over"}),
"has reached its database limit",
)
self.assertContains(
self.client.post(mailbox_url, {"local_part": "over"}),
"has reached its mailbox limit",
)
database = HostingDatabase.objects.get(account=self.account)
mailbox = HostingMailbox.objects.get(mail_domain__account=self.account)
self.assertEqual(mailbox.quota_mib, 50)
for model, resource in (("database", database), ("mailbox", mailbox)):
url = reverse(f"panel:{model}_provision", args=[resource.pk])
manifest = reverse(f"panel:{model}_manifest", args=[resource.pk])
self.assertContains(self.client.get(detail), f"Provision {model}")
self.assertEqual(self.client.get(manifest).status_code, 403)
for _ in range(2):
self.assertRedirects(self.client.post(url), detail)
self.assertEqual(DatabaseProvisioning.objects.get().database, database)
self.assertEqual(MailboxProvisioning.objects.get().mailbox, mailbox)
self.assertEqual(
list(
self.alice.audit_events.order_by("pk").values_list(
"action", flat=True
)
),
[Action.RESOURCE_REQUEST] * 2 + [Action.RESOURCE_PROVISION] * 2,
)
DatabaseProvisioning.objects.update(
finished_at=timezone.now(), problem="Failed"
)
retry = reverse("panel:database_provision", args=[database.pk])
self.assertRedirects(self.client.post(retry), detail)
self.assertEqual(DatabaseProvisioning.objects.get().state, "queued")
self.assertEqual(
self.alice.audit_events.filter(
action=Action.RESOURCE_PROVISION
).count(),
3,
)
def test_resource_scope_and_policy_are_checked_for_each_request(self):
database = HostingDatabase.objects.create(
account=self.other, suffix="shop"
)
domain = HostingMailDomain.objects.create(
account=self.other, domain="bob.test"
)
mailbox = HostingMailbox.objects.create(
mail_domain=domain, local_part="info"
)
for model, resource in (("database", database), ("mailbox", mailbox)):
location = reverse(f"panel:{model}_provision", args=[resource.pk])
self.assertEqual(self.client.post(location).status_code, 404)
for model, values in (
("database", {"suffix": "test"}),
("mailbox", {"local_part": "test"}),
):
path = reverse(f"panel:{model}_create", args=[self.other.pk])
self.assertEqual(self.client.post(path, values).status_code, 404)
self.assertFalse(DatabaseProvisioning.objects.exists())
self.assertFalse(MailboxProvisioning.objects.exists())
original = request_reseller_resource
def revoke_before_save(*args, **kwargs):
Reseller.objects.filter(pk=self.alice.pk).update(
manage_resources=False
)
return original(*args, **kwargs)
path = reverse("panel:database_create", args=[self.account.pk])
with patch(
"coulobre.hosting.views.request_reseller_resource",
side_effect=revoke_before_save,
):
self.assertEqual(
self.client.post(path, {"suffix": "test"}).status_code, 403
)
self.assertFalse(self.account.databases.exists())
self.assertFalse(self.alice.audit_events.exists())
@override_settings(LOCAL_DATABASE_PROVISIONING=False)
def test_deployment_can_disable_a_provisioner(self):
database = HostingDatabase.objects.create(
account=self.account, suffix="shop"
)
path = reverse("panel:database_provision", args=[database.pk])
self.assertNotContains(
self.client.get(self.account.get_absolute_url()),
"Provision database",
)
self.assertEqual(self.client.post(path).status_code, 404)
self.assertFalse(DatabaseProvisioning.objects.exists())