123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- # -*- coding: utf-8 -*-
- '''
- :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
- :codeauthor: Gareth J. Greenaway <gareth@saltstack.com>
- '''
- # Import Python libs
- from __future__ import absolute_import, unicode_literals, print_function
- # Import Salt Testing Libs
- from tests.support.mixins import LoaderModuleMockMixin
- from tests.support.unit import TestCase
- from tests.support.mock import (
- MagicMock,
- patch
- )
- # Import Salt Libs
- import salt.states.slack as slack
- class SlackTestCase(TestCase, LoaderModuleMockMixin):
- '''
- Test cases for salt.states.slack
- '''
- def setup_loader_modules(self):
- return {slack: {}}
- # 'post_message' function tests: 1
- def test_post_message_apikey(self):
- '''
- Test to send a message to a Slack channel using an API Key.
- '''
- name = 'slack-message'
- channel = '#general'
- from_name = 'SuperAdmin'
- message = 'This state was executed successfully.'
- 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,
- from_name=from_name,
- message=message,
- api_key=api_key), 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=None,
- from_name=from_name,
- message=message,
- api_key=None), ret)
- comt = 'Slack channel is missing.'
- ret.update({'comment': comt, 'result': False})
- self.assertDictEqual(slack.post_message(name,
- channel=None,
- from_name=from_name,
- message=message,
- api_key=api_key), ret)
- comt = 'Slack from name is missing.'
- ret.update({'comment': comt, 'result': False})
- self.assertDictEqual(slack.post_message(name,
- channel=channel,
- from_name=None,
- message=message,
- api_key=api_key), ret)
- comt = 'Slack message is missing.'
- ret.update({'comment': comt, 'result': False})
- self.assertDictEqual(slack.post_message(name,
- channel=channel,
- from_name=from_name,
- message=None,
- api_key=api_key), 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=channel,
- from_name=from_name,
- message=message,
- api_key=api_key),
- 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 = 'SuperAdmin'
- 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)
|