Fixed mattermost channel handling (#1180)

This commit is contained in:
Chris Caron 2024-08-05 11:28:49 -04:00 committed by GitHub
parent 47540523e0
commit 1ee08d14fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 69 additions and 5 deletions

View File

@ -132,6 +132,9 @@ class NotifyMattermost(NotifyBase):
'name': _('Channels'), 'name': _('Channels'),
'type': 'list:string', 'type': 'list:string',
}, },
'channel': {
'alias_of': 'channels',
},
'image': { 'image': {
'name': _('Include Image'), 'name': _('Include Image'),
'type': 'bool', 'type': 'bool',
@ -357,16 +360,21 @@ class NotifyMattermost(NotifyBase):
# Support both 'to' (for yaml configuration) and channel= # Support both 'to' (for yaml configuration) and channel=
if 'to' in results['qsd'] and len(results['qsd']['to']): if 'to' in results['qsd'] and len(results['qsd']['to']):
# Allow the user to specify the channel to post to # Allow the user to specify the channel to post to
results['channels'].append( results['channels'].extend(
NotifyMattermost.parse_list(results['qsd']['to'])) NotifyMattermost.parse_list(results['qsd']['to']))
if 'channel' in results['qsd'] and len(results['qsd']['channel']): if 'channel' in results['qsd'] and len(results['qsd']['channel']):
# Allow the user to specify the channel to post to # Allow the user to specify the channel to post to
results['channels'].append( results['channels'].extend(
NotifyMattermost.parse_list(results['qsd']['channel'])) NotifyMattermost.parse_list(results['qsd']['channel']))
if 'channels' in results['qsd'] and len(results['qsd']['channels']):
# Allow the user to specify the channel to post to
results['channels'].extend(
NotifyMattermost.parse_list(results['qsd']['channels']))
# Image manipulation # Image manipulation
results['include_image'] = \ results['include_image'] = parse_bool(results['qsd'].get(
parse_bool(results['qsd'].get('image', False)) 'image', NotifyMattermost.template_args['image']['default']))
return results return results

View File

@ -27,8 +27,9 @@
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
import pytest import pytest
import json
import requests import requests
from apprise import Apprise
from apprise.plugins.mattermost import NotifyMattermost from apprise.plugins.mattermost import NotifyMattermost
from helpers import AppriseURLTester from helpers import AppriseURLTester
@ -57,6 +58,9 @@ apprise_url_tests = (
('mmost://user@localhost/3ccdd113474722377935511fc85d3dd4?channel=test', { ('mmost://user@localhost/3ccdd113474722377935511fc85d3dd4?channel=test', {
'instance': NotifyMattermost, 'instance': NotifyMattermost,
}), }),
('mmost://user@localhost/3ccdd113474722377935511fc85d3dd4?channels=test', {
'instance': NotifyMattermost,
}),
('mmost://user@localhost/3ccdd113474722377935511fc85d3dd4?to=test', { ('mmost://user@localhost/3ccdd113474722377935511fc85d3dd4?to=test', {
'instance': NotifyMattermost, 'instance': NotifyMattermost,
@ -117,6 +121,17 @@ apprise_url_tests = (
) )
@pytest.fixture
def request_mock(mocker):
"""
Prepare requests mock.
"""
mock_post = mocker.patch("requests.post")
mock_post.return_value = requests.Request()
mock_post.return_value.status_code = requests.codes.ok
return mock_post
def test_plugin_mattermost_urls(): def test_plugin_mattermost_urls():
""" """
NotifyMattermost() Apprise URLs NotifyMattermost() Apprise URLs
@ -138,3 +153,44 @@ def test_plugin_mattermost_edge_cases():
NotifyMattermost(None) NotifyMattermost(None)
with pytest.raises(TypeError): with pytest.raises(TypeError):
NotifyMattermost(" ") NotifyMattermost(" ")
def test_plugin_mattermost_channels(request_mock):
"""
NotifyMattermost() Channel Testing
"""
# Test channels with/without hashtag (#)
user = 'user1'
token = 'token'
channels = ['#one', 'two']
# Instantiate our URL
obj = Apprise.instantiate(
'mmost://{user}@localhost:8065/{token}?channels={channels}'.format(
user=user,
token=token,
channels=','.join(channels)))
assert isinstance(obj, NotifyMattermost)
assert obj.notify(body="body", title='title') is True
assert request_mock.called is True
assert request_mock.call_count == 2
assert request_mock.call_args_list[0][0][0].startswith(
'http://localhost:8065/hooks/token')
# Our Posted JSON Object
posted_json = json.loads(request_mock.call_args_list[0][1]['data'])
assert 'username' in posted_json
assert 'channel' in posted_json
assert 'text' in posted_json
assert posted_json['username'] == 'user1'
assert posted_json['channel'] == 'one'
assert posted_json['text'] == 'title\r\nbody'
# Our second Posted JSON Object
posted_json = json.loads(request_mock.call_args_list[1][1]['data'])
assert posted_json['username'] == 'user1'
assert posted_json['channel'] == 'two'
assert posted_json['text'] == 'title\r\nbody'