Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_alerta_customer.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 import json
8 from unittest.mock import patch
9
10 import pytest
11 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
12 AnsibleExitJson,
13 AnsibleFailJson,
14 ModuleTestCase,
15 set_module_args,
16 )
17
18 from ansible_collections.community.general.plugins.modules import alerta_customer
19
20
21 class MockedReponse:
22 def __init__(self, data):
23 self.data = data
24
25 def read(self):
26 return self.data
27
28
29 def customer_response_page1():
30 server_response = json.dumps(
31 {
32 "customers": [
33 {
34 "customer": "admin",
35 "href": "http://localhost:8080/api/customer/d89664a7-9c87-4ab9-8be8-830e7e5f0616",
36 "id": "d89664a7-9c87-4ab9-8be8-830e7e5f0616",
37 "match": "[email protected]",
38 },
39 {
40 "customer": "Developer",
41 "href": "http://localhost:8080/api/customer/188ed093-84cc-4f46-bf80-4c9127180d9c",
42 "id": "188ed093-84cc-4f46-bf80-4c9127180d9c",
43 "match": "[email protected]",
44 },
45 ],
46 "more": True,
47 "page": 1,
48 "pageSize": 50,
49 "pages": 1,
50 "status": "ok",
51 "total": 2,
52 }
53 )
54 return (MockedReponse(server_response), {"status": 200})
55
56
57 def customer_response_page2():
58 server_response = json.dumps(
59 {
60 "customers": [
61 {
62 "customer": "admin",
63 "href": "http://localhost:8080/api/customer/d89664a7-9c87-4ab9-8be8-830e7e5f0616",
64 "id": "d89664a7-9c87-4ab9-8be8-830e7e5f0616",
65 "match": "[email protected]",
66 },
67 {
68 "customer": "Developer",
69 "href": "http://localhost:8080/api/customer/188ed093-84cc-4f46-bf80-4c9127180d9c",
70 "id": "188ed093-84cc-4f46-bf80-4c9127180d9c",
71 "match": "[email protected]",
72 },
73 ],
74 "more": True,
75 "page": 2,
76 "pageSize": 50,
77 "pages": 2,
78 "status": "ok",
79 "total": 52,
80 }
81 )
82 return (MockedReponse(server_response), {"status": 200})
83
84
85 class TestAlertaCustomerModule(ModuleTestCase):
86 def setUp(self):
87 super().setUp()
88 self.module = alerta_customer
89
90 def tearDown(self):
91 super().tearDown()
92
93 @pytest.fixture
94 def fetch_url_mock(self, mocker):
95 return mocker.patch()
96
97 def test_without_parameters(self):
98 """Failure if no parameters set"""
99 with self.assertRaises(AnsibleFailJson):
100 with set_module_args({}):
101 self.module.main()
102
103 def test_without_content(self):
104 """Failure if customer and match are missing"""
105 with set_module_args(
106 {"alerta_url": "http://localhost:8080", "api_username": "[email protected]", "api_password": "password"}
107 ):
108 with self.assertRaises(AnsibleFailJson):
109 self.module.main()
110
111 def test_successful_existing_customer_creation(self):
112 """Test the customer creation (already exists)."""
113 with set_module_args(
114 {
115 "alerta_url": "http://localhost:8080",
116 "api_username": "[email protected]",
117 "api_password": "password",
118 "customer": "Developer",
119 "match": "[email protected]",
120 }
121 ):
122 with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
123 fetch_url_mock.return_value = customer_response_page1()
124 with self.assertRaises(AnsibleExitJson):
125 self.module.main()
126 self.assertTrue(fetch_url_mock.call_count, 1)
127
128 def test_successful_customer_creation(self):
129 """Test the customer creation."""
130 with set_module_args(
131 {
132 "alerta_url": "http://localhost:8080",
133 "api_username": "[email protected]",
134 "api_password": "password",
135 "customer": "Developer",
136 "match": "[email protected]",
137 }
138 ):
139 with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
140 fetch_url_mock.return_value = customer_response_page1()
141 with self.assertRaises(AnsibleExitJson):
142 self.module.main()
143
144 self.assertTrue(fetch_url_mock.call_count, 1)
145 call_data = json.loads(fetch_url_mock.call_args[1]["data"])
146 assert call_data["match"] == "[email protected]"
147 assert call_data["customer"] == "Developer"
148
149 def test_successful_customer_creation_key(self):
150 """Test the customer creation using api_key."""
151 with set_module_args(
152 {
153 "alerta_url": "http://localhost:8080",
154 "api_key": "demo-key",
155 "customer": "Developer",
156 "match": "[email protected]",
157 }
158 ):
159 with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
160 fetch_url_mock.return_value = customer_response_page1()
161 with self.assertRaises(AnsibleExitJson):
162 self.module.main()
163
164 self.assertTrue(fetch_url_mock.call_count, 1)
165 call_data = json.loads(fetch_url_mock.call_args[1]["data"])
166 assert call_data["match"] == "[email protected]"
167 assert call_data["customer"] == "Developer"
168
169 def test_failed_not_found(self):
170 """Test failure with wrong URL."""
171
172 with set_module_args(
173 {
174 "alerta_url": "http://localhost:8080/s",
175 "api_username": "[email protected]",
176 "api_password": "password",
177 "customer": "Developer",
178 "match": "[email protected]",
179 }
180 ):
181 with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
182 fetch_url_mock.return_value = (
183 None,
184 {"status": 404, "msg": "Not found for request GET on http://localhost:8080/a/api/customers"},
185 )
186 with self.assertRaises(AnsibleFailJson):
187 self.module.main()
188
189 def test_failed_forbidden(self):
190 """Test failure with wrong user."""
191
192 with set_module_args(
193 {
194 "alerta_url": "http://localhost:8080",
195 "api_username": "[email protected]",
196 "api_password": "password",
197 "customer": "Developer",
198 "match": "[email protected]",
199 }
200 ):
201 with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
202 fetch_url_mock.return_value = (
203 None,
204 {"status": 403, "msg": "Permission Denied for GET on http://localhost:8080/api/customers"},
205 )
206 with self.assertRaises(AnsibleFailJson):
207 self.module.main()
208
209 def test_failed_unauthorized(self):
210 """Test failure with wrong username or password."""
211
212 with set_module_args(
213 {
214 "alerta_url": "http://localhost:8080",
215 "api_username": "[email protected]",
216 "api_password": "password_wrong",
217 "customer": "Developer",
218 "match": "[email protected]",
219 }
220 ):
221 with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
222 fetch_url_mock.return_value = (
223 None,
224 {"status": 401, "msg": "Unauthorized to request GET on http://localhost:8080/api/customers"},
225 )
226 with self.assertRaises(AnsibleFailJson):
227 self.module.main()
228
229 def test_successful_customer_deletion(self):
230 """Test the customer deletion."""
231
232 with set_module_args(
233 {
234 "alerta_url": "http://localhost:8080",
235 "api_username": "[email protected]",
236 "api_password": "password",
237 "customer": "Developer",
238 "match": "[email protected]",
239 "state": "absent",
240 }
241 ):
242 with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
243 fetch_url_mock.return_value = customer_response_page1()
244 with self.assertRaises(AnsibleExitJson):
245 self.module.main()
246
247 def test_successful_customer_deletion_page2(self):
248 """Test the customer deletion on the second page."""
249
250 with set_module_args(
251 {
252 "alerta_url": "http://localhost:8080",
253 "api_username": "[email protected]",
254 "api_password": "password",
255 "customer": "Developer",
256 "match": "[email protected]",
257 "state": "absent",
258 }
259 ):
260 with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
261 fetch_url_mock.return_value = customer_response_page2()
262 with self.assertRaises(AnsibleExitJson):
263 self.module.main()
264
265 def test_successful_nonexisting_customer_deletion(self):
266 """Test the customer deletion (non existing)."""
267
268 with set_module_args(
269 {
270 "alerta_url": "http://localhost:8080",
271 "api_username": "[email protected]",
272 "api_password": "password",
273 "customer": "Billing",
274 "match": "[email protected]",
275 "state": "absent",
276 }
277 ):
278 with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
279 fetch_url_mock.return_value = customer_response_page1()
280 with self.assertRaises(AnsibleExitJson):
281 self.module.main()