From c368b62f83439848fb29e446f5fa314f8457c746 Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Wed, 15 Feb 2017 14:03:39 +0100 Subject: [PATCH] Fixed unit tests. see #1030 --- modoboa/admin/forms/account.py | 2 -- modoboa/admin/serializers.py | 9 +++++---- modoboa/admin/tests/test_api.py | 6 +++--- modoboa/admin/tests/test_domain_alias.py | 11 ++++++----- modoboa/admin/tests/test_import_.py | 4 ++-- modoboa/limits/tests/test_domain_limits.py | 2 ++ modoboa/limits/tests/test_user_limits.py | 12 +++++++----- modoboa/relaydomains/tests.py | 16 ++++++++++++---- 8 files changed, 37 insertions(+), 25 deletions(-) diff --git a/modoboa/admin/forms/account.py b/modoboa/admin/forms/account.py index e41484396..bf6963664 100644 --- a/modoboa/admin/forms/account.py +++ b/modoboa/admin/forms/account.py @@ -278,8 +278,6 @@ class AccountFormMail(forms.Form, DynamicForm): Check if quota is >= 0 only when the domain value is not used. """ cleaned_data = super(AccountFormMail, self).clean() - if not cleaned_data["email"]: - return cleaned_data use_default_domain_quota = cleaned_data["quota_act"] condition = ( not use_default_domain_quota and diff --git a/modoboa/admin/serializers.py b/modoboa/admin/serializers.py index 6930c92cd..e0a6a33b0 100644 --- a/modoboa/admin/serializers.py +++ b/modoboa/admin/serializers.py @@ -23,7 +23,9 @@ class DomainSerializer(serializers.ModelSerializer): class Meta: model = models.Domain - fields = ("pk", "name", "default_mailbox_quota", "enabled", "type", ) + fields = ( + "pk", "name", "quota", "default_mailbox_quota", "enabled", "type", + ) def create(self, validated_data): """Set permissions.""" @@ -236,11 +238,10 @@ class WritableAccountSerializer(AccountSerializer): except lib_exceptions.ModoboaException as inst: raise serializers.ValidationError({ "domain": unicode(inst)}) + quota = data.pop("quota", None) mb = admin_models.Mailbox( user=account, address=address, domain=domain, **data) - mb.set_quota( - data.get("quota"), creator.has_perm("admin.add_domain") - ) + mb.set_quota(quota, creator.has_perm("admin.add_domain")) mb.save(creator=creator) account.email = full_address return mb diff --git a/modoboa/admin/tests/test_api.py b/modoboa/admin/tests/test_api.py index cee1f4c44..a56eb0161 100644 --- a/modoboa/admin/tests/test_api.py +++ b/modoboa/admin/tests/test_api.py @@ -44,14 +44,14 @@ class DomainAPITestCase(ModoAPITestCase): """Check domain creation.""" url = reverse("external_api:domain-list") response = self.client.post( - url, {"name": "test3.com", "default_mailbox_quota": 10}) + url, {"name": "test3.com", "quota": 0, "default_mailbox_quota": 10} + ) self.assertEqual(response.status_code, 201) self.assertTrue( models.Domain.objects.filter(name="test3.com").exists()) response = self.client.post(url, {}) self.assertEqual(response.status_code, 400) self.assertIn("name", response.data) - self.assertIn("default_mailbox_quota", response.data) self.client.credentials( HTTP_AUTHORIZATION='Token ' + self.da_token.key) @@ -302,7 +302,7 @@ class AccountAPITestCase(ModoAPITestCase): self.client.credentials( HTTP_AUTHORIZATION='Token ' + self.da_token.key) data = copy.deepcopy(self.ACCOUNT_DATA) - data["mailbox"]["quota"] = 20 + data["mailbox"]["quota"] = 1000 url = reverse("external_api:account-list") response = self.client.post(url, data, format="json") self.assertEqual(response.status_code, 400) diff --git a/modoboa/admin/tests/test_domain_alias.py b/modoboa/admin/tests/test_domain_alias.py index ef731097c..f5f21efcc 100644 --- a/modoboa/admin/tests/test_domain_alias.py +++ b/modoboa/admin/tests/test_domain_alias.py @@ -38,11 +38,12 @@ class DomainAliasTestCase(ModoTestCase): def test_form(self): dom = Domain.objects.get(name="test.com") - values = dict( - name=dom.name, default_mailbox_quota=dom.default_mailbox_quota, - enabled=dom.enabled, aliases="domalias.net", - aliases_1="domaliasé.com", type="domain" - ) + values = { + "name": dom.name, "quota": dom.quota, + "default_mailbox_quota": dom.default_mailbox_quota, + "enabled": dom.enabled, "aliases": "domalias.net", + "aliases_1": "domaliasé.com", "type": "domain" + } self.ajax_post( reverse("admin:domain_change", args=[dom.id]), diff --git a/modoboa/admin/tests/test_import_.py b/modoboa/admin/tests/test_import_.py index 3f33a6c2a..634a6a64c 100644 --- a/modoboa/admin/tests/test_import_.py +++ b/modoboa/admin/tests/test_import_.py @@ -181,13 +181,13 @@ domainalias; domalias1.com; test.com; True self.client.logout() self.client.login(username="admin@test.com", password="toto") f = ContentFile(b""" -account; user1@test.com; toto; User; One; True; SimpleUsers; user1@test.com; 20 +account; user1@test.com; toto; User; One; True; SimpleUsers; user1@test.com; 40 """, name="identities.csv") resp = self.client.post( reverse("admin:identity_import"), {"sourcefile": f, "crypt_password": True} ) - self.assertIn('Quota is greater than the allowed', resp.content) + self.assertIn("Domain quota exceeded", resp.content) def test_import_missing_quota(self): f = ContentFile(b""" diff --git a/modoboa/limits/tests/test_domain_limits.py b/modoboa/limits/tests/test_domain_limits.py index bf70ac4c9..dede7d468 100644 --- a/modoboa/limits/tests/test_domain_limits.py +++ b/modoboa/limits/tests/test_domain_limits.py @@ -34,6 +34,7 @@ class DomainLimitsTestCase(lib_tests.ModoTestCase): domain = Domain.objects.get(name="test.com") values = { "name": domain.name, "quota": domain.quota, + "default_mailbox_quota": domain.default_mailbox_quota, "enabled": domain.enabled, "type": "domain", "mailboxes_limit": 3, "mailbox_aliases_limit": 3, "domain_aliases_limit": 3, "domain_admins_limit": 3 @@ -62,6 +63,7 @@ class DomainLimitsTestCase(lib_tests.ModoTestCase): self.assertFalse(limit.is_exceeded()) values = { "name": domain.name, "quota": domain.quota, + "default_mailbox_quota": domain.default_mailbox_quota, "enabled": domain.enabled, "type": "domain", "mailboxes_limit": 2, "mailbox_aliases_limit": 2, "domain_aliases_limit": 2, "domain_admins_limit": 2, diff --git a/modoboa/limits/tests/test_user_limits.py b/modoboa/limits/tests/test_user_limits.py index 78c2b109b..8a2f72c46 100644 --- a/modoboa/limits/tests/test_user_limits.py +++ b/modoboa/limits/tests/test_user_limits.py @@ -78,7 +78,8 @@ class ResourceTestCase(lib_tests.ModoTestCase): def _create_domain(self, name, status=200, withtpl=False): values = { - "name": name, "quota": 100, "create_dom_admin": False, + "name": name, "quota": 100, "default_mailbox_quota": 10, + "create_dom_admin": False, "create_aliases": False, "stepid": "step3", "type": "domain" } if withtpl: @@ -95,16 +96,17 @@ class ResourceTestCase(lib_tests.ModoTestCase): def _domain_alias_operation(self, optype, domain, name, status=200): dom = Domain.objects.get(name=domain) values = { - 'name': dom.name, 'quota': dom.quota, 'enabled': dom.enabled, - "type": "domain" + "name": dom.name, "quota": dom.quota, "enabled": dom.enabled, + "type": "domain", + "default_mailbox_quota": dom.default_mailbox_quota, } aliases = [alias.name for alias in dom.domainalias_set.all()] - if optype == 'add': + if optype == "add": aliases.append(name) else: aliases.remove(name) for cpt, alias in enumerate(aliases): - fname = 'aliases' if not cpt else 'aliases_%d' % cpt + fname = "aliases" if not cpt else "aliases_%d" % cpt values[fname] = alias self.ajax_post( reverse("admin:domain_change", args=[dom.id]), diff --git a/modoboa/relaydomains/tests.py b/modoboa/relaydomains/tests.py index 443d1523e..b76606928 100644 --- a/modoboa/relaydomains/tests.py +++ b/modoboa/relaydomains/tests.py @@ -23,7 +23,8 @@ class Operations(object): values = { "name": name, "create_dom_admin": False, "type": "relaydomain", "target_host": "external.host.tld", "target_port": 25, - "service": srv.id, "enabled": True, "stepid": "step3" + "service": srv.id, "enabled": True, "stepid": "step3", + "quota": 0, "default_mailbox_quota": 0 } return self.ajax_post( reverse("admin:domain_add"), @@ -35,7 +36,8 @@ class Operations(object): values = { "name": rdom.domain.name, "target_host": rdom.target_host, "target_port": rdom.target_port, - "type": "relaydomain", "service": rdom.service.id + "type": "relaydomain", "service": rdom.service.id, + "quota": 0, "default_mailbox_quota": 0 } aliases = [alias.name for alias in rdom.domain.domainalias_set.all()] if optype == 'add': @@ -144,7 +146,8 @@ class RelayDomainsTestCase(ModoTestCase, Operations): values = { "name": "relaydomain.org", "target_host": self.rdom.target_host, "target_port": 4040, "type": "relaydomain", "enabled": True, - "service": self.rdom.service.id + "service": self.rdom.service.id, + "quota": 0, "default_mailbox_quota": 0 } self.ajax_post( reverse("admin:domain_change", args=[self.rdom.domain.id]), @@ -159,6 +162,8 @@ class RelayDomainsTestCase(ModoTestCase, Operations): values = { "name": "relaydomain.tld", "type": "domain", + "quota": 0, + "default_mailbox_quota": 0, "enabled": True, "target_host": self.rdom.target_host, "target_port": self.rdom.target_port, @@ -176,6 +181,8 @@ class RelayDomainsTestCase(ModoTestCase, Operations): "name": "relaydomain.tld", "type": "relaydomain", "enabled": True, + "quota": 0, + "default_mailbox_quota": 0 } self.ajax_post( reverse("admin:domain_change", args=[domain_pk]), values) @@ -195,7 +202,8 @@ class RelayDomainsTestCase(ModoTestCase, Operations): "name": "relaydomain.org", "target_host": self.rdom.target_host, "target_port": self.rdom.target_port, "type": "relaydomain", "service": self.rdom.service.id, - "aliases": "relaydomainalias.net" + "aliases": "relaydomainalias.net", + "quota": 0, "default_mailbox_quota": 0 } self.ajax_post( reverse('admin:domain_change',