Unit tests: make set_module_args() a context manager, and remove copies of it in some tests (#9838)

Make set_module_args() a context manager, and remove copies of set_module_args().

Prepares for Data Tagging.
This commit is contained in:
Felix Fontein 2025-03-07 07:21:03 +01:00 committed by GitHub
parent 402f725424
commit a1781d09dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
84 changed files with 4043 additions and 4302 deletions

View file

@ -28,30 +28,30 @@ class TestDiscordModule(ModuleTestCase):
def test_without_parameters(self):
"""Failure if no parameters set"""
with self.assertRaises(AnsibleFailJson):
set_module_args({})
self.module.main()
with set_module_args({}):
self.module.main()
def test_without_content(self):
"""Failure if content and embeds both are missing"""
set_module_args({
with set_module_args({
'webhook_id': 'xxx',
'webhook_token': 'xxx'
})
with self.assertRaises(AnsibleFailJson):
self.module.main()
}):
with self.assertRaises(AnsibleFailJson):
self.module.main()
def test_successful_message(self):
"""Test a basic message successfully."""
set_module_args({
with set_module_args({
'webhook_id': 'xxx',
'webhook_token': 'xxx',
'content': 'test'
})
}):
with patch.object(discord, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 204, 'msg': 'OK (0 bytes)'})
with self.assertRaises(AnsibleExitJson):
self.module.main()
with patch.object(discord, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 204, 'msg': 'OK (0 bytes)'})
with self.assertRaises(AnsibleExitJson):
self.module.main()
self.assertTrue(fetch_url_mock.call_count, 1)
call_data = json.loads(fetch_url_mock.call_args[1]['data'])
@ -59,17 +59,17 @@ class TestDiscordModule(ModuleTestCase):
def test_message_with_username(self):
"""Test a message with username set successfully."""
set_module_args({
with set_module_args({
'webhook_id': 'xxx',
'webhook_token': 'xxx',
'content': 'test',
'username': 'Ansible Bot'
})
}):
with patch.object(discord, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 204, 'msg': 'OK (0 bytes)'})
with self.assertRaises(AnsibleExitJson):
self.module.main()
with patch.object(discord, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 204, 'msg': 'OK (0 bytes)'})
with self.assertRaises(AnsibleExitJson):
self.module.main()
self.assertTrue(fetch_url_mock.call_count, 1)
call_data = json.loads(fetch_url_mock.call_args[1]['data'])
@ -79,27 +79,30 @@ class TestDiscordModule(ModuleTestCase):
def test_failed_message(self):
"""Test failure because webhook id is wrong."""
set_module_args({
with set_module_args({
'webhook_id': 'wrong',
'webhook_token': 'xxx',
'content': 'test'
})
}):
with patch.object(discord, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 404, 'msg': 'HTTP Error 404: Not Found', 'body': '{"message": "Unknown Webhook", "code": 10015}'})
with self.assertRaises(AnsibleFailJson):
self.module.main()
with patch.object(discord, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (
None,
{"status": 404, 'msg': 'HTTP Error 404: Not Found', 'body': '{"message": "Unknown Webhook", "code": 10015}'},
)
with self.assertRaises(AnsibleFailJson):
self.module.main()
def test_failed_message_without_body(self):
"""Test failure with empty response body."""
set_module_args({
with set_module_args({
'webhook_id': 'wrong',
'webhook_token': 'xxx',
'content': 'test'
})
}):
with patch.object(discord, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 404, 'msg': 'HTTP Error 404: Not Found'})
with self.assertRaises(AnsibleFailJson):
self.module.main()
with patch.object(discord, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 404, 'msg': 'HTTP Error 404: Not Found'})
with self.assertRaises(AnsibleFailJson):
self.module.main()