Debian Codegraph @

test/units/modules/test_uri.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright:
3 # (c) 2023 Ansible Project
4 # License: GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import annotations
7
8 from unittest.mock import MagicMock, patch
9
10 import pytest
11
12 from ansible.module_utils.testing import patch_module_args
13 from ansible.modules import uri
14
15
16 class TestUri:
17
18 def test_main_no_args(self):
19 """Module must fail if called with no args."""
20 with pytest.raises(SystemExit), \
21 patch_module_args():
22 uri.main()
23
24 def test_main_no_force(self, mocker):
25 """The "force" parameter to fetch_url() must be absent or false when the module is called without "force"."""
26 resp = MagicMock()
27 resp.headers.get_content_type.return_value = "text/html"
28 info = {"url": "http://example.com/", "status": 200}
29 with patch.object(uri, "fetch_url", return_value=(resp, info)) as fetch_url, \
30 patch_module_args({"url": "http://example.com/"}):
31 with pytest.raises(SystemExit):
32 uri.main()
33 fetch_url.assert_called_once()
34 assert not fetch_url.call_args[1].get("force")
35
36 def test_main_force(self):
37 """The "force" parameter to fetch_url() must be true when the module is called with "force"."""
38 resp = MagicMock()
39 resp.headers.get_content_type.return_value = "text/html"
40 info = {"url": "http://example.com/", "status": 200}
41 with patch.object(uri, "fetch_url", return_value=(resp, info)) as fetch_url, \
42 patch_module_args({"url": "http://example.com/", "force": True}):
43 with pytest.raises(SystemExit):
44 uri.main()
45 fetch_url.assert_called_once()
46 assert fetch_url.call_args[1].get("force")