mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 08:58:59 +00:00
Merge pull request #52972 from garethgreenaway/52817_unit_modules_test_telegram_telegrammoduletest_test_post_message
[2019.2.1] Fix to unit.modules.test_telegram
This commit is contained in:
commit
06fa91bd33
@ -8,11 +8,13 @@ Tests for the Telegram execution module.
|
||||
# Import Python Libs
|
||||
from __future__ import absolute_import
|
||||
|
||||
import logging
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import TestCase, skipIf
|
||||
from tests.support.mock import (
|
||||
patch,
|
||||
Mock,
|
||||
MagicMock,
|
||||
NO_MOCK,
|
||||
NO_MOCK_REASON
|
||||
@ -22,6 +24,47 @@ from tests.support.mock import (
|
||||
import salt.modules.telegram as telegram
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class RequestMock(Mock):
|
||||
'''
|
||||
Request Mock
|
||||
'''
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
return RequestResponseMock()
|
||||
|
||||
def put(self, *args, **kwargs):
|
||||
self.args = args
|
||||
self.kwargs = kwargs
|
||||
return RequestPutResponseMock()
|
||||
|
||||
def delete(self, *args, **kwargs):
|
||||
self.args = args
|
||||
self.kwargs = kwargs
|
||||
return RequestResponseMock()
|
||||
|
||||
|
||||
class RequestResponseMock(Mock):
|
||||
'''
|
||||
Request Response Mock
|
||||
'''
|
||||
def json(self):
|
||||
return [{'url': 'http://example.org',
|
||||
'_id': 1234}, ]
|
||||
|
||||
|
||||
class RequestPutResponseMock(Mock):
|
||||
'''
|
||||
Request Put Response Mock
|
||||
'''
|
||||
ok = True
|
||||
|
||||
def json(self):
|
||||
return {'_id': 4321}
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class TelegramModuleTest(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
@ -29,35 +72,22 @@ class TelegramModuleTest(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
def setup_loader_modules(self):
|
||||
module_globals = {
|
||||
telegram: {
|
||||
'__salt__': {
|
||||
'config.get': MagicMock(return_value={
|
||||
'telegram': {
|
||||
'chat_id': '123456789',
|
||||
'token': '000000000:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
|
||||
}
|
||||
})
|
||||
}),
|
||||
'requests.put': Mock(),
|
||||
},
|
||||
'requests': RequestMock()
|
||||
}
|
||||
}
|
||||
}
|
||||
if telegram.HAS_REQUESTS is False:
|
||||
module_globals['sys.modules'] = {'requests': MagicMock()}
|
||||
return module_globals
|
||||
return {telegram: module_globals}
|
||||
|
||||
def test_post_message(self):
|
||||
'''
|
||||
Test the post_message function.
|
||||
'''
|
||||
message = 'Hello World!'
|
||||
|
||||
class MockRequests(object):
|
||||
"""
|
||||
Mock of requests response.
|
||||
"""
|
||||
def json(self):
|
||||
return {'ok': True}
|
||||
|
||||
with patch('salt.modules.telegram.requests.post',
|
||||
MagicMock(return_value=MockRequests())):
|
||||
|
||||
self.assertTrue(telegram.post_message(message))
|
||||
|
Loading…
Reference in New Issue
Block a user