import pytest from repo_cloner.lib.cred_helper import * from repo_cloner.lib import cred_helper import os from unittest.mock import MagicMock, patch from cloner_test_fixtures import path_repo_base, support_data_path from git import Repo def test_gen_gh_token_candidates(monkeypatch): with monkeypatch.context() as mp: mp.setenv("HOME", "/mocked/home") candidates = gen_gh_token_candidates() assert candidates[0].as_posix() == os.path.join(os.getcwd(), ".gh-token") assert candidates[1].as_posix() == "/mocked/home/.config/gh-token" assert candidates[2].as_posix() == "/etc/cloner-gh-token" assert len(candidates) == 3 def test_load_gh_token(tmp_path): # no token token_file = tmp_path.joinpath("gh-token") load_gh_token(token_file) assert cred_helper.token is None token_file.write_text("test-token\n") load_gh_token(token_file) assert cred_helper.token == "test-token" def test_init_gh_token(tmp_path, monkeypatch, caplog): with patch("repo_cloner.lib.cred_helper.load_gh_token", autospec = True) as p: cred_helper.token = None init_gh_token() assert p.call_count == 3 # "loaded" token with monkeypatch.context() as mp: mp.setattr(os, "getcwd", lambda: tmp_path.as_posix()) tmp_path.joinpath(".gh-token").write_text("testtoken") caplog.set_level(0) init_gh_token() assert "Token succesfully loaded" in caplog.text def test_config_try_override(tmp_path, path_repo_base, monkeypatch): tmp_path.joinpath("git").mkdir() with monkeypatch.context() as mp: mp.setenv("XDG_CONFIG_HOME", tmp_path.as_posix()) r = Repo(path_repo_base) cfgw = r.config_writer("user") config_try_override(cfgw, "user", "name", "Mocker") assert cfgw.get("user", "name") == "Mocker" config_try_override(cfgw, "user", "name", "Loser") assert cfgw.get("user", "name") == "Loser" x = tmp_path.joinpath("git", "config") assert x.is_file() x = x.read_text() assert x == "[user]\n name = Loser\n" def test_prepare_git_auth(tmp_path, monkeypatch): tmp_path.joinpath("git").mkdir() cred_helper.token = None with monkeypatch.context() as mp: mp.setenv("XDG_CONFIG_HOME", tmp_path.as_posix()) prepare_git_auth(tmp_path.as_posix()) x = tmp_path.joinpath("git", "config") assert x.exists() config = x.read_text() assert config == \ f"[credential]\n" \ f" helper = store --file={tmp_path.as_posix()}/auth/git-credentials\n" \ "[core]\n" \ f" sshcommand = ssh -i {tmp_path.as_posix()}/auth/ssh/identity -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o BatchMode=yes -q\n" def test_prepare_git_auth_token(tmp_path, monkeypatch): # tmp_path.joinpath("git").mkdir() with monkeypatch.context() as mp: mp.setenv("XDG_CONFIG_HOME", tmp_path.as_posix()) cred_helper.token = "token123" prepare_git_auth(tmp_path.as_posix()) x = tmp_path.joinpath("git", "config") assert x.exists() config = x.read_text() assert config == \ "[url \"https://token123:x-oauth-basic@github.com/\"]\n" \ " insteadOf = https://github.com/\n" \ f"[credential]\n" \ f" helper = store --file={tmp_path.as_posix()}/auth/git-credentials\n" \ "[core]\n" \ f" sshcommand = ssh -i {tmp_path.as_posix()}/auth/ssh/identity -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o BatchMode=yes -q\n"