2023-02-18 22:46:48 +01:00
|
|
|
from unittest.mock import Mock
|
|
|
|
|
2014-06-01 18:25:30 +02:00
|
|
|
from django import forms
|
2023-02-18 22:46:48 +01:00
|
|
|
from django.contrib.admin import AdminSite
|
|
|
|
from django.contrib.auth import get_user_model
|
2014-06-01 18:25:30 +02:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.utils import override_settings
|
2019-01-30 21:27:25 +01:00
|
|
|
from django.urls import reverse
|
2014-06-01 18:25:30 +02:00
|
|
|
from django.utils.html import format_html
|
2023-02-18 22:46:48 +01:00
|
|
|
from django.utils.translation import gettext as _
|
2014-06-01 18:25:30 +02:00
|
|
|
|
|
|
|
from managemails.admin import (
|
2023-02-18 22:46:48 +01:00
|
|
|
PASSWORD_MISMATCH_ERROR,
|
2014-06-01 18:25:30 +02:00
|
|
|
ActivationChangeMixin,
|
|
|
|
MailboxAdmin,
|
|
|
|
MailboxChangeForm,
|
|
|
|
MailboxCreationForm,
|
|
|
|
ReadOnlyPasswordHashField,
|
|
|
|
ReadOnlyPasswordHashWidget,
|
|
|
|
)
|
2019-01-30 21:27:25 +01:00
|
|
|
from managemails.models import Mailbox
|
2023-02-18 22:46:48 +01:00
|
|
|
from osusers.models import User
|
2014-06-01 18:25:30 +02:00
|
|
|
|
2015-02-01 20:12:23 +01:00
|
|
|
Customer = get_user_model()
|
|
|
|
|
2014-06-01 18:25:30 +02:00
|
|
|
|
2014-06-01 22:41:42 +02:00
|
|
|
class ReadOnlyPasswordHashWidgetTest(TestCase):
|
2014-06-01 18:25:30 +02:00
|
|
|
def test_render(self):
|
|
|
|
widget = ReadOnlyPasswordHashWidget()
|
2019-01-30 21:27:25 +01:00
|
|
|
rendered = widget.render("password", "secret", {"class": "test"})
|
2014-06-01 18:25:30 +02:00
|
|
|
self.assertEqual(
|
|
|
|
rendered,
|
|
|
|
format_html(
|
|
|
|
'<div class="test">{0}</div>',
|
2019-01-30 21:27:25 +01:00
|
|
|
format_html("<strong>{0}</strong>: secret ", _("Hash")),
|
|
|
|
),
|
|
|
|
)
|
2014-06-01 18:25:30 +02:00
|
|
|
|
|
|
|
|
2014-06-01 22:41:42 +02:00
|
|
|
class ReadOnlyPasswordHashFieldTest(TestCase):
|
2014-06-01 18:25:30 +02:00
|
|
|
def test___init__(self):
|
|
|
|
field = ReadOnlyPasswordHashField()
|
|
|
|
self.assertFalse(field.required)
|
|
|
|
|
|
|
|
def test_bound_data(self):
|
|
|
|
field = ReadOnlyPasswordHashField()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(field.bound_data("new", "old"), "old")
|
2014-06-01 18:25:30 +02:00
|
|
|
|
|
|
|
def test__has_changed(self):
|
|
|
|
field = ReadOnlyPasswordHashField()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertFalse(field.has_changed("new", "old"))
|
2014-06-01 18:25:30 +02:00
|
|
|
|
|
|
|
|
2015-02-01 20:12:23 +01:00
|
|
|
class CustomerTestCase(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
super(CustomerTestCase, self).setUp()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.customer = Customer.objects.create(username="test")
|
2015-02-01 20:12:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
class MailboxCreationFormTest(CustomerTestCase):
|
2014-06-01 18:25:30 +02:00
|
|
|
def test_clean_password2_same(self):
|
|
|
|
form = MailboxCreationForm()
|
2019-01-30 21:27:25 +01:00
|
|
|
form.cleaned_data = {"password1": "secret", "password2": "secret"}
|
|
|
|
self.assertEqual(form.clean_password2(), "secret")
|
2014-06-01 18:25:30 +02:00
|
|
|
|
|
|
|
def test_clean_password2_empty(self):
|
|
|
|
form = MailboxCreationForm()
|
|
|
|
form.cleaned_data = {}
|
|
|
|
self.assertIsNone(form.clean_password2())
|
|
|
|
|
|
|
|
def test_clean_password2_mismatch(self):
|
|
|
|
form = MailboxCreationForm()
|
2019-01-30 21:27:25 +01:00
|
|
|
form.cleaned_data = {"password1": "secretx", "password2": "secrety"}
|
2014-06-01 18:25:30 +02:00
|
|
|
with self.assertRaises(forms.ValidationError) as cm:
|
|
|
|
form.clean_password2()
|
|
|
|
self.assertEqual(cm.exception.message, PASSWORD_MISMATCH_ERROR)
|
|
|
|
|
|
|
|
@override_settings(
|
2019-01-30 21:27:25 +01:00
|
|
|
CELERY_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND="memory", BROKER_BACKEND="memory"
|
2014-06-01 18:25:30 +02:00
|
|
|
)
|
|
|
|
def test_save_commit(self):
|
2015-02-01 20:12:23 +01:00
|
|
|
user = User.objects.create_user(customer=self.customer)
|
2019-01-30 21:27:25 +01:00
|
|
|
form = MailboxCreationForm(
|
|
|
|
data={"osuser": user.uid, "password1": "secret", "password2": "secret"}
|
|
|
|
)
|
2014-06-01 18:25:30 +02:00
|
|
|
mailbox = form.save()
|
|
|
|
self.assertIsNotNone(mailbox)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(len(Mailbox.objects.filter(osuser=user)), 1)
|
2014-06-01 18:25:30 +02:00
|
|
|
|
|
|
|
@override_settings(
|
2019-01-30 21:27:25 +01:00
|
|
|
CELERY_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND="memory", BROKER_BACKEND="memory"
|
2014-06-01 18:25:30 +02:00
|
|
|
)
|
|
|
|
def test_save_no_commit(self):
|
2015-02-01 20:12:23 +01:00
|
|
|
user = User.objects.create_user(customer=self.customer)
|
2019-01-30 21:27:25 +01:00
|
|
|
form = MailboxCreationForm(
|
|
|
|
data={"osuser": user.uid, "password1": "secret", "password2": "secret"}
|
|
|
|
)
|
2014-06-01 18:25:30 +02:00
|
|
|
mailbox = form.save(commit=False)
|
|
|
|
self.assertIsNotNone(mailbox)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(len(Mailbox.objects.filter(osuser=user)), 0)
|
2014-06-01 18:25:30 +02:00
|
|
|
|
|
|
|
|
2015-02-01 20:12:23 +01:00
|
|
|
class MailboxChangeFormTest(CustomerTestCase):
|
2014-06-01 18:25:30 +02:00
|
|
|
@override_settings(
|
2019-01-30 21:27:25 +01:00
|
|
|
CELERY_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND="memory", BROKER_BACKEND="memory"
|
2014-06-01 18:25:30 +02:00
|
|
|
)
|
|
|
|
def test_clean_password(self):
|
2015-02-01 20:12:23 +01:00
|
|
|
mailbox = Mailbox(
|
2019-01-30 21:27:25 +01:00
|
|
|
username="test", osuser=User.objects.create_user(customer=self.customer)
|
|
|
|
)
|
|
|
|
mailbox.set_password("test")
|
2014-06-01 18:25:30 +02:00
|
|
|
mailbox.save()
|
2019-01-30 21:27:25 +01:00
|
|
|
form = MailboxChangeForm(instance=mailbox, data={"password": "blub"})
|
2014-06-01 18:25:30 +02:00
|
|
|
self.assertEqual(form.clean_password(), mailbox.password)
|
|
|
|
|
|
|
|
|
2014-06-01 22:41:42 +02:00
|
|
|
class ActivationChangeMixinTest(TestCase):
|
2014-06-01 18:25:30 +02:00
|
|
|
def test_activate(self):
|
|
|
|
querysetmock = Mock()
|
|
|
|
activationchange = ActivationChangeMixin()
|
|
|
|
activationchange.activate(Mock(), querysetmock)
|
2024-08-23 17:57:40 +02:00
|
|
|
querysetmock.update.assert_called_with(active=True)
|
2014-06-01 18:25:30 +02:00
|
|
|
|
|
|
|
def test_deactivate(self):
|
|
|
|
querysetmock = Mock()
|
|
|
|
activationchange = ActivationChangeMixin()
|
|
|
|
activationchange.deactivate(Mock(), querysetmock)
|
2024-08-23 17:57:40 +02:00
|
|
|
querysetmock.update.assert_called_with(active=False)
|
2014-06-01 18:25:30 +02:00
|
|
|
|
|
|
|
|
2015-02-01 20:12:23 +01:00
|
|
|
class MailBoxAdminTest(CustomerTestCase):
|
2014-06-01 18:25:30 +02:00
|
|
|
def setUp(self):
|
2015-02-01 20:12:23 +01:00
|
|
|
super(MailBoxAdminTest, self).setUp()
|
2014-06-01 18:25:30 +02:00
|
|
|
site = AdminSite()
|
|
|
|
self.mbadmin = MailboxAdmin(Mailbox, site)
|
|
|
|
|
|
|
|
def test_get_fieldsets_without_object(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(self.mbadmin.get_fieldsets(Mock()), self.mbadmin.add_fieldsets)
|
2014-06-01 18:25:30 +02:00
|
|
|
|
|
|
|
@override_settings(
|
2019-01-30 21:27:25 +01:00
|
|
|
CELERY_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND="memory", BROKER_BACKEND="memory"
|
2014-06-01 18:25:30 +02:00
|
|
|
)
|
|
|
|
def test_get_fieldsets_with_object(self):
|
2015-02-01 20:12:23 +01:00
|
|
|
mailbox = Mailbox(
|
2019-01-30 21:27:25 +01:00
|
|
|
username="test", osuser=User.objects.create_user(customer=self.customer)
|
|
|
|
)
|
|
|
|
mailbox.set_password("test")
|
2014-06-01 18:25:30 +02:00
|
|
|
mailbox.save()
|
|
|
|
self.assertEqual(
|
2019-01-30 21:27:25 +01:00
|
|
|
self.mbadmin.get_fieldsets(Mock(), mailbox), self.mbadmin.fieldsets
|
|
|
|
)
|
2014-06-01 18:25:30 +02:00
|
|
|
|
|
|
|
def test_get_form_without_object(self):
|
|
|
|
form = self.mbadmin.get_form(Mock)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(form.Meta.fields, ["osuser", "password1", "password2"])
|
2014-06-01 18:25:30 +02:00
|
|
|
|
|
|
|
@override_settings(
|
2019-01-30 21:27:25 +01:00
|
|
|
CELERY_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND="memory", BROKER_BACKEND="memory"
|
2014-06-01 18:25:30 +02:00
|
|
|
)
|
|
|
|
def test_get_form_with_object(self):
|
2015-02-01 20:12:23 +01:00
|
|
|
mailbox = Mailbox(
|
2019-01-30 21:27:25 +01:00
|
|
|
username="test", osuser=User.objects.create_user(customer=self.customer)
|
|
|
|
)
|
|
|
|
mailbox.set_password("test")
|
2014-06-01 18:25:30 +02:00
|
|
|
mailbox.save()
|
|
|
|
form = self.mbadmin.get_form(Mock, mailbox)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(form.Meta.fields, ["osuser", "username", "password", "active"])
|
2014-06-01 18:25:30 +02:00
|
|
|
|
|
|
|
def test_admin_for_mailbox(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
admin_url = reverse("admin:managemails_mailaddress_changelist")
|
2014-06-01 18:25:30 +02:00
|
|
|
self.assertIsNotNone(admin_url)
|
|
|
|
|
|
|
|
|
2014-06-01 22:41:42 +02:00
|
|
|
class MailAddressAdminTest(TestCase):
|
2014-06-01 18:25:30 +02:00
|
|
|
def test_admin_for_mailaddress(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
admin_url = reverse("admin:managemails_mailaddress_changelist")
|
2014-06-01 18:25:30 +02:00
|
|
|
self.assertIsNotNone(admin_url)
|