def test_same_user(self): # Adding the address of an existing User must not create another user. user = self._usermanager.create_user('*****@*****.**', 'Anne') user.register('*****@*****.**') # secondary email import_config_pck(self._mlist, self._pckdict) member = self._mlist.members.get_member('*****@*****.**') self.assertEqual(member.user, user)
def process(self, args): """See `ICLISubCommand`.""" # Could be None or sequence of length 0. if args.listname is None: self.parser.error(_('List name is required')) return assert len(args.listname) == 1, ( 'Unexpected positional arguments: %s' % args.listname) fqdn_listname = args.listname[0] mlist = getUtility(IListManager).get(fqdn_listname) if mlist is None: self.parser.error(_('No such list: $fqdn_listname')) return if args.pickle_file is None: self.parser.error(_('config.pck file is required')) return assert len(args.pickle_file) == 1, ( 'Unexpected positional arguments: %s' % args.pickle_file) filename = args.pickle_file[0] with open(filename) as fp: while True: try: config_dict = cPickle.load(fp) except EOFError: break except cPickle.UnpicklingError: self.parser.error( _('Not a Mailman 2.1 configuration file: $filename')) return else: if not isinstance(config_dict, dict): print(_('Ignoring non-dictionary: {0!r}').format( config_dict), file=sys.stderr) continue import_config_pck(mlist, config_dict)
def test_keep_default(self): # If the value was not changed from MM2.1's default, don't import it. default_msg_footer = ( '_______________________________________________\n' '%(real_name)s mailing list\n' '%(real_name)s@%(host_name)s\n' '%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s\n' ) loader = getUtility(ITemplateLoader) for oldvar in ('msg_footer', 'digest_footer'): newvar = self._conf_mapping[oldvar] self._pckdict[str(oldvar)] = str(default_msg_footer) try: old_value = loader.get(newvar, self._mlist) except URLError: old_value = None import_config_pck(self._mlist, self._pckdict) try: new_value = loader.get(newvar, self._mlist) except URLError: new_value = None self.assertEqual( old_value, new_value, '{} changed unexpectedly: {} != {}'.format( newvar, old_value, new_value))
def test_keep_default_if_fqdn_changed(self): # Use case: importing the old [email protected] into [email protected]. We can't check # if it changed from the default so don't import. We may do more harm # than good and it's easy to change if needed. test_value = b'TEST-VALUE' # We need an IDomain for this mail_host. getUtility(IDomainManager).add('test.example.com') manager = getUtility(ITemplateManager) for oldvar, newvar in self._conf_mapping.items(): self._mlist.mail_host = 'example.com' self._pckdict['mail_host'] = b'test.example.com' self._pckdict[str(oldvar)] = test_value try: old_value = manager.get(newvar, 'blank.example.com') except URLError: old_value = None # Suppress warning messages in the test output. with mock.patch('sys.stderr'): import_config_pck(self._mlist, self._pckdict) try: new_value = manager.get(newvar, 'test.example.com') except URLError: new_value = None self.assertEqual( old_value, new_value, '{} changed unexpectedly: {} != {}'.format( newvar, old_value, new_value))
def import21(ctx, listspec, pickle_file): mlist = getUtility(IListManager).get(listspec) if mlist is None: ctx.fail(_('No such list: $listspec')) with ExitStack() as resources: resources.enter_context(hacked_sys_modules('Mailman.Bouncer', Bouncer)) resources.enter_context(transaction()) while True: try: config_dict = pickle.load(pickle_file, encoding='utf-8', errors='ignore') except EOFError: break except pickle.UnpicklingError: ctx.fail( _('Not a Mailman 2.1 configuration file: $pickle_file')) else: if not isinstance(config_dict, dict): print(_('Ignoring non-dictionary: {0!r}').format( config_dict), file=sys.stderr) continue try: import_config_pck(mlist, config_dict) except Import21Error as error: print(error, file=sys.stderr) sys.exit(1)
def test_new_language(self): self._pckdict[b'language']['*****@*****.**'] = b'xx_XX' try: import_config_pck(self._mlist, self._pckdict) except Import21Error as error: self.assertIn('[language.xx_XX]', str(error)) else: self.fail('Import21Error was not raised')
def test_language(self): import_config_pck(self._mlist, self._pckdict) for name in ('anne', 'bob', 'cindy', 'dave'): addr = '*****@*****.**' % name member = self._mlist.members.get_member(addr) self.assertIsNotNone(member, 'Address %s was not imported' % addr) self.assertEqual(member.preferred_language.code, self._pckdict['language'][addr])
def test_new_language(self): self._pckdict['language']['*****@*****.**'] = b'xx_XX' try: import_config_pck(self._mlist, self._pckdict) except Import21Error as error: self.assertIn('[language.xx_XX]', str(error)) else: # pragma: no cover self.fail('Import21Error was not raised')
def test_text_to_uri(self): for oldvar, newvar in self._conf_mapping.items(): self._pckdict[str(oldvar)] = b'TEST VALUE' import_config_pck(self._mlist, self._pckdict) text = decorate(newvar, self._mlist) self.assertEqual( text, 'TEST VALUE', 'Old variable %s was not properly imported to %s' % (oldvar, newvar))
def test_text_to_uri(self): for oldvar, newvar in self._conf_mapping.items(): self._pckdict[str(oldvar)] = b'TEST VALUE' import_config_pck(self._mlist, self._pckdict) newattr = getattr(self._mlist, newvar) text = decorate(self._mlist, newattr) self.assertEqual(text, 'TEST VALUE', 'Old variable %s was not properly imported to %s' % (oldvar, newvar))
def test_address_already_exists_but_no_user(self): # An address already exists, but it is not linked to a user nor # subscribed. anne_addr = self._usermanager.create_address('*****@*****.**', 'Anne') import_config_pck(self._mlist, self._pckdict) anne = self._usermanager.get_user('*****@*****.**') self.assertTrue(anne.controls('*****@*****.**')) self.assertIn(anne_addr, self._mlist.regular_members.addresses)
def test_password(self): #self.anne.password = config.password_context.encrypt('abc123') import_config_pck(self._mlist, self._pckdict) for name in ('anne', 'bob', 'cindy', 'dave'): addr = '*****@*****.**' % name user = self._usermanager.get_user(addr) self.assertIsNotNone(user, 'Address %s was not imported' % addr) self.assertEqual(user.password, '{plaintext}%spass' % name, 'Password for %s was not imported' % addr)
def test_address_already_exists_but_no_user(self): # An address already exists, but it is not linked to a user nor # subscribed. anne_addr = self._usermanager.create_address( '*****@*****.**', 'Anne') import_config_pck(self._mlist, self._pckdict) anne = self._usermanager.get_user('*****@*****.**') self.assertTrue(anne.controls('*****@*****.**')) self.assertIn(anne_addr, self._mlist.regular_members.addresses)
def test_address_already_subscribed_but_no_user(self): # An address is already subscribed, but it is not linked to a user. anne_addr = self._usermanager.create_address('*****@*****.**', 'Anne') self._mlist.subscribe(anne_addr) # Suppress warning messages in test output. with mock.patch('sys.stderr'): import_config_pck(self._mlist, self._pckdict) anne = self._usermanager.get_user('*****@*****.**') self.assertTrue(anne.controls('*****@*****.**'))
def test_unicode(self): # non-ascii templates for oldvar in self._conf_mapping: self._pckdict[str(oldvar)] = b'Ol\xe1!' import_config_pck(self._mlist, self._pckdict) for oldvar, newvar in self._conf_mapping.iteritems(): newattr = getattr(self._mlist, newvar) text = decorate(self._mlist, newattr) expected = u'Ol\ufffd!' self.assertEqual(text, expected)
def test_unicode(self): # non-ascii templates for oldvar in self._conf_mapping: self._pckdict[str(oldvar)] = b'Ol\xe1!' import_config_pck(self._mlist, self._pckdict) for oldvar, newvar in self._conf_mapping.items(): newattr = getattr(self._mlist, newvar) text = decorate(self._mlist, newattr) expected = u'Ol\ufffd!' self.assertEqual(text, expected)
def test_moderator(self): import_config_pck(self._mlist, self._pckdict) for name in ('bob', 'fred'): addr = '*****@*****.**' % name self.assertIn(addr, [a.email for a in self._mlist.moderators.addresses], 'Address %s was not imported as moderator' % addr) self.assertNotIn('*****@*****.**', [a.email for a in self._mlist.members.addresses], 'Address fred@ was wrongly added to the members list')
def test_owner_and_moderator_not_lowercase(self): # In the v2.1 pickled dict, the owner and moderator lists are not # necessarily lowercased already. self._pckdict['owner'] = [b'*****@*****.**'] self._pckdict['moderator'] = [b'*****@*****.**'] import_config_pck(self._mlist, self._pckdict) self.assertIn('*****@*****.**', [a.email for a in self._mlist.owners.addresses]) self.assertIn('*****@*****.**', [a.email for a in self._mlist.moderators.addresses])
def test_password(self): #self.anne.password = config.password_context.encrypt('abc123') import_config_pck(self._mlist, self._pckdict) for name in ('anne', 'bob', 'cindy', 'dave'): addr = '*****@*****.**' % name user = self._usermanager.get_user(addr) self.assertIsNotNone(user, 'Address %s was not imported' % addr) self.assertEqual( user.password, b'{plaintext}%spass' % name, 'Password for %s was not imported' % addr)
def test_language(self): import_config_pck(self._mlist, self._pckdict) for name in ('anne', 'bob', 'cindy', 'dave'): addr = '*****@*****.**' % name member = self._mlist.members.get_member(addr) self.assertIsNotNone(member, 'Address %s was not imported' % addr) code = self._pckdict['language'][addr] if isinstance(code, bytes): code = code.decode('utf-8') self.assertEqual(member.preferred_language.code, code)
def test_address_already_subscribed_but_no_user(self): # An address is already subscribed, but it is not linked to a user. anne_addr = self._usermanager.create_address( '*****@*****.**', 'Anne') self._mlist.subscribe(anne_addr) # Suppress warning messages in test output. with mock.patch('sys.stderr'): import_config_pck(self._mlist, self._pckdict) anne = self._usermanager.get_user('*****@*****.**') self.assertTrue(anne.controls('*****@*****.**'))
def test_unicode(self): # non-ascii templates for oldvar in self._conf_mapping: self._pckdict[str(oldvar)] = b'Ol\xe1!' import_config_pck(self._mlist, self._pckdict) for oldvar, newvar in self._conf_mapping.items(): text = decorate(newvar, self._mlist) expected = u'Ol\ufffd!' self.assertEqual( text, expected, '{} -> {} did not get converted'.format(oldvar, newvar))
def test_owner(self): import_config_pck(self._mlist, self._pckdict) for name in ('anne', 'emily'): addr = '*****@*****.**' % name self.assertIn(addr, [a.email for a in self._mlist.owners.addresses], 'Address %s was not imported as owner' % addr) self.assertNotIn( '*****@*****.**', [a.email for a in self._mlist.members.addresses], 'Address emily@ was wrongly added to the members list')
def test_username(self): import_config_pck(self._mlist, self._pckdict) for name in ('anne', 'bob', 'cindy', 'dave'): addr = '*****@*****.**' % name user = self._usermanager.get_user(addr) address = self._usermanager.get_address(addr) self.assertIsNotNone(user, 'User %s was not imported' % addr) self.assertIsNotNone(address, 'Address %s was not imported' % addr) display_name = self._pckdict['usernames'][addr] self.assertEqual(user.display_name, display_name, 'The display name was not set for User %s' % addr) self.assertEqual(address.display_name, display_name, 'The display name was not set for Address %s' % addr)
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('*****@*****.**', [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)
def test_unicode_in_default(self): # What if the default template is already in UTF-8? For example, if # you import it twice. footer = b'\xe4\xb8\xad $listinfo_uri' footer_path = os.path.join(config.VAR_DIR, 'templates', 'lists', '*****@*****.**', 'en', 'footer.txt') makedirs(os.path.dirname(footer_path)) with open(footer_path, 'wb') as fp: fp.write(footer) self._pckdict['msg_footer'] = b'NEW-VALUE' import_config_pck(self._mlist, self._pckdict) text = decorate('list:member:regular:footer', self._mlist) self.assertEqual(text, 'NEW-VALUE')
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']['*****@*****.**'] = b'invalid email address' try: import_config_pck(self._mlist, self._pckdict) except InvalidEmailAddressError as error: self.fail(error) self.assertIn('*****@*****.**', [a.email for a in self._mlist.members.addresses]) anne = self._usermanager.get_address('*****@*****.**') self.assertEqual(anne.original_email, '*****@*****.**')
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('*****@*****.**', [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)
def test_invalid_email(self): # When a member's email address is invalid, that member is skipped # during the import. self._pckdict['members'] = { '*****@*****.**': 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(['*****@*****.**'], [a.email for a in self._mlist.members.addresses])
def test_unicode_in_default(self): # What if the default template is already in UTF-8? For example, if # you import it twice. footer = b'\xe4\xb8\xad $listinfo_uri' footer_path = os.path.join( config.VAR_DIR, 'templates', 'lists', '*****@*****.**', 'en', 'footer-generic.txt') makedirs(os.path.dirname(footer_path)) with open(footer_path, 'wb') as fp: fp.write(footer) self._pckdict[b'msg_footer'] = b'NEW-VALUE' import_config_pck(self._mlist, self._pckdict) text = decorate(self._mlist, self._mlist.footer_uri) self.assertEqual(text, 'NEW-VALUE')
def test_substitutions(self): test_text = ('UNIT TESTING %(real_name)s mailing list\n' '%(real_name)s@%(host_name)s') expected_text = ('UNIT TESTING $display_name mailing list ' '-- $listname\n' 'To unsubscribe send an email to ' '${short_listname}-leave@${domain}') for oldvar, newvar in self._conf_mapping.items(): self._pckdict[str(oldvar)] = str(test_text) import_config_pck(self._mlist, self._pckdict) text = getUtility(ITemplateLoader).get(newvar, self._mlist) self.assertEqual( text, expected_text, 'Old variables were not converted for %s' % newvar)
def test_username(self): import_config_pck(self._mlist, self._pckdict) for name in ('anne', 'bob', 'cindy', 'dave'): addr = '*****@*****.**' % name user = self._usermanager.get_user(addr) address = self._usermanager.get_address(addr) self.assertIsNotNone(user, 'User %s was not imported' % addr) self.assertIsNotNone(address, 'Address %s was not imported' % addr) display_name = self._pckdict['usernames'][addr] self.assertEqual(user.display_name, display_name, 'The display name was not set for User %s' % addr) self.assertEqual( address.display_name, display_name, 'The display name was not set for Address %s' % addr)
def test_member(self): import_config_pck(self._mlist, self._pckdict) for name in ('anne', 'bob', 'cindy', 'dave'): addr = '*****@*****.**' % name self.assertIn(addr, [a.email for a in self._mlist.members.addresses], 'Address %s was not imported' % addr) self.assertIn('*****@*****.**', [a.email for a in self._mlist.regular_members.addresses]) self.assertIn('*****@*****.**', [a.email for a in self._mlist.regular_members.addresses]) self.assertIn('*****@*****.**', [a.email for a in self._mlist.digest_members.addresses]) self.assertIn('*****@*****.**', [a.email for a in self._mlist.digest_members.addresses])
def _do_test(self, oldvalue, expected): self._pckdict['user_options']['*****@*****.**'] = oldvalue import_config_pck(self._mlist, self._pckdict) user = self._usermanager.get_user('*****@*****.**') self.assertIsNotNone(user, 'User was not imported') member = self._mlist.members.get_member('*****@*****.**') self.assertIsNotNone(member, 'Address was not subscribed') for exp_name, exp_val in expected.items(): try: currentval = getattr(member, exp_name) except AttributeError: # hide_address has no direct getter currentval = getattr(member.preferences, exp_name) self.assertEqual(currentval, exp_val, 'Preference %s was not imported' % exp_name)
def test_keep_default(self): # If the value was not changed from MM2.1's default, don't import it. default_msg_footer = ( '_______________________________________________\n' '%(real_name)s mailing list\n' '%(real_name)s@%(host_name)s\n' '%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s') for oldvar in ('msg_footer', 'digest_footer'): newvar = self._conf_mapping[oldvar] self._pckdict[str(oldvar)] = str(default_msg_footer) old_value = getattr(self._mlist, newvar) import_config_pck(self._mlist, self._pckdict) new_value = getattr(self._mlist, newvar) self.assertEqual(old_value, new_value, 'Default value was not preserved for %s' % newvar)
def _do_test(self, oldvalue, expected): self._pckdict['user_options']['*****@*****.**'] = oldvalue import_config_pck(self._mlist, self._pckdict) user = self._usermanager.get_user('*****@*****.**') self.assertIsNotNone(user, 'User was not imported') member = self._mlist.members.get_member('*****@*****.**') self.assertIsNotNone(member, 'Address was not subscribed') for exp_name, exp_val in expected.iteritems(): try: currentval = getattr(member, exp_name) except AttributeError: # hide_address has no direct getter currentval = getattr(member.preferences, exp_name) self.assertEqual( currentval, exp_val, 'Preference %s was not imported' % exp_name)
def test_keep_default(self): # If the value was not changed from MM2.1's default, don't import it. default_msg_footer = ( '_______________________________________________\n' '%(real_name)s mailing list\n' '%(real_name)s@%(host_name)s\n' '%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s' ) for oldvar in ('msg_footer', 'digest_footer'): newvar = self._conf_mapping[oldvar] self._pckdict[str(oldvar)] = str(default_msg_footer) old_value = getattr(self._mlist, newvar) import_config_pck(self._mlist, self._pckdict) new_value = getattr(self._mlist, newvar) self.assertEqual(old_value, new_value, 'Default value was not preserved for %s' % newvar)
def test_keep_default_if_fqdn_changed(self): # Use case: importing the old [email protected] into [email protected]. We can't check # if it changed from the default so don't import. We may do more harm # than good and it's easy to change if needed. test_value = b'TEST-VALUE' for oldvar, newvar in self._conf_mapping.items(): self._mlist.mail_host = 'example.com' self._pckdict['mail_host'] = b'test.example.com' self._pckdict[str(oldvar)] = test_value old_value = getattr(self._mlist, newvar) # Suppress warning messages in the test output. with mock.patch('sys.stderr'): import_config_pck(self._mlist, self._pckdict) new_value = getattr(self._mlist, newvar) self.assertEqual(old_value, new_value, 'Default value was not preserved for %s' % newvar)
def test_keep_default_if_fqdn_changed(self): # Use case: importing the old [email protected] into [email protected]. We can't check # if it changed from the default so don't import. We may do more harm # than good and it's easy to change if needed. test_value = b'TEST-VALUE' for oldvar, newvar in self._conf_mapping.iteritems(): self._mlist.mail_host = 'example.com' self._pckdict[b'mail_host'] = b'test.example.com' self._pckdict[str(oldvar)] = test_value old_value = getattr(self._mlist, newvar) # Suppress warning messages in the test output. with mock.patch('sys.stderr'): import_config_pck(self._mlist, self._pckdict) new_value = getattr(self._mlist, newvar) self.assertEqual(old_value, new_value, 'Default value was not preserved for %s' % newvar)
def process(self, args): """See `ICLISubCommand`.""" # Could be None or sequence of length 0. if args.listname is None: self.parser.error(_('List name is required')) return assert len( args.listname) == 1, ('Unexpected positional arguments: %s' % args.listname) fqdn_listname = args.listname[0] mlist = getUtility(IListManager).get(fqdn_listname) if mlist is None: self.parser.error(_('No such list: $fqdn_listname')) return if args.pickle_file is None: self.parser.error(_('config.pck file is required')) return assert len( args.pickle_file) == 1, ('Unexpected positional arguments: %s' % args.pickle_file) filename = args.pickle_file[0] 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, encoding='utf-8', errors='ignore') except EOFError: break except pickle.UnpicklingError: self.parser.error( _('Not a Mailman 2.1 configuration file: $filename')) return else: if not isinstance(config_dict, dict): print(_('Ignoring non-dictionary: {0!r}').format( config_dict), file=sys.stderr) continue try: import_config_pck(mlist, config_dict) except Import21Error as error: print(error, file=sys.stderr) sys.exit(1)
def test_delivery_status(self): # Look for the pckdict['delivery_status'] key which will look like # (status, time) where status is among the following: # ENABLED = 0 # enabled # UNKNOWN = 1 # legacy disabled # BYUSER = 2 # disabled by user choice # BYADMIN = 3 # disabled by admin choice # BYBOUNCE = 4 # disabled by bounces for oldval, expected in enumerate(( DeliveryStatus.enabled, DeliveryStatus.unknown, DeliveryStatus.by_user, DeliveryStatus.by_moderator, DeliveryStatus.by_bounces)): self._pckdict['delivery_status']['*****@*****.**'] = (oldval, 0) import_config_pck(self._mlist, self._pckdict) member = self._mlist.members.get_member('*****@*****.**') self.assertIsNotNone(member, 'Address was not subscribed') self.assertEqual(member.delivery_status, expected) member.unsubscribe()
def test_delivery_status(self): # Look for the pckdict['delivery_status'] key which will look like # (status, time) where status is among the following: # ENABLED = 0 # enabled # UNKNOWN = 1 # legacy disabled # BYUSER = 2 # disabled by user choice # BYADMIN = 3 # disabled by admin choice # BYBOUNCE = 4 # disabled by bounces for oldval, expected in enumerate( (DeliveryStatus.enabled, DeliveryStatus.unknown, DeliveryStatus.by_user, DeliveryStatus.by_moderator, DeliveryStatus.by_bounces)): self._pckdict['delivery_status']['*****@*****.**'] = (oldval, 0) import_config_pck(self._mlist, self._pckdict) member = self._mlist.members.get_member('*****@*****.**') self.assertIsNotNone(member, 'Address was not subscribed') self.assertEqual(member.delivery_status, expected) member.unsubscribe()
def test_substitutions(self): test_text = ('UNIT TESTING %(real_name)s mailing list\n' '%(real_name)s@%(host_name)s\n' '%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s') expected_text = ('UNIT TESTING $display_name mailing list\n' '$fqdn_listname\n' '$listinfo_uri') for oldvar, newvar in self._conf_mapping.items(): self._pckdict[str(oldvar)] = str(test_text) import_config_pck(self._mlist, self._pckdict) newattr = getattr(self._mlist, newvar) template_uri = expand(newattr, dict( listname=self._mlist.fqdn_listname, language=self._mlist.preferred_language.code, )) loader = getUtility(ITemplateLoader) text = loader.get(template_uri) self.assertEqual(text, expected_text, 'Old variables were not converted for %s' % newvar)
def process(self, args): """See `ICLISubCommand`.""" # Could be None or sequence of length 0. if args.listname is None: self.parser.error(_('List name is required')) return assert len(args.listname) == 1, ( 'Unexpected positional arguments: %s' % args.listname) fqdn_listname = args.listname[0] mlist = getUtility(IListManager).get(fqdn_listname) if mlist is None: self.parser.error(_('No such list: $fqdn_listname')) return if args.pickle_file is None: self.parser.error(_('config.pck file is required')) return assert len(args.pickle_file) == 1, ( 'Unexpected positional arguments: %s' % args.pickle_file) filename = args.pickle_file[0] 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, encoding='utf-8', errors='ignore') except EOFError: break except pickle.UnpicklingError: self.parser.error( _('Not a Mailman 2.1 configuration file: $filename')) return else: if not isinstance(config_dict, dict): print(_('Ignoring non-dictionary: {0!r}').format( config_dict), file=sys.stderr) continue try: import_config_pck(mlist, config_dict) except Import21Error as error: print(error, file=sys.stderr) sys.exit(1)
def test_nonmembers(self): import_config_pck(self._mlist, self._pckdict) expected = { 'gene': Action.accept, 'homer': Action.hold, 'iris': Action.reject, 'kenny': Action.discard, } for name, action in expected.items(): self.assertIn('{}@example.com'.format(name), [a.email for a in self._mlist.nonmembers.addresses], 'Address {} was not imported'.format(name)) member = self._mlist.nonmembers.get_member( '{}@example.com'.format(name)) self.assertEqual(member.moderation_action, action) # Only regexps should remain in the list property. list_prop = getattr(self._mlist, '{}_these_nonmembers'.format(action.name)) self.assertEqual(len(list_prop), 1) self.assertTrue(all(addr.startswith('^') for addr in list_prop))
def test_nonmembers(self): import_config_pck(self._mlist, self._pckdict) expected = { 'gene': Action.accept, 'homer': Action.hold, 'iris': Action.reject, 'kenny': Action.discard, } for name, action in expected.items(): self.assertIn('{}@example.com'.format(name), [a.email for a in self._mlist.nonmembers.addresses], 'Address {} was not imported'.format(name)) member = self._mlist.nonmembers.get_member( '{}@example.com'.format(name)) self.assertEqual(member.moderation_action, action) # Only regexps should remain in the list property. list_prop = getattr( self._mlist, '{}_these_nonmembers'.format(action.name)) self.assertEqual(len(list_prop), 1) self.assertTrue(all(addr.startswith('^') for addr in list_prop))
def test_substitutions(self): test_text = ('UNIT TESTING %(real_name)s mailing list\n' '%(real_name)s@%(host_name)s\n' '%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s') expected_text = ('UNIT TESTING $display_name mailing list\n' '$fqdn_listname\n' '$listinfo_uri') for oldvar, newvar in self._conf_mapping.items(): self._pckdict[str(oldvar)] = str(test_text) import_config_pck(self._mlist, self._pckdict) newattr = getattr(self._mlist, newvar) template_uri = expand( newattr, dict( listname=self._mlist.fqdn_listname, language=self._mlist.preferred_language.code, )) loader = getUtility(ITemplateLoader) text = loader.get(template_uri) self.assertEqual( text, expected_text, 'Old variables were not converted for %s' % newvar)
def _import(self): import_config_pck(self._mlist, self._pckdict)
def _do_test(self, pckdict, expected): import_config_pck(self._mlist, pckdict) self.assertEqual(self._mlist.archive_policy, expected)
def _do_test(self, original, expected): import_config_pck(self._mlist, dict(filter_action=original)) self.assertEqual(self._mlist.filter_action, expected)
def _do_test(self, expected): # Suppress warning messages in the test output. with mock.patch('sys.stderr'): import_config_pck(self._mlist, self._pckdict) for key, value in expected.iteritems(): self.assertEqual(getattr(self._mlist, key), value)
def test_original_email(self): import_config_pck(self._mlist, self._pckdict) bob = self._usermanager.get_address('*****@*****.**') self.assertEqual(bob.original_email, '*****@*****.**') dave = self._usermanager.get_address('*****@*****.**') self.assertEqual(dave.original_email, '*****@*****.**')