| 1 |
from __future__ import absolute_import, division, print_function |
| 2 |
import pytest |
| 3 |
from ansible_collections.community.clickhouse.plugins.modules.clickhouse_user import ClickHouseUser |
| 4 |
|
| 5 |
|
| 6 |
@pytest.fixture |
| 7 |
def user(mocker): |
| 8 |
mocker.patch("ansible_collections.community.clickhouse.plugins.modules.clickhouse_user.execute_query", return_value=[]) |
| 9 |
|
| 10 |
mock_module = mocker.MagicMock() |
| 11 |
mock_module.check_mode = False |
| 12 |
mock_client = mocker.MagicMock() |
| 13 |
|
| 14 |
return ClickHouseUser(module=mock_module, client=mock_client, name="alice") |
| 15 |
|
| 16 |
|
| 17 |
@pytest.fixture |
| 18 |
def mock_execute(mocker): |
| 19 |
return mocker.patch("ansible_collections.community.clickhouse.plugins.modules.clickhouse_user.execute_query", return_value=[]) |
| 20 |
|
| 21 |
|
| 22 |
def get_executed_query(mock_execute): |
| 23 |
return mock_execute.call_args[0][2] |
| 24 |
|
| 25 |
|
| 26 |
def test_user_not_exists(user): |
| 27 |
assert user.user_exists is False |
| 28 |
|
| 29 |
|
| 30 |
def test_create_ldap_user(user, mock_execute): |
| 31 |
user.create( |
| 32 |
None, None, None, None, None, None, |
| 33 |
'listed_only', None, 'listed_only', |
| 34 |
{'type': 'ldap', 'server': 'ad'}) |
| 35 |
actual_query = get_executed_query(mock_execute) |
| 36 |
assert actual_query == "CREATE USER 'alice' IDENTIFIED WITH ldap SERVER 'ad'" |
| 37 |
|
| 38 |
|
| 39 |
def test_create_ldap_user_without_server(user, mock_execute): |
| 40 |
user.create( |
| 41 |
None, None, None, None, None, None, |
| 42 |
'listed_only', None, 'listed_only', |
| 43 |
{'type': 'ldap'}) |
| 44 |
user.module.fail_json.assert_called_once() |
| 45 |
|
| 46 |
|
| 47 |
def test_create_sha256_pass_user(user, mock_execute): |
| 48 |
user.create( |
| 49 |
None, None, None, None, None, None, |
| 50 |
'listed_only', None, 'listed_only', |
| 51 |
{'type': 'sha256_password', 'password': 'password1'}) |
| 52 |
actual_query = get_executed_query(mock_execute) |
| 53 |
assert actual_query == "CREATE USER 'alice' IDENTIFIED WITH sha256_password BY 'password1'" |
| 54 |
|
| 55 |
|
| 56 |
def test_create_sha256_hash_user(user, mock_execute): |
| 57 |
user.create( |
| 58 |
None, None, None, None, None, None, |
| 59 |
'listed_only', None, 'listed_only', |
| 60 |
{'type': 'sha256_hash', 'password': '0b14d501a594442a01c6859541bcb3e8164d183d32937b851835442f69d5c94e'}) |
| 61 |
actual_query = get_executed_query(mock_execute) |
| 62 |
assert actual_query == "CREATE USER 'alice' IDENTIFIED WITH sha256_hash BY '0b14d501a594442a01c6859541bcb3e8164d183d32937b851835442f69d5c94e'" |
| 63 |
|
| 64 |
|
| 65 |
def test_create_sha256_hash_user_old_way(user, mock_execute): |
| 66 |
user.create( |
| 67 |
'sha256_hash', '0b14d501a594442a01c6859541bcb3e8164d183d32937b851835442f69d5c94e', |
| 68 |
None, [], None, None, 'listed_only', None, 'listed_only', None) |
| 69 |
actual_query = get_executed_query(mock_execute) |
| 70 |
assert actual_query == "CREATE USER 'alice' IDENTIFIED WITH sha256_hash BY '0b14d501a594442a01c6859541bcb3e8164d183d32937b851835442f69d5c94e'" |
| 71 |
|
| 72 |
|
| 73 |
def test_create_sha256_pass_user_old_way(user, mock_execute): |
| 74 |
user.create( |
| 75 |
'sha256_password', 'password1', |
| 76 |
None, None, None, None, 'listed_only', None, 'listed_only', None) |
| 77 |
actual_query = get_executed_query(mock_execute) |
| 78 |
assert actual_query == "CREATE USER 'alice' IDENTIFIED WITH sha256_password BY 'password1'" |
| 79 |
|
| 80 |
|
| 81 |
def test_create_not_identified_user(user, mock_execute): |
| 82 |
user.create( |
| 83 |
None, None, None, None, None, None, |
| 84 |
'listed_only', None, 'listed_only', |
| 85 |
{'type': 'not_identified'}) |
| 86 |
actual_query = get_executed_query(mock_execute) |
| 87 |
assert actual_query == "CREATE USER 'alice' NOT IDENTIFIED" |
| 88 |
|
| 89 |
|
| 90 |
def test_create_no_password_user(user, mock_execute): |
| 91 |
user.create( |
| 92 |
None, None, None, None, None, None, |
| 93 |
'listed_only', None, 'listed_only', |
| 94 |
{'type': 'no_password'}) |
| 95 |
actual_query = get_executed_query(mock_execute) |
| 96 |
assert actual_query == "CREATE USER 'alice' IDENTIFIED WITH NO_PASSWORD" |
| 97 |
|
| 98 |
|
| 99 |
def test_create_user_with_settings(user, mock_execute): |
| 100 |
user.create( |
| 101 |
None, None, None, None, |
| 102 |
["max_concurrent_queries=3", "max_threads=8"], |
| 103 |
None, 'listed_only', None, 'listed_only', |
| 104 |
{'type': 'sha256_password', 'password': 'password1'}) |
| 105 |
actual_query = get_executed_query(mock_execute) |
| 106 |
assert actual_query == "CREATE USER 'alice' IDENTIFIED WITH sha256_password BY 'password1' SETTINGS max_concurrent_queries=3, max_threads=8" |
| 107 |
|
| 108 |
|
| 109 |
def test_create_user_with_host_name_passed(user, mock_execute): |
| 110 |
user.create( |
| 111 |
None, None, None, |
| 112 |
[{'type': 'NAME', 'hosts': ['host1']}], |
| 113 |
None, None, 'listed_only', |
| 114 |
None, 'listed_only', |
| 115 |
{'type': 'sha256_password', 'password': 'password1'}) |
| 116 |
actual_query = get_executed_query(mock_execute) |
| 117 |
assert actual_query == "CREATE USER 'alice' IDENTIFIED WITH sha256_password BY 'password1' HOST NAME 'host1'" |
| 118 |
|
| 119 |
|
| 120 |
def test_create_user_with_host_ip_passed(user, mock_execute): |
| 121 |
user.create( |
| 122 |
None, None, None, |
| 123 |
[{'type': 'IP', 'hosts': ['127.0.0.1']}], |
| 124 |
None, None, |
| 125 |
'listed_only', None, 'listed_only', |
| 126 |
{'type': 'sha256_password', 'password': 'password1'}) |
| 127 |
actual_query = get_executed_query(mock_execute) |
| 128 |
assert actual_query == "CREATE USER 'alice' IDENTIFIED WITH sha256_password BY 'password1' HOST IP '127.0.0.1'" |
| 129 |
|
| 130 |
|
| 131 |
def test_create_user_with_host_cidr_passed(user, mock_execute): |
| 132 |
user.create( |
| 133 |
None, None, None, |
| 134 |
[{'type': 'IP', 'hosts': ['10.0.0.0/8']}], |
| 135 |
None, None, |
| 136 |
'listed_only', None, |
| 137 |
'listed_only', {'type': 'sha256_password', 'password': 'password1'}) |
| 138 |
actual_query = get_executed_query(mock_execute) |
| 139 |
assert actual_query == "CREATE USER 'alice' IDENTIFIED WITH sha256_password BY 'password1' HOST IP '10.0.0.0/8'" |
| 140 |
|
| 141 |
|
| 142 |
def test_create_user_with_host_complex_passed(user, mock_execute): |
| 143 |
user.create( |
| 144 |
None, None, None, |
| 145 |
[{'type': 'IP', 'hosts': ['10.0.0.0/8']}, {'type': 'NAME', 'hosts': ['host1']}], |
| 146 |
None, None, |
| 147 |
'listed_only', None, 'listed_only', |
| 148 |
{'type': 'sha256_password', 'password': 'password1'}) |
| 149 |
actual_query = get_executed_query(mock_execute) |
| 150 |
assert actual_query == "CREATE USER 'alice' IDENTIFIED WITH sha256_password BY 'password1' HOST IP '10.0.0.0/8' HOST NAME 'host1'" |