Debian Codegraph @

ansible_collections/community/postgresql/tests/unit/plugins/modules/test_postgresql_user.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright: Ansible Project
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7
8 __metaclass__ = type
9
10 import sys
11
12 import pytest
13
14 if sys.version_info[0] == 3:
15 from plugins.modules.postgresql_user import parse_user_configuration, compare_user_configurations, _pg_quote_user
16 elif sys.version_info[0] == 2:
17 from ansible_collections.community.postgresql.plugins.modules.postgresql_user import parse_user_configuration, \
18 compare_user_configurations, _pg_quote_user
19
20
21 def test_parse_user_configuration(mocker):
22 """Tests if correct inputs return the expected results"""
23 module = mocker.MagicMock()
24 test_input = ["some_setting=some_value", "another.setting=20MB", "nested=something=null"]
25 expected = {"some_setting": "some_value", "another.setting": "20MB", "nested": "something=null"}
26 result = parse_user_configuration(module, test_input)
27 assert result == expected
28 assert parse_user_configuration(module, None) == {}
29
30
31 def test_parse_user_config_incorrect_input(mocker):
32 """Tests if incorrect input is handled properly"""
33 module = mocker.MagicMock()
34 faulty_input = ["key=value", "incorrect"]
35 parse_user_configuration(module, faulty_input)
36 module.fail_json.assert_called_once_with(
37 msg="Expecting a list of strings where each string has the format 'key=value'.")
38
39
40 def test_compare_user_configurations():
41 """Tests if the correct update-path is created from input"""
42 # for some reason this one fails in Python 2
43 if sys.version_info[0] == 2:
44 return
45
46 desired = {"some_setting": "some_value", "another_setting": "different_value"}
47 current = {"some_setting": "some_value", "another_setting": "DIFFERENT_VALUE", "ghost_setting": "no_value"}
48 expected = {"reset": ["ghost_setting"],
49 "update": {"another_setting": "different_value"}
50 }
51 output = compare_user_configurations(current, desired, True)
52 assert output == expected
53 output = compare_user_configurations(current, {}, True)
54 # TODO the last for lines of the following assertion were added because the assertion fails
55 # when testing against stable-2.15 probably due to some specific Python version used.
56 # Remove them when stable-2.15 gets EOL in November 2024.
57 assert output == {"reset": ["some_setting", "another_setting", "ghost_setting"], "update": {}} or \
58 output == {'reset': ['another_setting', 'some_setting', 'ghost_setting'], 'update': {}} or \
59 output == {'reset': ['some_setting', 'ghost_setting', 'another_setting'], 'update': {}} or \
60 output == {'reset': ['ghost_setting', 'some_setting', 'another_setting'], 'update': {}} or \
61 output == {'reset': ['ghost_setting', 'another_setting', 'some_setting'], 'update': {}} or \
62 output == {'reset': ['another_setting', 'ghost_setting', 'some_setting'], 'update': {}}
63 output = compare_user_configurations({}, desired, True)
64 assert output == {"reset": [], "update": desired}
65 output = compare_user_configurations(current, desired, False)
66 no_reset_expected = {"reset": [],
67 "update": {"another_setting": "different_value"}
68 }
69 assert output == no_reset_expected
70 output = compare_user_configurations(current, {}, False)
71 assert output == {"reset": [], "update": {}}
72
73
74 def test__pg_quote_user(mocker):
75 """Tests if quoting users works correctly"""
76 module = mocker.MagicMock()
77 output = _pg_quote_user('someuser', module)
78 assert output == '"someuser"'
79 output = _pg_quote_user('"someuser"', module)
80 assert output == '"someuser"'
81 output = _pg_quote_user('some.user.with.dots', module)
82 assert output == '"some.user.with.dots"'
83 output = _pg_quote_user('some.user.with\"\"quotes', module)
84 assert output == '"some.user.with\"\"quotes"'
85 _pg_quote_user('someuser"', module)
86 module.fail_json.assert_called_once_with("The value of the user-field can't contain a double-quote in the end "
87 "if it doesn't start with one and vice-versa.")
88 module = mocker.MagicMock()
89 _pg_quote_user('"someuser', module)
90 module.fail_json.assert_called_once_with("The value of the user-field can't contain a double-quote in the end "
91 "if it doesn't start with one and vice-versa.")
92 module = mocker.MagicMock()
93 with pytest.raises(Exception, match='User escaped identifiers must escape extra quotes'):
94 _pg_quote_user('some.user.with\"illegal.quotes', module)