Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_rundeck_acl_policy.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 exit_json,
13 fail_json,
14 set_module_args,
15 )
16
17 from ansible_collections.community.general.plugins.modules import rundeck_acl_policy
18
19
20 @pytest.fixture(autouse=True)
21 def module():
22 with patch.multiple(
23 "ansible.module_utils.basic.AnsibleModule",
24 exit_json=exit_json,
25 fail_json=fail_json,
26 ):
27 yield
28
29
30 # define our two table entries: system ACL vs. project ACL
31 PROJECT_TABLE = [
32 (None, "system/acl"),
33 ("test_project", "project/test_project/acl"),
34 ]
35
36
37 @pytest.mark.parametrize("project, prefix", PROJECT_TABLE)
38 @patch.object(rundeck_acl_policy, "api_request")
39 def test_acl_create(api_request_mock, project, prefix):
40 """Test creating a new ACL, both system-level and project-level."""
41 name = "my_policy"
42 policy = "test_policy_yaml"
43 # simulate: GET→404, POST→201, final GET→200
44 api_request_mock.side_effect = [
45 (None, {"status": 404}),
46 (None, {"status": 201}),
47 ({"contents": policy}, {"status": 200}),
48 ]
49 args = {
50 "name": name,
51 "url": "https://rundeck.example.org",
52 "api_token": "mytoken",
53 "policy": policy,
54 }
55 if project:
56 args["project"] = project
57
58 with pytest.raises(AnsibleExitJson):
59 with set_module_args(args):
60 rundeck_acl_policy.main()
61
62 # should have done GET → POST → GET
63 assert api_request_mock.call_count == 3
64 args, kwargs = api_request_mock.call_args_list[1]
65 assert kwargs["endpoint"] == f"{prefix}/{name}.aclpolicy"
66 assert kwargs["method"] == "POST"
67
68
69 @pytest.mark.parametrize("project, prefix", PROJECT_TABLE)
70 @patch.object(rundeck_acl_policy, "api_request")
71 def test_acl_unchanged(api_request_mock, project, prefix):
72 """Test no-op when existing ACL contents match the desired policy."""
73 name = "unchanged_policy"
74 policy = "same_policy_yaml"
75 # first GET returns matching contents
76 api_request_mock.return_value = ({"contents": policy}, {"status": 200})
77
78 args = {
79 "name": name,
80 "url": "https://rundeck.example.org",
81 "api_token": "mytoken",
82 "policy": policy,
83 }
84 if project:
85 args["project"] = project
86
87 with pytest.raises(AnsibleExitJson):
88 with set_module_args(args):
89 rundeck_acl_policy.main()
90
91 # only a single GET
92 assert api_request_mock.call_count == 1
93 args, kwargs = api_request_mock.call_args
94 assert kwargs["endpoint"] == f"{prefix}/{name}.aclpolicy"
95 # default method is GET
96 assert kwargs.get("method", "GET") == "GET"
97
98
99 @pytest.mark.parametrize("project, prefix", PROJECT_TABLE)
100 @patch.object(rundeck_acl_policy, "api_request")
101 def test_acl_remove(api_request_mock, project, prefix):
102 """Test removing an existing ACL, both system- and project-level."""
103 name = "remove_me"
104 # GET finds it, DELETE removes it
105 api_request_mock.side_effect = [
106 ({"contents": "old_yaml"}, {"status": 200}),
107 (None, {"status": 204}),
108 ]
109
110 args = {
111 "name": name,
112 "url": "https://rundeck.example.org",
113 "api_token": "mytoken",
114 "state": "absent",
115 }
116 if project:
117 args["project"] = project
118
119 with pytest.raises(AnsibleExitJson):
120 with set_module_args(args):
121 rundeck_acl_policy.main()
122
123 # GET → DELETE
124 assert api_request_mock.call_count == 2
125 args, kwargs = api_request_mock.call_args_list[1]
126 assert kwargs["endpoint"] == f"{prefix}/{name}.aclpolicy"
127 assert kwargs["method"] == "DELETE"
128
129
130 @pytest.mark.parametrize("project, prefix", PROJECT_TABLE)
131 @patch.object(rundeck_acl_policy, "api_request")
132 def test_acl_remove_nonexistent(api_request_mock, project, prefix):
133 """Test removing a non-existent ACL results in no change."""
134 name = "not_there"
135 # GET returns 404
136 api_request_mock.return_value = (None, {"status": 404})
137
138 args = {
139 "name": name,
140 "url": "https://rundeck.example.org",
141 "api_token": "mytoken",
142 "state": "absent",
143 }
144 if project:
145 args["project"] = project
146
147 with pytest.raises(AnsibleExitJson):
148 with set_module_args(args):
149 rundeck_acl_policy.main()
150
151 # only the initial GET
152 assert api_request_mock.call_count == 1
153 args, kwargs = api_request_mock.call_args
154 assert kwargs["endpoint"] == f"{prefix}/{name}.aclpolicy"
155 assert kwargs.get("method", "GET") == "GET"