diff options
Diffstat (limited to 'src/mailman')
| -rw-r--r-- | src/mailman/commands/cli_import.py | 27 | ||||
| -rw-r--r-- | src/mailman/commands/tests/test_import.py | 61 | ||||
| -rw-r--r-- | src/mailman/database/tests/test_factory.py | 11 | ||||
| -rw-r--r-- | src/mailman/docs/NEWS.rst | 2 | ||||
| -rw-r--r-- | src/mailman/model/listmanager.py | 4 | ||||
| -rw-r--r-- | src/mailman/model/mailinglist.py | 3 | ||||
| -rw-r--r-- | src/mailman/model/tests/test_mailinglist.py | 24 | ||||
| -rw-r--r-- | src/mailman/rest/tests/test_lists.py | 16 | ||||
| -rw-r--r-- | src/mailman/testing/config-with-instances.pck | bin | 0 -> 4667 bytes | |||
| -rw-r--r-- | src/mailman/utilities/importer.py | 23 | ||||
| -rw-r--r-- | src/mailman/utilities/tests/test_import.py | 43 |
11 files changed, 197 insertions, 17 deletions
diff --git a/src/mailman/commands/cli_import.py b/src/mailman/commands/cli_import.py index 30aeb7894..344d5baee 100644 --- a/src/mailman/commands/cli_import.py +++ b/src/mailman/commands/cli_import.py @@ -25,6 +25,7 @@ __all__ = [ import sys import pickle +from contextlib import ExitStack, contextmanager from mailman.core.i18n import _ from mailman.database.transaction import transactional from mailman.interfaces.command import ICLISubCommand @@ -35,6 +36,25 @@ from zope.interface import implementer +# A fake Bouncer class from Mailman 2.1, we don't use it but there are +# instances in the .pck files. +class Bouncer: + class _BounceInfo: + pass + + +@contextmanager +def hacked_sys_modules(): + assert 'Mailman.Bouncer' not in sys.modules + sys.modules['Mailman.Bouncer'] = Bouncer + try: + yield + finally: + del sys.modules['Mailman.Bouncer'] + + + + @implementer(ICLISubCommand) class Import21: """Import Mailman 2.1 list data.""" @@ -74,10 +94,13 @@ class Import21: assert len(args.pickle_file) == 1, ( 'Unexpected positional arguments: %s' % args.pickle_file) filename = args.pickle_file[0] - with open(filename, 'rb') as fp: + with ExitStack() as resources: + fp = resources.enter_context(open(filename, 'rb')) + resources.enter_context(hacked_sys_modules()) while True: try: - config_dict = pickle.load(fp) + config_dict = pickle.load( + fp, encoding='utf-8', errors='ignore') except EOFError: break except pickle.UnpicklingError: diff --git a/src/mailman/commands/tests/test_import.py b/src/mailman/commands/tests/test_import.py new file mode 100644 index 000000000..96f955e52 --- /dev/null +++ b/src/mailman/commands/tests/test_import.py @@ -0,0 +1,61 @@ +# Copyright (C) 2015 by the Free Software Foundation, Inc. +# +# This file is part of GNU Mailman. +# +# GNU Mailman is free software: you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# +# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with +# GNU Mailman. If not, see <http://www.gnu.org/licenses/>. + +"""Test the `mailman import21` subcommand.""" + +__all__ = [ + 'TestImport', + ] + + +import unittest + +from mailman.app.lifecycle import create_list +from mailman.commands.cli_import import Import21 +from mailman.testing.layers import ConfigLayer +from mock import patch +from pkg_resources import resource_filename + + + +class FakeArgs: + listname = ['test@example.com'] + pickle_file = [ + resource_filename('mailman.testing', 'config-with-instances.pck'), + ] + + + +class TestImport(unittest.TestCase): + layer = ConfigLayer + + def setUp(self): + self.command = Import21() + self.args = FakeArgs() + self.mlist = create_list('test@example.com') + + @patch('mailman.commands.cli_import.import_config_pck') + def test_process_pickle_with_bounce_info(self, import_config_pck): + # The sample data contains Mailman 2 bounce info, represented as + # _BounceInfo instances. We throw these away when importing to + # Mailman 3, but we have to fake the instance's classes, otherwise + # unpickling the dictionaries will fail. + try: + self.command.process(self.args) + except ImportError as error: + self.fail('The pickle failed loading: {}'.format(error)) + self.assertTrue(import_config_pck.called) diff --git a/src/mailman/database/tests/test_factory.py b/src/mailman/database/tests/test_factory.py index 6a16c74ab..acb7c32a4 100644 --- a/src/mailman/database/tests/test_factory.py +++ b/src/mailman/database/tests/test_factory.py @@ -129,17 +129,14 @@ class TestSchemaManager(unittest.TestCase): md.tables['alembic_version'].select()).scalar() self.assertEqual(current_rev, head_rev) - @patch('alembic.command.stamp') - def test_storm(self, alembic_command_stamp): + @patch('alembic.command') + def test_storm(self, alembic_command): # Existing Storm database. Model.metadata.create_all(config.db.engine) self._create_storm_database(LAST_STORM_SCHEMA_VERSION) self.schema_mgr.setup_database() - self.assertFalse(alembic_command_stamp.called) - self.assertTrue( - self._table_exists('mailinglist') - and self._table_exists('alembic_version') - and not self._table_exists('version')) + self.assertFalse(alembic_command.stamp.called) + self.assertTrue(alembic_command.upgrade.called) @patch('alembic.command') def test_old_storm(self, alembic_command): diff --git a/src/mailman/docs/NEWS.rst b/src/mailman/docs/NEWS.rst index 1fda90a89..77c45375d 100644 --- a/src/mailman/docs/NEWS.rst +++ b/src/mailman/docs/NEWS.rst @@ -28,6 +28,8 @@ Bugs by Manish Gill. (LP: #1166911) * When deleting a user object, make sure their preferences are also deleted. Given by Abhishek. (LP: #1418276) + * Be sure a mailing list's acceptable aliases are deleted when the mailing + list itself is deleted. (LP: #1432239) Configuration ------------- diff --git a/src/mailman/model/listmanager.py b/src/mailman/model/listmanager.py index be0e153a3..8fc739543 100644 --- a/src/mailman/model/listmanager.py +++ b/src/mailman/model/listmanager.py @@ -27,7 +27,7 @@ from mailman.interfaces.address import InvalidEmailAddressError from mailman.interfaces.listmanager import ( IListManager, ListAlreadyExistsError, ListCreatedEvent, ListCreatingEvent, ListDeletedEvent, ListDeletingEvent) -from mailman.model.mailinglist import MailingList +from mailman.model.mailinglist import IAcceptableAliasSet, MailingList from mailman.model.mime import ContentFilter from mailman.utilities.datetime import now from zope.event import notify @@ -74,6 +74,8 @@ class ListManager: """See `IListManager`.""" fqdn_listname = mlist.fqdn_listname notify(ListDeletingEvent(mlist)) + # First delete information associated with the mailing list. + IAcceptableAliasSet(mlist).clear() store.query(ContentFilter).filter_by(mailing_list=mlist).delete() store.delete(mlist) notify(ListDeletedEvent(fqdn_listname)) diff --git a/src/mailman/model/mailinglist.py b/src/mailman/model/mailinglist.py index 0806ce398..cef272437 100644 --- a/src/mailman/model/mailinglist.py +++ b/src/mailman/model/mailinglist.py @@ -506,10 +506,11 @@ class AcceptableAlias(Model): mailing_list_id = Column( Integer, ForeignKey('mailinglist.id'), index=True, nullable=False) - mailing_list = relationship('MailingList', backref='acceptable_alias') + mailing_list = relationship('MailingList', backref='acceptablealias') alias = Column(Unicode, index=True, nullable=False) def __init__(self, mailing_list, alias): + super(AcceptableAlias, self).__init__() self.mailing_list = mailing_list self.alias = alias diff --git a/src/mailman/model/tests/test_mailinglist.py b/src/mailman/model/tests/test_mailinglist.py index 843918e5e..745096b4b 100644 --- a/src/mailman/model/tests/test_mailinglist.py +++ b/src/mailman/model/tests/test_mailinglist.py @@ -18,6 +18,7 @@ """Test MailingLists and related model objects..""" __all__ = [ + 'TestAcceptableAliases', 'TestDisabledListArchiver', 'TestListArchiver', 'TestMailingList', @@ -28,7 +29,10 @@ import unittest from mailman.app.lifecycle import create_list from mailman.config import config -from mailman.interfaces.mailinglist import IListArchiverSet +from mailman.database.transaction import transaction +from mailman.interfaces.listmanager import IListManager +from mailman.interfaces.mailinglist import ( + IAcceptableAliasSet, IListArchiverSet) from mailman.interfaces.member import ( AlreadySubscribedError, MemberRole, MissingPreferredAddressError) from mailman.interfaces.usermanager import IUserManager @@ -141,3 +145,21 @@ class TestDisabledListArchiver(unittest.TestCase): archiver = archiver_set.get('prototype') self.assertTrue(archiver.is_enabled) config.pop('enable prototype') + + + +class TestAcceptableAliases(unittest.TestCase): + layer = ConfigLayer + + def setUp(self): + self._mlist = create_list('ant@example.com') + + def test_delete_list_with_acceptable_aliases(self): + # LP: #1432239 - deleting a mailing list with acceptable aliases + # causes a SQLAlchemy error. The aliases must be deleted first. + with transaction(): + alias_set = IAcceptableAliasSet(self._mlist) + alias_set.add('bee@example.com') + self.assertEqual(['bee@example.com'], list(alias_set.aliases)) + getUtility(IListManager).delete(self._mlist) + self.assertEqual(len(list(alias_set.aliases)), 0) diff --git a/src/mailman/rest/tests/test_lists.py b/src/mailman/rest/tests/test_lists.py index a365db969..8e89f423c 100644 --- a/src/mailman/rest/tests/test_lists.py +++ b/src/mailman/rest/tests/test_lists.py @@ -28,8 +28,12 @@ __all__ = [ import unittest from mailman.app.lifecycle import create_list +from mailman.config import config from mailman.database.transaction import transaction +from mailman.interfaces.listmanager import IListManager +from mailman.interfaces.mailinglist import IAcceptableAliasSet from mailman.interfaces.usermanager import IUserManager +from mailman.model.mailinglist import AcceptableAlias from mailman.testing.helpers import call_api from mailman.testing.layers import RESTLayer from urllib.error import HTTPError @@ -176,6 +180,18 @@ class TestLists(unittest.TestCase): self.assertEqual(member['email'], 'bart@example.com') self.assertEqual(member['role'], 'member') + def test_delete_list_with_acceptable_aliases(self): + # LP: #1432239 - deleting a mailing list with acceptable aliases + # causes a SQLAlchemy error. The aliases must be deleted first. + with transaction(): + alias_set = IAcceptableAliasSet(self._mlist) + alias_set.add('bee@example.com') + call_api('http://localhost:9001/3.0/lists/test.example.com', + method='DELETE') + # Neither the mailing list, nor the aliases are present. + self.assertIsNone(getUtility(IListManager).get('test@example.com')) + self.assertEqual(config.db.store.query(AcceptableAlias).count(), 0) + class TestListArchivers(unittest.TestCase): diff --git a/src/mailman/testing/config-with-instances.pck b/src/mailman/testing/config-with-instances.pck Binary files differnew file mode 100644 index 000000000..b5173f58f --- /dev/null +++ b/src/mailman/testing/config-with-instances.pck diff --git a/src/mailman/utilities/importer.py b/src/mailman/utilities/importer.py index 8590d9b1b..bb4273b12 100644 --- a/src/mailman/utilities/importer.py +++ b/src/mailman/utilities/importer.py @@ -32,6 +32,7 @@ from mailman.config import config from mailman.core.errors import MailmanError from mailman.handlers.decorate import decorate, decorate_template from mailman.interfaces.action import Action, FilterAction +from mailman.interfaces.address import IEmailValidator from mailman.interfaces.archiver import ArchivePolicy from mailman.interfaces.autorespond import ResponseAction from mailman.interfaces.bans import IBanManager @@ -387,11 +388,17 @@ def import_config_pck(mlist, config_dict): regulars_set = set(config_dict.get('members', {})) digesters_set = set(config_dict.get('digest_members', {})) members = regulars_set.union(digesters_set) - import_roster(mlist, config_dict, members, MemberRole.member) - import_roster(mlist, config_dict, config_dict.get('owner', []), - MemberRole.owner) - import_roster(mlist, config_dict, config_dict.get('moderator', []), - MemberRole.moderator) + # Don't send welcome messages when we import the rosters. + send_welcome_message = mlist.send_welcome_message + mlist.send_welcome_message = False + try: + import_roster(mlist, config_dict, members, MemberRole.member) + import_roster(mlist, config_dict, config_dict.get('owner', []), + MemberRole.owner) + import_roster(mlist, config_dict, config_dict.get('moderator', []), + MemberRole.moderator) + finally: + mlist.send_welcome_message = send_welcome_message @@ -408,6 +415,7 @@ def import_roster(mlist, config_dict, members, role): :type role: MemberRole enum """ usermanager = getUtility(IUserManager) + validator = getUtility(IEmailValidator) roster = mlist.get_roster(role) for email in members: # For owners and members, the emails can have a mixed case, so @@ -427,8 +435,13 @@ def import_roster(mlist, config_dict, members, role): merged_members.update(config_dict.get('digest_members', {})) if merged_members.get(email, 0) != 0: original_email = bytes_to_str(merged_members[email]) + if not validator.is_valid(original_email): + original_email = email else: original_email = email + if not validator.is_valid(original_email): + # Skip this one entirely. + continue address = usermanager.create_address(original_email) address.verified_on = datetime.datetime.now() user.link(address) diff --git a/src/mailman/utilities/tests/test_import.py b/src/mailman/utilities/tests/test_import.py index b0ab9938d..938ef7d2e 100644 --- a/src/mailman/utilities/tests/test_import.py +++ b/src/mailman/utilities/tests/test_import.py @@ -38,6 +38,7 @@ from mailman.app.lifecycle import create_list from mailman.config import config from mailman.handlers.decorate import decorate from mailman.interfaces.action import Action, FilterAction +from mailman.interfaces.address import InvalidEmailAddressError from mailman.interfaces.archiver import ArchivePolicy from mailman.interfaces.autorespond import ResponseAction from mailman.interfaces.bans import IBanManager @@ -747,6 +748,48 @@ class TestRosterImport(unittest.TestCase): anne = self._usermanager.get_user('anne@example.com') self.assertTrue(anne.controls('anne@example.com')) + def test_invalid_original_email(self): + # When the member has an original email address (i.e. the + # case-preserved version) that is invalid, their new address record's + # original_email attribute will only be the case insensitive version. + self._pckdict['members']['anne@example.com'] = b'invalid email address' + try: + import_config_pck(self._mlist, self._pckdict) + except InvalidEmailAddressError as error: + self.fail(error) + self.assertIn('anne@example.com', + [a.email for a in self._mlist.members.addresses]) + anne = self._usermanager.get_address('anne@example.com') + self.assertEqual(anne.original_email, 'anne@example.com') + + def test_invalid_email(self): + # When a member's email address is invalid, that member is skipped + # during the import. + self._pckdict['members'] = { + 'anne@example.com': 0, + 'invalid email address': b'invalid email address' + } + self._pckdict['digest_members'] = {} + try: + import_config_pck(self._mlist, self._pckdict) + except InvalidEmailAddressError as error: + self.fail(error) + self.assertEqual(['anne@example.com'], + [a.email for a in self._mlist.members.addresses]) + + def test_no_email_sent(self): + # No welcome message is sent to newly imported members. + self.assertTrue(self._mlist.send_welcome_message) + import_config_pck(self._mlist, self._pckdict) + self.assertIn('anne@example.com', + [a.email for a in self._mlist.members.addresses]) + # There are no messages in any of the queues. + for queue, switchboard in config.switchboards.items(): + file_count = len(switchboard.files) + self.assertEqual(file_count, 0, + "Unexpected queue '{}' file count: {}".format( + queue, file_count)) + self.assertTrue(self._mlist.send_welcome_message) |
