Skip to content

Commit 8480832

Browse files
committed
unify how we set GITHUB_TOKEN in tests
1 parent 2b8bc14 commit 8480832

6 files changed

Lines changed: 117 additions & 96 deletions

File tree

integrations/github/tests/test_file_editor.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_init_default(self, monkeypatch):
2222
assert editor.raise_on_failure is True
2323

2424
def test_init_with_parameters(self):
25-
token = Secret.from_token("test_token")
25+
token = Secret.from_token("test-token")
2626
editor = GithubFileEditor(github_token=token, repo="owner/repo", branch="feature", raise_on_failure=False)
2727
assert editor.github_token == token
2828
assert editor.default_repo == "owner/repo"
@@ -33,7 +33,7 @@ def test_init_with_parameters(self):
3333
GithubFileEditor(github_token="not_a_secret")
3434

3535
def test_to_dict(self, monkeypatch):
36-
monkeypatch.setenv("ENV_VAR", "test_token")
36+
monkeypatch.setenv("ENV_VAR", "test-token")
3737

3838
token = Secret.from_env_var("ENV_VAR")
3939

@@ -52,7 +52,7 @@ def test_to_dict(self, monkeypatch):
5252
}
5353

5454
def test_from_dict(self, monkeypatch):
55-
monkeypatch.setenv("ENV_VAR", "test_token")
55+
monkeypatch.setenv("ENV_VAR", "test-token")
5656
data = {
5757
"type": "haystack_integrations.components.connectors.github.file_editor.GithubFileEditor",
5858
"init_parameters": {
@@ -72,16 +72,17 @@ def test_from_dict(self, monkeypatch):
7272

7373
@patch("requests.get")
7474
@patch("requests.put")
75-
def test_run_edit(self, mock_put, mock_get):
75+
def test_run_edit(self, mock_put, mock_get, monkeypatch):
76+
monkeypatch.setenv("GITHUB_TOKEN", "test-token")
77+
7678
mock_get.return_value.json.return_value = {
7779
"content": "SGVsbG8gV29ybGQ=", # Base64 encoded "Hello World"
7880
"sha": "abc123",
7981
}
8082
mock_get.return_value.raise_for_status.return_value = None
8183
mock_put.return_value.raise_for_status.return_value = None
8284

83-
token = Secret.from_token("test_token")
84-
editor = GithubFileEditor(github_token=token)
85+
editor = GithubFileEditor()
8586

8687
result = editor.run(
8788
command=Command.EDIT,
@@ -97,7 +98,7 @@ def test_run_edit(self, mock_put, mock_get):
9798
headers={
9899
"Accept": "application/vnd.github.v3+json",
99100
"User-Agent": "Haystack/GithubFileEditor",
100-
"Authorization": "Bearer test_token",
101+
"Authorization": "Bearer test-token",
101102
},
102103
params={"ref": "main"},
103104
timeout=10,
@@ -112,7 +113,9 @@ def test_run_edit(self, mock_put, mock_get):
112113

113114
@patch("requests.get")
114115
@patch("requests.patch")
115-
def test_run_undo(self, mock_patch, mock_get):
116+
def test_run_undo(self, mock_patch, mock_get, monkeypatch):
117+
monkeypatch.setenv("GITHUB_TOKEN", "test-token")
118+
116119
def create_mock_response(json_data, status_code=200):
117120
class MockResponse:
118121
def __init__(self, data, code):
@@ -143,8 +146,7 @@ def get_side_effect(url, **_):
143146

144147
mock_patch.return_value.raise_for_status.return_value = None
145148

146-
token = Secret.from_token("test_token")
147-
editor = GithubFileEditor(github_token=token)
149+
editor = GithubFileEditor()
148150

149151
result = editor.run(
150152
command=Command.UNDO, payload={"message": "Undo last change"}, repo="owner/repo", branch="main"
@@ -158,18 +160,19 @@ def get_side_effect(url, **_):
158160
headers={
159161
"Accept": "application/vnd.github.v3+json",
160162
"User-Agent": "Haystack/GithubFileEditor",
161-
"Authorization": "Bearer test_token",
163+
"Authorization": "Bearer test-token",
162164
},
163165
json={"sha": "def456", "force": True},
164166
timeout=10,
165167
)
166168

167169
@patch("requests.put")
168-
def test_run_create(self, mock_put):
170+
def test_run_create(self, mock_put, monkeypatch):
171+
monkeypatch.setenv("GITHUB_TOKEN", "test-token")
172+
169173
mock_put.return_value.raise_for_status.return_value = None
170174

171-
token = Secret.from_token("test_token")
172-
editor = GithubFileEditor(github_token=token)
175+
editor = GithubFileEditor()
173176

174177
result = editor.run(
175178
command=Command.CREATE,
@@ -185,7 +188,7 @@ def test_run_create(self, mock_put):
185188
headers={
186189
"Accept": "application/vnd.github.v3+json",
187190
"User-Agent": "Haystack/GithubFileEditor",
188-
"Authorization": "Bearer test_token",
191+
"Authorization": "Bearer test-token",
189192
},
190193
json={
191194
"message": "Create new file",
@@ -197,7 +200,9 @@ def test_run_create(self, mock_put):
197200

198201
@patch("requests.get")
199202
@patch("requests.delete")
200-
def test_run_delete(self, mock_delete, mock_get):
203+
def test_run_delete(self, mock_delete, mock_get, monkeypatch):
204+
monkeypatch.setenv("GITHUB_TOKEN", "test-token")
205+
201206
mock_get.return_value.json.return_value = {
202207
"content": "SGVsbG8gV29ybGQ=", # Base64 encoded "Hello World"
203208
"sha": "abc123",
@@ -206,8 +211,7 @@ def test_run_delete(self, mock_delete, mock_get):
206211

207212
mock_delete.return_value.raise_for_status.return_value = None
208213

209-
token = Secret.from_token("test_token")
210-
editor = GithubFileEditor(github_token=token)
214+
editor = GithubFileEditor()
211215

212216
result = editor.run(
213217
command=Command.DELETE,
@@ -223,7 +227,7 @@ def test_run_delete(self, mock_delete, mock_get):
223227
headers={
224228
"Accept": "application/vnd.github.v3+json",
225229
"User-Agent": "Haystack/GithubFileEditor",
226-
"Authorization": "Bearer test_token",
230+
"Authorization": "Bearer test-token",
227231
},
228232
params={"ref": "main"},
229233
timeout=10,
@@ -234,18 +238,19 @@ def test_run_delete(self, mock_delete, mock_get):
234238
headers={
235239
"Accept": "application/vnd.github.v3+json",
236240
"User-Agent": "Haystack/GithubFileEditor",
237-
"Authorization": "Bearer test_token",
241+
"Authorization": "Bearer test-token",
238242
},
239243
json={"message": "Delete file", "sha": "abc123", "branch": "main"},
240244
timeout=10,
241245
)
242246

243247
@patch("requests.get")
244-
def test_run_error_handling(self, mock_get):
248+
def test_run_error_handling(self, mock_get, monkeypatch):
249+
monkeypatch.setenv("GITHUB_TOKEN", "test-token")
250+
245251
mock_get.side_effect = requests.RequestException("API Error")
246252

247-
token = Secret.from_token("test_token")
248-
editor = GithubFileEditor(github_token=token, raise_on_failure=False)
253+
editor = GithubFileEditor(raise_on_failure=False)
249254

250255
result = editor.run(
251256
command=Command.EDIT,
@@ -256,7 +261,7 @@ def test_run_error_handling(self, mock_get):
256261

257262
assert "Error: API Error" in result["result"]
258263

259-
editor = GithubFileEditor(github_token=token, raise_on_failure=True)
264+
editor = GithubFileEditor(raise_on_failure=True)
260265
with pytest.raises(requests.RequestException):
261266
editor.run(
262267
command=Command.EDIT,

integrations/github/tests/test_issue_commenter.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ def test_init_default(self, monkeypatch):
2121
assert commenter.retry_attempts == 2
2222

2323
def test_init_with_parameters(self):
24-
token = Secret.from_token("test_token")
24+
token = Secret.from_token("test-token")
2525
commenter = GithubIssueCommenter(github_token=token, raise_on_failure=False, retry_attempts=3)
2626
assert commenter.github_token == token
2727
assert commenter.raise_on_failure is False
2828
assert commenter.retry_attempts == 3
2929

3030
def test_to_dict(self, monkeypatch):
31-
monkeypatch.setenv("ENV_VAR", "test_token")
31+
monkeypatch.setenv("ENV_VAR", "test-token")
3232

3333
token = Secret.from_env_var("ENV_VAR")
3434

@@ -46,7 +46,7 @@ def test_to_dict(self, monkeypatch):
4646
}
4747

4848
def test_from_dict(self, monkeypatch):
49-
monkeypatch.setenv("ENV_VAR", "test_token")
49+
monkeypatch.setenv("ENV_VAR", "test-token")
5050

5151
data = {
5252
"type": "haystack_integrations.components.connectors.github.issue_commenter.GithubIssueCommenter",
@@ -64,11 +64,12 @@ def test_from_dict(self, monkeypatch):
6464
assert commenter.retry_attempts == 3
6565

6666
@patch("requests.post")
67-
def test_run(self, mock_post):
67+
def test_run(self, mock_post, monkeypatch):
68+
monkeypatch.setenv("GITHUB_TOKEN", "test-token")
69+
6870
mock_post.return_value.raise_for_status.return_value = None
6971

70-
token = Secret.from_token("test_token")
71-
commenter = GithubIssueCommenter(github_token=token)
72+
commenter = GithubIssueCommenter()
7273

7374
result = commenter.run(url="https://github.com/owner/repo/issues/123", comment="Test comment")
7475

@@ -79,30 +80,32 @@ def test_run(self, mock_post):
7980
headers={
8081
"Accept": "application/vnd.github.v3+json",
8182
"User-Agent": "Haystack/GithubIssueCommenter",
82-
"Authorization": "Bearer test_token",
83+
"Authorization": "Bearer test-token",
8384
},
8485
json={"body": "Test comment"},
8586
timeout=10,
8687
)
8788

8889
@patch("requests.post")
89-
def test_run_error_handling(self, mock_post):
90+
def test_run_error_handling(self, mock_post, monkeypatch):
91+
monkeypatch.setenv("GITHUB_TOKEN", "test-token")
92+
9093
mock_post.side_effect = requests.RequestException("API Error")
9194

92-
token = Secret.from_token("test_token")
93-
commenter = GithubIssueCommenter(github_token=token, raise_on_failure=False)
95+
commenter = GithubIssueCommenter(raise_on_failure=False)
9496

9597
result = commenter.run(url="https://github.com/owner/repo/issues/123", comment="Test comment")
9698

9799
assert result["success"] is False
98100

99-
commenter = GithubIssueCommenter(github_token=token, raise_on_failure=True)
101+
commenter = GithubIssueCommenter(raise_on_failure=True)
100102
with pytest.raises(requests.RequestException):
101103
commenter.run(url="https://github.com/owner/repo/issues/123", comment="Test comment")
102104

103-
def test_parse_github_url(self):
104-
token = Secret.from_token("test_token")
105-
commenter = GithubIssueCommenter(github_token=token)
105+
def test_parse_github_url(self, monkeypatch):
106+
monkeypatch.setenv("GITHUB_TOKEN", "test-token")
107+
108+
commenter = GithubIssueCommenter()
106109

107110
owner, repo, issue_number = commenter._parse_github_url("https://github.com/owner/repo/issues/123")
108111
assert owner == "owner"

integrations/github/tests/test_issue_viewer.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212

1313
class TestGithubIssueViewer:
14-
def test_init_default(self):
14+
def test_init_default(self, monkeypatch):
15+
monkeypatch.setenv("GITHUB_TOKEN", "test-token")
16+
1517
viewer = GithubIssueViewer()
1618
assert viewer.github_token is None
1719
assert viewer.raise_on_failure is True
@@ -27,7 +29,7 @@ def test_init_with_parameters(self, monkeypatch):
2729
assert viewer.retry_attempts == 3
2830

2931
def test_to_dict(self, monkeypatch):
30-
monkeypatch.setenv("ENV_VAR", "test_token")
32+
monkeypatch.setenv("ENV_VAR", "test-token")
3133

3234
token = Secret.from_env_var("ENV_VAR")
3335

@@ -45,7 +47,7 @@ def test_to_dict(self, monkeypatch):
4547
}
4648

4749
def test_from_dict(self, monkeypatch):
48-
monkeypatch.setenv("ENV_VAR", "test_token")
50+
monkeypatch.setenv("ENV_VAR", "test-token")
4951

5052
data = {
5153
"type": "haystack_integrations.components.connectors.github.issue_viewer.GithubIssueViewer",
@@ -63,7 +65,9 @@ def test_from_dict(self, monkeypatch):
6365
assert viewer.retry_attempts == 3
6466

6567
@patch("requests.get")
66-
def test_run(self, mock_get):
68+
def test_run(self, mock_get, monkeypatch):
69+
monkeypatch.setenv("GITHUB_TOKEN", "test-token")
70+
6771
mock_get.return_value.json.return_value = {
6872
"body": "Issue body",
6973
"title": "Issue title",
@@ -105,8 +109,7 @@ def test_run(self, mock_get):
105109
),
106110
]
107111

108-
token = Secret.from_token("test_token")
109-
viewer = GithubIssueViewer(github_token=token)
112+
viewer = GithubIssueViewer()
110113

111114
result = viewer.run(url="https://github.com/owner/repo/issues/123")
112115

@@ -118,25 +121,27 @@ def test_run(self, mock_get):
118121
assert mock_get.call_count == 2
119122

120123
@patch("requests.get")
121-
def test_run_error_handling(self, mock_get):
124+
def test_run_error_handling(self, mock_get, monkeypatch):
125+
monkeypatch.setenv("GITHUB_TOKEN", "test-token")
126+
122127
mock_get.side_effect = requests.RequestException("API Error")
123128

124-
token = Secret.from_token("test_token")
125-
viewer = GithubIssueViewer(github_token=token, raise_on_failure=False)
129+
viewer = GithubIssueViewer(raise_on_failure=False)
126130

127131
result = viewer.run(url="https://github.com/owner/repo/issues/123")
128132

129133
assert len(result["documents"]) == 1
130134
assert result["documents"][0].meta["type"] == "error"
131135
assert result["documents"][0].meta["error"] is True
132136

133-
viewer = GithubIssueViewer(github_token=token, raise_on_failure=True)
137+
viewer = GithubIssueViewer(raise_on_failure=True)
134138
with pytest.raises(requests.RequestException):
135139
viewer.run(url="https://github.com/owner/repo/issues/123")
136140

137-
def test_parse_github_url(self):
138-
token = Secret.from_token("test_token")
139-
viewer = GithubIssueViewer(github_token=token)
141+
def test_parse_github_url(self, monkeypatch):
142+
monkeypatch.setenv("GITHUB_TOKEN", "test-token")
143+
144+
viewer = GithubIssueViewer()
140145

141146
owner, repo, issue_number = viewer._parse_github_url("https://github.com/owner/repo/issues/123")
142147
assert owner == "owner"

0 commit comments

Comments
 (0)