Skip to content

Commit

Permalink
SparkPost cc and bcc improvements + test cases (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
caronc authored Oct 6, 2020
1 parent 89eaffa commit b8105c3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
26 changes: 14 additions & 12 deletions apprise/plugins/NotifySparkPost.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class NotifySparkPost(NotifyBase):
# batch transfer based on:
# https://www.sparkpost.com/docs/tech-resources/\
# smtp-rest-api-performance/#sending-via-the-transmission-rest-api
default_batch_size = 10000
default_batch_size = 2000

# A URL that takes you to the setup/help of the specific protocol
setup_url = 'https://github.com/caronc/apprise/wiki/Notify_sparkpost'
Expand Down Expand Up @@ -616,10 +616,10 @@ def send(self, body, title='', notify_type=NotifyType.INFO, attach=None,
}

# Strip target out of cc list if in To
cc = (cc - set([addr]))
cc = (cc - set([addr[1]]))

# Strip target out of bcc list if in To
bcc = (bcc - set([addr]))
bcc = (bcc - set([addr[1]]))

if addr[0]:
entry['address']['name'] = addr[0]
Expand All @@ -633,7 +633,10 @@ def send(self, body, title='', notify_type=NotifyType.INFO, attach=None,
entry = {
'address': {
'email': addr,
}
'header_to':
# Take the first email in the To
self.targets[index:index + batch_size][0][1],
},
}

if self.names.get(addr):
Expand All @@ -644,7 +647,7 @@ def send(self, body, title='', notify_type=NotifyType.INFO, attach=None,

headers['CC'] = ','.join(cc)

# Handle our bcc List
# Handle our bcc
for addr in bcc:
# Add our recipient to our list
payload['recipients'].append({
Expand Down Expand Up @@ -693,19 +696,16 @@ def url(self, privacy=False, *args, **kwargs):
# from_name specified; pass it back on the url
params['name'] = self.from_name

if len(self.cc) > 0:
if self.cc:
# Handle our Carbon Copy Addresses
params['cc'] = ','.join(
['{}{}'.format(
'' if not e not in self.names
else '{}:'.format(self.names[e]), e) for e in self.cc])

if len(self.bcc) > 0:
if self.bcc:
# Handle our Blind Carbon Copy Addresses
params['bcc'] = ','.join(
['{}{}'.format(
'' if not e not in self.names
else '{}:'.format(self.names[e]), e) for e in self.bcc])
params['bcc'] = ','.join(self.bcc)

# a simple boolean check as to whether we display our target emails
# or not
Expand Down Expand Up @@ -781,5 +781,7 @@ def parse_url(url):

# Get Batch Mode Flag
results['batch'] = \
parse_bool(results['qsd'].get('batch', False))
parse_bool(results['qsd'].get(
'batch', NotifySparkPost.template_args['batch']['default']))

return results
29 changes: 27 additions & 2 deletions test/test_sparkpost.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
@mock.patch('requests.post')
def test_notify_sparkpost_plugin_throttling(mock_post):
"""
API: NotifySpark() Throttling
API: NotifySparkPost() Throttling
"""

Expand Down Expand Up @@ -115,7 +115,7 @@ def test_notify_sparkpost_plugin_throttling(mock_post):
@mock.patch('requests.post')
def test_notify_sparkpost_plugin_attachments(mock_post):
"""
API: NotifySpark() Attachments
API: NotifySparkPost() Attachments
"""
# Disable Throttling to speed testing
Expand Down Expand Up @@ -161,3 +161,28 @@ def test_notify_sparkpost_plugin_attachments(mock_post):
assert obj.notify(
body='body', title='title', notify_type=NotifyType.INFO,
attach=attach) is False

obj = Apprise.instantiate(
'sparkpost://[email protected]/{}/'
'[email protected]/[email protected]?batch=yes'.format(apikey))
assert isinstance(obj, plugins.NotifySparkPost)

# Force our batch to break into separate messages
obj.default_batch_size = 1
# We'll send 2 messages
mock_post.reset_mock()

assert obj.notify(
body='body', title='title', notify_type=NotifyType.INFO,
attach=attach) is True
assert mock_post.call_count == 2

# single batch
mock_post.reset_mock()
# We'll send 1 message
obj.default_batch_size = 2

assert obj.notify(
body='body', title='title', notify_type=NotifyType.INFO,
attach=attach) is True
assert mock_post.call_count == 1

0 comments on commit b8105c3

Please sign in to comment.