Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_campfire.py

raw
1 # Copyright (c) Ansible Project
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 from unittest.mock import patch
8
9 import pytest
10 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
11 AnsibleExitJson,
12 AnsibleFailJson,
13 ModuleTestCase,
14 set_module_args,
15 )
16
17 from ansible_collections.community.general.plugins.modules import campfire
18
19
20 class TestCampfireModule(ModuleTestCase):
21 def setUp(self):
22 super().setUp()
23 self.module = campfire
24
25 def tearDown(self):
26 super().tearDown()
27
28 @pytest.fixture
29 def fetch_url_mock(self, mocker):
30 return mocker.patch("ansible.module_utils.notification.campfire.fetch_url")
31
32 def test_without_required_parameters(self):
33 """Failure must occurs when all parameters are missing"""
34 with self.assertRaises(AnsibleFailJson):
35 with set_module_args({}):
36 self.module.main()
37
38 def test_successful_message(self):
39 """Test failure message"""
40 with set_module_args({"subscription": "test", "token": "abc", "room": "test", "msg": "test"}):
41 with patch.object(campfire, "fetch_url") as fetch_url_mock:
42 fetch_url_mock.return_value = (None, {"status": 200})
43 with self.assertRaises(AnsibleExitJson):
44 self.module.main()
45
46 assert fetch_url_mock.call_count == 1
47 url = fetch_url_mock.call_args[0][1]
48 data = fetch_url_mock.call_args[1]["data"]
49
50 assert url == "https://test.campfirenow.com/room/test/speak.xml"
51 assert data == "<message><body>test</body></message>"
52
53 def test_successful_message_with_notify(self):
54 """Test failure message"""
55 with set_module_args({"subscription": "test", "token": "abc", "room": "test", "msg": "test", "notify": "bell"}):
56 with patch.object(campfire, "fetch_url") as fetch_url_mock:
57 fetch_url_mock.return_value = (None, {"status": 200})
58 with self.assertRaises(AnsibleExitJson):
59 self.module.main()
60
61 assert fetch_url_mock.call_count == 2
62 notify_call = fetch_url_mock.mock_calls[0]
63 url = notify_call[1][1]
64 data = notify_call[2]["data"]
65
66 assert url == "https://test.campfirenow.com/room/test/speak.xml"
67 assert data == "<message><type>SoundMessage</type><body>bell</body></message>"
68
69 message_call = fetch_url_mock.mock_calls[1]
70 url = message_call[1][1]
71 data = message_call[2]["data"]
72
73 assert url == "https://test.campfirenow.com/room/test/speak.xml"
74 assert data == "<message><body>test</body></message>"
75
76 def test_failure_message(self):
77 """Test failure message"""
78 with set_module_args({"subscription": "test", "token": "abc", "room": "test", "msg": "test"}):
79 with patch.object(campfire, "fetch_url") as fetch_url_mock:
80 fetch_url_mock.return_value = (None, {"status": 403})
81 with self.assertRaises(AnsibleFailJson):
82 self.module.main()