def test_absorb_all_attributes(self): # Test that all attributes in the IPreferences interface are properly # absorbed, and that none are missed. attributes = [] for name in IPreferences.names(): attribute = IPreferences.getDescriptionFor(name) if (not isinstance(attribute, Method) and isinstance(attribute, Attribute)): # noqa: W503 attributes.append(name) values = { 'acknowledge_posts': True, 'hide_address': True, 'preferred_language': getUtility(ILanguageManager)['fr'], 'receive_list_copy': True, 'receive_own_postings': True, 'delivery_mode': DeliveryMode.mime_digests, 'delivery_status': DeliveryStatus.by_user, } # If this fails, the IPreferences interface has been mutated. Be sure # to update this test! self.assertEqual(sorted(attributes), sorted(values)) preferences_1 = Preferences() preferences_2 = Preferences() for name, value in values.items(): setattr(preferences_1, name, value) preferences_2.absorb(preferences_1) for name, value in values.items(): self.assertEqual(getattr(preferences_2, name), value)
def test_absorb_overwrite(self): # Only overwrite the preference if it is unset in the absorber. preferences_1 = Preferences() preferences_2 = Preferences() preferences_1.acknowledge_posts = False preferences_2.acknowledge_posts = True preferences_1.hide_address = True preferences_2.receive_list_copy = True # Ensure that our preconditions are met. self.assertIsNotNone(preferences_1.acknowledge_posts) self.assertIsNotNone(preferences_2.acknowledge_posts) self.assertIsNotNone(preferences_1.hide_address) self.assertIsNone(preferences_2.hide_address) self.assertIsNone(preferences_1.receive_list_copy) self.assertIsNotNone(preferences_2.receive_list_copy) # Do the absorb. preferences_1.absorb(preferences_2) # This attribute is set in both preferences, so it wasn't absorbed. self.assertFalse(preferences_1.acknowledge_posts) # This attribute is only set in the first preferences so it also # wasn't absorbed. self.assertTrue(preferences_1.hide_address) # This attribute is only set in the second preferences, so it was # absorbed. self.assertTrue(preferences_1.receive_list_copy)
def test_absorb_all_attributes(self): # Test that all attributes in the IPreferences interface are properly # absorbed, and that none are missed. attributes = [] for name in IPreferences.names(): attribute = IPreferences.getDescriptionFor(name) if (not isinstance(attribute, Method) and isinstance(attribute, Attribute)): # noqa attributes.append(name) values = { 'acknowledge_posts': True, 'hide_address': True, 'preferred_language': getUtility(ILanguageManager)['fr'], 'receive_list_copy': True, 'receive_own_postings': True, 'delivery_mode': DeliveryMode.mime_digests, 'delivery_status': DeliveryStatus.by_user, } # If this fails, the IPreferences interface has been mutated. Be sure # to update this test! self.assertEqual(sorted(attributes), sorted(values)) preferences_1 = Preferences() preferences_2 = Preferences() for name, value in values.items(): setattr(preferences_1, name, value) preferences_2.absorb(preferences_1) for name, value in values.items(): self.assertEqual(getattr(preferences_2, name), value)
def subscribe(self, store, subscriber, role=MemberRole.member): """See `IMailingList`.""" if IAddress.providedBy(subscriber): member = store.query(Member).filter( Member.role == role, Member.list_id == self._list_id, Member._address == subscriber).first() if member: raise AlreadySubscribedError(self.fqdn_listname, subscriber.email, role) elif IUser.providedBy(subscriber): if subscriber.preferred_address is None: raise MissingPreferredAddressError(subscriber) member = store.query(Member).filter( Member.role == role, Member.list_id == self._list_id, Member._user == subscriber).first() if member: raise AlreadySubscribedError( self.fqdn_listname, subscriber.preferred_address.email, role) else: raise ValueError('subscriber must be an address or user') member = Member(role=role, list_id=self._list_id, subscriber=subscriber) member.preferences = Preferences() store.add(member) notify(SubscriptionEvent(self, member)) return member
def create_user(self, email=None, display_name=None): """See `IUserManager`.""" if email: address = self.create_address(email, display_name) user = User(display_name, Preferences()) if email: user.link(address) return user
def subscribe(self, store, subscriber, role=MemberRole.member): """See `IMailingList`.""" member, email = self._get_subscriber(store, subscriber, role) if member is not None: raise AlreadySubscribedError(self.fqdn_listname, email, role) member = Member(role=role, list_id=self._list_id, subscriber=subscriber) member.preferences = Preferences() store.add(member) notify(SubscriptionEvent(self, member)) return member
def register(self, store, email, display_name=None): """See `IUser`.""" # First, see if the address already exists address = store.query(Address).filter_by(email=email).first() if address is None: if display_name is None: display_name = '' address = Address(email=email, display_name=display_name) address.preferences = Preferences() # Link the address to the user if it is not already linked. if address.user is not None: raise AddressAlreadyLinkedError(address) address.user = self return address
def create_address(self, store, email, display_name=None): """See `IUserManager`.""" addresses = store.query(Address).filter(Address.email == email.lower()) if addresses.count() == 1: found = addresses[0] raise ExistingAddressError(found.original_email) assert addresses.count() == 0, 'Unexpected results' if display_name is None: display_name = '' # It's okay not to lower case the 'email' argument because the # constructor will do the right thing. address = Address(email, display_name) address.preferences = Preferences() store.add(address) return address
def test_decorate_user_name_or_address_as_address(self): site_dir = os.path.join(config.TEMPLATE_DIR, 'site', 'en') os.makedirs(site_dir) footer_path = os.path.join(site_dir, 'myfooter.txt') with open(footer_path, 'w', encoding='utf-8') as fp: print('$user_name_or_address', file=fp) getUtility(ITemplateManager).set('list:member:regular:footer', None, 'mailman:///myfooter.txt') self._mlist.preferred_language = 'en' user = getUtility(IUserManager).make_user('*****@*****.**') member = Member(MemberRole.member, self._mlist.list_id, user) member.preferences = Preferences() member.preferences.preferred_language = 'en' msgdata = dict(member=member) decorate.process(self._mlist, self._msg, msgdata) self.assertIn('*****@*****.**', self._msg.as_string())
def subscribe(self, store, subscriber, role=MemberRole.member, send_welcome_message=None): """See `IMailingList`.""" member, email = self._get_subscriber(store, subscriber, role) test_email = email or subscriber.lower() # Allow list posting address only for nonmember role. if (test_email == self.posting_address and role != MemberRole.nonmember): raise InvalidEmailAddressError('List posting address not allowed') if member is not None: raise AlreadySubscribedError(self.fqdn_listname, email, role) if IBanManager(self).is_banned(test_email): raise MembershipIsBannedError(self, test_email) member = Member(role=role, list_id=self._list_id, subscriber=subscriber) member.preferences = Preferences() store.add(member) notify(SubscriptionEvent( self, member, send_welcome_message=send_welcome_message)) return member
def test_type_error(self): preferences = Preferences() self.assertRaises(TypeError, preferences.absorb, None)