Debian Codegraph @

ansible_collections/community/clickhouse/tests/unit/plugins/modules/test_clickhouse_grants.py

raw
1 from __future__ import (absolute_import, division, print_function)
2
3 __metaclass__ = type
4
5 import pytest
6 from unittest.mock import MagicMock, patch
7
8 from ansible_collections.community.clickhouse.plugins.modules.clickhouse_grants import (
9 ClickHouseGrants,
10 GRANT_REGEX,
11 )
12
13
14 class TestGrantRegex:
15 """Test the GRANT statement regex pattern"""
16
17 @pytest.mark.parametrize(
18 'grant_statement,expected',
19 [
20 ('GRANT SELECT ON foo.* TO alice',
21 ('SELECT', 'foo.*', None)),
22 ('GRANT SELECT, INSERT ON foo.* TO alice',
23 ('SELECT, INSERT', 'foo.*', None)),
24 ('GRANT SELECT ON foo.* TO alice WITH GRANT OPTION',
25 ('SELECT', 'foo.*', ' WITH GRANT OPTION')),
26 ('GRANT CREATE USER ON *.* TO alice',
27 ('CREATE USER', '*.*', None)),
28 ('GRANT ALTER DELETE ON db.table TO bob WITH GRANT OPTION',
29 ('ALTER DELETE', 'db.table', ' WITH GRANT OPTION')),
30 ('GRANT SELECT(x, y) ON foo.test_table TO carol',
31 ('SELECT(x, y)', 'foo.test_table', None)),
32 ('GRANT SELECT ON foo.* TO alice WITH GRANT OPTION ON CLUSTER test_cluster',
33 ('SELECT', 'foo.*', ' WITH GRANT OPTION')),
34 ('GRANT SELECT ON foo.* TO alice ON CLUSTER test_cluster',
35 ('SELECT', 'foo.*', None)),
36 ('GRANT SELECT ON foo.* TO alice-with-dash',
37 ('SELECT', 'foo.*', None)),
38 ]
39 )
40 def test_grant_regex_match(self, grant_statement, expected):
41 """Test that GRANT_REGEX correctly parses various grant statements"""
42 match = GRANT_REGEX.match(grant_statement)
43 assert match is not None
44 assert match.groups() == expected
45
46 @pytest.mark.parametrize(
47 'invalid_statement',
48 [
49 'INVALID GRANT STATEMENT',
50 'SELECT * FROM table',
51 'REVOKE SELECT ON foo.* FROM alice',
52 '',
53 ]
54 )
55 def test_grant_regex_no_match(self, invalid_statement):
56 """Test that GRANT_REGEX doesn't match invalid statements"""
57 match = GRANT_REGEX.match(invalid_statement)
58 assert match is None
59
60
61 class TestClickHouseGrantsGet:
62 """Test the get() method that parses SHOW GRANTS output"""
63
64 def setup_method(self):
65 """Set up mock module and client for each test"""
66 self.mock_module = MagicMock()
67 self.mock_module.params = {'login_user': 'default'}
68 self.mock_client = MagicMock()
69
70 @patch('ansible_collections.community.clickhouse.plugins.modules.clickhouse_grants.execute_query')
71 def test_get_simple_grants(self, mock_execute):
72 """Test parsing simple grant statements"""
73 mock_execute.return_value = [
74 ('GRANT SELECT ON foo.* TO alice',),
75 ('GRANT INSERT ON foo.* TO alice',),
76 ]
77
78 grants_obj = ClickHouseGrants(self.mock_module, self.mock_client, 'alice')
79 # Override the grantee_exists check
80 grants_obj.grantee_exists = True
81
82 result = grants_obj.get()
83
84 assert result == {
85 'foo.*': {
86 'SELECT': False,
87 'INSERT': False,
88 }
89 }
90
91 @patch('ansible_collections.community.clickhouse.plugins.modules.clickhouse_grants.execute_query')
92 def test_get_grants_with_grant_option(self, mock_execute):
93 """Test parsing grants with GRANT OPTION"""
94 mock_execute.return_value = [
95 ('GRANT SELECT ON foo.* TO alice WITH GRANT OPTION',),
96 ('GRANT INSERT ON foo.* TO alice',),
97 ]
98
99 grants_obj = ClickHouseGrants(self.mock_module, self.mock_client, 'alice')
100 grants_obj.grantee_exists = True
101
102 result = grants_obj.get()
103
104 assert result == {
105 'foo.*': {
106 'SELECT': True, # WITH GRANT OPTION
107 'INSERT': False, # Without GRANT OPTION
108 }
109 }
110
111 @patch('ansible_collections.community.clickhouse.plugins.modules.clickhouse_grants.execute_query')
112 def test_get_multiple_objects(self, mock_execute):
113 """Test parsing grants on multiple database objects"""
114 mock_execute.return_value = [
115 ('GRANT SELECT ON foo.* TO alice',),
116 ('GRANT INSERT ON bar.* TO alice',),
117 ('GRANT CREATE USER ON *.* TO alice',),
118 ]
119
120 grants_obj = ClickHouseGrants(self.mock_module, self.mock_client, 'alice')
121 grants_obj.grantee_exists = True
122
123 result = grants_obj.get()
124
125 assert result == {
126 'foo.*': {'SELECT': False},
127 'bar.*': {'INSERT': False},
128 '*.*': {'CREATE USER': False},
129 }
130
131 @patch('ansible_collections.community.clickhouse.plugins.modules.clickhouse_grants.execute_query')
132 def test_get_multiple_privileges_in_one_statement(self, mock_execute):
133 """Test parsing multiple privileges in a single GRANT statement"""
134 mock_execute.return_value = [
135 ('GRANT SELECT, INSERT, DELETE ON foo.* TO alice WITH GRANT OPTION',),
136 ]
137
138 grants_obj = ClickHouseGrants(self.mock_module, self.mock_client, 'alice')
139 grants_obj.grantee_exists = True
140
141 result = grants_obj.get()
142
143 assert result == {
144 'foo.*': {
145 'SELECT': True,
146 'INSERT': True,
147 'DELETE': True,
148 }
149 }
150
151 @patch('ansible_collections.community.clickhouse.plugins.modules.clickhouse_grants.execute_query')
152 def test_get_column_level_privileges(self, mock_execute):
153 """Test parsing column-level privileges"""
154 mock_execute.return_value = [
155 ('GRANT SELECT(x) ON foo.test_table TO alice',),
156 ]
157
158 grants_obj = ClickHouseGrants(self.mock_module, self.mock_client, 'alice')
159 grants_obj.grantee_exists = True
160
161 result = grants_obj.get()
162
163 assert result == {
164 'foo.test_table': {
165 'SELECT(X)': False, # Note: uppercase conversion
166 }
167 }
168
169 @patch('ansible_collections.community.clickhouse.plugins.modules.clickhouse_grants.execute_query')
170 def test_get_normalizes_global_object(self, mock_execute):
171 """Test that '*' is normalized to '*.*' for ClickHouse 25.x compatibility"""
172 mock_execute.return_value = [
173 ('GRANT CREATE USER ON * TO alice',), # ClickHouse 25.x format
174 ]
175
176 grants_obj = ClickHouseGrants(self.mock_module, self.mock_client, 'alice')
177 grants_obj.grantee_exists = True
178
179 result = grants_obj.get()
180
181 # Should normalize '*' to '*.*'
182 assert result == {
183 '*.*': {'CREATE USER': False},
184 }
185 # Should NOT have '*' as key
186 assert '*' not in result
187
188 @patch('ansible_collections.community.clickhouse.plugins.modules.clickhouse_grants.execute_query')
189 def test_get_empty_grants(self, mock_execute):
190 """Test parsing when no grants exist"""
191 mock_execute.return_value = []
192
193 grants_obj = ClickHouseGrants(self.mock_module, self.mock_client, 'alice')
194 grants_obj.grantee_exists = True
195
196 result = grants_obj.get()
197
198 assert result == {}
199
200 @patch('ansible_collections.community.clickhouse.plugins.modules.clickhouse_grants.execute_query')
201 def test_get_grants_with_cluster_setting(self, mock_execute):
202 """Test parsing grants with ON CLUSTER"""
203 mock_execute.return_value = [
204 ('GRANT SELECT ON foo.* TO alice WITH GRANT OPTION ON CLUSTER test_cluster',),
205 ('GRANT INSERT ON foo.* TO alice ON CLUSTER test_cluster',),
206 ]
207
208 grants_obj = ClickHouseGrants(self.mock_module, self.mock_client, 'alice', 'test_cluster')
209 grants_obj.grantee_exists = True
210
211 result = grants_obj.get()
212
213 assert result == {
214 'foo.*': {
215 'SELECT': True, # WITH GRANT OPTION
216 'INSERT': False, # Without GRANT OPTION
217 }
218 }
219
220 @patch('ansible_collections.community.clickhouse.plugins.modules.clickhouse_grants.execute_query')
221 def test_get_multiple_objects(self, mock_execute):
222 """Test parsing grants with dashes in grantee name"""
223 mock_execute.return_value = [
224 ('GRANT SELECT ON foo.* TO alice-with-dash',),
225 ('GRANT INSERT ON bar.* TO alice-with-dash',),
226 ('GRANT CREATE USER ON *.* TO alice-with-dash',),
227 ]
228
229 grants_obj = ClickHouseGrants(self.mock_module, self.mock_client, 'alice-with-dash')
230 grants_obj.grantee_exists = True
231
232 result = grants_obj.get()
233
234 assert result == {
235 'foo.*': {'SELECT': False},
236 'bar.*': {'INSERT': False},
237 '*.*': {'CREATE USER': False},
238 }
239
240
241 class TestClickHouseGrantsGetDesiredGrants:
242 """Test the _get_desired_grants() method"""
243
244 def setup_method(self):
245 """Set up mock module and client for each test"""
246 self.mock_module = MagicMock()
247 self.mock_client = MagicMock()
248
249 @patch('ansible_collections.community.clickhouse.plugins.modules.clickhouse_grants.execute_query')
250 def test_get_desired_grants_simple(self, mock_execute):
251 """Test converting privileges parameter to desired grants format"""
252 self.mock_module.params = {
253 'login_user': 'default',
254 'privileges': [
255 {
256 'object': 'foo.*',
257 'privs': {
258 'SELECT': True,
259 'INSERT': False,
260 }
261 }
262 ]
263 }
264
265 mock_execute.return_value = [('1',)] # Mock user exists check
266 grants_obj = ClickHouseGrants(self.mock_module, self.mock_client, 'alice')
267
268 result = grants_obj._get_desired_grants()
269
270 assert result == {
271 'foo.*': {
272 'SELECT': True,
273 'INSERT': False,
274 }
275 }
276
277 @patch('ansible_collections.community.clickhouse.plugins.modules.clickhouse_grants.execute_query')
278 def test_get_desired_grants_multiple_objects(self, mock_execute):
279 """Test multiple database objects in privileges"""
280 self.mock_module.params = {
281 'login_user': 'default',
282 'privileges': [
283 {
284 'object': 'foo.*',
285 'privs': {'SELECT': True}
286 },
287 {
288 'object': 'bar.*',
289 'privs': {'INSERT': False}
290 }
291 ]
292 }
293
294 mock_execute.return_value = [('1',)]
295 grants_obj = ClickHouseGrants(self.mock_module, self.mock_client, 'alice')
296
297 result = grants_obj._get_desired_grants()
298
299 assert result == {
300 'foo.*': {'SELECT': True},
301 'bar.*': {'INSERT': False},
302 }
303
304 @patch('ansible_collections.community.clickhouse.plugins.modules.clickhouse_grants.execute_query')
305 def test_get_desired_grants_with_grant_option_override(self, mock_execute):
306 """Test grant_option parameter overrides individual privilege settings"""
307 self.mock_module.params = {
308 'login_user': 'default',
309 'privileges': [
310 {
311 'object': 'foo.*',
312 'grant_option': True, # Override
313 'privs': {
314 'SELECT': False, # Should be overridden to True
315 'INSERT': False, # Should be overridden to True
316 }
317 }
318 ]
319 }
320
321 mock_execute.return_value = [('1',)]
322 grants_obj = ClickHouseGrants(self.mock_module, self.mock_client, 'alice')
323
324 result = grants_obj._get_desired_grants()
325
326 assert result == {
327 'foo.*': {
328 'SELECT': True, # Overridden by grant_option
329 'INSERT': True, # Overridden by grant_option
330 }
331 }
332
333 @patch('ansible_collections.community.clickhouse.plugins.modules.clickhouse_grants.execute_query')
334 def test_get_desired_grants_privilege_name_uppercase(self, mock_execute):
335 """Test that privilege names are converted to uppercase"""
336 self.mock_module.params = {
337 'login_user': 'default',
338 'privileges': [
339 {
340 'object': 'foo.*',
341 'privs': {
342 'select': True, # lowercase
343 'InSeRt': False, # mixed case
344 }
345 }
346 ]
347 }
348
349 mock_execute.return_value = [('1',)]
350 grants_obj = ClickHouseGrants(self.mock_module, self.mock_client, 'alice')
351
352 result = grants_obj._get_desired_grants()
353
354 assert result == {
355 'foo.*': {
356 'SELECT': True, # Uppercase
357 'INSERT': False, # Uppercase
358 }
359 }
360
361 @patch('ansible_collections.community.clickhouse.plugins.modules.clickhouse_grants.execute_query')
362 def test_get_desired_grants_empty_privileges(self, mock_execute):
363 """Test with empty privileges list"""
364 self.mock_module.params = {
365 'login_user': 'default',
366 'privileges': None
367 }
368
369 mock_execute.return_value = [('1',)]
370 grants_obj = ClickHouseGrants(self.mock_module, self.mock_client, 'alice')
371
372 result = grants_obj._get_desired_grants()
373
374 assert result == {}