def test_subscribe_moderate_undetected(self): # The list requires moderation but we failed to detect it in the # possible subscription policies. If the subscription requires a # confirmation, Mailman will reply with a 202 code, and mailman.client # will return the response content (a dict) instead of a Member # instance. Make sure we can handle that. cache.set("User:%s:subscriptions" % self.user.id, "test-value", version=2) self.ml.settings["subscription_policy"] = "open" self.ml.get_member.side_effect = ValueError response_dict = { 'token_owner': 'subscriber', 'http_etag': '"deadbeef"', 'token': 'deadbeefdeadbeef' } self.ml.subscribe.side_effect = lambda *a, **kw: response_dict try: mailman.subscribe("*****@*****.**", self.user) except AttributeError: self.fail("This use case was not properly handled") self.assertTrue(self.ml.get_member.called) # There must be no exception even if the response is not a Member. # Cache was not cleared because the subscription was not done self.assertEqual( cache.get("User:%s:subscriptions" % self.user.id, version=2), "test-value")
def post_to_list(request, mlist, subject, message, headers={}, attachments=None): if not mlist: # Make sure the list exists to avoid posting to any email addess raise SuspiciousOperation("I don't know this mailing-list") # Check that the user is subscribed try: mailman.subscribe(mlist.name, request.user) except MailmanConnectionError: raise PostingFailed("Can't connect to Mailman's REST server, " "your message has not been sent.") # send the message headers["User-Agent"] = "HyperKitty on %s" % request.build_absolute_uri("/") if not request.user.first_name and not request.user.last_name: from_email = request.user.email else: from_email = '"%s %s" <%s>' % (request.user.first_name, request.user.last_name, request.user.email) msg = EmailMessage( subject=subject, body=message, from_email=from_email, to=[mlist.name], headers=headers, ) # Attachments if attachments: if not isinstance(attachments, list): attachments = [attachments] for attach in attachments: msg.attach(attach.name, attach.read()) msg.send()
def test_subscribe_moderate(self): self.ml.settings["subscription_policy"] = "moderate" mailman.subscribe("*****@*****.**", self.user) self.assertFalse(self.ml.get_member.called) self.assertFalse(self.ml.subscribe.called) self.ml.settings["subscription_policy"] = "confirm_then_moderate" mailman.subscribe("*****@*****.**", self.user) self.assertFalse(self.ml.get_member.called) self.assertFalse(self.ml.subscribe.called)
def test_subscribe_different_address(self): self.ml.settings["subscription_policy"] = "open" self.ml.get_member.side_effect = ValueError self.ml.subscribe.side_effect = lambda *a, **kw: {} mailman.subscribe("*****@*****.**", self.user, "*****@*****.**", "Other Display Name") self.assertTrue(self.ml.get_member.called) self.ml.subscribe.assert_called_with('*****@*****.**', "Other Display Name", pre_verified=True, pre_confirmed=True)
def test_subscribe_already_subscribed_moderated(self): # Subscribing to a moderated list a user is already subscribed to # should just do nothing self.ml.settings["subscription_policy"] = "moderate" try: mailman.subscribe("*****@*****.**", self.user) except mailman.ModeratedListException: self.fail("A ModeratedListException was raised") self.assertFalse(self.ml.subscribe.called) self.ml.settings["subscription_policy"] = "confirm_then_moderate" try: mailman.subscribe("*****@*****.**", self.user) except mailman.ModeratedListException: self.fail("A ModeratedListException was raised") self.assertFalse(self.ml.subscribe.called)
def test_subscribe_moderate_undetected(self): # The list requires moderation but we failed to detect it in the # possible subscription policies. If the subscription requires a # confirmation, Mailman will reply with a 202 code, and mailman.client # will return the response content (a dict) instead of a Member # instance. Make sure we can handle that. self.ml.settings["subscription_policy"] = "open" self.ml.get_member.side_effect = ValueError response_dict = {'token_owner': 'subscriber', 'http_etag': '"deadbeef"', 'token': 'deadbeefdeadbeef'} self.ml.subscribe.side_effect = lambda *a, **kw: response_dict try: mailman.subscribe("*****@*****.**", self.user) except AttributeError: self.fail("This use case was not properly handled") self.assertTrue(self.ml.get_member.called)
def post_to_list(request, mlist, subject, message, headers=None, attachments=None): if not mlist: # Make sure the list exists to avoid posting to any email addess raise SuspiciousOperation("I don't know this mailing-list") if headers is None: headers = {} # Check that the user is subscribed try: subscribed_now = mailman.subscribe(mlist.name, request.user) except MailmanConnectionError: raise PostingFailed("Can't connect to Mailman's REST server, " "your message has not been sent.") # send the message headers["User-Agent"] = "HyperKitty on %s" % request.build_absolute_uri("/") from_email = get_sender(request, mlist) msg = EmailMessage( subject=subject, body=message, from_email=from_email, to=[mlist.name], headers=headers, ) # Attachments if attachments: if not isinstance(attachments, list): attachments = [attachments] for attach in attachments: msg.attach(attach.name, attach.read()) # XXX: Inject into the incoming queue instead? msg.send() return subscribed_now
def test_subscribe_not_subscribed(self): self.ml.settings["subscription_policy"] = "open" self.ml.get_member.side_effect = ValueError cache.set("User:%s:subscriptions" % self.user.id, "test-value") class Prefs(dict): save = Mock() member = Mock() member.preferences = Prefs() self.ml.subscribe.side_effect = lambda *a: member mailman.subscribe("*****@*****.**", self.user) self.assertTrue(self.ml.get_member.called) self.assertTrue(self.ml.subscribe.called) self.assertEqual(member.preferences["delivery_status"], "by_user") self.assertTrue(member.preferences.save.called) self.assertEqual(cache.get("User:%s:subscriptions" % self.user.id), None)
def test_subscribe_different_address(self): self.ml.settings["subscription_policy"] = "open" self.ml.get_member.side_effect = ValueError #class Prefs(dict): # save = Mock() #member = Mock() #member.preferences = Prefs() #self.ml.subscribe.side_effect = lambda *a, **kw: member self.ml.subscribe.side_effect = lambda *a, **kw: {} mailman.subscribe( "*****@*****.**", self.user, "*****@*****.**", "Other Display Name") self.assertTrue(self.ml.get_member.called) self.ml.subscribe.assert_called_with( '*****@*****.**', "Other Display Name", pre_verified=True, pre_confirmed=True)
def post_to_list(request, mlist, subject, message, headers=None, attachments=None): if not mlist: # Make sure the list exists to avoid posting to any email addess raise SuspiciousOperation("I don't know this mailing-list") if headers is None: headers = {} sender = headers.pop("From", get_sender(request, mlist)) display_name = "%s %s" % (request.user.first_name, request.user.last_name) if display_name.strip(): from_email = '"%s" <%s>' % (display_name, sender) else: from_email = sender # Unwrap and collapse spaces subject = re.sub(r'\n+', ' ', subject) subject = re.sub(r'\s+', ' ', subject) # Check that the user is subscribed try: subscribed_now = mailman.subscribe(mlist.list_id, request.user, sender, display_name) except MailmanConnectionError: raise PostingFailed("Can't connect to Mailman's REST server, " "your message has not been sent.") # send the message headers["User-Agent"] = ("HyperKitty on %s" % request.build_absolute_uri("/")) msg = EmailMessage( subject=subject, body=message, from_email=from_email, to=[mlist.name], headers=headers, ) # Attachments if attachments: if not isinstance(attachments, list): attachments = [attachments] for attach in attachments: msg.attach(attach.name, attach.read()) # XXX: Inject into the incoming queue instead? if not settings.DEBUG: # Don't send mail in debug mode, just in case... try: msg.send() except SMTPResponseException as e: raise PostingFailed('Message not sent: SMTP error {} {}'.format( e.smtp_code, e.smtp_error)) return subscribed_now
def test_subscribe_not_subscribed(self): self.ml.settings["subscription_policy"] = "open" self.ml.get_member.side_effect = ValueError cache.set("User:%s:subscriptions" % self.user.id, "test-value") class Prefs(dict): save = Mock() member = Mock() member.preferences = Prefs() self.ml.subscribe.side_effect = lambda *a, **kw: member mailman.subscribe("*****@*****.**", self.user) self.assertTrue(self.ml.get_member.called) self.ml.subscribe.assert_called_with('*****@*****.**', ' ', pre_verified=True, pre_confirmed=True) self.assertEqual(member.preferences["delivery_status"], "by_user") self.assertTrue(member.preferences.save.called) self.assertEqual(cache.get("User:%s:subscriptions" % self.user.id), None)
def post_to_list(request, mlist, subject, message, headers={}): if not mlist: # Make sure the list exists to avoid posting to any email addess raise SuspiciousOperation("I don't know this mailing-list") # Check that the user is subscribed try: mailman.subscribe(mlist.name, request.user) except mailman.MailmanConnectionError: raise PostingFailed("Can't connect to Mailman's REST server, " "your message has not been sent.") # send the message headers["User-Agent"] = "HyperKitty on %s" % request.build_absolute_uri("/") msg = EmailMessage( subject=subject, body=message, from_email='"%s %s" <%s>' % (request.user.first_name, request.user.last_name, request.user.email), to=[mlist.name], headers=headers, ) msg.send()
def post_to_list(request, mlist, subject, message, headers=None, attachments=None): if not mlist: # Make sure the list exists to avoid posting to any email addess raise SuspiciousOperation("I don't know this mailing-list") if headers is None: headers = {} sender = headers.pop("From", get_sender(request, mlist)) display_name = "%s %s" % (request.user.first_name, request.user.last_name) if display_name.strip(): from_email = '"%s" <%s>' % (display_name, sender) else: from_email = sender # Unwrap and collapse spaces subject = re.sub(r'\n+', ' ', subject) subject = re.sub(r'\s+', ' ', subject) # Check that the user is subscribed try: subscribed_now = mailman.subscribe( mlist.name, request.user, sender, display_name) except MailmanConnectionError: raise PostingFailed("Can't connect to Mailman's REST server, " "your message has not been sent.") # send the message headers["User-Agent"] = "HyperKitty on %s" % request.build_absolute_uri("/") msg = EmailMessage( subject=subject, body=message, from_email=from_email, to=[mlist.name], headers=headers, ) # Attachments if attachments: if not isinstance(attachments, list): attachments = [attachments] for attach in attachments: msg.attach(attach.name, attach.read()) # XXX: Inject into the incoming queue instead? if not settings.DEBUG: msg.send() # Don't send mail in debug mode, just in case... return subscribed_now
def test_subscribe_moderate_then_open(self): # The list required moderation, the user tried to subscribe, and then # the list was set to open before the subscription could be approved. # There is therefore a pending subscription on an open list, make sure # we can handle that. # https://gitlab.com/mailman/hyperkitty/issues/152 self.ml.settings["subscription_policy"] = "open" self.ml.get_member.side_effect = ValueError self.ml.subscribe.side_effect = HTTPError( url=None, code=409, msg="Subscription request already pending", hdrs=None, fp=None) try: subscribed_now = mailman.subscribe("*****@*****.**", self.user) except HTTPError: self.fail("This use case was not properly handled") self.assertTrue(self.ml.get_member.called) self.assertFalse(subscribed_now)
def post_to_list(request, mlist, subject, message, headers=None, attachments=None): if not mlist: # Make sure the list exists to avoid posting to any email addess raise SuspiciousOperation("I don't know this mailing-list") if headers is None: headers = {} # Check that the user is subscribed try: subscribed_now = mailman.subscribe(mlist.name, request.user) except MailmanConnectionError: raise PostingFailed("Can't connect to Mailman's REST server, " "your message has not been sent.") # send the message headers["User-Agent"] = "HyperKitty on %s" % request.build_absolute_uri( "/") from_email = get_sender(request, mlist) msg = EmailMessage( subject=subject, body=message, from_email=from_email, to=[mlist.name], headers=headers, ) # Attachments if attachments: if not isinstance(attachments, list): attachments = [attachments] for attach in attachments: msg.attach(attach.name, attach.read()) # XXX: Inject into the incoming queue instead? msg.send() return subscribed_now
def test_subscribe_already_subscribed(self): self.ml.settings["subscription_policy"] = "open" mailman.subscribe("*****@*****.**", self.user) self.assertTrue(self.ml.get_member.called) self.assertFalse(self.ml.subscribe.called)