-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Ensure that no-token case is actually logged
Prior to this patch, there was an unreachable code path that seems to have been intended for marking the case when no token is provided with the `"NOTOKEN"` string in the debug log output. However the logic behind choosing to show it contained two opposite checks. Said place in the logging utils, didn't have any coverage either. This change updates the code to make all of its branches reachable. Moreover, it adds test cases for all the branches that remain in the implementation.
- Loading branch information
Showing
2 changed files
with
99 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
"""Tests ensuring that an env-provided token can be found.""" | ||
|
||
from pathlib import Path | ||
|
||
from click.testing import CliRunner | ||
from pytest import MonkeyPatch | ||
from pytest_mock import MockerFixture | ||
|
||
from codecov_cli.commands import upload | ||
from codecov_cli.main import cli | ||
|
||
|
||
def test_no_token_anywhere( | ||
mocker: MockerFixture, | ||
monkeypatch: MonkeyPatch, | ||
tmp_path: Path, | ||
) -> None: | ||
"""Test that a missing token produces `NOTOKEN` in logs.""" | ||
# NOTE: The pytest's `caplog` fixture is not used in this test as it | ||
# NOTE: doesn't play well with Click's testing CLI runner, and does | ||
# NOTE: not capture any log entries for mysterious reasons. | ||
# | ||
# Refs: | ||
# * https://github.com/pallets/click/issues/2573#issuecomment-1649773563 | ||
# * https://github.com/pallets/click/issues/1763#issuecomment-767687608 | ||
monkeypatch.chdir(tmp_path) | ||
|
||
mocker.patch.object(upload, "do_upload_logic") | ||
do_upload_cmd_spy = mocker.spy(upload, "do_upload_logic") | ||
debug_log_spy = mocker.spy(upload.logger, "debug") | ||
|
||
cov_upload_cmd_output = ( | ||
CliRunner() | ||
.invoke( | ||
cli, | ||
[ | ||
"-v", # <- NOTE: this is the only way to turn on debug logger | ||
"do-upload", | ||
"--commit-sha=deadbeef", | ||
], | ||
obj={}, | ||
) | ||
.output | ||
) | ||
|
||
assert ( | ||
debug_log_spy.call_args[1]["extra"]["extra_log_attributes"]["token"] | ||
== "NOTOKEN" | ||
) | ||
assert '"token": "NOTOKEN"' in cov_upload_cmd_output | ||
assert do_upload_cmd_spy.call_args[-1]["token"] is None | ||
|
||
|
||
def test_cli_token_masked_in_logs( | ||
mocker: MockerFixture, | ||
monkeypatch: MonkeyPatch, | ||
tmp_path: Path, | ||
) -> None: | ||
"""Test that a present token is masked in logs.""" | ||
# NOTE: The pytest's `caplog` fixture is not used in this test as it | ||
# NOTE: doesn't play well with Click's testing CLI runner, and does | ||
# NOTE: not capture any log entries for mysterious reasons. | ||
# | ||
# Refs: | ||
# * https://github.com/pallets/click/issues/2573#issuecomment-1649773563 | ||
# * https://github.com/pallets/click/issues/1763#issuecomment-767687608 | ||
monkeypatch.chdir(tmp_path) | ||
|
||
mocker.patch.object(upload, "do_upload_logic") | ||
do_upload_cmd_spy = mocker.spy(upload, "do_upload_logic") | ||
debug_log_spy = mocker.spy(upload.logger, "debug") | ||
|
||
cov_upload_cmd_output = ( | ||
CliRunner() | ||
.invoke( | ||
cli, | ||
[ | ||
"-v", # <- NOTE: this is the only way to turn on debug logger | ||
"do-upload", | ||
"--commit-sha=deadbeef", | ||
"--token=sentinel-value", | ||
], | ||
obj={}, | ||
) | ||
.output | ||
) | ||
|
||
assert ( | ||
debug_log_spy.call_args[1]["extra"]["extra_log_attributes"]["token"] | ||
== "s" + "*" * 18 | ||
) | ||
assert f'"token": "s{"*" * 18}"' in cov_upload_cmd_output | ||
assert do_upload_cmd_spy.call_args[-1]["token"] == "sentinel-value" |