|
| 1 | +"""Tests ensuring that an env-provided token can be found.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from click.testing import CliRunner |
| 6 | +from pytest import MonkeyPatch |
| 7 | +from pytest_mock import MockerFixture |
| 8 | + |
| 9 | +from codecov_cli.commands import upload |
| 10 | +from codecov_cli.main import cli |
| 11 | + |
| 12 | + |
| 13 | +def test_no_token_anywhere( |
| 14 | + mocker: MockerFixture, |
| 15 | + monkeypatch: MonkeyPatch, |
| 16 | + tmp_path: Path, |
| 17 | +) -> None: |
| 18 | + """Test that a missing token produces `NOTOKEN` in logs.""" |
| 19 | + # NOTE: The pytest's `caplog` fixture is not used in this test as it |
| 20 | + # NOTE: doesn't play well with Click's testing CLI runner, and does |
| 21 | + # NOTE: not capture any log entries for mysterious reasons. |
| 22 | + # |
| 23 | + # Refs: |
| 24 | + # * https://github.com/pallets/click/issues/2573#issuecomment-1649773563 |
| 25 | + # * https://github.com/pallets/click/issues/1763#issuecomment-767687608 |
| 26 | + monkeypatch.chdir(tmp_path) |
| 27 | + |
| 28 | + mocker.patch.object(upload, "do_upload_logic") |
| 29 | + do_upload_cmd_spy = mocker.spy(upload, "do_upload_logic") |
| 30 | + debug_log_spy = mocker.spy(upload.logger, "debug") |
| 31 | + |
| 32 | + cov_upload_cmd_output = ( |
| 33 | + CliRunner() |
| 34 | + .invoke( |
| 35 | + cli, |
| 36 | + [ |
| 37 | + "-v", # <- NOTE: this is the only way to turn on debug logger |
| 38 | + "do-upload", |
| 39 | + "--commit-sha=deadbeef", |
| 40 | + ], |
| 41 | + obj={}, |
| 42 | + ) |
| 43 | + .output |
| 44 | + ) |
| 45 | + |
| 46 | + assert ( |
| 47 | + debug_log_spy.call_args[1]["extra"]["extra_log_attributes"]["token"] |
| 48 | + == "NOTOKEN" |
| 49 | + ) |
| 50 | + assert '"token": "NOTOKEN"' in cov_upload_cmd_output |
| 51 | + assert do_upload_cmd_spy.call_args[-1]["token"] is None |
| 52 | + |
| 53 | + |
| 54 | +def test_cli_token_masked_in_logs( |
| 55 | + mocker: MockerFixture, |
| 56 | + monkeypatch: MonkeyPatch, |
| 57 | + tmp_path: Path, |
| 58 | +) -> None: |
| 59 | + """Test that a present token is masked in logs.""" |
| 60 | + # NOTE: The pytest's `caplog` fixture is not used in this test as it |
| 61 | + # NOTE: doesn't play well with Click's testing CLI runner, and does |
| 62 | + # NOTE: not capture any log entries for mysterious reasons. |
| 63 | + # |
| 64 | + # Refs: |
| 65 | + # * https://github.com/pallets/click/issues/2573#issuecomment-1649773563 |
| 66 | + # * https://github.com/pallets/click/issues/1763#issuecomment-767687608 |
| 67 | + monkeypatch.chdir(tmp_path) |
| 68 | + |
| 69 | + mocker.patch.object(upload, "do_upload_logic") |
| 70 | + do_upload_cmd_spy = mocker.spy(upload, "do_upload_logic") |
| 71 | + debug_log_spy = mocker.spy(upload.logger, "debug") |
| 72 | + |
| 73 | + cov_upload_cmd_output = ( |
| 74 | + CliRunner() |
| 75 | + .invoke( |
| 76 | + cli, |
| 77 | + [ |
| 78 | + "-v", # <- NOTE: this is the only way to turn on debug logger |
| 79 | + "do-upload", |
| 80 | + "--commit-sha=deadbeef", |
| 81 | + "--token=sentinel-value", |
| 82 | + ], |
| 83 | + obj={}, |
| 84 | + ) |
| 85 | + .output |
| 86 | + ) |
| 87 | + |
| 88 | + assert ( |
| 89 | + debug_log_spy.call_args[1]["extra"]["extra_log_attributes"]["token"] |
| 90 | + == "s" + "*" * 18 |
| 91 | + ) |
| 92 | + assert f'"token": "s{"*" * 18}"' in cov_upload_cmd_output |
| 93 | + assert do_upload_cmd_spy.call_args[-1]["token"] == "sentinel-value" |
0 commit comments