mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-17 05:41:25 +02:00
refactor using queryset; add coverage report to tox
This commit is contained in:
@@ -42,8 +42,9 @@ class Repair(BaseCommand):
|
||||
if not quiet:
|
||||
print(message)
|
||||
|
||||
def fix_owner(self, model, dry_run=False, **options):
|
||||
for obj in model.objects.all():
|
||||
def fix_owner(self, qs, dry_run=False, **options):
|
||||
model = qs.model
|
||||
for obj in qs:
|
||||
kw = dict(
|
||||
cls=model.__name__,
|
||||
obj=obj
|
||||
@@ -62,12 +63,7 @@ class Repair(BaseCommand):
|
||||
elif isinstance(obj, models.DomainAlias):
|
||||
admin = obj.target.admins.first()
|
||||
else:
|
||||
if obj.domain:
|
||||
admin = obj.domain.admins.first()
|
||||
else:
|
||||
# Alias is an internal alias for domain alias
|
||||
# we don't care if it's not owned
|
||||
continue
|
||||
admin = obj.domain.admins.first()
|
||||
if not admin:
|
||||
# domain has no admin. use the first superuser found
|
||||
admin = User.objects.filter(is_superuser=True,
|
||||
@@ -81,11 +77,11 @@ class Repair(BaseCommand):
|
||||
@known_problem
|
||||
def sometimes_objects_have_no_owner(self, **options):
|
||||
owned_models = (
|
||||
User,
|
||||
models.Domain,
|
||||
models.Mailbox,
|
||||
models.Alias,
|
||||
models.DomainAlias
|
||||
User.objects.all(),
|
||||
models.Domain.objects.all(),
|
||||
models.DomainAlias.objects.all(),
|
||||
models.Alias.objects.filter(domain__isnull=False),
|
||||
models.Mailbox.objects.filter(domain__isnull=False),
|
||||
)
|
||||
for model in owned_models:
|
||||
self.fix_owner(model, **options)
|
||||
for qs in owned_models:
|
||||
self.fix_owner(qs, **options)
|
||||
|
||||
@@ -26,7 +26,8 @@ class RepairTestCase(ModoTestCase):
|
||||
# assert mbox has no owner
|
||||
self.assertIs(get_object_owner(mbox), None)
|
||||
# fix it. run in quiet mode because we dont want output in tests
|
||||
management.call_command("modo", "repair", "--quiet")
|
||||
ret = management.call_command("modo", "repair", "--quiet")
|
||||
assert ret is None
|
||||
# assert its fixed
|
||||
self.assertIsNot(get_object_owner(mbox), None)
|
||||
self.assertIsNot(get_object_owner(alias), None)
|
||||
@@ -38,11 +39,13 @@ class RepairTestCase(ModoTestCase):
|
||||
# assert mbox has no owner
|
||||
self.assertIs(get_object_owner(mbox), None)
|
||||
# show problems. run in quiet mode because we dont want output in tests
|
||||
management.call_command("modo", "repair", "--quiet", "--dry-run")
|
||||
ret = management.call_command("modo", "repair", "--quiet", "--dry-run")
|
||||
assert ret is None
|
||||
# assert its not fixed
|
||||
self.assertIs(get_object_owner(mbox), None)
|
||||
|
||||
def test_management_command_with_nul_domain(self):
|
||||
""" Just assume nothing raise when an alias has no domain"""
|
||||
models.Alias.objects.create(address='@modoboa.xxx')
|
||||
management.call_command("modo", "repair", "--quiet")
|
||||
ret = management.call_command("modo", "repair", "--quiet")
|
||||
assert ret is None
|
||||
|
||||
@@ -5,11 +5,13 @@ envlist = py27
|
||||
changedir=test_project
|
||||
commands =
|
||||
python ../tests.py
|
||||
coverage run --source ../modoboa manage.py test modoboa.core modoboa.lib modoboa.admin.tests modoboa.limits modoboa.relaydomains
|
||||
coverage run --source modoboa manage.py test modoboa.core modoboa.lib modoboa.admin.tests modoboa.limits modoboa.relaydomains
|
||||
coverage report --show-missing
|
||||
deps =
|
||||
-r{toxinidir}/test-requirements.txt
|
||||
coverage
|
||||
setenv =
|
||||
COVERAGE_FILE={envdir}/.coverage
|
||||
DB=SQLITE
|
||||
|
||||
[testenv:serve]
|
||||
|
||||
Reference in New Issue
Block a user