Debian Codegraph @

ansible_collections/community/proxmox/tests/unit/plugins/modules/test_proxmox_acme_account.py

raw
1 #
2 # Copyright (c) 2026, Clément Cruau (@PendaGTP) <[email protected]>
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6
7 from unittest.mock import Mock, patch
8
9 import pytest
10
11 proxmoxer = pytest.importorskip("proxmoxer")
12
13 from ansible.module_utils import basic
14 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
15 ModuleTestCase,
16 set_module_args,
17 )
18
19 from ansible_collections.community.proxmox.plugins.modules import proxmox_acme_account
20
21 SAMPLE_GET = {
22 "account": {
23 "contact": ["mailto:[email protected]"],
24 "createdAt": "2026-01-01T00:00:00Z",
25 "status": "valid",
26 },
27 "directory": "https://acme-staging-v02.api.letsencrypt.org/directory",
28 "location": "https://acme-v02.api.letsencrypt.org/acme/acct/3198781481",
29 "tos": "https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf",
30 }
31
32 SAMPLE_GET_NO_CONTACT = {
33 "account": {
34 "contact": [],
35 "createdAt": "2026-01-01T00:00:00Z",
36 "status": "valid",
37 },
38 "directory": "https://acme-staging-v02.api.letsencrypt.org/directory",
39 "location": "https://acme-v02.api.letsencrypt.org/acme/acct/3198781481",
40 "tos": "https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf",
41 }
42
43 UPID = "UPID:pve:0018BD11:0188245B:69CCD6A7:acmeregister::root@pam:"
44
45
46 def exit_json(*args, **kwargs):
47 kwargs.setdefault("changed", False)
48 raise SystemExit(kwargs)
49
50
51 def fail_json(*args, **kwargs):
52 kwargs["failed"] = True
53 raise SystemExit(kwargs)
54
55
56 def build_module_args(state="present", omit_contact=False, **overrides):
57 args = {
58 "api_host": "host",
59 "api_user": "user",
60 "api_password": "password",
61 "name": "default",
62 "state": state,
63 }
64 args.update(overrides)
65 if not omit_contact and "contact" not in args:
66 args["contact"] = "[email protected]"
67 return args
68
69
70 class TestProxmoxClusterAcmeAccountModule(ModuleTestCase):
71 def setUp(self):
72 super().setUp()
73 self.module = proxmox_acme_account
74 self.warn_mock = Mock()
75 self.mock_module_helper = patch.multiple(
76 basic.AnsibleModule,
77 exit_json=exit_json,
78 fail_json=fail_json,
79 warn=self.warn_mock,
80 )
81 self.mock_module_helper.start()
82 self.connect_mock = patch(
83 "ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect",
84 ).start()
85 mock_api = self.connect_mock.return_value
86 account_attr = mock_api.cluster.return_value.acme.return_value.account
87 self.named_account = account_attr.return_value
88 self.named_account.return_value = self.named_account
89 self.named_account.post.return_value = UPID
90 self.named_account.put.return_value = UPID
91 self.named_account.delete.return_value = UPID
92 self.wait_mock = patch.object(
93 proxmox_acme_account.ProxmoxClusterAcmeAccountAnsible,
94 "_wait_acme_task",
95 ).start()
96
97 def tearDown(self):
98 self.wait_mock.stop()
99 self.connect_mock.stop()
100 self.mock_module_helper.stop()
101 super().tearDown()
102
103 def _run_module(self, args):
104 with pytest.raises(SystemExit) as exc_info, set_module_args(args):
105 self.module.main()
106 return exc_info.value.args[0]
107
108 def test_create_requires_contact(self):
109 self.named_account.get.return_value = None
110
111 result = self._run_module(build_module_args(omit_contact=True))
112
113 assert result.get("failed") is True
114 assert "contact is required to create" in result["msg"]
115
116 def test_existing_omit_contact_no_put(self):
117 self.named_account.get.return_value = SAMPLE_GET
118
119 result = self._run_module(build_module_args(omit_contact=True))
120
121 assert result["changed"] is False
122 assert "no update attempted" in result["msg"]
123 assert self.named_account.put.call_count == 0
124
125 def test_existing_contact_api_missing_warn_and_put(self):
126 self.named_account.get.side_effect = [SAMPLE_GET_NO_CONTACT, SAMPLE_GET_NO_CONTACT]
127
128 result = self._run_module(build_module_args(contact="[email protected]"))
129
130 assert result["changed"] is True
131 self.named_account.put.assert_called_once()
132 assert self.warn_mock.call_count == 1
133 assert "cannot verify drift" in self.warn_mock.call_args[0][0]
134
135 def test_existing_contact_api_match_no_put(self):
136 self.named_account.get.return_value = SAMPLE_GET
137
138 result = self._run_module(build_module_args(contact="[email protected]"))
139
140 assert result["changed"] is False
141 assert self.named_account.put.call_count == 0
142
143 def test_existing_contact_api_diff_put(self):
144 other = {
145 **SAMPLE_GET,
146 "account": {**SAMPLE_GET["account"], "contact": ["mailto:[email protected]"]},
147 }
148 self.named_account.get.side_effect = [other, SAMPLE_GET]
149
150 result = self._run_module(build_module_args(contact="[email protected]"))
151
152 assert result["changed"] is True
153 self.named_account.put.assert_called_once_with(contact="[email protected]")