Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding unit tests to existing code #10

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ requires-python = ">= 3.8"
dynamic = ["version"]
dependencies = [
"PyGithub",
"salt>=3005",
]

[project.readme]
Expand Down
42 changes: 40 additions & 2 deletions tests/unit/modules/test_github.py
Original file line number Diff line number Diff line change
@@ -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__
49 changes: 49 additions & 0 deletions tests/unit/utils/test_github.py
Original file line number Diff line number Diff line change
@@ -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
Loading