1+ from unittest .mock import patch , PropertyMock
2+
3+ from rest_framework import status
4+ from rest_framework .test import APITestCase
5+ from shared .django_apps .core .tests .factories import (
6+ BranchFactory ,
7+ CommitFactory ,
8+ OwnerFactory ,
9+ RepositoryFactory ,
10+ )
11+
12+ from services .bundle_analysis import load_report
13+
14+
15+ class TestBundleBadgeHandler (APITestCase ):
16+ def _get (self , kwargs = {}, data = {}):
17+ path = f"/{ kwargs .get ('service' )} /{ kwargs .get ('owner_username' )} /{ kwargs .get ('repo_name' )} /graphs/bundle/{ kwargs .get ('bundle' )} /badge.{ kwargs .get ('ext' )} "
18+ return self .client .get (path , data = data )
19+
20+ def _get_branch (self , kwargs = {}, data = {}):
21+ path = f"/{ kwargs .get ('service' )} /{ kwargs .get ('owner_username' )} /{ kwargs .get ('repo_name' )} /branch/{ kwargs .get ('branch' )} /graphs/bundle/{ kwargs .get ('bundle' )} /badge.{ kwargs .get ('ext' )} "
22+ return self .client .get (path , data = data )
23+
24+ def test_invalid_precision (self ):
25+ response = self ._get (
26+ kwargs = {
27+ "service" : "gh" ,
28+ "owner_username" : "user" ,
29+ "repo_name" : "repo" ,
30+ "bundle" : "main" ,
31+ "ext" : "svg" ,
32+ },
33+ data = {"precision" : "3" },
34+ )
35+ assert response .status_code == status .HTTP_404_NOT_FOUND
36+ assert (
37+ response .data ["detail" ]
38+ == "Bundle size precision should be one of [ 0 || 1 || 2 ]"
39+ )
40+
41+ def test_invalid_extension (self ):
42+ response = self ._get (
43+ kwargs = {
44+ "service" : "gh" ,
45+ "owner_username" : "user" ,
46+ "repo_name" : "repo" ,
47+ "bundle" : "main" ,
48+ "ext" : "png" ,
49+ }
50+ )
51+ assert response .status_code == status .HTTP_404_NOT_FOUND
52+ assert (
53+ response .data ["detail" ] == "File extension should be one of [ svg || txt ]"
54+ )
55+
56+ def test_unknown_bundle_badge_incorrect_service (self ):
57+ response = self ._get (
58+ kwargs = {
59+ "service" : "gih" ,
60+ "owner_username" : "user" ,
61+ "repo_name" : "repo" ,
62+ "bundle" : "main" ,
63+ "ext" : "svg" ,
64+ }
65+ )
66+ expected_badge = """<svg xmlns="http://www.w3.org/2000/svg" width="106" height="20">
67+ <linearGradient id="CodecovBadgeGradient" x2="0" y2="100%">
68+ <stop offset="0" stop-color="#bbb" stop-opacity=".1" />
69+ <stop offset="1" stop-opacity=".1" />
70+ </linearGradient>
71+ <mask id="CodecovBadgeMask106px">
72+ <rect width="106" height="20" rx="3" fill="#fff" />
73+ </mask>
74+ <g mask="url(#CodecovBadgeMask106px)">
75+ <path fill="#555" d="M0 0h47v20H0z" />
76+ <path fill="#2C2433" d="M47 0h59v20H47z" />
77+ <path fill="url(#CodecovBadgeGradient)" d="M0 0h106v20H0z" />
78+ </g>
79+ <g fill="#fff" text-anchor="left" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
80+ <text x="5" y="15" fill="#010101" fill-opacity=".3">bundle</text>
81+ <text x="5" y="14">bundle</text>
82+ <text x="52" y="15" fill="#010101" fill-opacity=".3">unknown</text>
83+ <text x="52" y="14">unknown</text>
84+ </g>
85+ </svg>
86+ """
87+ badge = response .content .decode ("utf-8" )
88+ badge = [line .strip () for line in badge .split ("\n " )]
89+ expected_badge = [line .strip () for line in expected_badge .split ("\n " )]
90+ assert expected_badge == badge
91+ assert response .status_code == status .HTTP_200_OK
92+
93+ def test_unknown_bundle_badge_incorrect_owner (self ):
94+ response = self ._get (
95+ kwargs = {
96+ "service" : "gh" ,
97+ "owner_username" : "user1233" ,
98+ "repo_name" : "repo" ,
99+ "bundle" : "main" ,
100+ "ext" : "svg" ,
101+ }
102+ )
103+ expected_badge = """<svg xmlns="http://www.w3.org/2000/svg" width="106" height="20">
104+ <linearGradient id="CodecovBadgeGradient" x2="0" y2="100%">
105+ <stop offset="0" stop-color="#bbb" stop-opacity=".1" />
106+ <stop offset="1" stop-opacity=".1" />
107+ </linearGradient>
108+ <mask id="CodecovBadgeMask106px">
109+ <rect width="106" height="20" rx="3" fill="#fff" />
110+ </mask>
111+ <g mask="url(#CodecovBadgeMask106px)">
112+ <path fill="#555" d="M0 0h47v20H0z" />
113+ <path fill="#2C2433" d="M47 0h59v20H47z" />
114+ <path fill="url(#CodecovBadgeGradient)" d="M0 0h106v20H0z" />
115+ </g>
116+ <g fill="#fff" text-anchor="left" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
117+ <text x="5" y="15" fill="#010101" fill-opacity=".3">bundle</text>
118+ <text x="5" y="14">bundle</text>
119+ <text x="52" y="15" fill="#010101" fill-opacity=".3">unknown</text>
120+ <text x="52" y="14">unknown</text>
121+ </g>
122+ </svg>
123+ """
124+ badge = response .content .decode ("utf-8" )
125+ badge = [line .strip () for line in badge .split ("\n " )]
126+ expected_badge = [line .strip () for line in expected_badge .split ("\n " )]
127+ assert expected_badge == badge
128+ assert response .status_code == status .HTTP_200_OK
129+
130+ def test_unknown_bundle_badge_incorrect_repo (self ):
131+ gh_owner = OwnerFactory (service = "github" )
132+ response = self ._get (
133+ kwargs = {
134+ "service" : "gh" ,
135+ "owner_username" : gh_owner .username ,
136+ "repo_name" : "repo" ,
137+ "bundle" : "main" ,
138+ "ext" : "svg" ,
139+ }
140+ )
141+ expected_badge = """<svg xmlns="http://www.w3.org/2000/svg" width="106" height="20">
142+ <linearGradient id="CodecovBadgeGradient" x2="0" y2="100%">
143+ <stop offset="0" stop-color="#bbb" stop-opacity=".1" />
144+ <stop offset="1" stop-opacity=".1" />
145+ </linearGradient>
146+ <mask id="CodecovBadgeMask106px">
147+ <rect width="106" height="20" rx="3" fill="#fff" />
148+ </mask>
149+ <g mask="url(#CodecovBadgeMask106px)">
150+ <path fill="#555" d="M0 0h47v20H0z" />
151+ <path fill="#2C2433" d="M47 0h59v20H47z" />
152+ <path fill="url(#CodecovBadgeGradient)" d="M0 0h106v20H0z" />
153+ </g>
154+ <g fill="#fff" text-anchor="left" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
155+ <text x="5" y="15" fill="#010101" fill-opacity=".3">bundle</text>
156+ <text x="5" y="14">bundle</text>
157+ <text x="52" y="15" fill="#010101" fill-opacity=".3">unknown</text>
158+ <text x="52" y="14">unknown</text>
159+ </g>
160+ </svg>
161+ """
162+ badge = response .content .decode ("utf-8" )
163+ badge = [line .strip () for line in badge .split ("\n " )]
164+ expected_badge = [line .strip () for line in expected_badge .split ("\n " )]
165+ assert expected_badge == badge
166+ assert response .status_code == status .HTTP_200_OK
167+
168+ def test_unknown_bundle_badge_no_branch (self ):
169+ gh_owner = OwnerFactory (service = "github" )
170+ RepositoryFactory (author = gh_owner , active = True , private = False , name = "repo1" )
171+ response = self ._get (
172+ kwargs = {
173+ "service" : "gh" ,
174+ "owner_username" : gh_owner .username ,
175+ "repo_name" : "repo1" ,
176+ "bundle" : "main" ,
177+ "ext" : "svg" ,
178+ }
179+ )
180+ expected_badge = """<svg xmlns="http://www.w3.org/2000/svg" width="106" height="20">
181+ <linearGradient id="CodecovBadgeGradient" x2="0" y2="100%">
182+ <stop offset="0" stop-color="#bbb" stop-opacity=".1" />
183+ <stop offset="1" stop-opacity=".1" />
184+ </linearGradient>
185+ <mask id="CodecovBadgeMask106px">
186+ <rect width="106" height="20" rx="3" fill="#fff" />
187+ </mask>
188+ <g mask="url(#CodecovBadgeMask106px)">
189+ <path fill="#555" d="M0 0h47v20H0z" />
190+ <path fill="#2C2433" d="M47 0h59v20H47z" />
191+ <path fill="url(#CodecovBadgeGradient)" d="M0 0h106v20H0z" />
192+ </g>
193+ <g fill="#fff" text-anchor="left" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
194+ <text x="5" y="15" fill="#010101" fill-opacity=".3">bundle</text>
195+ <text x="5" y="14">bundle</text>
196+ <text x="52" y="15" fill="#010101" fill-opacity=".3">unknown</text>
197+ <text x="52" y="14">unknown</text>
198+ </g>
199+ </svg>
200+ """
201+ badge = response .content .decode ("utf-8" )
202+ badge = [line .strip () for line in badge .split ("\n " )]
203+ expected_badge = [line .strip () for line in expected_badge .split ("\n " )]
204+ assert expected_badge == badge
205+ assert response .status_code == status .HTTP_200_OK
206+
207+ def test_unknown_bundle_badge_no_commit (self ):
208+ gh_owner = OwnerFactory (service = "github" )
209+ repo = RepositoryFactory (
210+ author = gh_owner , active = True , private = False , name = "repo1"
211+ )
212+ BranchFactory (repository = repo , name = "master" )
213+ response = self ._get (
214+ kwargs = {
215+ "service" : "gh" ,
216+ "owner_username" : gh_owner .username ,
217+ "repo_name" : "repo1" ,
218+ "bundle" : "main" ,
219+ "ext" : "svg" ,
220+ }
221+ )
222+ expected_badge = """<svg xmlns="http://www.w3.org/2000/svg" width="106" height="20">
223+ <linearGradient id="CodecovBadgeGradient" x2="0" y2="100%">
224+ <stop offset="0" stop-color="#bbb" stop-opacity=".1" />
225+ <stop offset="1" stop-opacity=".1" />
226+ </linearGradient>
227+ <mask id="CodecovBadgeMask106px">
228+ <rect width="106" height="20" rx="3" fill="#fff" />
229+ </mask>
230+ <g mask="url(#CodecovBadgeMask106px)">
231+ <path fill="#555" d="M0 0h47v20H0z" />
232+ <path fill="#2C2433" d="M47 0h59v20H47z" />
233+ <path fill="url(#CodecovBadgeGradient)" d="M0 0h106v20H0z" />
234+ </g>
235+ <g fill="#fff" text-anchor="left" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
236+ <text x="5" y="15" fill="#010101" fill-opacity=".3">bundle</text>
237+ <text x="5" y="14">bundle</text>
238+ <text x="52" y="15" fill="#010101" fill-opacity=".3">unknown</text>
239+ <text x="52" y="14">unknown</text>
240+ </g>
241+ </svg>
242+ """
243+ badge = response .content .decode ("utf-8" )
244+ badge = [line .strip () for line in badge .split ("\n " )]
245+ expected_badge = [line .strip () for line in expected_badge .split ("\n " )]
246+ assert expected_badge == badge
247+ assert response .status_code == status .HTTP_200_OK
248+
249+ @patch ("services.bundle_analysis.load_report" )
250+ def test_text_badge (self , mock_load_report ):
251+ class MockBundle :
252+ def total_size (self ):
253+ return 1500000
254+
255+ class MockBundleReport :
256+ def bundle_report (self , name ):
257+ if name == "main" :
258+ return MockBundle ()
259+ return None
260+
261+ gh_owner = OwnerFactory (service = "github" )
262+ repo = RepositoryFactory (
263+ author = gh_owner , active = True , private = False , name = "repo1"
264+ )
265+ commit = CommitFactory (repository = repo , author = gh_owner )
266+ mock_load_report .return_value = MockBundleReport ()
267+
268+ # test default precision
269+ response = self ._get (
270+ kwargs = {
271+ "service" : "gh" ,
272+ "owner_username" : gh_owner .username ,
273+ "repo_name" : "repo1" ,
274+ "bundle" : "main" ,
275+ "ext" : "txt" ,
276+ }
277+ )
278+
279+ badge = response .content .decode ("utf-8" )
280+ assert badge == "1.5MB"
281+ assert response .status_code == status .HTTP_200_OK
282+
283+ # test precision = 1
284+ response = self ._get (
285+ kwargs = {
286+ "service" : "gh" ,
287+ "owner_username" : gh_owner .username ,
288+ "repo_name" : "repo1" ,
289+ "bundle" : "main" ,
290+ "ext" : "txt" ,
291+ },
292+ data = {"precision" : "1" },
293+ )
294+
295+ badge = response .content .decode ("utf-8" )
296+ assert badge == "1.5MB"
297+ assert response .status_code == status .HTTP_200_OK
298+
299+ # test precision = 0
300+ response = self ._get (
301+ kwargs = {
302+ "service" : "gh" ,
303+ "owner_username" : gh_owner .username ,
304+ "repo_name" : "repo1" ,
305+ "bundle" : "main" ,
306+ "ext" : "txt" ,
307+ },
308+ data = {"precision" : "0" },
309+ )
310+
311+ badge = response .content .decode ("utf-8" )
312+ assert badge == "2MB"
313+ assert response .status_code == status .HTTP_200_OK
314+
315+ @patch ("services.bundle_analysis.load_report" )
316+ def test_svg_badge (self , mock_load_report ):
317+ class MockBundle :
318+ def total_size (self ):
319+ return 1500000
320+
321+ class MockBundleReport :
322+ def bundle_report (self , name ):
323+ if name == "main" :
324+ return MockBundle ()
325+ return None
326+
327+ gh_owner = OwnerFactory (service = "github" )
328+ repo = RepositoryFactory (
329+ author = gh_owner , active = True , private = False , name = "repo1"
330+ )
331+ commit = CommitFactory (repository = repo , author = gh_owner )
332+ mock_load_report .return_value = MockBundleReport ()
333+
334+ # test default precision
335+ response = self ._get (
336+ kwargs = {
337+ "service" : "gh" ,
338+ "owner_username" : gh_owner .username ,
339+ "repo_name" : "repo1" ,
340+ "bundle" : "main" ,
341+ "ext" : "svg" ,
342+ }
343+ )
344+
345+ badge = response .content .decode ("utf-8" )
346+ assert "bundle" in badge
347+ assert "1.5MB" in badge
348+ assert response .status_code == status .HTTP_200_OK
0 commit comments