summaryrefslogtreecommitdiff
path: root/src/mailman/testing/helpers.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/mailman/testing/helpers.py')
-rw-r--r--src/mailman/testing/helpers.py33
1 files changed, 26 insertions, 7 deletions
diff --git a/src/mailman/testing/helpers.py b/src/mailman/testing/helpers.py
index a869c8d55..476211a08 100644
--- a/src/mailman/testing/helpers.py
+++ b/src/mailman/testing/helpers.py
@@ -33,6 +33,7 @@ __all__ = [
'reset_the_world',
'specialized_message_from_string',
'subscribe',
+ 'subscribe_ex',
'temporary_db',
'wait_for_webservice',
]
@@ -435,10 +436,11 @@ class chdir:
-def subscribe(mlist, first_name, role=MemberRole.member):
+def subscribe(mlist, first_name, role=MemberRole.member, email=None):
"""Helper for subscribing a sample person to a mailing list."""
user_manager = getUtility(IUserManager)
- email = '{0}person@example.com'.format(first_name[0].lower())
+ email = ('{0}person@example.com'.format(first_name[0].lower())
+ if email is None else email)
full_name = '{0} Person'.format(first_name)
with transaction():
person = user_manager.get_user(email)
@@ -446,13 +448,30 @@ def subscribe(mlist, first_name, role=MemberRole.member):
address = user_manager.get_address(email)
if address is None:
person = user_manager.create_user(email, full_name)
- preferred_address = list(person.addresses)[0]
- mlist.subscribe(preferred_address, role)
+ subscription_address = list(person.addresses)[0]
else:
- mlist.subscribe(address, role)
+ subscription_address = address
else:
- preferred_address = list(person.addresses)[0]
- mlist.subscribe(preferred_address, role)
+ subscription_address = list(person.addresses)[0]
+ # We can't return the newly created member because that will
+ # implicitly open a new transaction, which can break doctests. If you
+ # really need the newly created member, look it up.
+ mlist.subscribe(subscription_address, role)
+
+
+def subscribe_ex(mlist, first_name, role=MemberRole.member, email=None):
+ """Like ``subscribe()`` but returns the newly created member object.
+
+ Only use this in contexts where you can accept the opening of an implicit
+ transaction (i.e. *not* in REST tests) unless you explicitly close said
+ transaction. Otherwise you will lock the database.
+ """
+ # Blarg. I wish we didn't have to duplicate this logic.
+ email = ('{0}person@example.com'.format(first_name[0].lower())
+ if email is None else email)
+ subscribe(mlist, first_name, role, email)
+ roster = mlist.get_roster(role)
+ return roster.get_member(email)