Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit 91f7f55

Browse files
add tests
1 parent d947bd4 commit 91f7f55

File tree

6 files changed

+36
-41
lines changed

6 files changed

+36
-41
lines changed

upload/helpers.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ def dispatch_upload_task(
756756

757757

758758
def validate_activated_repo(repository):
759-
if not repository.activated:
759+
if repository.active and not repository.activated:
760760
settings_url = f"{settings.CODECOV_DASHBOARD_URL}/{repository.author.service}/{repository.author.username}/{repository.name}/settings"
761761
raise ValidationError(
762762
f"This repository is deactivated. To resume uploading to it, please activate the repository in the codecov UI: {settings_url}"
@@ -790,15 +790,14 @@ def get_version_from_headers(headers):
790790
return "unknown-user-agent"
791791

792792

793-
def generate_upload_prometheus_metrics_labels(
793+
def generate_upload_sentry_metrics_tags(
794794
action,
795795
request,
796796
is_shelter_request,
797797
endpoint: Optional[str] = None,
798798
repository: Optional[Repository] = None,
799799
position: Optional[str] = None,
800800
upload_version: Optional[str] = None,
801-
include_empty_labels: bool = True,
802801
):
803802
metrics_tags = dict(
804803
agent=get_agent_from_headers(request.headers),
@@ -807,23 +806,13 @@ def generate_upload_prometheus_metrics_labels(
807806
endpoint=endpoint,
808807
is_using_shelter="yes" if is_shelter_request else "no",
809808
)
810-
811-
repo_visibility = None
812809
if repository:
813-
repo_visibility = "private" if repository.private else "public"
814-
815-
optional_fields = {
816-
"repo_visibility": repo_visibility,
817-
"position": position,
818-
"upload_version": upload_version,
819-
}
820-
821-
metrics_tags.update(
822-
{
823-
field: value
824-
for field, value in optional_fields.items()
825-
if value or include_empty_labels
826-
}
827-
)
810+
metrics_tags["repo_visibility"] = (
811+
"private" if repository.private is True else "public"
812+
)
813+
if position:
814+
metrics_tags["position"] = position
815+
if upload_version:
816+
metrics_tags["upload_version"] = upload_version
828817

829818
return metrics_tags

upload/tests/test_upload.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def test_get_global_tokens(self, mock_get_config):
298298
def test_determine_repo_upload(self):
299299
with self.subTest("token found"):
300300
org = G(Owner)
301-
repo = G(Repository, author=org, activated=True)
301+
repo = G(Repository, author=org)
302302

303303
params = {
304304
"version": "v4",
@@ -310,7 +310,7 @@ def test_determine_repo_upload(self):
310310

311311
with self.subTest("token not found"):
312312
org = G(Owner)
313-
repo = G(Repository, author=org, activated=True)
313+
repo = G(Repository, author=org)
314314

315315
params = {
316316
"version": "v4",
@@ -330,7 +330,7 @@ def test_determine_repo_upload(self):
330330
@patch.object(requests, "get")
331331
def test_determine_repo_upload_tokenless(self, mock_get):
332332
org = G(Owner, username="codecov", service="github")
333-
repo = G(Repository, author=org, activated=True)
333+
repo = G(Repository, author=org)
334334
expected_response = {
335335
"id": 732059764,
336336
"finishTime": f"{datetime.now()}",
@@ -503,7 +503,7 @@ def test_determine_upload_commit_to_use(
503503
service="bitbucket",
504504
oauth_token=encryptor.encode("hahahahaha").decode(),
505505
)
506-
repo = G(Repository, author=org, activated=True)
506+
repo = G(Repository, author=org)
507507
upload_params = {
508508
"service": "bitbucket",
509509
"commit": "3be5c52bd748c508a7e96993c02cf3518c816e84",
@@ -519,7 +519,7 @@ def test_determine_upload_commit_to_use(
519519
service="github",
520520
oauth_token=encryptor.encode("hahahahaha").decode(),
521521
)
522-
repo = G(Repository, author=org, activated=True)
522+
repo = G(Repository, author=org)
523523
upload_params = {
524524
"service": "github",
525525
"commit": "3084886b7ff869dcf327ad1d28a8b7d34adc7584",
@@ -532,7 +532,7 @@ def test_determine_upload_commit_to_use(
532532

533533
with self.subTest("just no bot available"):
534534
org = G(Owner, service="github", oauth_token=None)
535-
repo = G(Repository, author=org, private=True, activated=True)
535+
repo = G(Repository, author=org, private=True)
536536
upload_params = {
537537
"service": "github",
538538
"commit": "3084886b7ff869dcf327ad1d28a8b7d34adc7584",
@@ -549,7 +549,7 @@ def test_determine_upload_commit_to_use(
549549
service="github",
550550
oauth_token=encryptor.encode("hahahahaha").decode(),
551551
)
552-
repo = G(Repository, author=org, activated=True)
552+
repo = G(Repository, author=org)
553553
upload_params = {
554554
"service": "github",
555555
"commit": "3084886b7ff869dcf327ad1d28a8b7d34adc7584",
@@ -564,7 +564,7 @@ def test_determine_upload_commit_to_use(
564564
with self.subTest("use repo bot token when available"):
565565
bot = OwnerFactory()
566566
org = G(Owner, service="github")
567-
repo = G(Repository, author=org, bot=bot, activated=True)
567+
repo = G(Repository, author=org, bot=bot)
568568

569569
upload_params = {
570570
"service": "github",
@@ -597,7 +597,7 @@ def test_determine_upload_commit_to_use(
597597

598598
with self.subTest("HTTP error"):
599599
org = G(Owner, service="github")
600-
repo = G(Repository, author=org, activated=True)
600+
repo = G(Repository, author=org)
601601
upload_params = {
602602
"service": "github",
603603
"commit": "3084886b7ff869dcf327ad1d28a8b7d34adc7584",
@@ -610,7 +610,7 @@ def test_determine_upload_commit_to_use(
610610

611611
def test_insert_commit(self):
612612
org = G(Owner)
613-
repo = G(Repository, author=org, activated=True)
613+
repo = G(Repository, author=org)
614614

615615
with self.subTest("newly created"):
616616
insert_commit(
@@ -743,7 +743,7 @@ def test_store_report_in_redis(self):
743743
def test_validate_upload_repository_moved(self):
744744
redis = MockRedis()
745745
owner = G(Owner, plan="users-free")
746-
repo = G(Repository, author=owner, name="", activated=True)
746+
repo = G(Repository, author=owner, name="")
747747
commit = G(Commit)
748748

749749
with self.assertRaises(ValidationError) as err:
@@ -757,7 +757,7 @@ def test_validate_upload_repository_moved(self):
757757
def test_validate_upload_empty_totals(self):
758758
redis = MockRedis()
759759
owner = G(Owner, plan="5m")
760-
repo = G(Repository, author=owner, activated=True)
760+
repo = G(Repository, author=owner)
761761
commit = G(Commit, totals=None, repository=repo)
762762

763763
validate_upload({"commit": commit.commitid}, repo, redis)
@@ -769,7 +769,7 @@ def test_validate_upload_empty_totals(self):
769769
def test_validate_upload_too_many_uploads_for_commit(self):
770770
redis = MockRedis()
771771
owner = G(Owner, plan="users-free")
772-
repo = G(Repository, author=owner, activated=True)
772+
repo = G(Repository, author=owner)
773773
commit = G(Commit, totals={"s": 151}, repository=repo)
774774
report = CommitReportFactory.create(commit=commit)
775775
for i in range(151):
@@ -782,7 +782,7 @@ def test_validate_upload_too_many_uploads_for_commit(self):
782782
def test_validate_upload_repository_blacklisted(self):
783783
redis = MockRedis(blacklisted=True)
784784
owner = G(Owner, plan="users-free")
785-
repo = G(Repository, author=owner, activated=True)
785+
repo = G(Repository, author=owner)
786786
commit = G(Commit)
787787

788788
with self.assertRaises(ValidationError) as err:
@@ -795,7 +795,7 @@ def test_validate_upload_repository_blacklisted(self):
795795
def test_validate_upload_per_repo_billing_invalid(self):
796796
redis = MockRedis()
797797
owner = G(Owner, plan="1m")
798-
G(Repository, author=owner, private=True, activated=False, active=True)
798+
G(Repository, author=owner, private=True, activated=True, active=True)
799799
repo = G(Repository, author=owner, private=True, activated=False, active=False)
800800
commit = G(Commit)
801801

@@ -876,7 +876,7 @@ def test_validate_upload_valid_upload_repo_activated(self):
876876
@freeze_time("2023-01-01T00:00:00")
877877
@patch("services.task.TaskService.upload")
878878
def test_dispatch_upload_task(self, upload):
879-
repo = G(Repository, activated=True)
879+
repo = G(Repository)
880880
task_arguments = {
881881
"commit": "commit123",
882882
"version": "v4",

upload/tests/views/test_commits.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def test_get_repo_not_found(db):
4242
upload_views.get_repo()
4343
assert exp.match("Repository not found")
4444

45+
4546
def test_deactivated_repo(db):
4647
repo = RepositoryFactory(
4748
name="the_repo",

upload/tests/views/test_reports.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def test_reports_get_not_allowed(client, mocker, db):
3939
res = client.get(url, **headers)
4040
assert res.status_code == 405
4141

42+
4243
def test_deactivated_repo(db):
4344
repo = RepositoryFactory(
4445
name="the_repo",
@@ -59,9 +60,7 @@ def test_deactivated_repo(db):
5960
args=["github", repo_slug, commit.commitid],
6061
)
6162
response = client.post(
62-
url,
63-
data={"code": "code1"},
64-
headers={"User-Agent": "codecov-cli/0.4.7"}
63+
url, data={"code": "code1"}, headers={"User-Agent": "codecov-cli/0.4.7"}
6564
)
6665
response_json = response.json()
6766
assert response.status_code == 400

upload/views/commits.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
repo_auth_custom_exception_handler,
1515
)
1616
from core.models import Commit
17-
from upload.helpers import generate_upload_prometheus_metrics_labels, validate_activated_repo
17+
from upload.helpers import (
18+
generate_upload_prometheus_metrics_labels,
19+
validate_activated_repo,
20+
)
1821
from upload.metrics import API_UPLOAD_COUNTER
1922
from upload.serializers import CommitSerializer
2023
from upload.views.base import GetterMixin

upload/views/reports.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
)
1717
from reports.models import CommitReport, ReportResults
1818
from services.task import TaskService
19-
from upload.helpers import generate_upload_prometheus_metrics_labels, validate_activated_repo
19+
from upload.helpers import (
20+
generate_upload_prometheus_metrics_labels,
21+
validate_activated_repo,
22+
)
2023
from upload.metrics import API_UPLOAD_COUNTER
2124
from upload.serializers import CommitReportSerializer, ReportResultsSerializer
2225
from upload.views.base import GetterMixin

0 commit comments

Comments
 (0)