Dre4m Shell
Server IP : 85.214.239.14  /  Your IP : 18.217.10.152
Web Server : Apache/2.4.62 (Debian)
System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64
User : www-data ( 33)
PHP Version : 7.4.18
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
MySQL : OFF  |  cURL : OFF  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : OFF
Directory :  /srv/modoboa/env/lib/python3.5/site-packages/modoboa/admin/tests/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME SHELL ]     

Current File : /srv/modoboa/env/lib/python3.5/site-packages/modoboa/admin/tests/test_mailbox_operations.py
"""Management command tests."""

import os
import shutil
import tempfile
from unittest import mock

from django.core.management import call_command
from django.test import override_settings
from django.urls import reverse

from modoboa.lib.tests import ModoTestCase
from .. import factories, models


@override_settings(
    DOVECOT_LOOKUP_PATH=["{}/dovecot".format(os.path.dirname(__file__))])
class MailboxOperationTestCase(ModoTestCase):
    """Test management command."""

    @classmethod
    def setUpTestData(cls):  # NOQA:N802
        """Create test data."""
        super(MailboxOperationTestCase, cls).setUpTestData()
        factories.populate_database()

    def setUp(self):
        """Initiate test env."""
        super(MailboxOperationTestCase, self).setUp()
        self.workdir = tempfile.mkdtemp()
        path = "{}/test.com/admin".format(self.workdir)
        os.makedirs(path)
        self.set_global_parameter("handle_mailboxes", True)
        self.set_global_parameter("enable_admin_limits", False, app="limits")

    def tearDown(self):
        """Reset test env."""
        shutil.rmtree(self.workdir)

    @mock.patch("modoboa.admin.models.Mailbox.mail_home")
    def test_delete_account(self, mail_home_mock):
        """Check delete operation."""
        path = "{}/test.com/admin".format(self.workdir)
        mail_home_mock.__get__ = mock.Mock(return_value=path)
        mb = models.Mailbox.objects.select_related("user").get(
            address="admin", domain__name="test.com")
        self.ajax_post(
            reverse("admin:account_delete", args=[mb.user.pk]), {}
        )
        call_command("handle_mailbox_operations")
        self.assertFalse(models.MailboxOperation.objects.exists())
        self.assertFalse(os.path.exists(mb.mail_home))

    @mock.patch("modoboa.admin.models.Mailbox.mail_home")
    def test_rename_account(self, mail_home_mock):
        """Check rename operation."""
        path = "{}/test.com/admin".format(self.workdir)
        mail_home_mock.__get__ = mock.Mock(return_value=path)
        mb = models.Mailbox.objects.select_related("user").get(
            address="admin", domain__name="test.com")
        values = {
            "username": "admin2@test.com", "role": "DomainAdmins",
            "is_active": True, "email": "admin2@test.com",
            "language": "en"
        }
        self.ajax_post(
            reverse("admin:account_change", args=[mb.user.pk]), values
        )
        path = "{}/test.com/admin2".format(self.workdir)
        mail_home_mock.__get__ = mock.Mock(return_value=path)
        call_command("handle_mailbox_operations")
        self.assertFalse(models.MailboxOperation.objects.exists())
        self.assertTrue(os.path.exists(mb.mail_home))

    @mock.patch("modoboa.admin.models.Mailbox.mail_home")
    def test_delete_domain(self, mail_home_mock):
        """Check delete operation."""
        path = "{}/test.com/admin".format(self.workdir)
        mail_home_mock.__get__ = mock.Mock(return_value=path)
        domain = models.Domain.objects.get(name="test.com")
        self.ajax_post(reverse("admin:domain_delete", args=[domain.pk]))
        call_command("handle_mailbox_operations")
        self.assertFalse(models.MailboxOperation.objects.exists())
        self.assertFalse(os.path.exists(path))

Anon7 - 2022
AnonSec Team