community.general/tests/unit/plugins/modules/test_campfire.py
Felix Fontein 8f8a0e1d7c
Some checks are pending
EOL CI / EOL Sanity (Ⓐ2.17) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.17+py3.10) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.17+py3.12) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.17+py3.7) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+alpine319+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+alpine319+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+alpine319+py:azp/posix/3/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+fedora39+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+fedora39+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+fedora39+py:azp/posix/3/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+ubuntu2004+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+ubuntu2004+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+ubuntu2004+py:azp/posix/3/) (push) Waiting to run
nox / Run extra sanity tests (push) Waiting to run
Fix __future__ imports, __metaclass__ = type, and remove explicit UTF-8 encoding statement for Python files (#10886)
* Adjust all __future__ imports:

for i in $(grep -REl "__future__.*absolute_import" plugins/ tests/); do
  sed -e 's/from __future__ import .*/from __future__ import annotations/g' -i $i;
done

* Remove all UTF-8 encoding specifications for Python source files:

for i in $(grep -REl '[-][*]- coding: utf-8 -[*]-' plugins/ tests/); do
  sed -e '/^# -\*- coding: utf-8 -\*-/d' -i $i;
done

* Remove __metaclass__ = type:

for i in $(grep -REl '__metaclass__ = type' plugins/ tests/); do
  sed -e '/^__metaclass__ = type/d' -i $i;
done
2025-10-10 19:52:04 +02:00

92 lines
3.4 KiB
Python

# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import annotations
import pytest
from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import patch
from ansible_collections.community.general.plugins.modules import campfire
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args
class TestCampfireModule(ModuleTestCase):
def setUp(self):
super(TestCampfireModule, self).setUp()
self.module = campfire
def tearDown(self):
super(TestCampfireModule, self).tearDown()
@pytest.fixture
def fetch_url_mock(self, mocker):
return mocker.patch('ansible.module_utils.notification.campfire.fetch_url')
def test_without_required_parameters(self):
"""Failure must occurs when all parameters are missing"""
with self.assertRaises(AnsibleFailJson):
with set_module_args({}):
self.module.main()
def test_successful_message(self):
"""Test failure message"""
with set_module_args({
'subscription': 'test',
'token': 'abc',
'room': 'test',
'msg': 'test'
}):
with patch.object(campfire, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 200})
with self.assertRaises(AnsibleExitJson):
self.module.main()
assert fetch_url_mock.call_count == 1
url = fetch_url_mock.call_args[0][1]
data = fetch_url_mock.call_args[1]['data']
assert url == 'https://test.campfirenow.com/room/test/speak.xml'
assert data == '<message><body>test</body></message>'
def test_successful_message_with_notify(self):
"""Test failure message"""
with set_module_args({
'subscription': 'test',
'token': 'abc',
'room': 'test',
'msg': 'test',
'notify': 'bell'
}):
with patch.object(campfire, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 200})
with self.assertRaises(AnsibleExitJson):
self.module.main()
assert fetch_url_mock.call_count == 2
notify_call = fetch_url_mock.mock_calls[0]
url = notify_call[1][1]
data = notify_call[2]['data']
assert url == 'https://test.campfirenow.com/room/test/speak.xml'
assert data == '<message><type>SoundMessage</type><body>bell</body></message>'
message_call = fetch_url_mock.mock_calls[1]
url = message_call[1][1]
data = message_call[2]['data']
assert url == 'https://test.campfirenow.com/room/test/speak.xml'
assert data == '<message><body>test</body></message>'
def test_failure_message(self):
"""Test failure message"""
with set_module_args({
'subscription': 'test',
'token': 'abc',
'room': 'test',
'msg': 'test'
}):
with patch.object(campfire, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 403})
with self.assertRaises(AnsibleFailJson):
self.module.main()