|
| 1 | +import uuid |
| 2 | + |
| 3 | +from click.testing import CliRunner |
| 4 | + |
| 5 | +from codecov_cli.services.empty_upload import empty_upload_logic |
| 6 | +from codecov_cli.types import RequestError, RequestResult, RequestResultWarning |
| 7 | +from tests.test_helpers import parse_outstreams_into_log_lines |
| 8 | + |
| 9 | + |
| 10 | +def test_empty_upload_with_warnings(mocker): |
| 11 | + mock_send_commit_data = mocker.patch( |
| 12 | + "codecov_cli.services.empty_upload.send_post_request", |
| 13 | + return_value=RequestResult( |
| 14 | + error=None, |
| 15 | + warnings=[RequestResultWarning(message="somewarningmessage")], |
| 16 | + status_code=201, |
| 17 | + text="", |
| 18 | + ), |
| 19 | + ) |
| 20 | + runner = CliRunner() |
| 21 | + with runner.isolation() as outstreams: |
| 22 | + res = empty_upload_logic( |
| 23 | + "commit_sha", |
| 24 | + "owner/repo", |
| 25 | + uuid.uuid4(), |
| 26 | + "service", |
| 27 | + ) |
| 28 | + out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue()) |
| 29 | + assert out_bytes == [ |
| 30 | + ("info", "Empty Upload process had 1 warning"), |
| 31 | + ("warning", "Warning 1: somewarningmessage"), |
| 32 | + ] |
| 33 | + assert res == mock_send_commit_data.return_value |
| 34 | + mock_send_commit_data.assert_called_once() |
| 35 | + |
| 36 | + |
| 37 | +def test_empty_upload_with_error(mocker): |
| 38 | + mock_send_commit_data = mocker.patch( |
| 39 | + "codecov_cli.services.empty_upload.send_post_request", |
| 40 | + return_value=RequestResult( |
| 41 | + error=RequestError( |
| 42 | + code="HTTP Error 403", |
| 43 | + description="Permission denied", |
| 44 | + params={}, |
| 45 | + ), |
| 46 | + warnings=[], |
| 47 | + status_code=403, |
| 48 | + text="Permission denied", |
| 49 | + ), |
| 50 | + ) |
| 51 | + runner = CliRunner() |
| 52 | + with runner.isolation() as outstreams: |
| 53 | + res = empty_upload_logic( |
| 54 | + "commit_sha", |
| 55 | + "owner/repo", |
| 56 | + uuid.uuid4(), |
| 57 | + "service", |
| 58 | + ) |
| 59 | + |
| 60 | + print(outstreams) |
| 61 | + out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue()) |
| 62 | + assert out_bytes == [("error", "Empty Upload failed: Permission denied")] |
| 63 | + assert res == mock_send_commit_data.return_value |
| 64 | + mock_send_commit_data.assert_called_once() |
| 65 | + |
| 66 | + |
| 67 | +def test_empty_upload_200(mocker): |
| 68 | + mocked_response = mocker.patch( |
| 69 | + "codecov_cli.helpers.request.requests.post", |
| 70 | + return_value=mocker.MagicMock(status_code=200), |
| 71 | + ) |
| 72 | + token = uuid.uuid4() |
| 73 | + res = empty_upload_logic("commit_sha", "owner/repo", token, "service") |
| 74 | + assert res.error is None |
| 75 | + assert res.warnings == [] |
| 76 | + mocked_response.assert_called_once() |
| 77 | + |
| 78 | + |
| 79 | +def test_empty_upload_403(mocker): |
| 80 | + mocked_response = mocker.patch( |
| 81 | + "codecov_cli.helpers.request.requests.post", |
| 82 | + return_value=mocker.MagicMock(status_code=403, text="Permission denied"), |
| 83 | + ) |
| 84 | + token = uuid.uuid4() |
| 85 | + res = empty_upload_logic("commit_sha", "owner/repo", token, "service") |
| 86 | + assert res.error == RequestError( |
| 87 | + code="HTTP Error 403", |
| 88 | + description="Permission denied", |
| 89 | + params={}, |
| 90 | + ) |
| 91 | + mocked_response.assert_called_once() |
0 commit comments