Skip to content

Commit 3c2f45a

Browse files
authored
Merge branch 'quantumlib:main' into main
2 parents 0c6295c + a311619 commit 3c2f45a

3 files changed

Lines changed: 76 additions & 6 deletions

File tree

dev_tools/prepared_env.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ def report_status_to_github(
9797
if target_url is not None:
9898
payload['target_url'] = target_url
9999

100-
url = "https://api.github.com/repos/{}/{}/statuses/{}?access_token={}".format(
101-
self.repository.organization,
102-
self.repository.name,
103-
self.actual_commit_id,
104-
self.repository.access_token,
100+
if self.actual_commit_id is None:
101+
return
102+
url = "https://api.github.com/repos/{}/{}/statuses/{}".format(
103+
self.repository.organization, self.repository.name, self.actual_commit_id
105104
)
105+
headers = {'Authorization': 'Bearer {}'.format(self.repository.access_token)}
106106

107-
response = requests.post(url, json=payload)
107+
response = requests.post(url, json=payload, headers=headers, timeout=30)
108108

109109
if response.status_code != 201:
110110
raise IOError(
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import unittest
2+
from unittest.mock import patch, MagicMock
3+
from dev_tools.prepared_env import PreparedEnv
4+
from dev_tools.github_repository import GithubRepository
5+
6+
7+
class TestPreparedEnvSecurity(unittest.TestCase):
8+
@patch('requests.post')
9+
def test_report_status_to_github_token_in_header(self, mock_post):
10+
# Setup
11+
mock_response = MagicMock()
12+
mock_response.status_code = 201
13+
mock_post.return_value = mock_response
14+
15+
repo = GithubRepository('my-org', 'my-repo', 'my-token')
16+
env = PreparedEnv(repo, 'my-commit', 'compare-commit', None, None)
17+
18+
# Execute
19+
env.report_status_to_github('success', 'desc', 'ctx')
20+
21+
# Verify
22+
args, kwargs = mock_post.call_args
23+
url = args[0]
24+
headers = kwargs.get('headers', {})
25+
26+
# Security check: Token should NOT be in the URL
27+
self.assertNotIn('access_token=my-token', url, "Token should not be passed in the URL")
28+
29+
# Security check: Token should be in the Authorization header
30+
self.assertEqual(
31+
headers.get('Authorization'),
32+
'Bearer my-token',
33+
"Token should be passed in the Authorization header",
34+
)
35+
36+
37+
if __name__ == '__main__':
38+
unittest.main()

src/openfermion/utils/commutators_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,38 @@ def test_double_commtator_more_info_both_hopping(self):
217217
com, (FermionOperator('4^ 3^ 4 2', 2.73) + FermionOperator('4^ 2^ 4 3', 2.73))
218218
)
219219

220+
def test_double_commutator_hopping_no_intersection(self):
221+
# Case where intersection is empty
222+
op1 = FermionOperator('0^ 0')
223+
op2 = FermionOperator('1^ 2') + FermionOperator('2^ 1')
224+
op3 = FermionOperator('3^ 4') + FermionOperator('4^ 3')
225+
res = double_commutator(
226+
op1,
227+
op2,
228+
op3,
229+
indices2={1, 2},
230+
indices3={3, 4},
231+
is_hopping_operator2=True,
232+
is_hopping_operator3=True,
233+
)
234+
self.assertEqual(res, FermionOperator.zero())
235+
236+
def test_double_commutator_hopping_multi_intersection(self):
237+
# Case where intersection has more than one element
238+
op1 = FermionOperator('0^ 0')
239+
op2 = FermionOperator('1^ 2') + FermionOperator('2^ 1')
240+
op3 = FermionOperator('1^ 2') + FermionOperator('2^ 1')
241+
res = double_commutator(
242+
op1,
243+
op2,
244+
op3,
245+
indices2={1, 2},
246+
indices3={1, 2},
247+
is_hopping_operator2=True,
248+
is_hopping_operator3=True,
249+
)
250+
self.assertEqual(res, FermionOperator.zero())
251+
220252

221253
class TriviallyDoubleCommutesDualBasisUsingTermInfoTest(unittest.TestCase):
222254
def test_number_operators_trivially_commute(self):

0 commit comments

Comments
 (0)