def test_from_header_from_address_not_a_user(self): context = MagicMock() context.acl_users.get_userByEmail.return_value = None ms = MessageSender(context, MagicMock()) with self.assertRaises(ValueError): ms.from_header_from_address('*****@*****.**')
def notify(self, userInfo, adminInfo): subject = 'Welcome to %s' % (self.groupInfo.name) text = self.textTemplate(userInfo=userInfo, adminInfo=adminInfo) html = self.htmlTemplate(userInfo=userInfo, adminInfo=adminInfo) ms = MessageSender(self.context, userInfo) ms.send_message(subject, text, html) self.request.response.setHeader(b'Content-Type', self.oldContentType)
def notify(self, adminInfo): s = 'New group: {0}'.format(self.groupInfo.name) text = self.textTemplate(adminInfo=adminInfo) html = self.htmlTemplate(adminInfo=adminInfo) ms = MessageSender(self.context, adminInfo) ms.send_message(s, text, html) self.request.response.setHeader(b'Content-Type', self.oldContentType)
def test_from_address(self): 'Test we get the From address when asked' ms = MessageSender(MagicMock(), MagicMock()) e = '*****@*****.**' r = ms.from_address(e) self.assertEqual(e, r)
def test_from_header_from_address_user(self, m_IGSUI): context = MagicMock() m_IGSUI().name = 'Example person' ms = MessageSender(context, MagicMock()) r = ms.from_header_from_address('*****@*****.**') self.assertEqual('Example person <*****@*****.**>', r)
def notify(self, userInfo, problemAddress, toAddresses): subject = 'Email delivery problems (action required)' text = self.textTemplate(userInfo=userInfo, email=problemAddress) html = self.htmlTemplate(userInfo=userInfo, email=problemAddress) ms = MessageSender(self.context, userInfo) ms.send_message(subject, text, html, toAddresses=toAddresses) self.request.response.setHeader(to_ascii('Content-Type'), self.oldContentType)
def test_from_address_none(self, m_sI): 'Test that we get the default From address' e = '*****@*****.**' m_sI().get_support_email.return_value = e ms = MessageSender(MagicMock(), MagicMock()) r = ms.from_address(None) self.assertEqual(e, r)
def test_from_header_from_address_none(self, m_sI): m_sI().name = 'Example' m_sI().get_support_email.return_value = '*****@*****.**' context = MagicMock() ms = MessageSender(context, MagicMock()) r = ms.from_header_from_address(None) self.assertEqual('Example Support <*****@*****.**>', r)
def test_to_addresses_none(self, m_eU): 'Test that we get the default To address' e = ['*****@*****.**', ] m_eU().get_delivery_addresses.return_value = e ms = MessageSender(MagicMock(), MagicMock()) r = ms.to_addresses(None) self.assertEqual(e, r)
def test_to_addresses(self): ms = MessageSender(MagicMock(), MagicMock()) e = [ '*****@*****.**', ] r = ms.to_addresses(e) self.assertEqual(e, r)
def test_to_header_from_addresses_single(self): 'Ensure we handle a single to-addresses correctly' addr = ['*****@*****.**', ] toUserInfo = MagicMock() toUserInfo.name = 'Example Person' ms = MessageSender(MagicMock(), toUserInfo) r = ms.to_header_from_addresses(addr) self.assertEqual('Example Person <*****@*****.**>', r)
def notify(self, siteInfo, userInfo, resetLink, toAddr): s = 'Password reset at {0} (action required)' subject = s.format(siteInfo.name) text = self.textTemplate(userInfo=userInfo, resetLink=resetLink) html = self.htmlTemplate(userInfo=userInfo, resetLink=resetLink) ms = MessageSender(self.context, userInfo) ms.send_message(subject, text, html, toAddresses=[toAddr]) self.request.response.setHeader(to_ascii('Content-Type'), self.oldContentType)
def test_to_header_from_addresses_none(self, m_t_a): 'Ensure we used the default address if we are given no address' m_t_a.return_value = ['*****@*****.**', ] toUserInfo = MagicMock() toUserInfo.name = 'Example Person' ms = MessageSender(MagicMock(), toUserInfo) r = ms.to_header_from_addresses(None) self.assertEqual('Example Person <*****@*****.**>', r)
def test_to_header_from_addresses_none_multiple(self, m_t_a): 'Ensure we handle multiple to-addresses correctly if they are the default' m_t_a.return_value = ['*****@*****.**', '*****@*****.**', ] toUserInfo = MagicMock() toUserInfo.name = 'Example Person' ms = MessageSender(MagicMock(), toUserInfo) r = ms.to_header_from_addresses(None) expected = 'Example Person <*****@*****.**>, Example Person <*****@*****.**>' self.assertEqual(expected, r)
def test_to_header_from_addresses_multiple(self): 'Ensure we handle multiple to-addresses correctly' addr = ['*****@*****.**', '*****@*****.**', ] toUserInfo = MagicMock() toUserInfo.name = 'Example Person' ms = MessageSender(MagicMock(), toUserInfo) r = ms.to_header_from_addresses(addr) expected = 'Example Person <*****@*****.**>, Example Person <*****@*****.**>' self.assertEqual(expected, r)
def test_to_addresses_none(self, m_eU): 'Test that we get the default To address' e = [ '*****@*****.**', ] m_eU().get_delivery_addresses.return_value = e ms = MessageSender(MagicMock(), MagicMock()) r = ms.to_addresses(None) self.assertEqual(e, r)
def notify(self, userInfo, emailAddress, verifyLink): subject = 'Verify your email address (action required)'.encode(UTF8) text = self.textTemplate(userInfo=userInfo, emailAddress=emailAddress, verifyLink=verifyLink) html = self.htmlTemplate(userInfo=userInfo, emailAddress=emailAddress, verifyLink=verifyLink) ms = MessageSender(self.context, userInfo) ms.send_message(subject, text, html, toAddresses=[emailAddress]) self.request.response.setHeader(b'Content-Type', self.oldContentType)
def test_to_header_from_addresses_none(self, m_t_a): 'Ensure we used the default address if we are given no address' m_t_a.return_value = [ '*****@*****.**', ] toUserInfo = MagicMock() toUserInfo.name = 'Example Person' ms = MessageSender(MagicMock(), toUserInfo) r = ms.to_header_from_addresses(None) self.assertEqual('Example Person <*****@*****.**>', r)
def test_to_header_from_addresses_single(self): 'Ensure we handle a single to-addresses correctly' addr = [ '*****@*****.**', ] toUserInfo = MagicMock() toUserInfo.name = 'Example Person' ms = MessageSender(MagicMock(), toUserInfo) r = ms.to_header_from_addresses(addr) self.assertEqual('Example Person <*****@*****.**>', r)
def test_send_message(self, m_NU, m_A, m_IGUI, m_eU, m_sI): m_sI().name = 'Example' m_sI().get_support_email.return_value = '*****@*****.**' m_eU().get_delivery_addresses.return_value = ['*****@*****.**', ] toUserInfo = MagicMock() toUserInfo.name = m_IGUI().name = 'Example Person' ms = MessageSender(MagicMock(), toUserInfo) ms.send_message('Ethel the Frog', 'Tonight on Ethel the Frog we look at violence\u2026', '<p>Tonight on Ethel the Frog we look at violence…</p>', '*****@*****.**', ['*****@*****.**', '*****@*****.**', ]) self.assertEqual(2, m_NU().send_message.call_count)
def test_to_header_from_addresses_multiple(self): 'Ensure we handle multiple to-addresses correctly' addr = [ '*****@*****.**', '*****@*****.**', ] toUserInfo = MagicMock() toUserInfo.name = 'Example Person' ms = MessageSender(MagicMock(), toUserInfo) r = ms.to_header_from_addresses(addr) expected = 'Example Person <*****@*****.**>, Example Person <*****@*****.**>' self.assertEqual(expected, r)
def test_to_header_from_addresses_none_multiple(self, m_t_a): 'Ensure we handle multiple to-addresses correctly if they are the default' m_t_a.return_value = [ '*****@*****.**', '*****@*****.**', ] toUserInfo = MagicMock() toUserInfo.name = 'Example Person' ms = MessageSender(MagicMock(), toUserInfo) r = ms.to_header_from_addresses(None) expected = 'Example Person <*****@*****.**>, Example Person <*****@*****.**>' self.assertEqual(expected, r)
def notify(self, adminInfo, userInfo, groupInfo): s = 'Invitation to {0} accepted' subject = s.format(groupInfo.name).encode(UTF8) text = self.textTemplate(userInfo=userInfo, adminInfo=adminInfo, groupInfo=groupInfo) html = self.htmlTemplate(userInfo=userInfo, adminInfo=adminInfo, groupInfo=groupInfo) ms = MessageSender(self.context, adminInfo) ms.send_message(subject, text, html) self.request.response.setHeader(to_ascii('Content-Type'), self.oldContentType)
def test_create_message_no_html(self, m_A, m_IGUI, m_eU, m_sI): m_sI().name = 'Example' m_sI().get_support_email.return_value = '*****@*****.**' m_eU().get_delivery_addresses.return_value = ['*****@*****.**', ] toUserInfo = MagicMock() toUserInfo.name = 'Example Person' ms = MessageSender(MagicMock(), toUserInfo) r = ms.create_message('Ethel the Frog', 'Tonight on Ethel the Frog we look at violence\u2026', None, None, None) self.assertNotIn('html', r) self.assertIn('text/plain', r) self.assertIn('Subject: Ethel the Frog', r) self.assertIn('To: Example Person <*****@*****.**>', r)
def test_set_headers(self, m_to, m_from): m_to.return_value = '*****@*****.**' m_from.return_value = '*****@*****.**' container = MIMEMultipart('alternative') ms = MessageSender(MagicMock(), MagicMock()) subj = 'Ethel the Frog' ms.set_headers(container, subj, 'mockFrom', 'mockTo') self.assertEqual(subj, container['Subject']) m_from.assert_called_once_with('mockFrom') self.assertEqual(m_from(), container['From']) m_to.assert_called_once_with('mockTo') self.assertEqual(m_to(), container['To']) # Only test the presence of a date, because race-conditions with dates self.assertIn('Date', container)
def test_create_message_no_html(self, m_A, m_IGUI, m_eU, m_sI): m_sI().name = 'Example' m_sI().get_support_email.return_value = '*****@*****.**' m_eU().get_delivery_addresses.return_value = [ '*****@*****.**', ] toUserInfo = MagicMock() toUserInfo.name = 'Example Person' ms = MessageSender(MagicMock(), toUserInfo) r = ms.create_message( 'Ethel the Frog', 'Tonight on Ethel the Frog we look at violence\u2026', None, None, None) self.assertNotIn('html', r) self.assertIn('text/plain', r) self.assertIn('Subject: Ethel the Frog', r) self.assertIn('To: Example Person <*****@*****.**>', r)
def test_send_message(self, m_NU, m_A, m_IGUI, m_eU, m_sI): m_sI().name = 'Example' m_sI().get_support_email.return_value = '*****@*****.**' m_eU().get_delivery_addresses.return_value = [ '*****@*****.**', ] toUserInfo = MagicMock() toUserInfo.name = m_IGUI().name = 'Example Person' ms = MessageSender(MagicMock(), toUserInfo) ms.send_message( 'Ethel the Frog', 'Tonight on Ethel the Frog we look at violence\u2026', '<p>Tonight on Ethel the Frog we look at violence…</p>', '*****@*****.**', [ '*****@*****.**', '*****@*****.**', ]) self.assertEqual(2, m_NU().send_message.call_count)
def send_message(self, fromAddress, adminInfo, message): sender = MessageSender(self.context, adminInfo) subject = _('request-admin-message-subject', 'Request to join ${groupName}', mapping={'groupName': self.groupInfo.name}) newRequest = self.request newRequest.form['userId'] = self.userInfo.id newRequest.form['email'] = fromAddress newRequest.form['mesg'] = message newRequest.form['adminId'] = adminInfo.id txt = getMultiAdapter((self.context, newRequest), name="request_message.txt")() html = getMultiAdapter((self.context, newRequest), name="request_message.html")() try: sender.send_message(subject, txt, html, fromAddress) except ValueError: # No valid email address m = 'Cannot send a "Request to join" notification to the administrator %s (%s) for '\ 'the group %s (%s) on %s (%s) because they lack a verified email address. '\ 'Skipping the notification.' log.warn(m, adminInfo.name, adminInfo.id, self.groupInfo.name, self.groupInfo.id, self.siteInfo.name, self.siteInfo.id) self.request.response.setHeader(b'Content-Type', self.oldContentType)
def test_to_addresses(self): ms = MessageSender(MagicMock(), MagicMock()) e = ['*****@*****.**', ] r = ms.to_addresses(e) self.assertEqual(e, r)
def test_to_header_from_addresses_issues(self, m_t_a): 'Test we raise a ValueError if we lack and address to send to' m_t_a.return_value = None ms = MessageSender(MagicMock(), MagicMock()) with self.assertRaises(ValueError): ms.to_header_from_addresses(None)
def test_get_addr_line_no_name(self): ms = MessageSender(MagicMock(), MagicMock()) r = ms.get_addr_line('', '*****@*****.**') self.assertEqual('*****@*****.**', r)
def test_get_addr_line_unicode(self): ms = MessageSender(MagicMock(), MagicMock()) r = ms.get_addr_line('Example support\u203d', '*****@*****.**') self.assertEqual('=?utf-8?q?Example_support=E2=80=BD?= <*****@*****.**>', r)
def test_get_addr_line_unicode(self): ms = MessageSender(MagicMock(), MagicMock()) r = ms.get_addr_line('Example support\u203d', '*****@*****.**') self.assertEqual( '=?utf-8?q?Example_support=E2=80=BD?= <*****@*****.**>', r)