| 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 builtins |
| 8 |
import json |
| 9 |
from unittest.mock import ( |
| 10 |
MagicMock, |
| 11 |
mock_open, |
| 12 |
patch, |
| 13 |
) |
| 14 |
|
| 15 |
from ansible_collections.community.general.plugins.modules import jenkins_credential |
| 16 |
|
| 17 |
|
| 18 |
def test_validate_file_exist_passes_when_file_exists(): |
| 19 |
module = MagicMock() |
| 20 |
with patch("os.path.exists", return_value=True): |
| 21 |
jenkins_credential.validate_file_exist(module, "/some/file/path") |
| 22 |
module.fail_json.assert_not_called() |
| 23 |
|
| 24 |
|
| 25 |
def test_validate_file_exist_fails_when_file_missing(): |
| 26 |
module = MagicMock() |
| 27 |
with patch("os.path.exists", return_value=False): |
| 28 |
jenkins_credential.validate_file_exist(module, "/missing/file/path") |
| 29 |
module.fail_json.assert_called_once_with(msg="File not found: /missing/file/path") |
| 30 |
|
| 31 |
|
| 32 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url") |
| 33 |
def test_get_jenkins_crumb_sets_crumb_header(fetch_mock): |
| 34 |
module = MagicMock() |
| 35 |
module.params = {"type": "file", "url": "http://localhost:8080"} |
| 36 |
headers = {} |
| 37 |
|
| 38 |
fake_response = MagicMock() |
| 39 |
fake_response.read.return_value = json.dumps({"crumbRequestField": "crumb_field", "crumb": "abc123"}).encode( |
| 40 |
"utf-8" |
| 41 |
) |
| 42 |
|
| 43 |
fetch_mock.return_value = ( |
| 44 |
fake_response, |
| 45 |
{"status": 200, "set-cookie": "JSESSIONID=something; Path=/"}, |
| 46 |
) |
| 47 |
|
| 48 |
crumb_request_field, crumb, session_coockie = jenkins_credential.get_jenkins_crumb(module, headers) |
| 49 |
|
| 50 |
assert "Cookie" not in headers |
| 51 |
assert "crumb_field" in headers |
| 52 |
assert crumb == "abc123" |
| 53 |
assert headers[crumb_request_field] == crumb |
| 54 |
|
| 55 |
|
| 56 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url") |
| 57 |
def test_get_jenkins_crumb_sets_cookie_if_type_token(fetch_mock): |
| 58 |
module = MagicMock() |
| 59 |
module.params = {"type": "token", "url": "http://localhost:8080"} |
| 60 |
headers = {} |
| 61 |
|
| 62 |
fake_response = MagicMock() |
| 63 |
fake_response.read.return_value = json.dumps({"crumbRequestField": "crumb_field", "crumb": "secure"}).encode( |
| 64 |
"utf-8" |
| 65 |
) |
| 66 |
|
| 67 |
fetch_mock.return_value = ( |
| 68 |
fake_response, |
| 69 |
{"status": 200, "set-cookie": "JSESSIONID=token-cookie; Path=/"}, |
| 70 |
) |
| 71 |
|
| 72 |
crumb_request_field, crumb, session_cookie = jenkins_credential.get_jenkins_crumb(module, headers) |
| 73 |
|
| 74 |
assert "crumb_field" in headers |
| 75 |
assert crumb == "secure" |
| 76 |
assert headers[crumb_request_field] == crumb |
| 77 |
assert headers["Cookie"] == session_cookie |
| 78 |
|
| 79 |
|
| 80 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url") |
| 81 |
def test_get_jenkins_crumb_fails_on_non_200_status(fetch_mock): |
| 82 |
module = MagicMock() |
| 83 |
module.params = {"type": "file", "url": "http://localhost:8080"} |
| 84 |
headers = {} |
| 85 |
|
| 86 |
fetch_mock.return_value = (MagicMock(), {"status": 403}) |
| 87 |
|
| 88 |
jenkins_credential.get_jenkins_crumb(module, headers) |
| 89 |
|
| 90 |
module.fail_json.assert_called_once() |
| 91 |
assert "Failed to fetch Jenkins crumb" in module.fail_json.call_args[1]["msg"] |
| 92 |
|
| 93 |
|
| 94 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url") |
| 95 |
def test_get_jenkins_crumb_removes_job_from_url(fetch_mock): |
| 96 |
module = MagicMock() |
| 97 |
module.params = {"type": "file", "url": "http://localhost:8080/job/test"} |
| 98 |
headers = {} |
| 99 |
|
| 100 |
fake_response = MagicMock() |
| 101 |
fake_response.read.return_value = json.dumps({"crumbRequestField": "Jenkins-Crumb", "crumb": "xyz"}).encode("utf-8") |
| 102 |
|
| 103 |
fetch_mock.return_value = (fake_response, {"status": 200, "set-cookie": ""}) |
| 104 |
|
| 105 |
jenkins_credential.get_jenkins_crumb(module, headers) |
| 106 |
|
| 107 |
url_called = fetch_mock.call_args[0][1] |
| 108 |
assert url_called == "http://localhost:8080/crumbIssuer/api/json" |
| 109 |
|
| 110 |
|
| 111 |
def test_clean_data_removes_extraneous_fields(): |
| 112 |
data = { |
| 113 |
"id": "cred1", |
| 114 |
"description": "test", |
| 115 |
"jenkins_user": "admin", |
| 116 |
"token": "secret", |
| 117 |
"url": "http://localhost:8080", |
| 118 |
"file_path": None, |
| 119 |
} |
| 120 |
expected = {"id": "cred1", "description": "test"} |
| 121 |
result = jenkins_credential.clean_data(data) |
| 122 |
assert result == expected, f"Expected {expected}, got {result}" |
| 123 |
|
| 124 |
|
| 125 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url") |
| 126 |
def test_target_exists_returns_true_on_200(fetch_url_mock): |
| 127 |
module = MagicMock() |
| 128 |
module.params = { |
| 129 |
"url": "http://localhost:8080", |
| 130 |
"location": "system", |
| 131 |
"scope": "_", |
| 132 |
"id": "my-id", |
| 133 |
"jenkins_user": "admin", |
| 134 |
"token": "secret", |
| 135 |
"type": "file", |
| 136 |
} |
| 137 |
|
| 138 |
fetch_url_mock.return_value = (MagicMock(), {"status": 200}) |
| 139 |
assert jenkins_credential.target_exists(module) is True |
| 140 |
|
| 141 |
|
| 142 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url") |
| 143 |
def test_target_exists_returns_false_on_404(fetch_url_mock): |
| 144 |
module = MagicMock() |
| 145 |
module.params = { |
| 146 |
"url": "http://localhost:8080", |
| 147 |
"location": "system", |
| 148 |
"scope": "_", |
| 149 |
"id": "my-id", |
| 150 |
"jenkins_user": "admin", |
| 151 |
"token": "secret", |
| 152 |
"type": "file", |
| 153 |
} |
| 154 |
|
| 155 |
fetch_url_mock.return_value = (MagicMock(), {"status": 404}) |
| 156 |
assert jenkins_credential.target_exists(module) is False |
| 157 |
|
| 158 |
|
| 159 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url") |
| 160 |
def test_target_exists_calls_fail_json_on_unexpected_status(fetch_url_mock): |
| 161 |
module = MagicMock() |
| 162 |
module.params = { |
| 163 |
"url": "http://localhost:8080", |
| 164 |
"location": "system", |
| 165 |
"scope": "_", |
| 166 |
"id": "my-id", |
| 167 |
"jenkins_user": "admin", |
| 168 |
"token": "secret", |
| 169 |
"type": "file", |
| 170 |
} |
| 171 |
|
| 172 |
fetch_url_mock.return_value = (MagicMock(), {"status": 500}) |
| 173 |
jenkins_credential.target_exists(module) |
| 174 |
module.fail_json.assert_called_once() |
| 175 |
assert "Unexpected status code" in module.fail_json.call_args[1]["msg"] |
| 176 |
|
| 177 |
|
| 178 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url") |
| 179 |
def test_target_exists_skips_check_for_token_type(fetch_url_mock): |
| 180 |
module = MagicMock() |
| 181 |
module.params = { |
| 182 |
"type": "token", |
| 183 |
"url": "ignored", |
| 184 |
"location": "ignored", |
| 185 |
"scope": "ignored", |
| 186 |
"id": "ignored", |
| 187 |
"jenkins_user": "ignored", |
| 188 |
"token": "ignored", |
| 189 |
} |
| 190 |
|
| 191 |
assert jenkins_credential.target_exists(module) is False |
| 192 |
fetch_url_mock.assert_not_called() |
| 193 |
|
| 194 |
|
| 195 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url") |
| 196 |
def test_delete_target_fails_deleting(fetch_mock): |
| 197 |
module = MagicMock() |
| 198 |
module.params = { |
| 199 |
"type": "token", |
| 200 |
"jenkins_user": "admin", |
| 201 |
"url": "http://localhost:8080", |
| 202 |
"id": "token-id", |
| 203 |
"location": "system", |
| 204 |
"scope": "_", |
| 205 |
} |
| 206 |
headers = {"Authorization": "Basic abc", "Content-Type": "whatever"} |
| 207 |
|
| 208 |
fetch_mock.return_value = (MagicMock(), {"status": 500}) |
| 209 |
|
| 210 |
jenkins_credential.delete_target(module, headers) |
| 211 |
|
| 212 |
module.fail_json.assert_called_once() |
| 213 |
assert "Failed to delete" in module.fail_json.call_args[1]["msg"] |
| 214 |
|
| 215 |
|
| 216 |
@patch( |
| 217 |
"ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url", |
| 218 |
side_effect=Exception("network error"), |
| 219 |
) |
| 220 |
def test_delete_target_raises_exception(fetch_mock): |
| 221 |
module = MagicMock() |
| 222 |
module.params = { |
| 223 |
"type": "scope", |
| 224 |
"jenkins_user": "admin", |
| 225 |
"location": "system", |
| 226 |
"url": "http://localhost:8080", |
| 227 |
"id": "domain-id", |
| 228 |
"scope": "_", |
| 229 |
} |
| 230 |
headers = {"Authorization": "Basic auth"} |
| 231 |
|
| 232 |
jenkins_credential.delete_target(module, headers) |
| 233 |
|
| 234 |
module.fail_json.assert_called_once() |
| 235 |
assert "Exception during delete" in module.fail_json.call_args[1]["msg"] |
| 236 |
assert "network error" in module.fail_json.call_args[1]["msg"] |
| 237 |
|
| 238 |
|
| 239 |
def test_read_privateKey_returns_trimmed_contents(): |
| 240 |
module = MagicMock() |
| 241 |
module.params = {"private_key_path": "/fake/path/key.pem"} |
| 242 |
|
| 243 |
mocked_file = mock_open(read_data="\n \t -----BEGIN PRIVATE KEY-----\nKEYDATA\n-----END PRIVATE KEY----- \n\n") |
| 244 |
with patch("builtins.open", mocked_file): |
| 245 |
result = jenkins_credential.read_privateKey(module) |
| 246 |
|
| 247 |
expected = "-----BEGIN PRIVATE KEY-----\nKEYDATA\n-----END PRIVATE KEY-----" |
| 248 |
|
| 249 |
assert result == expected |
| 250 |
mocked_file.assert_called_once_with("/fake/path/key.pem") |
| 251 |
|
| 252 |
|
| 253 |
def test_read_privateKey_handles_file_read_error(): |
| 254 |
module = MagicMock() |
| 255 |
module.params = {"private_key_path": "/invalid/path.pem"} |
| 256 |
|
| 257 |
with patch("builtins.open", side_effect=OSError("cannot read file")): |
| 258 |
jenkins_credential.read_privateKey(module) |
| 259 |
|
| 260 |
module.fail_json.assert_called_once() |
| 261 |
assert "Failed to read private key file" in module.fail_json.call_args[1]["msg"] |
| 262 |
|
| 263 |
|
| 264 |
def test_embed_file_into_body_returns_multipart_fields(): |
| 265 |
module = MagicMock() |
| 266 |
file_path = "/fake/path/secret.pem" |
| 267 |
credentials = {"id": "my-id"} |
| 268 |
fake_file_content = b"MY SECRET DATA" |
| 269 |
|
| 270 |
mock = mock_open() |
| 271 |
mock.return_value.read.return_value = fake_file_content |
| 272 |
|
| 273 |
with patch("os.path.basename", return_value="secret.pem"), patch.object(builtins, "open", mock): |
| 274 |
body, content_type = jenkins_credential.embed_file_into_body(module, file_path, credentials.copy()) |
| 275 |
|
| 276 |
assert "multipart/form-data; boundary=" in content_type |
| 277 |
|
| 278 |
# Check if file content is embedded in body |
| 279 |
assert b"MY SECRET DATA" in body |
| 280 |
assert b'filename="secret.pem"' in body |
| 281 |
|
| 282 |
|
| 283 |
def test_embed_file_into_body_fails_when_file_unreadable(): |
| 284 |
module = MagicMock() |
| 285 |
file_path = "/fake/path/missing.pem" |
| 286 |
credentials = {"id": "something"} |
| 287 |
|
| 288 |
with patch("builtins.open", side_effect=OSError("can't read file")): |
| 289 |
jenkins_credential.embed_file_into_body(module, file_path, credentials) |
| 290 |
|
| 291 |
module.fail_json.assert_called_once() |
| 292 |
assert "Failed to read file" in module.fail_json.call_args[1]["msg"] |
| 293 |
|
| 294 |
|
| 295 |
def test_embed_file_into_body_injects_file_keys_into_credentials(): |
| 296 |
module = MagicMock() |
| 297 |
file_path = "/fake/path/file.txt" |
| 298 |
credentials = {"id": "test"} |
| 299 |
|
| 300 |
with patch("builtins.open", mock_open(read_data=b"1234")), patch("os.path.basename", return_value="file.txt"): |
| 301 |
jenkins_credential.embed_file_into_body(module, file_path, credentials) |
| 302 |
|
| 303 |
assert credentials["file"] == "file0" |
| 304 |
assert credentials["fileName"] == "file.txt" |