| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# Copyright (C) 2016-2023 by the Free Software Foundation, Inc. |
| 4 |
# |
| 5 |
# This file is part of Django-Mailman. |
| 6 |
# |
| 7 |
# Django-Mailman is free software: you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by the Free |
| 9 |
# Software Foundation, either version 3 of the License, or (at your option) |
| 10 |
# any later version. |
| 11 |
# |
| 12 |
# Django-Mailman is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 15 |
# more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License along with |
| 18 |
# Django-Mailman. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
# |
| 20 |
|
| 21 |
|
| 22 |
import logging |
| 23 |
import os |
| 24 |
import sys |
| 25 |
from contextlib import contextmanager |
| 26 |
from io import StringIO |
| 27 |
from unittest.mock import Mock, patch |
| 28 |
|
| 29 |
from django.contrib.messages.storage.cookie import CookieStorage |
| 30 |
from django.core.cache import cache |
| 31 |
from django.test import RequestFactory |
| 32 |
from django.test import TestCase as DjangoTestCase |
| 33 |
from django.utils.timezone import now |
| 34 |
|
| 35 |
import mailmanclient |
| 36 |
|
| 37 |
|
| 38 |
def get_flash_messages(response, empty=True): |
| 39 |
if "messages" not in response.cookies: |
| 40 |
return [] |
| 41 |
# A RequestFactory will not run the messages middleware, and thus will |
| 42 |
# not delete the messages after retrieval. |
| 43 |
dummy_request = RequestFactory().get("/") |
| 44 |
dummy_request.COOKIES["messages"] = response.cookies["messages"].value |
| 45 |
msgs = list(CookieStorage(dummy_request)) |
| 46 |
if empty: |
| 47 |
del response.client.cookies["messages"] |
| 48 |
return msgs |
| 49 |
get_flash_messages.__test__ = False # noqa: E305 |
| 50 |
|
| 51 |
|
| 52 |
def get_test_file(*fileparts): |
| 53 |
return os.path.join(os.path.dirname(__file__), "testdata", *fileparts) |
| 54 |
get_test_file.__test__ = False # noqa: E305 |
| 55 |
|
| 56 |
|
| 57 |
class FakeMMList: |
| 58 |
def __init__(self, name): |
| 59 |
if '@' not in name: |
| 60 |
name = name.replace('.', '@', 1) |
| 61 |
self.fqdn_listname = name |
| 62 |
self.display_name = name.partition("@")[0] |
| 63 |
self.list_id = name.replace("@", ".") |
| 64 |
self.settings = { |
| 65 |
"description": "", |
| 66 |
"subject_prefix": "[%s] " % self.display_name, |
| 67 |
"created_at": now().isoformat(), |
| 68 |
"archive_policy": "public", |
| 69 |
"advertised": False, |
| 70 |
"archive_rendering_mode": "text", |
| 71 |
} |
| 72 |
self.owners = [] |
| 73 |
self.moderators = [] |
| 74 |
|
| 75 |
|
| 76 |
class FakeMMMember: |
| 77 |
def __init__(self, list_id, address, role="member"): |
| 78 |
self.list_id = list_id |
| 79 |
self.address = address |
| 80 |
self.role = role |
| 81 |
|
| 82 |
|
| 83 |
class FakeMMPage(): |
| 84 |
|
| 85 |
def __init__(self, entries=None, count=20, page=1): |
| 86 |
self.entries = entries or [] |
| 87 |
self._count = count |
| 88 |
self._page = page |
| 89 |
|
| 90 |
def __iter__(self): |
| 91 |
first = (self._page - 1) * self._count |
| 92 |
last = self._page * self._count |
| 93 |
for entry in self.entries[first:last]: |
| 94 |
yield entry |
| 95 |
|
| 96 |
@property |
| 97 |
def has_previous(self): |
| 98 |
return self._page > 1 |
| 99 |
|
| 100 |
@property |
| 101 |
def has_next(self): |
| 102 |
return self._count * self._page < len(self.entries) |
| 103 |
|
| 104 |
@property |
| 105 |
def total_size(self): |
| 106 |
return len(self.entries) |
| 107 |
|
| 108 |
|
| 109 |
class FakeMMAddress: |
| 110 |
|
| 111 |
def __init__(self, email, verified=False): |
| 112 |
self.email = email |
| 113 |
self.verified = verified |
| 114 |
|
| 115 |
def __str__(self): |
| 116 |
return self.email |
| 117 |
|
| 118 |
def verify(self): |
| 119 |
self.verified = True |
| 120 |
|
| 121 |
def save(self): |
| 122 |
pass |
| 123 |
|
| 124 |
|
| 125 |
class FakeMMAddressList(list): |
| 126 |
|
| 127 |
def find_by_email(self, email): |
| 128 |
try: |
| 129 |
index = [a.email for a in self].index(email) |
| 130 |
except ValueError: |
| 131 |
return None |
| 132 |
return self[index] |
| 133 |
|
| 134 |
|
| 135 |
def setup_logging(extra_apps=[]): |
| 136 |
formatter = logging.Formatter(fmt="%(message)s") |
| 137 |
levels = ["debug", "info", "warning", "error"] |
| 138 |
handlers = [] |
| 139 |
log = StringIO() |
| 140 |
for level_name in levels: |
| 141 |
handler = logging.StreamHandler(log) |
| 142 |
handler.setLevel(getattr(logging, level_name.upper())) |
| 143 |
handler.setFormatter(formatter) |
| 144 |
handlers.append(handler) |
| 145 |
for logger_name in ["django", "django_mailman3"] + extra_apps: |
| 146 |
logger = logging.getLogger(logger_name) |
| 147 |
logger.propagate = False |
| 148 |
logger.setLevel(logging.DEBUG) |
| 149 |
del logger.handlers[:] |
| 150 |
for handler in handlers: |
| 151 |
logger.addHandler(handler) |
| 152 |
return log |
| 153 |
|
| 154 |
|
| 155 |
class TestCase(DjangoTestCase): |
| 156 |
|
| 157 |
def _pre_setup(self): |
| 158 |
super(TestCase, self)._pre_setup() |
| 159 |
# Logging |
| 160 |
self.log = setup_logging() |
| 161 |
# Mock the Mailman client |
| 162 |
self.mailman_client = Mock() |
| 163 |
self.mailman_client.get_user.side_effect = \ |
| 164 |
mailmanclient.MailmanConnectionError() |
| 165 |
self.mailman_client.get_list.side_effect = \ |
| 166 |
mailmanclient.MailmanConnectionError() |
| 167 |
self._mm_client_patcher = patch( |
| 168 |
"django_mailman3.lib.mailman.MailmanClient", |
| 169 |
lambda *a: self.mailman_client) |
| 170 |
self._mm_client_patcher.start() |
| 171 |
|
| 172 |
def _post_teardown(self): |
| 173 |
self._mm_client_patcher.stop() |
| 174 |
cache.clear() |
| 175 |
super(TestCase, self)._post_teardown() |
| 176 |
|
| 177 |
|
| 178 |
@contextmanager |
| 179 |
def captured_output(): |
| 180 |
new_out, new_err = StringIO(), StringIO() |
| 181 |
old_out, old_err = sys.stdout, sys.stderr |
| 182 |
try: |
| 183 |
sys.stdout, sys.stderr = new_out, new_err |
| 184 |
yield sys.stdout, sys.stderr |
| 185 |
finally: |
| 186 |
sys.stdout, sys.stderr = old_out, old_err |