Add better handling for missing groups

This commit is contained in:
Jan Dittberner 2023-05-07 13:45:30 +02:00
parent c7fda0e993
commit 3b04595c7a
2 changed files with 36 additions and 6 deletions

View file

@ -5,6 +5,7 @@ This module contains the hosting package models.
from __future__ import absolute_import
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db import models, transaction
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
@ -370,9 +371,14 @@ class CustomerHostingPackage(HostingPackageBase):
self.copy_template_attributes()
self.osuser = OsUser.objects.create_user(self.customer)
for group in settings.OSUSER_DEFAULT_GROUPS:
try:
AdditionalGroup.objects.create(
user=self.osuser, group=Group.objects.get(groupname=group)
)
except Group.DoesNotExist as e:
raise ImproperlyConfigured(
f"group {group} has not been defined"
) from e
return super(CustomerHostingPackage, self).save(*args, **kwargs)

View file

@ -2,10 +2,17 @@
Test for models.
"""
from django.contrib.auth import get_user_model
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase, override_settings
from django.test import TestCase
from hostingpackages.models import (
DISK_SPACE_UNITS,
CustomerHostingPackage,
HostingPackageTemplate,
)
from hostingpackages.models import DISK_SPACE_UNITS, CustomerHostingPackage
User = get_user_model()
class CustomerHostingPackageTest(TestCase):
@ -13,7 +20,7 @@ class CustomerHostingPackageTest(TestCase):
package = CustomerHostingPackage(
diskspace=10, diskspace_unit=DISK_SPACE_UNITS.G
)
self.assertEqual(package.get_disk_space(), 10 * 1024 ** 3)
self.assertEqual(package.get_disk_space(), 10 * 1024**3)
def test_get_disk_space_mib(self):
package = CustomerHostingPackage(
@ -26,3 +33,20 @@ class CustomerHostingPackageTest(TestCase):
diskspace=256, diskspace_unit=DISK_SPACE_UNITS.M
)
self.assertEqual(package.get_quota(), (262144, 275251))
@override_settings(OSUSER_DEFAULT_GROUPS=["testgroup"])
def test_additional_group_not_defined(self):
user = User.objects.create(username="test")
template = HostingPackageTemplate.objects.create(
description="Test package 1 - Description",
mailboxcount=10,
diskspace=100,
diskspace_unit=DISK_SPACE_UNITS.M,
name="Test package 1",
)
with self.assertRaises(ImproperlyConfigured) as ctx:
package = CustomerHostingPackage.objects.create_from_template(
customer=user, template=template, name="Test customer package"
)
package.save()
self.assertIn("testgroup", str(ctx.exception))