Skip to content

Commit

Permalink
adds test cases for new functionality
Browse files Browse the repository at this point in the history
adds changes from PR
  • Loading branch information
AlexScotland committed Dec 28, 2023
1 parent 0dcc497 commit 86d7b04
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
4 changes: 1 addition & 3 deletions apprise/plugins/NotifySlack.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@
# Used to break path apart into list of channels
CHANNEL_LIST_DELIM = re.compile(r'[ \t\r\n,#\\/]+')

# Regex for reply Detection


class SlackMode:
"""
Expand Down Expand Up @@ -566,6 +564,7 @@ def send(self, body, title='', notify_type=NotifyType.INFO, attach=None,
r':(.*)',
re.IGNORECASE).search(channel):
payload['thread_ts'] = timestamp_match.group(1)
# reset channel name to remove the timestamp that is given
channel = channel[:timestamp_match.start()]

if channel[0] == '+':
Expand Down Expand Up @@ -802,7 +801,6 @@ def _send(self, url, payload, attach=None, **kwargs):
"""
Wrapper to the requests (post) object
"""
print(payload)
self.logger.debug('Slack POST URL: %s (cert_verify=%r)' % (
url, self.verify_certificate,
))
Expand Down
62 changes: 58 additions & 4 deletions test/test_plugin_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@
'test_requests_exceptions': True,
'requests_response_text': 'ok',
}),
('slack://notify@T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/#b:100', {
'instance': NotifySlack,
'test_requests_exceptions': 200,
'requests_response_text': 'ok',
}),
)


Expand Down Expand Up @@ -705,16 +710,15 @@ def test_plugin_slack_markdown(mock_get, mock_post):


@mock.patch('requests.post')
def test_plugin_slack_thread_reply(mock_post):
def test_plugin_slack_single_thread_reply(mock_post):
"""
NotifySlack() Send Notification as a Reply
"""

# Generate a (valid) bot token
token = 'xoxb-1234-1234-abc124'
timestamp = 100.23

thread_id = 100
request = mock.Mock()
request.content = dumps({
'ok': True,
Expand All @@ -729,7 +733,7 @@ def test_plugin_slack_thread_reply(mock_post):
mock_post.return_value = request

# Variation Initializations
obj = NotifySlack(access_token=token)
obj = NotifySlack(access_token=token, targets=[f'#general:{thread_id}'])
assert isinstance(obj, NotifySlack) is True
assert isinstance(obj.url(), str) is True

Expand All @@ -744,3 +748,53 @@ def test_plugin_slack_thread_reply(mock_post):
assert mock_post.call_count == 1
assert mock_post.call_args_list[0][0][0] == \
'https://slack.com/api/chat.postMessage'
assert loads(mock_post.call_args_list[0][1]['data']).get("thread_ts") \
== str(thread_id)


@mock.patch('requests.post')
def test_plugin_slack_multiple_thread_reply(mock_post):
"""
NotifySlack() Send Notification to multiple channels as Reply
"""

# Generate a (valid) bot token
token = 'xoxb-1234-1234-abc124'
thread_id_1, thread_id_2 = 100, 200
request = mock.Mock()
request.content = dumps({
'ok': True,
'message': '',
'user': {
'id': 'ABCD1234'
}
})
request.status_code = requests.codes.ok

# Prepare Mock
mock_post.return_value = request

# Variation Initializations
obj = NotifySlack(access_token=token,
targets=[
f'#general:{thread_id_1}',
f'#other:{thread_id_2}'])
assert isinstance(obj, NotifySlack) is True
assert isinstance(obj.url(), str) is True

# No calls made yet
assert mock_post.call_count == 0

# Send our notification
assert obj.notify(
body='body', title='title', notify_type=NotifyType.INFO) is True

# Post was made
assert mock_post.call_count == 2
assert mock_post.call_args_list[0][0][0] == \
'https://slack.com/api/chat.postMessage'
assert loads(mock_post.call_args_list[0][1]['data']).get("thread_ts") \
== str(thread_id_1)
assert loads(mock_post.call_args_list[1][1]['data']).get("thread_ts") \
== str(thread_id_2)

0 comments on commit 86d7b04

Please sign in to comment.