1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-05-04 01:13:00 +00:00

Reformat everything.

This commit is contained in:
Felix Fontein 2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View file

@ -17,9 +17,11 @@ import sys
if sys.version_info[0] == 3:
import builtins
open_path = "builtins.open"
else:
import __builtin__ as builtins
open_path = "__builtin__.open"
@ -34,32 +36,26 @@ def test_validate_file_exist_fails_when_file_missing():
module = MagicMock()
with patch("os.path.exists", return_value=False):
jenkins_credential.validate_file_exist(module, "/missing/file/path")
module.fail_json.assert_called_once_with(
msg="File not found: /missing/file/path"
)
module.fail_json.assert_called_once_with(msg="File not found: /missing/file/path")
@patch(
"ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url"
)
@patch("ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url")
def test_get_jenkins_crumb_sets_crumb_header(fetch_mock):
module = MagicMock()
module.params = {"type": "file", "url": "http://localhost:8080"}
headers = {}
fake_response = MagicMock()
fake_response.read.return_value = json.dumps(
{"crumbRequestField": "crumb_field", "crumb": "abc123"}
).encode("utf-8")
fake_response.read.return_value = json.dumps({"crumbRequestField": "crumb_field", "crumb": "abc123"}).encode(
"utf-8"
)
fetch_mock.return_value = (
fake_response,
{"status": 200, "set-cookie": "JSESSIONID=something; Path=/"},
)
crumb_request_field, crumb, session_coockie = jenkins_credential.get_jenkins_crumb(
module, headers
)
crumb_request_field, crumb, session_coockie = jenkins_credential.get_jenkins_crumb(module, headers)
assert "Cookie" not in headers
assert "crumb_field" in headers
@ -67,27 +63,23 @@ def test_get_jenkins_crumb_sets_crumb_header(fetch_mock):
assert headers[crumb_request_field] == crumb
@patch(
"ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url"
)
@patch("ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url")
def test_get_jenkins_crumb_sets_cookie_if_type_token(fetch_mock):
module = MagicMock()
module.params = {"type": "token", "url": "http://localhost:8080"}
headers = {}
fake_response = MagicMock()
fake_response.read.return_value = json.dumps(
{"crumbRequestField": "crumb_field", "crumb": "secure"}
).encode("utf-8")
fake_response.read.return_value = json.dumps({"crumbRequestField": "crumb_field", "crumb": "secure"}).encode(
"utf-8"
)
fetch_mock.return_value = (
fake_response,
{"status": 200, "set-cookie": "JSESSIONID=token-cookie; Path=/"},
)
crumb_request_field, crumb, session_cookie = jenkins_credential.get_jenkins_crumb(
module, headers
)
crumb_request_field, crumb, session_cookie = jenkins_credential.get_jenkins_crumb(module, headers)
assert "crumb_field" in headers
assert crumb == "secure"
@ -95,9 +87,7 @@ def test_get_jenkins_crumb_sets_cookie_if_type_token(fetch_mock):
assert headers["Cookie"] == session_cookie
@patch(
"ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url"
)
@patch("ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url")
def test_get_jenkins_crumb_fails_on_non_200_status(fetch_mock):
module = MagicMock()
module.params = {"type": "file", "url": "http://localhost:8080"}
@ -111,18 +101,14 @@ def test_get_jenkins_crumb_fails_on_non_200_status(fetch_mock):
assert "Failed to fetch Jenkins crumb" in module.fail_json.call_args[1]["msg"]
@patch(
"ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url"
)
@patch("ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url")
def test_get_jenkins_crumb_removes_job_from_url(fetch_mock):
module = MagicMock()
module.params = {"type": "file", "url": "http://localhost:8080/job/test"}
headers = {}
fake_response = MagicMock()
fake_response.read.return_value = json.dumps(
{"crumbRequestField": "Jenkins-Crumb", "crumb": "xyz"}
).encode("utf-8")
fake_response.read.return_value = json.dumps({"crumbRequestField": "Jenkins-Crumb", "crumb": "xyz"}).encode("utf-8")
fetch_mock.return_value = (fake_response, {"status": 200, "set-cookie": ""})
@ -146,9 +132,7 @@ def test_clean_data_removes_extraneous_fields():
assert result == expected, f"Expected {expected}, got {result}"
@patch(
"ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url"
)
@patch("ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url")
def test_target_exists_returns_true_on_200(fetch_url_mock):
module = MagicMock()
module.params = {
@ -165,9 +149,7 @@ def test_target_exists_returns_true_on_200(fetch_url_mock):
assert jenkins_credential.target_exists(module) is True
@patch(
"ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url"
)
@patch("ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url")
def test_target_exists_returns_false_on_404(fetch_url_mock):
module = MagicMock()
module.params = {
@ -184,9 +166,7 @@ def test_target_exists_returns_false_on_404(fetch_url_mock):
assert jenkins_credential.target_exists(module) is False
@patch(
"ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url"
)
@patch("ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url")
def test_target_exists_calls_fail_json_on_unexpected_status(fetch_url_mock):
module = MagicMock()
module.params = {
@ -205,9 +185,7 @@ def test_target_exists_calls_fail_json_on_unexpected_status(fetch_url_mock):
assert "Unexpected status code" in module.fail_json.call_args[1]["msg"]
@patch(
"ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url"
)
@patch("ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url")
def test_target_exists_skips_check_for_token_type(fetch_url_mock):
module = MagicMock()
module.params = {
@ -224,9 +202,7 @@ def test_target_exists_skips_check_for_token_type(fetch_url_mock):
fetch_url_mock.assert_not_called()
@patch(
"ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url"
)
@patch("ansible_collections.community.general.plugins.modules.jenkins_credential.fetch_url")
def test_delete_target_fails_deleting(fetch_mock):
module = MagicMock()
module.params = {
@ -274,9 +250,7 @@ def test_read_privateKey_returns_trimmed_contents():
module = MagicMock()
module.params = {"private_key_path": "/fake/path/key.pem"}
mocked_file = mock_open(
read_data="\n \t -----BEGIN PRIVATE KEY-----\nKEYDATA\n-----END PRIVATE KEY----- \n\n"
)
mocked_file = mock_open(read_data="\n \t -----BEGIN PRIVATE KEY-----\nKEYDATA\n-----END PRIVATE KEY----- \n\n")
with patch(open_path, mocked_file):
result = jenkins_credential.read_privateKey(module)
@ -306,12 +280,8 @@ def test_embed_file_into_body_returns_multipart_fields():
mock = mock_open()
mock.return_value.read.return_value = fake_file_content
with patch("os.path.basename", return_value="secret.pem"), patch.object(
builtins, "open", mock
):
body, content_type = jenkins_credential.embed_file_into_body(
module, file_path, credentials.copy()
)
with patch("os.path.basename", return_value="secret.pem"), patch.object(builtins, "open", mock):
body, content_type = jenkins_credential.embed_file_into_body(module, file_path, credentials.copy())
assert "multipart/form-data; boundary=" in content_type
@ -337,10 +307,7 @@ def test_embed_file_into_body_injects_file_keys_into_credentials():
file_path = "/fake/path/file.txt"
credentials = {"id": "test"}
with patch(open_path, mock_open(read_data=b"1234")), patch(
"os.path.basename", return_value="file.txt"
):
with patch(open_path, mock_open(read_data=b"1234")), patch("os.path.basename", return_value="file.txt"):
jenkins_credential.embed_file_into_body(module, file_path, credentials)
assert credentials["file"] == "file0"