From f5759f319427e0795c2ce94484765fe601bdf846 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Thu, 15 Oct 2015 19:50:18 +0000 Subject: [PATCH] Add tests for domains app Add missing tests for domains.forms and domains.models. --- gnuviechadmin/domains/forms.py | 4 +- gnuviechadmin/domains/models.py | 2 +- gnuviechadmin/domains/tests/test_forms.py | 105 +++++++++++++++++++++ gnuviechadmin/domains/tests/test_models.py | 79 +++++++++++++++- 4 files changed, 186 insertions(+), 4 deletions(-) create mode 100644 gnuviechadmin/domains/tests/test_forms.py diff --git a/gnuviechadmin/domains/forms.py b/gnuviechadmin/domains/forms.py index fff18cb..5ad0532 100644 --- a/gnuviechadmin/domains/forms.py +++ b/gnuviechadmin/domains/forms.py @@ -21,7 +21,9 @@ from .models import HostingDomain 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: raise forms.ValidationError( diff --git a/gnuviechadmin/domains/models.py b/gnuviechadmin/domains/models.py index c827f2c..d545964 100644 --- a/gnuviechadmin/domains/models.py +++ b/gnuviechadmin/domains/models.py @@ -27,7 +27,7 @@ class DomainBase(TimeStampedModel): abstract = True def __str__(self): - return self.name + return self.domain @python_2_unicode_compatible diff --git a/gnuviechadmin/domains/tests/test_forms.py b/gnuviechadmin/domains/tests/test_forms.py new file mode 100644 index 0000000..df93be4 --- /dev/null +++ b/gnuviechadmin/domains/tests/test_forms.py @@ -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() diff --git a/gnuviechadmin/domains/tests/test_models.py b/gnuviechadmin/domains/tests/test_models.py index 233844b..76fdcc8 100644 --- a/gnuviechadmin/domains/tests/test_models.py +++ b/gnuviechadmin/domains/tests/test_models.py @@ -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): + def test__str__(self): md = MailDomain.objects.create(domain='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')