# Path: gg/main.py
# class Config:
#     def __init__(self):
#         self.verbose = False  # default
#         self.configfile = DEFAULT_CONFIGFILE
#         try:
#             self.repo = get_repo()
#         except git.InvalidGitRepositoryError as exception:
#             error_out(f"{exception.args[0]} is not a git repository")
# 
# Path: gg/builtins/github.py
# @cli.group()
# @click.option(
#     "-u",
#     "--github-url",
#     default=GITHUB_URL,
#     help=f"URL to GitHub instance (default: {GITHUB_URL})",
# )
# @pass_config
# def github(config, github_url):
#     """For setting up a GitHub API token."""
#     config.github_url = github_url
# 
# Path: gg/testing.py
# class Response:
#     def __init__(self, content, status_code=200):
#         self.content = content
#         self.status_code = status_code
# 
#     def json(self):
#         return self.content


import json
from click.testing import CliRunner
from gg.main import Config
from gg.builtins import github
from gg.testing import Response
    runner = CliRunner()
    config = Config()
    config.configfile = temp_configfile
    config.github_url = "https://example.com"
    result = runner.invoke(github.token, [], obj=config)
    assert result.exit_code == 0
    assert not result.exception

    with open(temp_configfile) as f:
        saved = json.load(f)
        assert "GITHUB" in saved
        assert saved["GITHUB"]["login"] == "peterbe"
        assert saved["GITHUB"]["token"] == "somelongapitokenhere"
        assert saved["GITHUB"]["github_url"] == "https://example.com"


def test_token_argument(temp_configfile, mocker):
    rget = mocker.patch("requests.get")

    def mocked_get(url, headers):
        assert url == "https://example.com/user"
        assert headers["Authorization"] == "token somelongapitokenhere"
        return Response({"name": "Peter", "login": "peterbe"})

    rget.side_effect = mocked_get

    runner = CliRunner()
    config = Config()
    config.configfile = temp_configfile
    config.github_url = "https://example.com"

# Path: gg/main.py
# class Config:
#     def __init__(self):
#         self.verbose = False  # default
#         self.configfile = DEFAULT_CONFIGFILE
#         try:
#             self.repo = get_repo()
#         except git.InvalidGitRepositoryError as exception:
#             error_out(f"{exception.args[0]} is not a git repository")
# 
# Path: gg/builtins/github.py
# @cli.group()
# @click.option(
#     "-u",
#     "--github-url",
#     default=GITHUB_URL,
#     help=f"URL to GitHub instance (default: {GITHUB_URL})",
# )
# @pass_config
# def github(config, github_url):
#     """For setting up a GitHub API token."""
#     config.github_url = github_url
# 
# Path: gg/testing.py
# class Response:
#     def __init__(self, content, status_code=200):
#         self.content = content
#         self.status_code = status_code
# 
#     def json(self):
#         return self.content


# Path: tests/test_github.py
import json
from click.testing import CliRunner
from gg.main import Config
from gg.builtins import github
from gg.testing import Response

    runner = CliRunner()
    config = Config()
    config.configfile = temp_configfile
    config.github_url = "https://example.com"
    result = runner.invoke(github.token, [], obj=config)
    assert result.exit_code == 0
    assert not result.exception

    with open(temp_configfile) as f:
        saved = json.load(f)
        assert "GITHUB" in saved
        assert saved["GITHUB"]["login"] == "peterbe"
        assert saved["GITHUB"]["token"] == "somelongapitokenhere"
        assert saved["GITHUB"]["github_url"] == "https://example.com"


def test_token_argument(temp_configfile, mocker):
    rget = mocker.patch("requests.get")

    def mocked_get(url, headers):
        assert url == "https://example.com/user"
        assert headers["Authorization"] == "token somelongapitokenhere"
        return Response({"name": "Peter", "login": "peterbe"})

    rget.side_effect = mocked_get

    runner = CliRunner()
    config = Config()
    config.configfile = temp_configfile
    config.github_url = "https://example.com"

    result = runner.invoke(github.token, ["somelongapitokenhere"], obj=config)