"""
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 hostingpackages.models import (
    DISK_SPACE_UNITS,
    CustomerHostingPackage,
    HostingPackageTemplate,
)

User = get_user_model()


class CustomerHostingPackageTest(TestCase):
    def test_get_disk_space_bytes(self):
        package = CustomerHostingPackage(
            diskspace=10, diskspace_unit=DISK_SPACE_UNITS.G
        )
        self.assertEqual(package.get_disk_space(), 10 * 1024**3)

    def test_get_disk_space_mib(self):
        package = CustomerHostingPackage(
            diskspace=10, diskspace_unit=DISK_SPACE_UNITS.G
        )
        self.assertEqual(package.get_disk_space(DISK_SPACE_UNITS.M), 10 * 1024)

    def test_get_quota(self):
        package = CustomerHostingPackage(
            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))
