From 6295888ef004d4827a1bdd410a1a036f8d1fc8da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanislav=20Ochotnick=C3=BD?= Date: Sun, 16 Mar 2025 21:26:06 +0100 Subject: [PATCH] Set default port for Mattermost notifications (#1293) --- apprise/plugins/mattermost.py | 8 ++++++-- test/test_plugin_mattermost.py | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/apprise/plugins/mattermost.py b/apprise/plugins/mattermost.py index bf2bd171..1de2dd72 100644 --- a/apprise/plugins/mattermost.py +++ b/apprise/plugins/mattermost.py @@ -205,6 +205,10 @@ class NotifyMattermost(NotifyBase): # Set our user payload['username'] = self.user if self.user else self.app_id + port = '' + if self.port is not None: + port = ':{}'.format(self.port) + # For error tracking has_error = False @@ -215,8 +219,8 @@ class NotifyMattermost(NotifyBase): if channel: payload['channel'] = channel - url = '{}://{}:{}{}/hooks/{}'.format( - self.schema, self.host, self.port, + url = '{}://{}{}{}/hooks/{}'.format( + self.schema, self.host, port, self.fullpath.rstrip('/'), self.token) self.logger.debug('Mattermost POST URL: %s (cert_verify=%r)' % ( diff --git a/test/test_plugin_mattermost.py b/test/test_plugin_mattermost.py index 0fdf94e1..a250e0ac 100644 --- a/test/test_plugin_mattermost.py +++ b/test/test_plugin_mattermost.py @@ -199,3 +199,28 @@ def test_plugin_mattermost_channels(request_mock): assert posted_json['username'] == 'user1' assert posted_json['channel'] == 'two' assert posted_json['text'] == 'title\r\nbody' + + +def test_mattermost_post_default_port(request_mock): + # Test token + token = 'token' + + # Instantiate our URL + obj = Apprise.instantiate( + 'mmosts://mattermost.example.com/{token}'.format( + token=token) + ) + + assert isinstance(obj, NotifyMattermost) + assert obj.notify(body="body", title='title') is True + + # Make sure we don't use port if not provided + assert request_mock.called is True + assert request_mock.call_count == 1 + assert request_mock.call_args_list[0][0][0].startswith( + 'https://mattermost.example.com/hooks/token') + + # Our Posted JSON Object + posted_json = json.loads(request_mock.call_args_list[0][1]['data']) + assert 'text' in posted_json + assert posted_json['text'] == 'title\r\nbody'