def test_post_message(self): """ Test to send a message to a Slack channel. """ name = "slack-message" channel = "#general" from_name = "SuperAdmin" message = "This state was executed successfully." ret = {"name": name, "changes": {}, "result": None, "comment": ""} with patch.dict(slack.__opts__, {"test": True}): comt = "The following message is to be sent to Slack: {0}".format(message) ret.update({"comment": comt}) self.assertDictEqual(slack.post_message(name, channel, from_name, message), ret) with patch.dict(slack.__opts__, {"test": False}): comt = "Slack channel is missing: None" ret.update({"comment": comt, "result": False}) self.assertDictEqual(slack.post_message(name, None, from_name, message), ret) comt = "Slack from name is missing: None" ret.update({"comment": comt, "result": False}) self.assertDictEqual(slack.post_message(name, channel, None, message), ret) comt = "Slack message is missing: None" ret.update({"comment": comt, "result": False}) self.assertDictEqual(slack.post_message(name, channel, from_name, None), ret) mock = MagicMock(return_value=True) with patch.dict(slack.__salt__, {"slack.post_message": mock}): comt = "Sent message: slack-message" ret.update({"comment": comt, "result": True}) self.assertDictEqual(slack.post_message(name, channel, from_name, message), ret)
def test_post_message(self): ''' Test to send a message to a Slack channel. ''' name = 'slack-message' channel = '#general' from_name = 'SuperAdmin' message = 'This state was executed successfully.' ret = {'name': name, 'changes': {}, 'result': None, 'comment': ''} with patch.dict(slack.__opts__, {'test': True}): comt = ('The following message is to be sent to Slack: {0}' .format(message)) ret.update({'comment': comt}) self.assertDictEqual(slack.post_message(name, channel, from_name, message), ret) with patch.dict(slack.__opts__, {'test': False}): comt = ('Slack channel is missing: None') ret.update({'comment': comt, 'result': False}) self.assertDictEqual(slack.post_message(name, None, from_name, message), ret) comt = ('Slack from name is missing: None') ret.update({'comment': comt, 'result': False}) self.assertDictEqual(slack.post_message(name, channel, None, message), ret) comt = ('Slack message is missing: None') ret.update({'comment': comt, 'result': False}) self.assertDictEqual(slack.post_message(name, channel, from_name, None), ret) mock = MagicMock(return_value=True) with patch.dict(slack.__salt__, {'slack.post_message': mock}): comt = ('Sent message: slack-message') ret.update({'comment': comt, 'result': True}) self.assertDictEqual(slack.post_message(name, channel, from_name, message), ret)
def test_post_message_webhook(): """ Test to send a message to a Slack channel using an webhook. """ name = "slack-message" channel = "#general" username = "******" message = "This state was executed successfully." webhook = "XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX" api_key = "xoxp-XXXXXXXXXX-XXXXXXXXXX-XXXXXXXXXX-XXXXXX" ret = {"name": name, "changes": {}, "result": None, "comment": ""} with patch.dict(slack.__opts__, {"test": True}): comt = "The following message is to be sent to Slack: {}".format( message) ret.update({"comment": comt}) assert (slack.post_message( name, channel=channel, username=username, message=message, webhook=webhook, ) == ret) with patch.dict(slack.__opts__, {"test": False}): comt = "Please specify api_key or webhook." ret.update({"comment": comt, "result": False}) assert (slack.post_message(name, channel=channel, username=username, message=None, webhook=None) == ret) comt = "Please specify only either api_key or webhook." ret.update({"comment": comt, "result": False}) assert (slack.post_message( name, channel=channel, username=username, message=message, api_key=api_key, webhook=webhook, ) == ret) comt = "Slack message is missing." ret.update({"comment": comt, "result": False}) assert (slack.post_message( name, channel=channel, username=username, message=None, webhook=webhook, ) == ret) mock = MagicMock(return_value=True) with patch.dict(slack.__salt__, {"slack.call_hook": mock}): comt = "Sent message: slack-message" ret.update({"comment": comt, "result": True}) assert (slack.post_message( name, channel=channel, username=username, message=message, webhook=webhook, ) == ret)
def test_post_message_webhook(self): ''' Test to send a message to a Slack channel using an webhook. ''' name = 'slack-message' channel = '#general' username = '******' message = 'This state was executed successfully.' webhook = 'XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX' api_key = 'xoxp-XXXXXXXXXX-XXXXXXXXXX-XXXXXXXXXX-XXXXXX' ret = {'name': name, 'changes': {}, 'result': None, 'comment': ''} with patch.dict(slack.__opts__, {'test': True}): comt = 'The following message is to be sent to Slack: {0}'.format(message) ret.update({'comment': comt}) self.assertDictEqual(slack.post_message(name, channel=channel, username=username, message=message, webhook=webhook), ret) with patch.dict(slack.__opts__, {'test': False}): comt = 'Please specify api_key or webhook.' ret.update({'comment': comt, 'result': False}) self.assertDictEqual(slack.post_message(name, channel=channel, username=username, message=None, webhook=None), ret) comt = 'Please specify only either api_key or webhook.' ret.update({'comment': comt, 'result': False}) self.assertDictEqual(slack.post_message(name, channel=channel, username=username, message=message, api_key=api_key, webhook=webhook), ret) comt = 'Slack message is missing.' ret.update({'comment': comt, 'result': False}) self.assertDictEqual(slack.post_message(name, channel=channel, username=username, message=None, webhook=webhook), ret) mock = MagicMock(return_value=True) with patch.dict(slack.__salt__, {'slack.call_hook': mock}): comt = 'Sent message: slack-message' ret.update({'comment': comt, 'result': True}) self.assertDictEqual(slack.post_message(name, channel=channel, username=username, message=message, webhook=webhook), ret)