From d7c68bab85cf6581e1ad262df0df99ace9b09fe0 Mon Sep 17 00:00:00 2001 From: Spencer Murray Date: Tue, 25 Mar 2025 09:32:12 -0400 Subject: [PATCH 1/6] Add bundle badge api --- graphs/urls.py | 10 +++++++ graphs/views.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/graphs/urls.py b/graphs/urls.py index bc0e0d45c8..3e926a40d5 100644 --- a/graphs/urls.py +++ b/graphs/urls.py @@ -13,6 +13,16 @@ BadgeHandler.as_view(), name="default-badge", ), + re_path( + "branch/(?P.+)/(graph|graphs)/bundle/badge.(?P[^/]+)", + BadgeHandler.as_view(), + "branch-bundle-badge", + ), + re_path( + "(graph|graphs)/bundle/badge.(?P[^/]+)", + BadgeHandler.as_view(), + "default-bundle-badge", + ), re_path( "pull/(?P[^/]+)/(graph|graphs)/(?Ptree|icicle|sunburst|commits).(?P[^/]+)", GraphHandler.as_view(), diff --git a/graphs/views.py b/graphs/views.py index 9eb9c52714..9209ce5378 100644 --- a/graphs/views.py +++ b/graphs/views.py @@ -13,10 +13,17 @@ from api.shared.mixins import RepoPropertyMixin from core.models import Branch, Pull +from graphql_api.dataloader.bundle_analysis import load_bundle_analysis_report from graphs.settings import settings +from services.bundle_analysis import BundleAnalysisReport from services.components import commit_components -from .helpers.badge import format_coverage_precision, get_badge +from .helpers.badge import ( + format_bundle_bytes, + format_coverage_precision, + get_badge, + get_bundle_badge, +) from .helpers.graphs import icicle, sunburst, tree from .mixins import GraphBadgeAPIMixin @@ -193,6 +200,72 @@ def component_coverage(self, component_identifier: str, commit: Commit): return filtered_report.totals.coverage +class BundleBadgeHandler(APIView, RepoPropertyMixin, GraphBadgeAPIMixin): + content_negotiation_class = IgnoreClientContentNegotiation + + permission_classes = [AllowAny] + + extensions = ["svg", "txt"] + precisions = ["0", "1", "2"] + filename = "bundle-badge" + + def get_object(self, request, *args, **kwargs): + # Validate coverage precision + precision = self.request.query_params.get("precision", "2") + if precision not in self.precisions: + raise NotFound("Bundle size precision should be one of [ 0 || 1 || 2 ]") + precision = int(precision) + + bundle_size_bytes = self.get_bundle_size() + + if self.kwargs.get("ext") == "txt": + return format_bundle_bytes(bundle_size_bytes, precision) + + return get_bundle_badge(bundle_size_bytes, precision) + + def get_bundle_size(self) -> int | None: + try: + repo = self.repo + except Http404: + log.warning("Repo not found", extra=dict(repo=self.kwargs.get("repo_name"))) + return None + + if repo.private and repo.image_token != self.request.query_params.get("token"): + log.warning( + "Token provided does not match repo's image token", + extra=dict(repo=repo), + ) + return None + + branch_name = self.kwargs.get("branch") or repo.branch + branch = Branch.objects.filter( + name=branch_name, repository_id=repo.repoid + ).first() + + if branch is None: + log.warning( + "Branch not found", extra=dict(branch_name=branch_name, repo=repo) + ) + return None + + try: + commit: Commit = repo.commits.filter(commitid=branch.head).first() + except ObjectDoesNotExist: + log.warning("Commit not found", extra=dict(commit=branch.head)) + return None + + bundle_report = load_bundle_analysis_report(commit) + + if not isinstance(bundle_report, BundleAnalysisReport): + log.warning( + "Bundle analysis report not found for commit", + extra=dict(commit=branch.head), + ) + return None + + return bundle_report.size_total + + class GraphHandler(APIView, RepoPropertyMixin, GraphBadgeAPIMixin): permission_classes = [AllowAny] From 8a8b3e3564e034ec0808c48268afc082e8ef0d4f Mon Sep 17 00:00:00 2001 From: Spencer Murray Date: Tue, 25 Mar 2025 09:54:26 -0400 Subject: [PATCH 2/6] Use load_report instead of gql dataloader version --- graphs/views.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/graphs/views.py b/graphs/views.py index 9209ce5378..dd8e083b1f 100644 --- a/graphs/views.py +++ b/graphs/views.py @@ -13,9 +13,8 @@ from api.shared.mixins import RepoPropertyMixin from core.models import Branch, Pull -from graphql_api.dataloader.bundle_analysis import load_bundle_analysis_report from graphs.settings import settings -from services.bundle_analysis import BundleAnalysisReport +from services.bundle_analysis import BundleAnalysisReport, load_report from services.components import commit_components from .helpers.badge import ( @@ -254,15 +253,17 @@ def get_bundle_size(self) -> int | None: log.warning("Commit not found", extra=dict(commit=branch.head)) return None - bundle_report = load_bundle_analysis_report(commit) + shared_bundle_report = load_report(commit) - if not isinstance(bundle_report, BundleAnalysisReport): + if shared_bundle_report is None: log.warning( "Bundle analysis report not found for commit", extra=dict(commit=branch.head), ) return None + bundle_report = BundleAnalysisReport(shared_bundle_report) + return bundle_report.size_total From cba024bf24dd46d30ecac1e0ff385f803d880269 Mon Sep 17 00:00:00 2001 From: Spencer Murray Date: Tue, 25 Mar 2025 10:52:19 -0400 Subject: [PATCH 3/6] Fix urls --- graphs/urls.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/graphs/urls.py b/graphs/urls.py index 3e926a40d5..d79480baea 100644 --- a/graphs/urls.py +++ b/graphs/urls.py @@ -1,6 +1,6 @@ from django.urls import re_path -from .views import BadgeHandler, GraphHandler +from .views import BadgeHandler, BundleBadgeHandler, GraphHandler urlpatterns = [ re_path( @@ -15,13 +15,13 @@ ), re_path( "branch/(?P.+)/(graph|graphs)/bundle/badge.(?P[^/]+)", - BadgeHandler.as_view(), - "branch-bundle-badge", + BundleBadgeHandler.as_view(), + name="branch-bundle-badge", ), re_path( "(graph|graphs)/bundle/badge.(?P[^/]+)", - BadgeHandler.as_view(), - "default-bundle-badge", + BundleBadgeHandler.as_view(), + name="default-bundle-badge", ), re_path( "pull/(?P[^/]+)/(graph|graphs)/(?Ptree|icicle|sunburst|commits).(?P[^/]+)", From 86818960136bea6ba4ec70b54790720a449caa7b Mon Sep 17 00:00:00 2001 From: Spencer Murray Date: Tue, 25 Mar 2025 11:37:30 -0400 Subject: [PATCH 4/6] Require bundle name in badge url --- graphs/urls.py | 4 ++-- graphs/views.py | 26 +++++++++++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/graphs/urls.py b/graphs/urls.py index d79480baea..cf069e9a42 100644 --- a/graphs/urls.py +++ b/graphs/urls.py @@ -14,12 +14,12 @@ name="default-badge", ), re_path( - "branch/(?P.+)/(graph|graphs)/bundle/badge.(?P[^/]+)", + "branch/(?P.+)/(graph|graphs)/bundle/(?P.+)/badge.(?P[^/]+)", BundleBadgeHandler.as_view(), name="branch-bundle-badge", ), re_path( - "(graph|graphs)/bundle/badge.(?P[^/]+)", + "(graph|graphs)/bundle/(?P.+)/badge.(?P[^/]+)", BundleBadgeHandler.as_view(), name="default-bundle-badge", ), diff --git a/graphs/views.py b/graphs/views.py index dd8e083b1f..788b7c8012 100644 --- a/graphs/views.py +++ b/graphs/views.py @@ -14,7 +14,7 @@ from api.shared.mixins import RepoPropertyMixin from core.models import Branch, Pull from graphs.settings import settings -from services.bundle_analysis import BundleAnalysisReport, load_report +from services.bundle_analysis import load_report from services.components import commit_components from .helpers.badge import ( @@ -209,7 +209,7 @@ class BundleBadgeHandler(APIView, RepoPropertyMixin, GraphBadgeAPIMixin): filename = "bundle-badge" def get_object(self, request, *args, **kwargs): - # Validate coverage precision + # Validate precision query param precision = self.request.query_params.get("precision", "2") if precision not in self.precisions: raise NotFound("Bundle size precision should be one of [ 0 || 1 || 2 ]") @@ -218,7 +218,11 @@ def get_object(self, request, *args, **kwargs): bundle_size_bytes = self.get_bundle_size() if self.kwargs.get("ext") == "txt": - return format_bundle_bytes(bundle_size_bytes, precision) + return ( + "unknown" + if bundle_size_bytes is None + else format_bundle_bytes(bundle_size_bytes, precision) + ) return get_bundle_badge(bundle_size_bytes, precision) @@ -253,18 +257,26 @@ def get_bundle_size(self) -> int | None: log.warning("Commit not found", extra=dict(commit=branch.head)) return None - shared_bundle_report = load_report(commit) + commit_bundles = load_report(commit) - if shared_bundle_report is None: + if commit_bundles is None: log.warning( "Bundle analysis report not found for commit", extra=dict(commit=branch.head), ) return None - bundle_report = BundleAnalysisReport(shared_bundle_report) + bundle_name = str(self.kwargs.get("bundle")) + bundle = commit_bundles.bundle_report(bundle_name) + + if bundle is None: + log.warning( + "Bundle with provided name not found for commit", + extra=dict(commit=branch.head), + ) + return None - return bundle_report.size_total + return bundle.total_size() class GraphHandler(APIView, RepoPropertyMixin, GraphBadgeAPIMixin): From 4b71900455067bfcb5011786c1c4e624f5341599 Mon Sep 17 00:00:00 2001 From: Spencer Murray Date: Tue, 25 Mar 2025 13:58:41 -0400 Subject: [PATCH 5/6] Add tests --- graphs/tests/test_badge_handler.py | 12 +- graphs/tests/test_bundle_badge_handler.py | 467 ++++++++++++++++++++++ graphs/views.py | 9 +- 3 files changed, 476 insertions(+), 12 deletions(-) create mode 100644 graphs/tests/test_bundle_badge_handler.py diff --git a/graphs/tests/test_badge_handler.py b/graphs/tests/test_badge_handler.py index e79f7d1214..c8b47ccf28 100644 --- a/graphs/tests/test_badge_handler.py +++ b/graphs/tests/test_badge_handler.py @@ -143,7 +143,7 @@ def test_invalid_extension(self): response.data["detail"] == "File extension should be one of [ svg || txt ]" ) - def test_unknown_bagde_incorrect_service(self): + def test_unknown_badge_incorrect_service(self): response = self._get( kwargs={ "service": "gih", @@ -182,7 +182,7 @@ def test_unknown_bagde_incorrect_service(self): assert expected_badge == badge assert response.status_code == status.HTTP_200_OK - def test_unknown_bagde_incorrect_owner(self): + def test_unknown_badge_incorrect_owner(self): response = self._get( kwargs={ "service": "gh", @@ -221,7 +221,7 @@ def test_unknown_bagde_incorrect_owner(self): assert expected_badge == badge assert response.status_code == status.HTTP_200_OK - def test_unknown_bagde_incorrect_repo(self): + def test_unknown_badge_incorrect_repo(self): gh_owner = OwnerFactory(service="github") response = self._get( kwargs={ @@ -261,7 +261,7 @@ def test_unknown_bagde_incorrect_repo(self): assert expected_badge == badge assert response.status_code == status.HTTP_200_OK - def test_unknown_bagde_no_branch(self): + def test_unknown_badge_no_branch(self): gh_owner = OwnerFactory(service="github") RepositoryFactory(author=gh_owner, active=True, private=False, name="repo1") response = self._get( @@ -302,7 +302,7 @@ def test_unknown_bagde_no_branch(self): assert expected_badge == badge assert response.status_code == status.HTTP_200_OK - def test_unknown_bagde_no_commit(self): + def test_unknown_badge_no_commit(self): gh_owner = OwnerFactory(service="github") repo = RepositoryFactory( author=gh_owner, active=True, private=False, name="repo1" @@ -346,7 +346,7 @@ def test_unknown_bagde_no_commit(self): assert expected_badge == badge assert response.status_code == status.HTTP_200_OK - def test_unknown_bagde_no_totals(self): + def test_unknown_badge_no_totals(self): gh_owner = OwnerFactory(service="github") repo = RepositoryFactory( author=gh_owner, active=True, private=False, name="repo1" diff --git a/graphs/tests/test_bundle_badge_handler.py b/graphs/tests/test_bundle_badge_handler.py new file mode 100644 index 0000000000..bff2aaaa76 --- /dev/null +++ b/graphs/tests/test_bundle_badge_handler.py @@ -0,0 +1,467 @@ +from unittest.mock import patch + +from rest_framework import status +from rest_framework.test import APITestCase +from shared.bundle_analysis import BundleAnalysisReport, BundleReport +from shared.django_apps.core.tests.factories import ( + BranchFactory, + CommitFactory, + OwnerFactory, + RepositoryFactory, +) + + +class MockBundleReport(BundleReport): + def __init__(self): + return + + def total_size(self): + return 1234567 + + +class MockBundleAnalysisReport(BundleAnalysisReport): + def bundle_report(self, bundle_name: str): + if bundle_name == "idk": + return None + return MockBundleReport() + + +class TestBundleBadgeHandler(APITestCase): + def _get(self, kwargs={}, data={}): + path = f"/{kwargs.get('service')}/{kwargs.get('owner_username')}/{kwargs.get('repo_name')}/graphs/bundle/{kwargs.get('bundle')}/badge.{kwargs.get('ext')}" + return self.client.get(path, data=data) + + def _get_branch(self, kwargs={}, data={}): + path = f"/{kwargs.get('service')}/{kwargs.get('owner_username')}/{kwargs.get('repo_name')}/branch/{kwargs.get('branch')}/graphs/{kwargs.get('bundle')}/badge.{kwargs.get('ext')}" + return self.client.get(path, data=data) + + def test_invalid_extension(self): + response = self._get( + kwargs={ + "service": "gh", + "owner_username": "user", + "repo_name": "repo", + "ext": "png", + "bundle": "asdf", + } + ) + assert response.status_code == status.HTTP_404_NOT_FOUND + assert ( + response.data["detail"] == "File extension should be one of [ svg || txt ]" + ) + + def test_unknown_badge_incorrect_service(self): + response = self._get( + kwargs={ + "service": "gih", + "owner_username": "user", + "repo_name": "repo", + "ext": "svg", + "bundle": "asdf", + } + ) + expected_badge = """ + + + + + + + + + + + + + + bundle + bundle + unknown + unknown + + +""" + badge = response.content.decode("utf-8") + badge = [line.strip() for line in badge.split("\n")] + expected_badge = [line.strip() for line in expected_badge.split("\n")] + assert expected_badge == badge + assert response.status_code == status.HTTP_200_OK + + def test_unknown_badge_incorrect_owner(self): + response = self._get( + kwargs={ + "service": "gh", + "owner_username": "user1233", + "repo_name": "repo", + "ext": "svg", + "bundle": "asdf", + } + ) + expected_badge = """ + + + + + + + + + + + + + + bundle + bundle + unknown + unknown + + +""" + badge = response.content.decode("utf-8") + badge = [line.strip() for line in badge.split("\n")] + expected_badge = [line.strip() for line in expected_badge.split("\n")] + assert expected_badge == badge + assert response.status_code == status.HTTP_200_OK + + def test_unknown_badge_incorrect_repo(self): + gh_owner = OwnerFactory(service="github") + response = self._get( + kwargs={ + "service": "gh", + "owner_username": gh_owner.username, + "repo_name": "repo", + "ext": "svg", + "bundle": "asdf", + } + ) + expected_badge = """ + + + + + + + + + + + + + + bundle + bundle + unknown + unknown + + +""" + badge = response.content.decode("utf-8") + badge = [line.strip() for line in badge.split("\n")] + expected_badge = [line.strip() for line in expected_badge.split("\n")] + assert expected_badge == badge + assert response.status_code == status.HTTP_200_OK + + def test_unknown_badge_no_branch(self): + gh_owner = OwnerFactory(service="github") + RepositoryFactory(author=gh_owner, active=True, private=False, name="repo1") + response = self._get( + kwargs={ + "service": "gh", + "owner_username": gh_owner.username, + "repo_name": "repo1", + "ext": "svg", + "bundle": "asdf", + } + ) + expected_badge = """ + + + + + + + + + + + + + + bundle + bundle + unknown + unknown + + +""" + badge = response.content.decode("utf-8") + badge = [line.strip() for line in badge.split("\n")] + expected_badge = [line.strip() for line in expected_badge.split("\n")] + assert expected_badge == badge + assert response.status_code == status.HTTP_200_OK + + def test_unknown_badge_no_commit(self): + gh_owner = OwnerFactory(service="github") + repo = RepositoryFactory( + author=gh_owner, active=True, private=False, name="repo1" + ) + branch = BranchFactory(name="main", repository=repo) + repo.branch = branch + response = self._get( + kwargs={ + "service": "gh", + "owner_username": gh_owner.username, + "repo_name": "repo1", + "ext": "svg", + "bundle": "asdf", + } + ) + expected_badge = """ + + + + + + + + + + + + + + bundle + bundle + unknown + unknown + + +""" + + badge = response.content.decode("utf-8") + badge = [line.strip() for line in badge.split("\n")] + expected_badge = [line.strip() for line in expected_badge.split("\n")] + assert expected_badge == badge + assert response.status_code == status.HTTP_200_OK + + @patch("graphs.views.load_report") + def test_unknown_badge_no_report(self, mock_load_report): + gh_owner = OwnerFactory(service="github") + repo = RepositoryFactory( + author=gh_owner, active=True, private=False, name="repo1" + ) + branch = BranchFactory(name="main", repository=repo) + repo.branch = branch + commit = CommitFactory( + repository=repo, commitid=repo.branch.head, branch="main" + ) + branch.head = commit.commitid + + mock_load_report.return_value = None + + response = self._get( + kwargs={ + "service": "gh", + "owner_username": gh_owner.username, + "repo_name": "repo1", + "ext": "svg", + "bundle": "asdf", + } + ) + expected_badge = """ + + + + + + + + + + + + + + bundle + bundle + unknown + unknown + + +""" + + badge = response.content.decode("utf-8") + badge = [line.strip() for line in badge.split("\n")] + expected_badge = [line.strip() for line in expected_badge.split("\n")] + assert expected_badge == badge + assert response.status_code == status.HTTP_200_OK + + @patch("graphs.views.load_report") + def test_unknown_badge_no_bundle(self, mock_load_report): + gh_owner = OwnerFactory(service="github") + repo = RepositoryFactory( + author=gh_owner, active=True, private=False, name="repo1" + ) + branch = BranchFactory(name="main", repository=repo) + repo.branch = branch + commit = CommitFactory( + repository=repo, commitid=repo.branch.head, branch="main" + ) + branch.head = commit.commitid + + mock_load_report.return_value = MockBundleAnalysisReport() + + response = self._get( + kwargs={ + "service": "gh", + "owner_username": gh_owner.username, + "repo_name": "repo1", + "ext": "svg", + "bundle": "idk", + } + ) + expected_badge = """ + + + + + + + + + + + + + + bundle + bundle + unknown + unknown + + +""" + + badge = response.content.decode("utf-8") + badge = [line.strip() for line in badge.split("\n")] + expected_badge = [line.strip() for line in expected_badge.split("\n")] + assert expected_badge == badge + assert response.status_code == status.HTTP_200_OK + + @patch("graphs.views.load_report") + def test_bundle_badge(self, mock_load_report): + gh_owner = OwnerFactory(service="github") + repo = RepositoryFactory( + author=gh_owner, active=True, private=False, name="repo1" + ) + branch = BranchFactory(name="main", repository=repo) + repo.branch = branch + commit = CommitFactory( + repository=repo, commitid=repo.branch.head, branch="main" + ) + branch.head = commit.commitid + + mock_load_report.return_value = MockBundleAnalysisReport() + + response = self._get( + kwargs={ + "service": "gh", + "owner_username": gh_owner.username, + "repo_name": "repo1", + "ext": "svg", + "bundle": "asdf", + } + ) + expected_badge = """ + + + + + + + + + + + + + + bundle + bundle + 1.23MB + 1.23MB + + +""" + + badge = response.content.decode("utf-8") + badge = [line.strip() for line in badge.split("\n")] + expected_badge = [line.strip() for line in expected_badge.split("\n")] + assert expected_badge == badge + assert response.status_code == status.HTTP_200_OK + + @patch("graphs.views.load_report") + def test_bundle_badge_text(self, mock_load_report): + gh_owner = OwnerFactory(service="github") + repo = RepositoryFactory( + author=gh_owner, active=True, private=False, name="repo1" + ) + branch = BranchFactory(name="main", repository=repo) + repo.branch = branch + commit = CommitFactory( + repository=repo, commitid=repo.branch.head, branch="main" + ) + branch.head = commit.commitid + + mock_load_report.return_value = MockBundleAnalysisReport() + + response = self._get( + kwargs={ + "service": "gh", + "owner_username": gh_owner.username, + "repo_name": "repo1", + "ext": "txt", + "bundle": "asdf", + } + ) + expected_badge = "1.23MB" + + badge = response.content.decode("utf-8") + badge = [line.strip() for line in badge.split("\n")] + expected_badge = [line.strip() for line in expected_badge.split("\n")] + assert expected_badge == badge + assert response.status_code == status.HTTP_200_OK + + @patch("graphs.views.load_report") + def test_bundle_badge_unsupported_precision_defaults_to_2(self, mock_load_report): + gh_owner = OwnerFactory(service="github") + repo = RepositoryFactory( + author=gh_owner, active=True, private=False, name="repo1" + ) + branch = BranchFactory(name="main", repository=repo) + repo.branch = branch + commit = CommitFactory( + repository=repo, commitid=repo.branch.head, branch="main" + ) + branch.head = commit.commitid + + mock_load_report.return_value = MockBundleAnalysisReport() + + response = self._get( + kwargs={ + "service": "gh", + "owner_username": gh_owner.username, + "repo_name": "repo1", + "ext": "txt", + "bundle": "asdf", + }, + data={"precision": "asdf"}, + ) + expected_badge = "1.23MB" + + badge = response.content.decode("utf-8") + badge = [line.strip() for line in badge.split("\n")] + expected_badge = [line.strip() for line in expected_badge.split("\n")] + assert expected_badge == badge + assert response.status_code == status.HTTP_200_OK diff --git a/graphs/views.py b/graphs/views.py index 788b7c8012..68f0779aeb 100644 --- a/graphs/views.py +++ b/graphs/views.py @@ -211,9 +211,7 @@ class BundleBadgeHandler(APIView, RepoPropertyMixin, GraphBadgeAPIMixin): def get_object(self, request, *args, **kwargs): # Validate precision query param precision = self.request.query_params.get("precision", "2") - if precision not in self.precisions: - raise NotFound("Bundle size precision should be one of [ 0 || 1 || 2 ]") - precision = int(precision) + precision = int(precision) if precision in self.precisions else 2 bundle_size_bytes = self.get_bundle_size() @@ -251,9 +249,8 @@ def get_bundle_size(self) -> int | None: ) return None - try: - commit: Commit = repo.commits.filter(commitid=branch.head).first() - except ObjectDoesNotExist: + commit: Commit = repo.commits.filter(commitid=branch.head).first() + if commit is None: log.warning("Commit not found", extra=dict(commit=branch.head)) return None From 75ae71ba38b222ece0c40d846af13570459c2e92 Mon Sep 17 00:00:00 2001 From: Spencer Murray Date: Tue, 25 Mar 2025 14:15:51 -0400 Subject: [PATCH 6/6] Add tests for private repo cases --- graphs/tests/test_bundle_badge_handler.py | 73 +++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/graphs/tests/test_bundle_badge_handler.py b/graphs/tests/test_bundle_badge_handler.py index bff2aaaa76..0317ab05b2 100644 --- a/graphs/tests/test_bundle_badge_handler.py +++ b/graphs/tests/test_bundle_badge_handler.py @@ -155,6 +155,47 @@ def test_unknown_badge_incorrect_repo(self): unknown +""" + badge = response.content.decode("utf-8") + badge = [line.strip() for line in badge.split("\n")] + expected_badge = [line.strip() for line in expected_badge.split("\n")] + assert expected_badge == badge + assert response.status_code == status.HTTP_200_OK + + def test_unknown_badge_private_repo_wrong_token(self): + gh_owner = OwnerFactory(service="github") + RepositoryFactory( + author=gh_owner, active=True, private=True, name="repo1", image_token="asdf" + ) + response = self._get( + kwargs={ + "service": "gh", + "owner_username": gh_owner.username, + "repo_name": "repo1", + "ext": "svg", + "bundle": "asdf", + } + ) + expected_badge = """ + + + + + + + + + + + + + + bundle + bundle + unknown + unknown + + """ badge = response.content.decode("utf-8") badge = [line.strip() for line in badge.split("\n")] @@ -464,4 +505,36 @@ def test_bundle_badge_unsupported_precision_defaults_to_2(self, mock_load_report badge = [line.strip() for line in badge.split("\n")] expected_badge = [line.strip() for line in expected_badge.split("\n")] assert expected_badge == badge + + @patch("graphs.views.load_report") + def test_bundle_badge_private_repo_correct_token(self, mock_load_report): + gh_owner = OwnerFactory(service="github") + repo = RepositoryFactory( + author=gh_owner, active=True, private=True, name="repo1", image_token="asdf" + ) + branch = BranchFactory(name="main", repository=repo) + repo.branch = branch + commit = CommitFactory( + repository=repo, commitid=repo.branch.head, branch="main" + ) + branch.head = commit.commitid + + mock_load_report.return_value = MockBundleAnalysisReport() + + response = self._get( + kwargs={ + "service": "gh", + "owner_username": gh_owner.username, + "repo_name": "repo1", + "ext": "txt", + "bundle": "asdf", + }, + data={"token": "asdf"}, + ) + expected_badge = "1.23MB" + + badge = response.content.decode("utf-8") + badge = [line.strip() for line in badge.split("\n")] + expected_badge = [line.strip() for line in expected_badge.split("\n")] + assert expected_badge == badge assert response.status_code == status.HTTP_200_OK