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/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__ 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