|
| 1 | +from click.testing import CliRunner |
| 2 | +from github_to_sqlite import cli |
| 3 | +import json |
| 4 | +import os |
| 5 | +import pytest |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def mocked_starred(requests_mock): |
| 10 | + requests_mock.get("https://api.github.com/user", json={"id": 1, "login": "test"}) |
| 11 | + m = requests_mock.get("https://api.github.com/user/starred", json=[]) |
| 12 | + return m |
| 13 | + |
| 14 | + |
| 15 | +def test_auth_command(): |
| 16 | + runner = CliRunner() |
| 17 | + with runner.isolated_filesystem(): |
| 18 | + assert [] == os.listdir(".") |
| 19 | + result = runner.invoke(cli.cli, ["auth"], input="zzz") |
| 20 | + assert 0 == result.exit_code |
| 21 | + assert ["auth.json"] == os.listdir(".") |
| 22 | + assert {"github_personal_token": "zzz"} == json.load(open("auth.json")) |
| 23 | + |
| 24 | + |
| 25 | +def test_auth_file(mocked_starred): |
| 26 | + runner = CliRunner() |
| 27 | + with runner.isolated_filesystem(): |
| 28 | + open("auth.json", "w").write(json.dumps({"github_personal_token": "xxx"})) |
| 29 | + result = runner.invoke( |
| 30 | + cli.cli, ["starred", "starred.db"], catch_exceptions=False |
| 31 | + ) |
| 32 | + assert 0 == result.exit_code |
| 33 | + assert mocked_starred.called |
| 34 | + assert "token xxx" == mocked_starred.last_request.headers["authorization"] |
| 35 | + |
| 36 | + |
| 37 | +def test_auth_environment_variable(mocked_starred, monkeypatch): |
| 38 | + monkeypatch.setenv("GITHUB_TOKEN", "xyz") |
| 39 | + runner = CliRunner() |
| 40 | + with runner.isolated_filesystem(): |
| 41 | + result = runner.invoke( |
| 42 | + cli.cli, ["starred", "starred.db"], catch_exceptions=False |
| 43 | + ) |
| 44 | + assert 0 == result.exit_code |
| 45 | + assert mocked_starred.called |
| 46 | + assert "token xyz" == mocked_starred.last_request.headers["authorization"] |
0 commit comments