gva/gnuviechadmin/domains/tests/test_models.py
Jan Dittberner 3d18392b67 Fix tests for Python 3
- drop Python 2 __future__ imports
- fix tests to handle new Django and Python 3 module names
- reformat changed files with black
2019-01-30 21:27:25 +01:00

126 lines
3.9 KiB
Python

"""
Tests for :py:mod:`domains.models`.
"""
from unittest.mock import patch
from django.test import TestCase
from django.contrib.auth import get_user_model
from domains.models import (
DNSComment,
DNSCryptoKey,
DNSDomain,
DNSDomainMetadata,
DNSRecord,
DNSSupermaster,
DNSTSIGKey,
HostingDomain,
MailDomain,
)
from hostingpackages.models import CustomerHostingPackage, HostingPackageTemplate
User = get_user_model()
TEST_USER = "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.objects.create(localpart="info", domain=md)
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")
class DNSDomainTest(TestCase):
def test___str__(self):
dnsdomain = DNSDomain(domain="test")
self.assertEqual(str(dnsdomain), "test")
class DNSRecordTest(TestCase):
def test___str__(self):
dnsrecord = DNSRecord(name="localhost", recordtype="A", content="127.0.0.1")
self.assertEqual(str(dnsrecord), "localhost IN A 127.0.0.1")
class DNSSupermasterTest(TestCase):
def test___str__(self):
dnssupermaster = DNSSupermaster(ip="127.0.0.1", nameserver="dns.example.org")
self.assertEqual(str(dnssupermaster), "127.0.0.1 dns.example.org")
class DNSCommentTest(TestCase):
def test___str__(self):
dnscomment = DNSComment(name="localhost", commenttype="A", comment="good stuff")
self.assertEqual(str(dnscomment), "localhost IN A: good stuff")
class DNSDomainMetadataTest(TestCase):
def test___str__(self):
dnsdomain = DNSDomain(domain="test")
dnsdomainmetadata = DNSDomainMetadata(
domain=dnsdomain, kind="SOA-EDIT", content="INCEPTION"
)
self.assertEqual(str(dnsdomainmetadata), "test SOA-EDIT INCEPTION")
class DNSCryptoKeyTest(TestCase):
def test___str__(self):
dnsdomain = DNSDomain(domain="test")
dnscryptokey = DNSCryptoKey(domain=dnsdomain, content="testvalue")
self.assertEqual(str(dnscryptokey), "test testvalue")
class DNSTSIGKeyTest(TestCase):
def test___str__(self):
dnstsigkey = DNSTSIGKey(name="testkey", algorithm="hmac-md5", secret="dummykey")
self.assertEqual(str(dnstsigkey), "testkey hmac-md5 XXXX")