mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
migrate test_telegram to pytest
This commit is contained in:
parent
454c998e7f
commit
708411a83a
2 changed files with 79 additions and 86 deletions
79
tests/pytests/unit/modules/test_telegram.py
Normal file
79
tests/pytests/unit/modules/test_telegram.py
Normal file
|
@ -0,0 +1,79 @@
|
|||
"""
|
||||
:codeauthor: :email:`Roald Nefs (info@roaldnefs.com)`
|
||||
|
||||
Test cases for salt.modules.telegram.
|
||||
"""
|
||||
|
||||
|
||||
import pytest
|
||||
|
||||
import salt.modules.telegram as telegram
|
||||
from tests.support.mock import MagicMock, Mock
|
||||
|
||||
|
||||
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}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def configure_loader_modules():
|
||||
module_globals = {
|
||||
"__salt__": {
|
||||
"config.get": MagicMock(
|
||||
return_value={
|
||||
"telegram": {
|
||||
"chat_id": "123456789",
|
||||
"token": "000000000:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
||||
}
|
||||
}
|
||||
),
|
||||
"requests.put": Mock(),
|
||||
},
|
||||
"requests": RequestMock(),
|
||||
}
|
||||
return {telegram: module_globals}
|
||||
|
||||
|
||||
def test_post_message():
|
||||
"""
|
||||
Test the post_message function.
|
||||
"""
|
||||
message = "Hello World!"
|
||||
assert telegram.post_message(message)
|
|
@ -1,86 +0,0 @@
|
|||
"""
|
||||
Tests for the Telegram execution module.
|
||||
|
||||
:codeauthor: :email:`Roald Nefs (info@roaldnefs.com)`
|
||||
"""
|
||||
|
||||
|
||||
import logging
|
||||
|
||||
import salt.modules.telegram as telegram
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.mock import MagicMock, Mock
|
||||
from tests.support.unit import TestCase
|
||||
|
||||
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}
|
||||
|
||||
|
||||
class TelegramModuleTest(TestCase, LoaderModuleMockMixin):
|
||||
"""
|
||||
Test cases for salt.modules.telegram.
|
||||
"""
|
||||
|
||||
def setup_loader_modules(self):
|
||||
module_globals = {
|
||||
"__salt__": {
|
||||
"config.get": MagicMock(
|
||||
return_value={
|
||||
"telegram": {
|
||||
"chat_id": "123456789",
|
||||
"token": "000000000:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
||||
}
|
||||
}
|
||||
),
|
||||
"requests.put": Mock(),
|
||||
},
|
||||
"requests": RequestMock(),
|
||||
}
|
||||
return {telegram: module_globals}
|
||||
|
||||
def test_post_message(self):
|
||||
"""
|
||||
Test the post_message function.
|
||||
"""
|
||||
message = "Hello World!"
|
||||
self.assertTrue(telegram.post_message(message))
|
Loading…
Add table
Reference in a new issue