Let operators create hosting for their own login
This commit is contained in:
1 parent
b215cd0ab1
commit
6ed566f1ee
3 files changed
+131
-5
No files matched your search
@@ -1,8 +1,10 @@
|
||||
from django.contrib.admin.models import ADDITION, LogEntry
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.test import TestCase, override_settings
|
||||
from django.urls import reverse
|
||||
from django_otp.plugins.otp_totp.models import TOTPDevice
|
||||
|
||||
from .models import HostingAccount, WebsiteAlias
|
||||
from .models import Customer, HostingAccount, PanelInvitation, WebsiteAlias
|
||||
|
||||
|
||||
@override_settings(
|
||||
@@ -102,3 +104,90 @@ class HostingAccountSetupTests(TestCase):
|
||||
self.assertIn(
|
||||
"check me", {record.text for record in zone.spec.txt_records}
|
||||
)
|
||||
|
||||
|
||||
class OperatorOwnHostingTests(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
users = get_user_model().objects
|
||||
cls.operator = users.create_superuser("operator")
|
||||
cls.device = TOTPDevice.objects.create(user=cls.operator, name="Panel")
|
||||
cls.customer = Customer.objects.create(user=users.create_user("other"))
|
||||
cls.other = HostingAccount.objects.create(
|
||||
name="Other site",
|
||||
primary_domain="other.example.test",
|
||||
customer=cls.customer,
|
||||
)
|
||||
|
||||
def setUp(self):
|
||||
self.client.force_login(self.operator)
|
||||
self.url = reverse("panel:account_create")
|
||||
self.data = {
|
||||
"name": "Own website",
|
||||
"primary_domain": "own.example.test",
|
||||
"own_hosting": "on",
|
||||
}
|
||||
|
||||
def verify_operator(self):
|
||||
session = self.client.session
|
||||
session["otp_device_id"] = self.device.persistent_id
|
||||
session.save()
|
||||
|
||||
def test_current_login_can_manage_its_own_accounts_as_customer(self):
|
||||
self.verify_operator()
|
||||
self.assertContains(self.client.get(self.url), "For my own hosting")
|
||||
for domain in ("own.example.test", "second.example.test"):
|
||||
response = self.client.post(
|
||||
self.url, self.data | {"primary_domain": domain}
|
||||
)
|
||||
account = HostingAccount.objects.get(primary_domain=domain)
|
||||
self.assertRedirects(response, account.get_absolute_url())
|
||||
self.assertEqual(account.customer.user_id, self.operator.pk)
|
||||
self.assertEqual(
|
||||
Customer.objects.filter(user=self.operator).count(), 1
|
||||
)
|
||||
self.assertEqual(
|
||||
LogEntry.objects.filter(
|
||||
user=self.operator, action_flag=ADDITION
|
||||
).count(),
|
||||
1,
|
||||
)
|
||||
self.assertFalse(PanelInvitation.objects.exists())
|
||||
response = self.client.post(
|
||||
reverse("panel:switch_panel_role"), {"role": "customer"}
|
||||
)
|
||||
self.assertRedirects(response, reverse("panel:accounts"))
|
||||
page = self.client.get(reverse("panel:accounts"))
|
||||
self.assertContains(page, "own.example.test")
|
||||
self.assertContains(page, "second.example.test")
|
||||
self.assertNotContains(page, "other.example.test")
|
||||
self.assertEqual(
|
||||
self.client.get(self.other.get_absolute_url()).status_code, 404
|
||||
)
|
||||
self.assertEqual(self.client.get(self.url).status_code, 403)
|
||||
|
||||
def test_own_hosting_cannot_override_an_explicit_customer(self):
|
||||
self.verify_operator()
|
||||
response = self.client.post(
|
||||
self.url, self.data | {"customer": self.customer.pk}
|
||||
)
|
||||
self.assertContains(response, "Leave Customer unassigned")
|
||||
self.assertFalse(Customer.objects.filter(user=self.operator).exists())
|
||||
self.assertEqual(HostingAccount.objects.count(), 1)
|
||||
|
||||
def test_reserved_domain_rolls_back_the_customer_role(self):
|
||||
self.verify_operator()
|
||||
self.other.website_domains.create(domain=self.data["primary_domain"])
|
||||
response = self.client.post(self.url, self.data)
|
||||
self.assertContains(response, "This domain is unavailable.")
|
||||
self.assertFalse(Customer.objects.filter(user=self.operator).exists())
|
||||
self.assertFalse(LogEntry.objects.filter(user=self.operator).exists())
|
||||
self.assertEqual(HostingAccount.objects.count(), 1)
|
||||
|
||||
def test_unverified_operator_cannot_create_a_customer_role(self):
|
||||
self.assertNotContains(self.client.get(self.url), "For my own hosting")
|
||||
self.assertEqual(
|
||||
self.client.post(self.url, self.data).status_code, 403
|
||||
)
|
||||
self.assertFalse(Customer.objects.filter(user=self.operator).exists())
|
||||
self.assertEqual(HostingAccount.objects.count(), 1)
|
||||
@@ -2,6 +2,7 @@ from operator import attrgetter
|
||||
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from django.contrib.admin.models import ADDITION, LogEntry
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.mixins import PermissionRequiredMixin
|
||||
from django.core.exceptions import PermissionDenied, ValidationError
|
||||
@@ -458,6 +459,15 @@ class AccountCreate(PermissionRequiredMixin, AccountEditor, CreateView):
|
||||
def get_form(self, form_class=None):
|
||||
form = super().get_form(form_class)
|
||||
actor = self.request.user
|
||||
if actor.is_superuser and actor.is_verified():
|
||||
form.fields["own_hosting"] = forms.BooleanField(
|
||||
label="For my own hosting",
|
||||
help_text=(
|
||||
"Leave Customer unassigned to use your current login "
|
||||
"in the Customer workspace."
|
||||
),
|
||||
required=False,
|
||||
)
|
||||
reseller = reseller_for_user(actor) is not None
|
||||
defaults = zone_defaults()
|
||||
web = panel_feature_allowed("LOCAL_WEB_ACTIVATION") and (
|
||||
@@ -516,14 +526,35 @@ class AccountCreate(PermissionRequiredMixin, AccountEditor, CreateView):
|
||||
return initial
|
||||
|
||||
def form_valid(self, form):
|
||||
reseller = reseller_for_user(self.request.user)
|
||||
actor = self.request.user
|
||||
if self.request.POST.get("own_hosting") and (
|
||||
"own_hosting" not in form.fields
|
||||
):
|
||||
raise PermissionDenied
|
||||
own_hosting = form.cleaned_data.get("own_hosting")
|
||||
if own_hosting and form.cleaned_data.get("customer"):
|
||||
form.add_error(
|
||||
"customer", "Leave Customer unassigned for your own hosting."
|
||||
)
|
||||
return self.form_invalid(form)
|
||||
reseller = reseller_for_user(actor)
|
||||
self.object = form.save(commit=False)
|
||||
try:
|
||||
with transaction.atomic():
|
||||
if reseller is None:
|
||||
save_operator_account(
|
||||
self.object, self.request.user.get_username()
|
||||
if own_hosting:
|
||||
customer, created = Customer.objects.get_or_create(
|
||||
user_id=actor.pk
|
||||
)
|
||||
self.object.customer = customer
|
||||
if created:
|
||||
LogEntry.objects.log_actions(
|
||||
actor.pk,
|
||||
[customer],
|
||||
ADDITION,
|
||||
"Assigned Customer role for own hosting.",
|
||||
)
|
||||
if reseller is None:
|
||||
save_operator_account(self.object, actor.get_username())
|
||||
else:
|
||||
create_reseller_account(reseller, self.object)
|
||||
setup_new_account(self.object, form.cleaned_data)
|
||||
|
||||
Reference in new issue
Block a user