| 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 json |
| 8 |
from unittest.mock import MagicMock, patch |
| 9 |
|
| 10 |
import pytest |
| 11 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 12 |
AnsibleExitJson, |
| 13 |
AnsibleFailJson, |
| 14 |
exit_json, |
| 15 |
fail_json, |
| 16 |
set_module_args, |
| 17 |
) |
| 18 |
|
| 19 |
from ansible_collections.community.general.plugins.modules import github_secrets |
| 20 |
|
| 21 |
PUBLIC_KEY_PAYLOAD = { |
| 22 |
"key_id": "100", |
| 23 |
"key": "j6gSA9mu5bO8ig+YBNU6oXRhGwd3Z4EFiS9rVmU9gwo=", |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
def make_fetch_url_response(body, status=200): |
| 28 |
response = MagicMock() |
| 29 |
response.read.return_value = json.dumps(body).encode("utf-8") |
| 30 |
info = {"status": status, "msg": f"OK ({len(json.dumps(body))} bytes)"} |
| 31 |
return (response, info) |
| 32 |
|
| 33 |
|
| 34 |
@pytest.fixture(autouse=True) |
| 35 |
def patch_module(): |
| 36 |
with patch.multiple( |
| 37 |
"ansible.module_utils.basic.AnsibleModule", |
| 38 |
exit_json=exit_json, |
| 39 |
fail_json=fail_json, |
| 40 |
): |
| 41 |
yield |
| 42 |
|
| 43 |
|
| 44 |
@pytest.fixture |
| 45 |
def fetch_url_mock(): |
| 46 |
with patch.object(github_secrets, "fetch_url") as mock: |
| 47 |
yield mock |
| 48 |
|
| 49 |
|
| 50 |
def test_encrypt_secret(): |
| 51 |
result = github_secrets.encrypt_secret(PUBLIC_KEY_PAYLOAD["key"], "ansible") |
| 52 |
assert isinstance(result, str) |
| 53 |
assert result != "ansible" |
| 54 |
assert len(result) > 70 |
| 55 |
|
| 56 |
|
| 57 |
def test_fail_without_parameters(): |
| 58 |
with pytest.raises(AnsibleFailJson): |
| 59 |
with set_module_args({}): |
| 60 |
github_secrets.main() |
| 61 |
|
| 62 |
|
| 63 |
def test_fail_present_without_value(): |
| 64 |
with pytest.raises(AnsibleFailJson) as exc: |
| 65 |
with set_module_args( |
| 66 |
{ |
| 67 |
"organization": "myorg", |
| 68 |
"repository": "myrepo", |
| 69 |
"key": "MY_SECRET", |
| 70 |
"value": None, |
| 71 |
"state": "present", |
| 72 |
"token": "ghp_test_token", |
| 73 |
} |
| 74 |
): |
| 75 |
github_secrets.main() |
| 76 |
assert "value' parameter cannot be empty" in exc.value.args[0]["details"] |
| 77 |
|
| 78 |
|
| 79 |
def test_fail_org_secret_present_without_visibility(): |
| 80 |
with pytest.raises(AnsibleFailJson) as exc: |
| 81 |
with set_module_args( |
| 82 |
{ |
| 83 |
"organization": "myorg", |
| 84 |
"key": "ORG_SECRET", |
| 85 |
"value": "org_value", |
| 86 |
"state": "present", |
| 87 |
"token": "ghp_test_token", |
| 88 |
} |
| 89 |
): |
| 90 |
github_secrets.main() |
| 91 |
assert "'visibility' must be provided" in exc.value.args[0]["details"] |
| 92 |
|
| 93 |
|
| 94 |
def test_create_repo_secret(fetch_url_mock): |
| 95 |
fetch_url_mock.side_effect = [ |
| 96 |
make_fetch_url_response(PUBLIC_KEY_PAYLOAD), |
| 97 |
make_fetch_url_response({}, status=201), |
| 98 |
] |
| 99 |
|
| 100 |
with set_module_args( |
| 101 |
{ |
| 102 |
"organization": "myorg", |
| 103 |
"repository": "myrepo", |
| 104 |
"key": "MY_SECRET", |
| 105 |
"value": "secret_value", |
| 106 |
"state": "present", |
| 107 |
"token": "ghp_test_token", |
| 108 |
} |
| 109 |
): |
| 110 |
with pytest.raises(AnsibleExitJson) as exc: |
| 111 |
github_secrets.main() |
| 112 |
|
| 113 |
result = exc.value.args[0] |
| 114 |
assert result["changed"] is True |
| 115 |
assert result["result"]["status"] == 201 |
| 116 |
assert result["result"]["response"] == "Secret created" |
| 117 |
|
| 118 |
|
| 119 |
def test_update_repo_secret(fetch_url_mock): |
| 120 |
fetch_url_mock.side_effect = [ |
| 121 |
make_fetch_url_response(PUBLIC_KEY_PAYLOAD), |
| 122 |
make_fetch_url_response({}, status=204), |
| 123 |
] |
| 124 |
|
| 125 |
with set_module_args( |
| 126 |
{ |
| 127 |
"organization": "myorg", |
| 128 |
"repository": "myrepo", |
| 129 |
"key": "MY_SECRET", |
| 130 |
"value": "new_value", |
| 131 |
"state": "present", |
| 132 |
"token": "ghp_test_token", |
| 133 |
} |
| 134 |
): |
| 135 |
with pytest.raises(AnsibleExitJson) as exc: |
| 136 |
github_secrets.main() |
| 137 |
|
| 138 |
result = exc.value.args[0] |
| 139 |
assert result["changed"] is True |
| 140 |
assert result["result"]["response"] == "Secret updated" |
| 141 |
|
| 142 |
|
| 143 |
def test_create_repo_secret_check_mode(fetch_url_mock): |
| 144 |
fetch_url_mock.side_effect = [ |
| 145 |
make_fetch_url_response(PUBLIC_KEY_PAYLOAD), |
| 146 |
make_fetch_url_response({}, status=404), |
| 147 |
] |
| 148 |
|
| 149 |
with set_module_args( |
| 150 |
{ |
| 151 |
"organization": "myorg", |
| 152 |
"repository": "myrepo", |
| 153 |
"key": "MY_SECRET", |
| 154 |
"value": "new_value", |
| 155 |
"state": "present", |
| 156 |
"token": "ghp_test_token", |
| 157 |
"_ansible_check_mode": True, |
| 158 |
} |
| 159 |
): |
| 160 |
with pytest.raises(AnsibleExitJson) as exc: |
| 161 |
github_secrets.main() |
| 162 |
|
| 163 |
result = exc.value.args[0] |
| 164 |
assert result["changed"] is True |
| 165 |
assert result["result"]["status"] == 201 |
| 166 |
assert result["result"]["response"] == "Secret created" |
| 167 |
|
| 168 |
|
| 169 |
def test_update_repo_secret_check_mode(fetch_url_mock): |
| 170 |
fetch_url_mock.side_effect = [ |
| 171 |
make_fetch_url_response(PUBLIC_KEY_PAYLOAD), |
| 172 |
make_fetch_url_response({}, status=200), |
| 173 |
] |
| 174 |
|
| 175 |
with set_module_args( |
| 176 |
{ |
| 177 |
"organization": "myorg", |
| 178 |
"repository": "myrepo", |
| 179 |
"key": "MY_SECRET", |
| 180 |
"value": "new_value", |
| 181 |
"state": "present", |
| 182 |
"token": "ghp_test_token", |
| 183 |
"_ansible_check_mode": True, |
| 184 |
} |
| 185 |
): |
| 186 |
with pytest.raises(AnsibleExitJson) as exc: |
| 187 |
github_secrets.main() |
| 188 |
|
| 189 |
result = exc.value.args[0] |
| 190 |
assert result["changed"] is True |
| 191 |
assert result["result"]["status"] == 204 |
| 192 |
assert result["result"]["response"] == "Secret updated" |
| 193 |
|
| 194 |
|
| 195 |
def test_delete_repo_secret_check_mode(fetch_url_mock): |
| 196 |
fetch_url_mock.side_effect = [ |
| 197 |
make_fetch_url_response(PUBLIC_KEY_PAYLOAD), |
| 198 |
make_fetch_url_response({}, status=200), |
| 199 |
] |
| 200 |
|
| 201 |
with set_module_args( |
| 202 |
{ |
| 203 |
"organization": "myorg", |
| 204 |
"repository": "myrepo", |
| 205 |
"key": "MY_SECRET", |
| 206 |
"state": "absent", |
| 207 |
"token": "ghp_test_token", |
| 208 |
"_ansible_check_mode": True, |
| 209 |
} |
| 210 |
): |
| 211 |
with pytest.raises(AnsibleExitJson) as exc: |
| 212 |
github_secrets.main() |
| 213 |
|
| 214 |
result = exc.value.args[0] |
| 215 |
assert result["changed"] is True |
| 216 |
assert result["result"]["status"] == 204 |
| 217 |
assert result["result"]["response"] == "Secret deleted" |
| 218 |
|
| 219 |
|
| 220 |
def test_update_empty_repo_secret(fetch_url_mock): |
| 221 |
fetch_url_mock.side_effect = [ |
| 222 |
make_fetch_url_response(PUBLIC_KEY_PAYLOAD), |
| 223 |
make_fetch_url_response({}, status=204), |
| 224 |
] |
| 225 |
|
| 226 |
with set_module_args( |
| 227 |
{ |
| 228 |
"organization": "myorg", |
| 229 |
"repository": "myrepo", |
| 230 |
"key": "MY_SECRET", |
| 231 |
"value": "", |
| 232 |
"state": "present", |
| 233 |
"token": "ghp_test_token", |
| 234 |
} |
| 235 |
): |
| 236 |
with pytest.raises(AnsibleExitJson) as exc: |
| 237 |
github_secrets.main() |
| 238 |
|
| 239 |
result = exc.value.args[0] |
| 240 |
assert result["changed"] is True |
| 241 |
assert result["result"]["response"] == "Secret updated" |
| 242 |
|
| 243 |
|
| 244 |
def test_update_missing_value_repo_secret(fetch_url_mock): |
| 245 |
fetch_url_mock.side_effect = [ |
| 246 |
make_fetch_url_response(PUBLIC_KEY_PAYLOAD), |
| 247 |
make_fetch_url_response({}, status=204), |
| 248 |
] |
| 249 |
|
| 250 |
with set_module_args( |
| 251 |
{ |
| 252 |
"organization": "myorg", |
| 253 |
"repository": "myrepo", |
| 254 |
"key": "MY_SECRET", |
| 255 |
"state": "present", |
| 256 |
"token": "ghp_test_token", |
| 257 |
} |
| 258 |
): |
| 259 |
with pytest.raises(AnsibleFailJson) as exc: |
| 260 |
github_secrets.main() |
| 261 |
|
| 262 |
assert "the following are missing: value" in exc.value.args[0]["msg"] |
| 263 |
|
| 264 |
|
| 265 |
def test_delete_repo_secret(fetch_url_mock): |
| 266 |
fetch_url_mock.return_value = make_fetch_url_response({}, status=204) |
| 267 |
|
| 268 |
with set_module_args( |
| 269 |
{ |
| 270 |
"organization": "myorg", |
| 271 |
"repository": "myrepo", |
| 272 |
"key": "MY_SECRET", |
| 273 |
"state": "absent", |
| 274 |
"token": "ghp_test_token", |
| 275 |
} |
| 276 |
): |
| 277 |
with pytest.raises(AnsibleExitJson) as exc: |
| 278 |
github_secrets.main() |
| 279 |
|
| 280 |
result = exc.value.args[0] |
| 281 |
assert result["changed"] is True |
| 282 |
assert result["result"]["status"] == 204 |
| 283 |
assert result["result"]["response"] == "Secret deleted" |
| 284 |
|
| 285 |
|
| 286 |
def test_delete_dne_repo_secret(fetch_url_mock): |
| 287 |
fetch_url_mock.return_value = make_fetch_url_response({}, status=404) |
| 288 |
|
| 289 |
with set_module_args( |
| 290 |
{ |
| 291 |
"organization": "myorg", |
| 292 |
"repository": "myrepo", |
| 293 |
"key": "DOES_NOT_EXIST", |
| 294 |
"state": "absent", |
| 295 |
"token": "ghp_test_token", |
| 296 |
} |
| 297 |
): |
| 298 |
with pytest.raises(AnsibleExitJson) as exc: |
| 299 |
github_secrets.main() |
| 300 |
|
| 301 |
result = exc.value.args[0] |
| 302 |
assert result["changed"] is False |
| 303 |
assert result["result"]["status"] == 404 |
| 304 |
assert result["result"]["response"] == "Secret not found" |
| 305 |
|
| 306 |
|
| 307 |
def test_fail_get_public_key(fetch_url_mock): |
| 308 |
fetch_url_mock.return_value = make_fetch_url_response({}, status=403) |
| 309 |
|
| 310 |
with set_module_args( |
| 311 |
{ |
| 312 |
"organization": "myorg", |
| 313 |
"repository": "myrepo", |
| 314 |
"key": "MY_SECRET", |
| 315 |
"value": "secret_value", |
| 316 |
"state": "present", |
| 317 |
"token": "ghp_test_token", |
| 318 |
} |
| 319 |
): |
| 320 |
with pytest.raises(AnsibleFailJson) as exc: |
| 321 |
github_secrets.main() |
| 322 |
assert "Failed to get public key" in exc.value.args[0]["msg"] |
| 323 |
|
| 324 |
|
| 325 |
def test_fail_upsert_secret(fetch_url_mock): |
| 326 |
fetch_url_mock.side_effect = [ |
| 327 |
make_fetch_url_response(PUBLIC_KEY_PAYLOAD), |
| 328 |
make_fetch_url_response({}, status=422), |
| 329 |
] |
| 330 |
|
| 331 |
with set_module_args( |
| 332 |
{ |
| 333 |
"organization": "myorg", |
| 334 |
"repository": "myrepo", |
| 335 |
"key": "MY_SECRET", |
| 336 |
"value": "secret_value", |
| 337 |
"state": "present", |
| 338 |
"token": "ghp_test_token", |
| 339 |
} |
| 340 |
): |
| 341 |
with pytest.raises(AnsibleFailJson) as exc: |
| 342 |
github_secrets.main() |
| 343 |
assert "Failed to upsert secret" in exc.value.args[0]["msg"] |
| 344 |
|
| 345 |
|
| 346 |
def test_fail_delete_secret(fetch_url_mock): |
| 347 |
fetch_url_mock.return_value = make_fetch_url_response({}, status=503) |
| 348 |
|
| 349 |
with set_module_args( |
| 350 |
{ |
| 351 |
"organization": "myorg", |
| 352 |
"repository": "myrepo", |
| 353 |
"key": "MY_SECRET", |
| 354 |
"state": "absent", |
| 355 |
"token": "ghp_test_token", |
| 356 |
} |
| 357 |
): |
| 358 |
with pytest.raises(AnsibleFailJson) as exc: |
| 359 |
github_secrets.main() |
| 360 |
assert "Failed to delete secret" in exc.value.args[0]["msg"] |