| 1 |
# Copyright (c) 2019, Guillaume Martinez ([email protected]) |
| 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 pytest |
| 8 |
|
| 9 |
from ansible_collections.community.general.plugins.modules.gitlab_user import GitLabUser |
| 10 |
|
| 11 |
|
| 12 |
def _dummy(x): |
| 13 |
"""Dummy function. Only used as a placeholder for toplevel definitions when the test is going |
| 14 |
to be skipped anyway""" |
| 15 |
return x |
| 16 |
|
| 17 |
|
| 18 |
pytestmark = [] |
| 19 |
try: |
| 20 |
# GitLab module requirements |
| 21 |
from gitlab.v4.objects import User |
| 22 |
|
| 23 |
from .gitlab import ( |
| 24 |
GitlabModuleTestCase, |
| 25 |
created_user_keys, |
| 26 |
deleted_user_key_ids, |
| 27 |
resp_add_member, |
| 28 |
resp_create_user, |
| 29 |
resp_create_user_keys, |
| 30 |
resp_delete_user, |
| 31 |
resp_delete_user_key, |
| 32 |
resp_find_user, |
| 33 |
resp_get_group, |
| 34 |
resp_get_member, |
| 35 |
resp_get_user, |
| 36 |
resp_get_user_duplicate_keys, |
| 37 |
resp_get_user_keys, |
| 38 |
resp_update_member, |
| 39 |
) |
| 40 |
except ImportError: |
| 41 |
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing")) |
| 42 |
# Need to set these to something so that we don't fail when parsing |
| 43 |
GitlabModuleTestCase = object # type: ignore |
| 44 |
created_user_keys = [] |
| 45 |
deleted_user_key_ids = [] |
| 46 |
resp_find_user = _dummy |
| 47 |
resp_get_user = _dummy |
| 48 |
resp_get_user_keys = _dummy |
| 49 |
resp_create_user_keys = _dummy |
| 50 |
resp_delete_user_key = _dummy |
| 51 |
resp_create_user = _dummy |
| 52 |
resp_delete_user = _dummy |
| 53 |
resp_get_user_duplicate_keys = _dummy |
| 54 |
resp_get_member = _dummy |
| 55 |
resp_get_group = _dummy |
| 56 |
resp_add_member = _dummy |
| 57 |
resp_update_member = _dummy |
| 58 |
resp_get_member = _dummy |
| 59 |
|
| 60 |
# Unit tests requirements |
| 61 |
try: |
| 62 |
from httmock import with_httmock # noqa |
| 63 |
except ImportError: |
| 64 |
pytestmark.append(pytest.mark.skip("Could not load httmock module required for testing")) |
| 65 |
with_httmock = _dummy |
| 66 |
|
| 67 |
|
| 68 |
class TestGitlabUser(GitlabModuleTestCase): |
| 69 |
def setUp(self): |
| 70 |
super().setUp() |
| 71 |
|
| 72 |
self.moduleUtil = GitLabUser(module=self.mock_module, gitlab_instance=self.gitlab_instance) |
| 73 |
|
| 74 |
@with_httmock(resp_find_user) |
| 75 |
def test_exist_user(self): |
| 76 |
rvalue = self.moduleUtil.exists_user("john_smith") |
| 77 |
|
| 78 |
self.assertEqual(rvalue, True) |
| 79 |
|
| 80 |
rvalue = self.moduleUtil.exists_user("paul_smith") |
| 81 |
|
| 82 |
self.assertEqual(rvalue, False) |
| 83 |
|
| 84 |
@with_httmock(resp_find_user) |
| 85 |
def test_find_user(self): |
| 86 |
user = self.moduleUtil.find_user("john_smith") |
| 87 |
|
| 88 |
self.assertEqual(type(user), User) |
| 89 |
self.assertEqual(user.name, "John Smith") |
| 90 |
self.assertEqual(user.id, 1) |
| 91 |
|
| 92 |
@with_httmock(resp_create_user) |
| 93 |
def test_create_user(self): |
| 94 |
user = self.moduleUtil.create_user( |
| 95 |
{"email": "[email protected]", "password": "s3cur3s3cr3T", "username": "john_smith", "name": "John Smith"} |
| 96 |
) |
| 97 |
self.assertEqual(type(user), User) |
| 98 |
self.assertEqual(user.name, "John Smith") |
| 99 |
self.assertEqual(user.id, 1) |
| 100 |
|
| 101 |
@with_httmock(resp_get_user) |
| 102 |
def test_update_user(self): |
| 103 |
user = self.gitlab_instance.users.get(1) |
| 104 |
|
| 105 |
changed, newUser = self.moduleUtil.update_user( |
| 106 |
user, {"name": {"value": "Jack Smith"}, "is_admin": {"value": "true", "setter": "admin"}}, {} |
| 107 |
) |
| 108 |
|
| 109 |
self.assertEqual(changed, True) |
| 110 |
self.assertEqual(newUser.name, "Jack Smith") |
| 111 |
self.assertEqual(newUser.admin, "true") |
| 112 |
|
| 113 |
changed, newUser = self.moduleUtil.update_user(user, {"name": {"value": "Jack Smith"}}, {}) |
| 114 |
|
| 115 |
self.assertEqual(changed, False) |
| 116 |
|
| 117 |
changed, newUser = self.moduleUtil.update_user( |
| 118 |
user, |
| 119 |
{}, |
| 120 |
{ |
| 121 |
"skip_reconfirmation": {"value": True}, |
| 122 |
"password": {"value": "super_secret-super_secret"}, |
| 123 |
}, |
| 124 |
) |
| 125 |
|
| 126 |
# note: uncheckable parameters dont set changed state |
| 127 |
self.assertEqual(changed, False) |
| 128 |
self.assertEqual(newUser.skip_reconfirmation, True) |
| 129 |
self.assertEqual(newUser.password, "super_secret-super_secret") |
| 130 |
|
| 131 |
@with_httmock(resp_find_user) |
| 132 |
@with_httmock(resp_delete_user) |
| 133 |
def test_delete_user(self): |
| 134 |
self.moduleUtil.exists_user("john_smith") |
| 135 |
rvalue = self.moduleUtil.delete_user() |
| 136 |
|
| 137 |
self.assertEqual(rvalue, None) |
| 138 |
|
| 139 |
@with_httmock(resp_get_user) |
| 140 |
@with_httmock(resp_get_user_keys) |
| 141 |
def test_sshkey_exist(self): |
| 142 |
user = self.gitlab_instance.users.get(1) |
| 143 |
|
| 144 |
exist = self.moduleUtil.ssh_key_exists(user, "Public key") |
| 145 |
self.assertEqual(exist, True) |
| 146 |
|
| 147 |
notExist = self.moduleUtil.ssh_key_exists(user, "Private key") |
| 148 |
self.assertEqual(notExist, False) |
| 149 |
|
| 150 |
@with_httmock(resp_get_user) |
| 151 |
@with_httmock(resp_create_user_keys) |
| 152 |
@with_httmock(resp_get_user_keys) |
| 153 |
def test_create_sshkey(self): |
| 154 |
user = self.gitlab_instance.users.get(1) |
| 155 |
|
| 156 |
rvalue = self.moduleUtil.add_ssh_key_to_user( |
| 157 |
user, |
| 158 |
{ |
| 159 |
"name": "Public key", |
| 160 |
"file": "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJe" |
| 161 |
"jgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4" |
| 162 |
"soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=", |
| 163 |
"expires_at": "", |
| 164 |
"update_mode": "create", |
| 165 |
}, |
| 166 |
) |
| 167 |
self.assertEqual(rvalue, False) |
| 168 |
|
| 169 |
rvalue = self.moduleUtil.add_ssh_key_to_user( |
| 170 |
user, |
| 171 |
{ |
| 172 |
"name": "Private key", |
| 173 |
"file": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA1YotVDm2mAyk2tPt4E7AHm01sS6JZmcU" |
| 174 |
"dRuSuA5zszUJzYPPUSRAX3BCgTqLqYx//UuVncK7YqLVSbbwjKR2Ez5lISgCnVfLVEXzwhv+" |
| 175 |
"xawxKWmI7hJ5S0tOv6MJ+IxyTa4xcKwJTwB86z22n9fVOQeJTR2dSOH1WJrf0PvRk+KVNY2j" |
| 176 |
"TiGHTi9AIjLnyD/jWRpOgtdfkLRc8EzAWrWlgNmH2WOKBw6za0az6XoG75obUdFVdW3qcD0x" |
| 177 |
"c809OHLi7FDf+E7U4wiZJCFuUizMeXyuK/SkaE1aee4Qp5R4dxTR4TP9M1XAYkf+kF0W9srZ+mhF069XD/zhUPJsvwEF", |
| 178 |
"expires_at": "2027-01-01", |
| 179 |
"update_mode": "create", |
| 180 |
}, |
| 181 |
) |
| 182 |
self.assertEqual(rvalue, True) |
| 183 |
|
| 184 |
@with_httmock(resp_get_user) |
| 185 |
@with_httmock(resp_get_user_keys) |
| 186 |
def test_sshkey_comment_change_is_ignored(self): |
| 187 |
user = self.gitlab_instance.users.get(1) |
| 188 |
|
| 189 |
rvalue = self.moduleUtil.add_ssh_key_to_user( |
| 190 |
user, |
| 191 |
{ |
| 192 |
"name": "Public key", |
| 193 |
"file": "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJe" |
| 194 |
"jgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4" |
| 195 |
"soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0= desired-comment", |
| 196 |
"expires_at": None, |
| 197 |
"update_mode": "update", |
| 198 |
}, |
| 199 |
) |
| 200 |
|
| 201 |
self.assertEqual(rvalue, False) |
| 202 |
|
| 203 |
@with_httmock(resp_get_user) |
| 204 |
@with_httmock(resp_delete_user_key) |
| 205 |
@with_httmock(resp_create_user_keys) |
| 206 |
@with_httmock(resp_get_user_keys) |
| 207 |
def test_update_sshkey_when_key_changes(self): |
| 208 |
user = self.gitlab_instance.users.get(1) |
| 209 |
|
| 210 |
rvalue = self.moduleUtil.add_ssh_key_to_user( |
| 211 |
user, |
| 212 |
{ |
| 213 |
"name": "Public key", |
| 214 |
"file": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA1YotVDm2mAyk2tPt4E7AHm01sS6JZmcU" |
| 215 |
"dRuSuA5zszUJzYPPUSRAX3BCgTqLqYx//UuVncK7YqLVSbbwjKR2Ez5lISgCnVfLVEXzwhv+" |
| 216 |
"xawxKWmI7hJ5S0tOv6MJ+IxyTa4xcKwJTwB86z22n9fVOQeJTR2dSOH1WJrf0PvRk+KVNY2j" |
| 217 |
"TiGHTi9AIjLnyD/jWRpOgtdfkLRc8EzAWrWlgNmH2WOKBw6za0az6XoG75obUdFVdW3qcD0x" |
| 218 |
"c809OHLi7FDf+E7U4wiZJCFuUizMeXyuK/SkaE1aee4Qp5R4dxTR4TP9M1XAYkf+kF0W9srZ+mhF069XD/zhUPJsvwEF", |
| 219 |
"expires_at": "2027-01-01", |
| 220 |
"update_mode": "update", |
| 221 |
}, |
| 222 |
) |
| 223 |
|
| 224 |
self.assertEqual(rvalue, True) |
| 225 |
|
| 226 |
@with_httmock(resp_get_user) |
| 227 |
@with_httmock(resp_get_user_keys) |
| 228 |
def test_create_sshkey_with_existing_same_name_does_not_change(self): |
| 229 |
user = self.gitlab_instance.users.get(1) |
| 230 |
|
| 231 |
rvalue = self.moduleUtil.add_ssh_key_to_user( |
| 232 |
user, |
| 233 |
{ |
| 234 |
"name": "Public key", |
| 235 |
"file": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA1YotVDm2mAyk2tPt4E7AHm01sS6JZmcU" |
| 236 |
"dRuSuA5zszUJzYPPUSRAX3BCgTqLqYx//UuVncK7YqLVSbbwjKR2Ez5lISgCnVfLVEXzwhv+" |
| 237 |
"xawxKWmI7hJ5S0tOv6MJ+IxyTa4xcKwJTwB86z22n9fVOQeJTR2dSOH1WJrf0PvRk+KVNY2j" |
| 238 |
"TiGHTi9AIjLnyD/jWRpOgtdfkLRc8EzAWrWlgNmH2WOKBw6za0az6XoG75obUdFVdW3qcD0x" |
| 239 |
"c809OHLi7FDf+E7U4wiZJCFuUizMeXyuK/SkaE1aee4Qp5R4dxTR4TP9M1XAYkf+kF0W9srZ+mhF069XD/zhUPJsvwEF", |
| 240 |
"expires_at": None, |
| 241 |
"update_mode": "create", |
| 242 |
}, |
| 243 |
) |
| 244 |
|
| 245 |
self.assertEqual(rvalue, False) |
| 246 |
|
| 247 |
@with_httmock(resp_get_user) |
| 248 |
@with_httmock(resp_get_user_duplicate_keys) |
| 249 |
def test_update_sshkey_with_multiple_same_name_keys_warns(self): |
| 250 |
user = self.gitlab_instance.users.get(1) |
| 251 |
|
| 252 |
rvalue = self.moduleUtil.add_ssh_key_to_user( |
| 253 |
user, |
| 254 |
{ |
| 255 |
"name": "Public key", |
| 256 |
"file": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA1YotVDm2mAyk2tPt4E7AHm01sS6JZmcU" |
| 257 |
"dRuSuA5zszUJzYPPUSRAX3BCgTqLqYx//UuVncK7YqLVSbbwjKR2Ez5lISgCnVfLVEXzwhv+" |
| 258 |
"xawxKWmI7hJ5S0tOv6MJ+IxyTa4xcKwJTwB86z22n9fVOQeJTR2dSOH1WJrf0PvRk+KVNY2j" |
| 259 |
"TiGHTi9AIjLnyD/jWRpOgtdfkLRc8EzAWrWlgNmH2WOKBw6za0az6XoG75obUdFVdW3qcD0x" |
| 260 |
"c809OHLi7FDf+E7U4wiZJCFuUizMeXyuK/SkaE1aee4Qp5R4dxTR4TP9M1XAYkf+kF0W9srZ+mhF069XD/zhUPJsvwEF", |
| 261 |
"expires_at": None, |
| 262 |
"update_mode": "update", |
| 263 |
}, |
| 264 |
) |
| 265 |
|
| 266 |
self.assertEqual(rvalue, False) |
| 267 |
self.mock_module.warn.assert_called_once_with( |
| 268 |
"Found multiple SSH keys named 'Public key'. Skipping update because sshkey_update_mode=update requires a single matching key." |
| 269 |
) |
| 270 |
|
| 271 |
@with_httmock(resp_get_user) |
| 272 |
@with_httmock(resp_delete_user_key) |
| 273 |
@with_httmock(resp_create_user_keys) |
| 274 |
@with_httmock(resp_get_user_duplicate_keys) |
| 275 |
def test_deduplicate_sshkey_deletes_all_same_name_keys(self): |
| 276 |
user = self.gitlab_instance.users.get(1) |
| 277 |
deleted_user_key_ids.clear() |
| 278 |
created_user_keys.clear() |
| 279 |
|
| 280 |
rvalue = self.moduleUtil.add_ssh_key_to_user( |
| 281 |
user, |
| 282 |
{ |
| 283 |
"name": "Public key", |
| 284 |
"file": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA1YotVDm2mAyk2tPt4E7AHm01sS6JZmcU" |
| 285 |
"dRuSuA5zszUJzYPPUSRAX3BCgTqLqYx//UuVncK7YqLVSbbwjKR2Ez5lISgCnVfLVEXzwhv+" |
| 286 |
"xawxKWmI7hJ5S0tOv6MJ+IxyTa4xcKwJTwB86z22n9fVOQeJTR2dSOH1WJrf0PvRk+KVNY2j" |
| 287 |
"TiGHTi9AIjLnyD/jWRpOgtdfkLRc8EzAWrWlgNmH2WOKBw6za0az6XoG75obUdFVdW3qcD0x" |
| 288 |
"c809OHLi7FDf+E7U4wiZJCFuUizMeXyuK/SkaE1aee4Qp5R4dxTR4TP9M1XAYkf+kF0W9srZ+mhF069XD/zhUPJsvwEF", |
| 289 |
"expires_at": "2027-01-01", |
| 290 |
"update_mode": "deduplicate", |
| 291 |
}, |
| 292 |
) |
| 293 |
|
| 294 |
self.assertEqual(rvalue, True) |
| 295 |
self.assertEqual(deleted_user_key_ids, ["1", "3"]) |
| 296 |
self.assertEqual( |
| 297 |
created_user_keys, |
| 298 |
[ |
| 299 |
{ |
| 300 |
"title": "Public key", |
| 301 |
"key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA1YotVDm2mAyk2tPt4E7AHm01sS6JZmcU" |
| 302 |
"dRuSuA5zszUJzYPPUSRAX3BCgTqLqYx//UuVncK7YqLVSbbwjKR2Ez5lISgCnVfLVEXzwhv+" |
| 303 |
"xawxKWmI7hJ5S0tOv6MJ+IxyTa4xcKwJTwB86z22n9fVOQeJTR2dSOH1WJrf0PvRk+KVNY2j" |
| 304 |
"TiGHTi9AIjLnyD/jWRpOgtdfkLRc8EzAWrWlgNmH2WOKBw6za0az6XoG75obUdFVdW3qcD0x" |
| 305 |
"c809OHLi7FDf+E7U4wiZJCFuUizMeXyuK/SkaE1aee4Qp5R4dxTR4TP9M1XAYkf+kF0W9srZ+mhF069XD/zhUPJsvwEF", |
| 306 |
"expires_at": "2027-01-01", |
| 307 |
}, |
| 308 |
], |
| 309 |
) |
| 310 |
|
| 311 |
@with_httmock(resp_get_group) |
| 312 |
@with_httmock(resp_get_member) |
| 313 |
def test_find_member(self): |
| 314 |
group = self.gitlab_instance.groups.get(1) |
| 315 |
|
| 316 |
user = self.moduleUtil.find_member(group, 1) |
| 317 |
self.assertEqual(user.username, "raymond_smith") |
| 318 |
|
| 319 |
@with_httmock(resp_get_user) |
| 320 |
@with_httmock(resp_get_group) |
| 321 |
@with_httmock(resp_get_group) |
| 322 |
@with_httmock(resp_get_member) |
| 323 |
@with_httmock(resp_add_member) |
| 324 |
@with_httmock(resp_update_member) |
| 325 |
def test_assign_user_to_group(self): |
| 326 |
group = self.gitlab_instance.groups.get(1) |
| 327 |
user = self.gitlab_instance.users.get(1) |
| 328 |
|
| 329 |
rvalue = self.moduleUtil.assign_user_to_group(user, group.id, "developer") |
| 330 |
self.assertEqual(rvalue, False) |
| 331 |
|
| 332 |
rvalue = self.moduleUtil.assign_user_to_group(user, group.id, "guest") |
| 333 |
self.assertEqual(rvalue, True) |