From baea16e107bd22123fa00fa613b7846932d788a5 Mon Sep 17 00:00:00 2001 From: Shaffan Date: Wed, 7 Feb 2024 15:06:13 -0500 Subject: [PATCH 1/3] Create github.py --- tests/unit/utils/github.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 tests/unit/utils/github.py diff --git a/tests/unit/utils/github.py b/tests/unit/utils/github.py new file mode 100644 index 00000000..c9a341e8 --- /dev/null +++ b/tests/unit/utils/github.py @@ -0,0 +1,6 @@ +from saltext.github.utils import github + +def test_get_user_pubkeys(): + # should fail, becausse we only want to allow a list + users = {} + assert(github.get_user_pubkeys(users) == {"Error": "A list of users is expected"}) From 7b1ef60fb602561550a5831cf64c52b9522bc107 Mon Sep 17 00:00:00 2001 From: nicholasmhughes Date: Mon, 12 Feb 2024 19:25:11 -0500 Subject: [PATCH 2/3] add utils unit tests --- pyproject.toml | 1 + tests/unit/utils/github.py | 6 ---- tests/unit/utils/test_github.py | 49 +++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 6 deletions(-) delete mode 100644 tests/unit/utils/github.py create mode 100644 tests/unit/utils/test_github.py diff --git a/pyproject.toml b/pyproject.toml index c5be683c..c1f88a6b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,6 +36,7 @@ requires-python = ">= 3.8" dynamic = ["version"] dependencies = [ "PyGithub", + "salt>=3005", ] [project.readme] diff --git a/tests/unit/utils/github.py b/tests/unit/utils/github.py deleted file mode 100644 index c9a341e8..00000000 --- a/tests/unit/utils/github.py +++ /dev/null @@ -1,6 +0,0 @@ -from saltext.github.utils import github - -def test_get_user_pubkeys(): - # should fail, becausse we only want to allow a list - users = {} - assert(github.get_user_pubkeys(users) == {"Error": "A list of users is expected"}) diff --git a/tests/unit/utils/test_github.py b/tests/unit/utils/test_github.py new file mode 100644 index 00000000..7503483e --- /dev/null +++ b/tests/unit/utils/test_github.py @@ -0,0 +1,49 @@ +from unittest.mock import MagicMock +from unittest.mock import patch + +import pytest +from saltext.github.utils import github + + +@pytest.fixture() +def users(): + return [ + "user1", + { + "user2": [ + "12345", + "67890", + ], + }, + ] + + +def test_get_user_pubkeys_not_a_list(): + # should fail, because we only want to allow a list + users = {} + assert github.get_user_pubkeys(users) == {"Error": "A list of users is expected"} + + +def test_get_user_pubkeys_empty_key_returns(users): + mock_query = MagicMock(return_value={"text": "{}"}) + expected = {"user1": {}, "user2": {}} + with patch("salt.utils.http.query", mock_query): + ret = github.get_user_pubkeys(users) + assert ret == expected + + +def test_get_user_pubkeys_key_returns(users): + mock_query = MagicMock( + side_effect=[ + { + "text": '[{"id": 1, "key": "ssh-rsa AAA..."}]', + }, + { + "text": '[{"id": 12345, "key": "ssh-rsa AAA..."},{"id": 99999, "key": "ssh-ed25519 AAA..."}]', + }, + ] + ) + expected = {"user1": {1: "ssh-rsa AAA..."}, "user2": {12345: "ssh-rsa AAA..."}} + with patch("salt.utils.http.query", mock_query): + ret = github.get_user_pubkeys(users) + assert ret == expected From 72aa24bd73f63d883ffdb9614a8106e47e2444ea Mon Sep 17 00:00:00 2001 From: nicholasmhughes Date: Mon, 12 Feb 2024 20:01:36 -0500 Subject: [PATCH 3/3] add stub of module unit tests --- tests/unit/modules/test_github.py | 42 +++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/tests/unit/modules/test_github.py b/tests/unit/modules/test_github.py index 02fecd24..86260d9d 100644 --- a/tests/unit/modules/test_github.py +++ b/tests/unit/modules/test_github.py @@ -1,13 +1,51 @@ +from unittest.mock import MagicMock +from unittest.mock import patch + import pytest -import salt.modules.test as testmod +import salt.modules.config as config_module import saltext.github.modules.github as github_module +from salt.exceptions import CommandExecutionError @pytest.fixture def configure_loader_modules(): module_globals = { - "__salt__": {"test.echo": testmod.echo}, + "__opts__": { + "valid": {"there": "found"}, + "github": { + "token": "abc1234", + "org_name": "my_organization", + "repo_name": "my_repo", + "allow_repo_privacy_changes": False, + }, + }, + "__salt__": {"config.option": config_module.option}, } return { + config_module: module_globals, github_module: module_globals, } + + +def test__get_config_value_bad_profile(): + with pytest.raises(CommandExecutionError): + github_module._get_config_value("not_there", "didnt_get_here") + + +def test__get_config_value_bad_config_name(): + with pytest.raises(CommandExecutionError): + github_module._get_config_value("valid", "not_there") + + +def test__get_config_value(): + ret = github_module._get_config_value("valid", "there") + assert ret == "found" + + +def test__get_client(): + mock_client = MagicMock() + assert not github_module.__context__ + with patch("github.Github", mock_client): + github_module._get_client("github") + mock_client.assert_called_with("abc1234", per_page=100) + assert "github.abc1234:my_organization" in github_module.__context__