Add tests for domains app

Add missing tests for domains.forms and domains.models.
This commit is contained in:
Jan Dittberner 2015-10-15 19:50:18 +00:00
parent 7d7a8941c3
commit f5759f3194
4 changed files with 186 additions and 4 deletions

View file

@ -21,7 +21,9 @@ from .models import HostingDomain
def relative_domain_validator(value): def relative_domain_validator(value):
""" """
This validator ensures that the given value is a valid lowercase This validator ensures that the given value is a valid lowercase domain
name.
""" """
if len(value) > 254: if len(value) > 254:
raise forms.ValidationError( raise forms.ValidationError(

View file

@ -27,7 +27,7 @@ class DomainBase(TimeStampedModel):
abstract = True abstract = True
def __str__(self): def __str__(self):
return self.name return self.domain
@python_2_unicode_compatible @python_2_unicode_compatible

View file

@ -0,0 +1,105 @@
"""
Tests for :py:mod:`domains.forms`.
"""
from __future__ import absolute_import, unicode_literals
from mock import MagicMock, Mock, patch
from django.core.urlresolvers import reverse
from django.forms import ValidationError
from django.test import TestCase
from django.utils.translation import ugettext_lazy as _
from domains.forms import relative_domain_validator, CreateHostingDomainForm
class RelativeDomainValidatorTest(TestCase):
def test_valid_domainname(self):
relative_domain_validator('example.org')
def test_domain_name_too_long(self):
with self.assertRaisesMessage(
ValidationError, _('host name too long')):
relative_domain_validator('e' * 255)
def test_domain_name_part_too_long(self):
with self.assertRaisesMessage(
ValidationError, _('invalid domain name')):
relative_domain_validator('a' * 64 + '.org')
def test_domain_name_illegal_characters(self):
with self.assertRaisesMessage(
ValidationError, _('invalid domain name')):
relative_domain_validator('eXampl3.org')
def test_domain_name_starts_with_dash(self):
with self.assertRaisesMessage(
ValidationError, _('invalid domain name')):
relative_domain_validator('-example.org')
def test_domain_name_ends_with_dash(self):
with self.assertRaisesMessage(
ValidationError, _('invalid domain name')):
relative_domain_validator('example-.org')
class CreateHostingDomainFormTest(TestCase):
def test_constructor_needs_hostingpackage(self):
instance = MagicMock()
with self.assertRaises(KeyError):
CreateHostingDomainForm(instance)
def test_constructor(self):
hostingpackage = Mock(id=42)
instance = MagicMock()
form = CreateHostingDomainForm(instance, hostingpackage=hostingpackage)
self.assertTrue(hasattr(form, 'hosting_package'))
self.assertEqual(form.hosting_package, hostingpackage)
self.assertTrue(hasattr(form, 'helper'))
self.assertEqual(form.helper.form_action, reverse(
'create_hosting_domain', kwargs={'package': 42}))
self.assertEqual(len(form.helper.layout.fields), 2)
self.assertEqual(form.helper.layout.fields[1].name, 'submit')
def test_domain_field_has_relative_domain_validator(self):
hostingpackage = Mock(id=42)
instance = MagicMock()
form = CreateHostingDomainForm(instance, hostingpackage=hostingpackage)
self.assertIn(
relative_domain_validator, form.fields['domain'].validators)
def test_clean(self):
hostingpackage = Mock(id=42)
instance = MagicMock()
form = CreateHostingDomainForm(
instance, hostingpackage=hostingpackage,
data={'domain': 'example.org'})
self.assertTrue(form.is_valid())
self.assertIn('hosting_package', form.cleaned_data)
self.assertEqual(hostingpackage, form.cleaned_data['hosting_package'])
def test_save(self):
hostingpackage = Mock(id=42)
instance = MagicMock()
form = CreateHostingDomainForm(
instance, hostingpackage=hostingpackage,
data={'domain': 'example.org'})
self.assertTrue(form.is_valid())
with patch('domains.forms.HostingDomain') as domain:
form.save()
domain.objects.create_for_hosting_package.assert_called_with(
commit=True, **form.cleaned_data)
form.save(commit=False)
domain.objects.create_for_hosting_package.assert_called_with(
commit=False, **form.cleaned_data)
def test_save_m2m(self):
hostingpackage = Mock(id=42)
instance = MagicMock()
form = CreateHostingDomainForm(
instance, hostingpackage=hostingpackage,
data={'domain': 'example.org'})
form.save_m2m()

View file

@ -1,9 +1,84 @@
from django.test import TestCase """
Tests for :py:mod:`domains.models`.
from domains.models import MailDomain """
from __future__ import absolute_import, unicode_literals
from mock import Mock, MagicMock, patch
from django.test import TestCase
from django.contrib.auth import get_user_model
from domains.models import (
DomainBase,
MailDomain,
HostingDomain,
)
from hostingpackages.models import (
CustomerHostingPackage,
HostingPackageTemplate,
)
User = get_user_model()
TEST_USER = 'test'
class DomainBaseTest(TestCase):
def test__str__(self):
db = DomainBase(domain='test')
self.assertEqual(str(db), 'test')
class MailDomainTest(TestCase): class MailDomainTest(TestCase):
def test__str__(self): def test__str__(self):
md = MailDomain.objects.create(domain='example.org') md = MailDomain.objects.create(domain='example.org')
self.assertEqual(str(md), 'example.org') self.assertEqual(str(md), 'example.org')
def test_get_mailaddresses(self):
md = MailDomain.objects.create(domain='example.org')
from managemails.models import MailAddress
addrmock = MailAddress(localpart='info')
md.mailaddress_set.add(addrmock)
self.assertIn(addrmock, md.get_mailaddresses())
self.assertIn(addrmock, md.mailaddresses)
class HostingDomainManagerTest(TestCase):
def _setup_hosting_package(self):
template = HostingPackageTemplate.objects.create(
name='testpackagetemplate', mailboxcount=0, diskspace=1,
diskspace_unit=0)
customer = User.objects.create_user(username=TEST_USER)
package = CustomerHostingPackage.objects.create_from_template(
customer, template, 'testpackage')
with patch('hostingpackages.models.settings') as hmsettings:
hmsettings.OSUSER_DEFAULT_GROUPS = []
package.save()
return package
def test_create_for_hosting_package_with_commit(self):
package = self._setup_hosting_package()
hostingdomain = HostingDomain.objects.create_for_hosting_package(
package, 'example.org', True)
self.assertIsNotNone(hostingdomain)
self.assertTrue(hostingdomain.customer, package.customer)
def test_create_for_hosting_package_no_commit(self):
package = self._setup_hosting_package()
hostingdomain = HostingDomain.objects.create_for_hosting_package(
package, 'example.org', False)
self.assertIsNotNone(hostingdomain)
self.assertTrue(hostingdomain.customer, package.customer)
class HostingDomainTest(TestCase):
def test__str__(self):
hostingdomain = HostingDomain(domain='test')
self.assertEqual(str(hostingdomain), 'test')