Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove token enforcement for true tokenless endpoints #533

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions codecov_cli/helpers/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ def send_get_request(
return request_result(get(url=url, headers=headers, params=params))


def get_token_header_or_fail(token: str) -> dict:
def get_token_header_or_fail(token: Optional[str]) -> dict:
if token is None:
raise click.ClickException(
"Codecov token not found. Please provide Codecov token with -t flag."
)
return {"Authorization": f"token {token}"}


def get_token_header(token: str) -> Optional[dict]:
def get_token_header(token: Optional[str]) -> Optional[dict]:
if token is None:
return None
return {"Authorization": f"token {token}"}
Expand Down
8 changes: 4 additions & 4 deletions codecov_cli/services/commit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import typing

from codecov_cli.helpers.config import CODECOV_API_URL
from codecov_cli.helpers.encoder import decode_slug, encode_slug
from codecov_cli.helpers.encoder import encode_slug
from codecov_cli.helpers.request import (
get_token_header_or_fail,
get_token_header,
log_warnings_and_errors_if_any,
send_post_request,
)
Expand All @@ -19,7 +19,7 @@ def create_commit_logic(
pr: typing.Optional[str],
branch: typing.Optional[str],
slug: typing.Optional[str],
token: str,
token: typing.Optional[str],
service: typing.Optional[str],
enterprise_url: typing.Optional[str] = None,
fail_on_error: bool = False,
Expand Down Expand Up @@ -61,7 +61,7 @@ def send_commit_data(
branch = tokenless # type: ignore
logger.info("The PR is happening in a forked repo. Using tokenless upload.")
else:
headers = get_token_header_or_fail(token)
headers = get_token_header(token)

data = {
"branch": branch,
Expand Down
14 changes: 6 additions & 8 deletions codecov_cli/services/report/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import json
import logging
import time

import requests
import typing

from codecov_cli.helpers import request
from codecov_cli.helpers.config import CODECOV_API_URL
from codecov_cli.helpers.encoder import decode_slug, encode_slug
from codecov_cli.helpers.encoder import encode_slug
from codecov_cli.helpers.request import (
get_token_header,
get_token_header_or_fail,
log_warnings_and_errors_if_any,
request_result,
send_post_request,
Expand All @@ -24,7 +22,7 @@ def create_report_logic(
code: str,
slug: str,
service: str,
token: str,
token: typing.Optional[str],
enterprise_url: str,
pull_request_number: int,
fail_on_error: bool = False,
Expand Down Expand Up @@ -70,7 +68,7 @@ def create_report_results_logic(
code: str,
slug: str,
service: str,
token: str,
token: typing.Optional[str],
enterprise_url: str,
fail_on_error: bool = False,
args: dict = None,
Expand Down Expand Up @@ -103,7 +101,7 @@ def send_reports_result_request(
data = {
"cli_args": args,
}
headers = get_token_header_or_fail(token)
headers = get_token_header(token)
upload_url = enterprise_url or CODECOV_API_URL
url = f"{upload_url}/upload/{service}/{encoded_slug}/commits/{commit_sha}/reports/{report_code}/results"
return send_post_request(url=url, data=data, headers=headers)
Expand All @@ -118,7 +116,7 @@ def send_reports_result_get_request(
enterprise_url,
fail_on_error=False,
):
headers = get_token_header_or_fail(token)
headers = get_token_header(token)
nora-codecov marked this conversation as resolved.
Show resolved Hide resolved
upload_url = enterprise_url or CODECOV_API_URL
url = f"{upload_url}/upload/{service}/{encoded_slug}/commits/{commit_sha}/reports/{report_code}/results"
number_tries = 0
Expand Down
2 changes: 1 addition & 1 deletion codecov_cli/services/upload/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def do_upload_logic(
pull_request_number: typing.Optional[str],
report_code: str,
slug: typing.Optional[str],
token: str,
token: typing.Optional[str],
upload_file_type: str = "coverage",
use_legacy_uploader: bool = False,
):
Expand Down
2 changes: 1 addition & 1 deletion codecov_cli/services/upload/upload_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def send_upload_data(
self,
upload_data: UploadCollectionResult,
commit_sha: str,
token: str,
token: typing.Optional[str],
env_vars: typing.Dict[str, str],
report_code: str,
upload_file_type: str = "coverage",
Expand Down
4 changes: 2 additions & 2 deletions codecov_cli/services/upload_completion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from codecov_cli.helpers.config import CODECOV_API_URL
from codecov_cli.helpers.encoder import encode_slug
from codecov_cli.helpers.request import (
get_token_header,
get_token_header_or_fail,
log_warnings_and_errors_if_any,
send_post_request,
)
Expand All @@ -22,7 +22,7 @@ def upload_completion_logic(
args=None,
):
encoded_slug = encode_slug(slug)
headers = get_token_header(token)
headers = get_token_header_or_fail(token)
upload_url = enterprise_url or CODECOV_API_URL
url = f"{upload_url}/upload/{git_service}/{encoded_slug}/commits/{commit_sha}/upload-complete"
data = {
Expand Down
1 change: 1 addition & 0 deletions tests/commands/test_process_test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def test_process_test_results(
# Ensure that there's an output
assert result.output


def test_process_test_results_create_github_message(
mocker,
tmpdir,
Expand Down
30 changes: 30 additions & 0 deletions tests/services/commit/test_commit_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,33 @@ def test_commit_sender_with_forked_repo(mocker):
},
headers=None,
)


def test_commit_without_token(mocker):
mocked_response = mocker.patch(
"codecov_cli.services.commit.send_post_request",
return_value=mocker.MagicMock(status_code=200, text="success"),
)

res = send_commit_data(
"commit_sha",
"parent_sha",
"1",
"branch",
"codecov::::codecov-cli",
None,
"github",
None,
None,
)
mocked_response.assert_called_with(
url="https://api.codecov.io/upload/github/codecov::::codecov-cli/commits",
data={
"branch": "branch",
"cli_args": None,
"commitid": "commit_sha",
"parent_commit_id": "parent_sha",
"pullid": "1",
},
headers=None,
)
15 changes: 15 additions & 0 deletions tests/services/empty_upload/test_empty_upload.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import uuid

import click
import pytest
from click.testing import CliRunner

from codecov_cli.services.empty_upload import empty_upload_logic
Expand Down Expand Up @@ -147,3 +149,16 @@ def test_empty_upload_force(mocker):
assert res.error is None
assert res.warnings == []
mocked_response.assert_called_once()


def test_empty_upload_no_token(mocker):
mocked_response = mocker.patch("codecov_cli.helpers.request.requests.post")
with pytest.raises(click.ClickException) as exp:
empty_upload_logic(
"commit_sha", "owner/repo", None, "service", None, False, False, None
)

assert "Codecov token not found. Please provide Codecov token with -t flag." in str(
exp.value
)
mocked_response.assert_not_called()
39 changes: 34 additions & 5 deletions tests/services/report/test_report_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ def test_report_results_request_200(mocker):
mocked_response.assert_called_once()


def test_report_results_request_no_token(mocker):
mocked_response = mocker.patch(
"codecov_cli.helpers.request.requests.post",
return_value=mocker.MagicMock(status_code=200),
)
res = send_reports_result_request(
"commit_sha", "report_code", "encoded_slug", "service", None, None, None
)
assert res.error is None
assert res.warnings == []
mocked_response.assert_called_once()


def test_report_results_403(mocker):
mocked_response = mocker.patch(
"codecov_cli.helpers.request.requests.post",
Expand All @@ -127,7 +140,7 @@ def test_report_results_403(mocker):

def test_get_report_results_200_completed(mocker, capsys):
mocked_response = mocker.patch(
"codecov_cli.services.report.requests.get",
"codecov_cli.helpers.request.requests.get",
return_value=mocker.MagicMock(
status_code=200,
text='{"state": "completed", "result": {"state": "failure","message": "33.33% of diff hit (target 77.77%)"}}',
Expand All @@ -147,11 +160,27 @@ def test_get_report_results_200_completed(mocker, capsys):
) in output


def test_get_report_results_no_token(mocker, capsys):
mocked_response = mocker.patch(
"codecov_cli.helpers.request.requests.get",
return_value=mocker.MagicMock(
status_code=200,
text='{"state": "completed", "result": {"state": "failure","message": "33.33% of diff hit (target 77.77%)"}}',
),
)
res = send_reports_result_get_request(
"commit_sha", "report_code", "encoded_slug", "service", None, None
)
assert res.error is None
assert res.warnings == []
mocked_response.assert_called_once()


@patch("codecov_cli.services.report.MAX_NUMBER_TRIES", 1)
def test_get_report_results_200_pending(mocker, capsys):
mocker.patch("codecov_cli.services.report.time.sleep")
mocked_response = mocker.patch(
"codecov_cli.services.report.requests.get",
"codecov_cli.helpers.request.requests.get",
return_value=mocker.MagicMock(
status_code=200, text='{"state": "pending", "result": {}}'
),
Expand All @@ -169,7 +198,7 @@ def test_get_report_results_200_pending(mocker, capsys):

def test_get_report_results_200_error(mocker, capsys):
mocked_response = mocker.patch(
"codecov_cli.services.report.requests.get",
"codecov_cli.helpers.request.requests.get",
return_value=mocker.MagicMock(
status_code=200, text='{"state": "error", "result": {}}'
),
Expand All @@ -190,7 +219,7 @@ def test_get_report_results_200_error(mocker, capsys):

def test_get_report_results_200_undefined_state(mocker, capsys):
mocked_response = mocker.patch(
"codecov_cli.services.report.requests.get",
"codecov_cli.helpers.request.requests.get",
return_value=mocker.MagicMock(
status_code=200, text='{"state": "undefined_state", "result": {}}'
),
Expand All @@ -208,7 +237,7 @@ def test_get_report_results_200_undefined_state(mocker, capsys):

def test_get_report_results_401(mocker, capsys):
mocked_response = mocker.patch(
"codecov_cli.services.report.requests.get",
"codecov_cli.helpers.request.requests.get",
return_value=mocker.MagicMock(
status_code=401, text='{"detail": "Invalid token."}'
),
Expand Down
24 changes: 22 additions & 2 deletions tests/services/report/test_report_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

def test_send_create_report_request_200(mocker):
mocked_response = mocker.patch(
"codecov_cli.services.report.requests.post",
"codecov_cli.helpers.request.requests.post",
return_value=mocker.MagicMock(status_code=200),
)
res = send_create_report_request(
Expand All @@ -27,9 +27,29 @@ def test_send_create_report_request_200(mocker):
mocked_response.assert_called_once()


def test_send_create_report_request_no_token(mocker):
mocked_response = mocker.patch(
"codecov_cli.helpers.request.requests.post",
return_value=mocker.MagicMock(status_code=200),
)
res = send_create_report_request(
"commit_sha",
"code",
"github",
None,
"owner::::repo",
"enterprise_url",
1,
None,
)
assert res.error is None
assert res.warnings == []
mocked_response.assert_called_once()


def test_send_create_report_request_403(mocker):
mocked_response = mocker.patch(
"codecov_cli.services.report.requests.post",
"codecov_cli.helpers.request.requests.post",
return_value=mocker.MagicMock(status_code=403, text="Permission denied"),
)
res = send_create_report_request(
Expand Down
14 changes: 14 additions & 0 deletions tests/services/upload_completion/test_upload_completion.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import uuid

import click
import pytest
from click.testing import CliRunner

from codecov_cli.services.upload_completion import upload_completion_logic
Expand Down Expand Up @@ -93,6 +95,18 @@ def test_upload_completion_200(mocker):
mocked_response.assert_called_once()


def test_upload_completion_no_token(mocker):
mocked_response = mocker.patch(
"codecov_cli.helpers.request.requests.post",
)
with pytest.raises(click.ClickException) as exp:
upload_completion_logic("commit_sha", "owner/repo", None, "service", None)
assert "Codecov token not found. Please provide Codecov token with -t flag." in str(
exp.value
)
mocked_response.assert_not_called()


def test_upload_completion_403(mocker):
mocked_response = mocker.patch(
"codecov_cli.helpers.request.requests.post",
Expand Down
Loading