33from pathlib import Path
44from unittest import TestCase
55from unittest .mock import call , Mock , patch
6+ import os
7+ import requests
68
79# NOTE: This test file file only works with scripts/ added to PYTHONPATH so pylint can't find the imports
810# pragma pylint: disable=import-error
@@ -31,7 +33,7 @@ def _git_run_command_mock(command):
3133 "If you have updated the code, please remember to add matching command fixtures above."
3234 )
3335
34- def _requests_get_mock (url , params , timeout ):
36+ def _requests_get_mock (url , params , headers , timeout ):
3537 response_mock = Mock ()
3638
3739 if url == 'https://api.github.com/repos/ethereum/solidity/pulls/12818' :
@@ -155,25 +157,37 @@ def _requests_get_mock(url, params, timeout):
155157 return response_mock
156158
157159 if url == 'https://circleci.com/api/v2/project/gh/ethereum/solidity/1018023/artifacts' :
158- response_mock .json .return_value = {
159- "next_page_token" : None ,
160- "items" : [
161- {
162- "path" : "reports/externalTests/all-benchmarks.json" ,
163- "url" : "https://circle-artifacts.com/0/reports/externalTests/all-benchmarks.json"
164- },
165- {
166- "path" : "reports/externalTests/summarized-benchmarks.json" ,
167- "url" : "https://circle-artifacts.com/0/reports/externalTests/summarized-benchmarks.json"
168- }
169- ]
170- }
160+ if (
161+ os .environ .get ('CIRCLECI_TOKEN' ) == 'valid_token' and
162+ headers .get ('Circle-Token' ) == os .environ .get ('CIRCLECI_TOKEN' )
163+ ):
164+ response_mock .json .return_value = {
165+ "next_page_token" : None ,
166+ "items" : [
167+ {
168+ "path" : "reports/externalTests/all-benchmarks.json" ,
169+ "url" : "https://circle-artifacts.com/0/reports/externalTests/all-benchmarks.json"
170+ },
171+ {
172+ "path" : "reports/externalTests/summarized-benchmarks.json" ,
173+ "url" : "https://circle-artifacts.com/0/reports/externalTests/summarized-benchmarks.json"
174+ }
175+ ]
176+ }
177+ else :
178+ response_mock .status_code = 401
179+ response_mock .json .return_value = {
180+ "message" : "Unauthorized"
181+ }
182+ error = requests .exceptions .HTTPError (f"401 Client Error: Unauthorized for url: { url } " )
183+ response_mock .raise_for_status .side_effect = error
171184 return response_mock
172185
173186 raise RuntimeError (
174187 "The test tried to perform an unexpected GET request.\n "
175188 f"URL: { url } \n " +
176189 (f"query: { params } \n " if len (params ) > 0 else "" ) +
190+ (f"headers: { headers } \n " if len (headers ) > 0 else "" ) +
177191 f"timeout: { timeout } \n " +
178192 "If you have updated the code, please remember to add matching response fixtures above."
179193 )
@@ -186,17 +200,20 @@ def setUp(self):
186200 @patch ('externalTests.download_benchmarks.download_file' )
187201 @patch ('requests.get' , _requests_get_mock )
188202 @patch ('common.git_helpers.run_git_command' ,_git_run_command_mock )
203+ @patch .dict (os .environ , {'CIRCLECI_TOKEN' : 'valid_token' })
189204 def test_download_benchmarks (download_file_mock ):
190205 download_benchmarks (None , None , None , silent = True )
191206 download_file_mock .assert_has_calls ([
192207 call (
193208 'https://circle-artifacts.com/0/reports/externalTests/summarized-benchmarks.json' ,
194209 Path ('summarized-benchmarks-benchmark-downloader-fa1ddc6f.json' ),
210+ {'Circle-Token' : 'valid_token' },
195211 False
196212 ),
197213 call (
198214 'https://circle-artifacts.com/0/reports/externalTests/all-benchmarks.json' ,
199215 Path ('all-benchmarks-benchmark-downloader-fa1ddc6f.json' ),
216+ {'Circle-Token' : 'valid_token' },
200217 False
201218 ),
202219 ])
@@ -205,17 +222,20 @@ def test_download_benchmarks(download_file_mock):
205222 @patch ('externalTests.download_benchmarks.download_file' )
206223 @patch ('requests.get' , _requests_get_mock )
207224 @patch ('common.git_helpers.run_git_command' ,_git_run_command_mock )
225+ @patch .dict (os .environ , {'CIRCLECI_TOKEN' : 'valid_token' })
208226 def test_download_benchmarks_branch (download_file_mock ):
209227 download_benchmarks ('develop' , None , None , silent = True )
210228 download_file_mock .assert_has_calls ([
211229 call (
212230 'https://circle-artifacts.com/0/reports/externalTests/summarized-benchmarks.json' ,
213231 Path ('summarized-benchmarks-develop-43f29c00.json' ),
232+ {'Circle-Token' : 'valid_token' },
214233 False
215234 ),
216235 call (
217236 'https://circle-artifacts.com/0/reports/externalTests/all-benchmarks.json' ,
218237 Path ('all-benchmarks-develop-43f29c00.json' ),
238+ {'Circle-Token' : 'valid_token' },
219239 False
220240 ),
221241 ])
@@ -224,17 +244,20 @@ def test_download_benchmarks_branch(download_file_mock):
224244 @patch ('externalTests.download_benchmarks.download_file' )
225245 @patch ('requests.get' , _requests_get_mock )
226246 @patch ('common.git_helpers.run_git_command' ,_git_run_command_mock )
247+ @patch .dict (os .environ , {'CIRCLECI_TOKEN' : 'valid_token' })
227248 def test_download_benchmarks_pr (download_file_mock ):
228249 download_benchmarks (None , 12818 , None , silent = True )
229250 download_file_mock .assert_has_calls ([
230251 call (
231252 'https://circle-artifacts.com/0/reports/externalTests/summarized-benchmarks.json' ,
232253 Path ('summarized-benchmarks-benchmark-downloader-fa1ddc6f.json' ),
254+ {'Circle-Token' : 'valid_token' },
233255 False
234256 ),
235257 call (
236258 'https://circle-artifacts.com/0/reports/externalTests/all-benchmarks.json' ,
237259 Path ('all-benchmarks-benchmark-downloader-fa1ddc6f.json' ),
260+ {'Circle-Token' : 'valid_token' },
238261 False
239262 ),
240263 ])
@@ -243,17 +266,30 @@ def test_download_benchmarks_pr(download_file_mock):
243266 @patch ('externalTests.download_benchmarks.download_file' )
244267 @patch ('requests.get' , _requests_get_mock )
245268 @patch ('common.git_helpers.run_git_command' ,_git_run_command_mock )
269+ @patch .dict (os .environ , {'CIRCLECI_TOKEN' : 'valid_token' })
246270 def test_download_benchmarks_base_of_pr (download_file_mock ):
247271 download_benchmarks (None , None , 12818 , silent = True )
248272 download_file_mock .assert_has_calls ([
249273 call (
250274 'https://circle-artifacts.com/0/reports/externalTests/summarized-benchmarks.json' ,
251275 Path ('summarized-benchmarks-develop-43f29c00.json' ),
276+ {'Circle-Token' : 'valid_token' },
252277 False
253278 ),
254279 call (
255280 'https://circle-artifacts.com/0/reports/externalTests/all-benchmarks.json' ,
256281 Path ('all-benchmarks-develop-43f29c00.json' ),
282+ {'Circle-Token' : 'valid_token' },
257283 False
258284 ),
259285 ])
286+
287+ # NOTE: No circleci token is set in the environment
288+ @patch ('externalTests.download_benchmarks.download_file' )
289+ @patch ('requests.get' , _requests_get_mock )
290+ @patch ('common.git_helpers.run_git_command' ,_git_run_command_mock )
291+ def test_download_benchmarks_unauthorized_request (self , _ ):
292+ with self .assertRaises (requests .exceptions .HTTPError ) as manager :
293+ download_benchmarks (None , None , None , silent = True )
294+
295+ self .assertIn ('401 Client Error: Unauthorized' , str (manager .exception ))
0 commit comments