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:
Pedro Algarvio 2019-05-15 08:50:12 +01:00 committed by GitHub
commit 06fa91bd33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,11 +8,13 @@ Tests for the Telegram execution module.
# Import Python Libs # Import Python Libs
from __future__ import absolute_import from __future__ import absolute_import
import logging
# Import Salt Testing Libs # Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase, skipIf from tests.support.unit import TestCase, skipIf
from tests.support.mock import ( from tests.support.mock import (
patch, Mock,
MagicMock, MagicMock,
NO_MOCK, NO_MOCK,
NO_MOCK_REASON NO_MOCK_REASON
@ -22,6 +24,47 @@ from tests.support.mock import (
import salt.modules.telegram as telegram 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) @skipIf(NO_MOCK, NO_MOCK_REASON)
class TelegramModuleTest(TestCase, LoaderModuleMockMixin): class TelegramModuleTest(TestCase, LoaderModuleMockMixin):
''' '''
@ -29,35 +72,22 @@ class TelegramModuleTest(TestCase, LoaderModuleMockMixin):
''' '''
def setup_loader_modules(self): def setup_loader_modules(self):
module_globals = { module_globals = {
telegram: {
'__salt__': { '__salt__': {
'config.get': MagicMock(return_value={ 'config.get': MagicMock(return_value={
'telegram': { 'telegram': {
'chat_id': '123456789', 'chat_id': '123456789',
'token': '000000000:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 'token': '000000000:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
} }
}) }),
'requests.put': Mock(),
},
'requests': RequestMock()
} }
} return {telegram: module_globals}
}
if telegram.HAS_REQUESTS is False:
module_globals['sys.modules'] = {'requests': MagicMock()}
return module_globals
def test_post_message(self): def test_post_message(self):
''' '''
Test the post_message function. Test the post_message function.
''' '''
message = 'Hello World!' 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)) self.assertTrue(telegram.post_message(message))