-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_env.py
More file actions
379 lines (356 loc) · 10.9 KB
/
test_env.py
File metadata and controls
379 lines (356 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
"""Test the get_env_vars function"""
import os
import unittest
from unittest.mock import patch
from env import get_env_vars, get_int_env_var
BODY = "Consider these updates to the CODEOWNERS file to remove users no longer in this organization."
COMMIT_MESSAGE = "Remove users no longer in this organization from CODEOWNERS file"
ORGANIZATION = "Organization01"
TITLE = "Clean up CODEOWNERS file"
TOKEN = "Token01"
class TestEnv(unittest.TestCase):
"""Test the get_env_vars function"""
def setUp(self):
env_keys = [
"BODY",
"COMMIT_MESSAGE",
"DRY_RUN",
"ENABLE_GITHUB_ACTIONS_STEP_SUMMARY",
"EXEMPT_REPOS",
"GH_APP_ID",
"GH_ENTERPRISE_URL",
"GH_APP_INSTALLATION_ID",
"GH_APP_PRIVATE_KEY",
"GH_TOKEN",
"ORGANIZATION",
"REPOSITORY",
"TITLE",
"ISSUE_REPORT",
]
for key in env_keys:
if key in os.environ:
del os.environ[key]
@patch.dict(
os.environ,
{
"BODY": BODY,
"COMMIT_MESSAGE": COMMIT_MESSAGE,
"DRY_RUN": "false",
"EXEMPT_REPOS": "repo4,repo5",
"GH_APP_ID": "",
"GH_ENTERPRISE_URL": "",
"GH_APP_INSTALLATION_ID": "",
"GH_APP_PRIVATE_KEY": "",
"GH_TOKEN": TOKEN,
"ORGANIZATION": ORGANIZATION,
"REPOSITORY": "org/repo1,org2/repo2",
"TITLE": TITLE,
},
)
def test_get_env_vars_with_org(self):
"""Test that all environment variables are set correctly using an organization"""
expected_result = (
ORGANIZATION,
["org/repo1", "org2/repo2"],
None,
None,
b"",
False,
TOKEN,
"",
["repo4", "repo5"],
False,
TITLE,
BODY,
COMMIT_MESSAGE,
False,
True,
)
result = get_env_vars(True)
self.assertEqual(result, expected_result)
@patch.dict(
os.environ,
{
"BODY": BODY,
"COMMIT_MESSAGE": COMMIT_MESSAGE,
"DRY_RUN": "true",
"EXEMPT_REPOS": "repo4,repo5",
"GH_APP_ID": "12345",
"GH_ENTERPRISE_URL": "",
"GH_APP_INSTALLATION_ID": "678910",
"GH_APP_PRIVATE_KEY": "hello",
"GH_TOKEN": "",
"ORGANIZATION": "",
"REPOSITORY": "org/repo1,org2/repo2",
"TITLE": TITLE,
},
clear=True,
)
def test_get_env_vars_with_github_app_and_repos(self):
"""Test that all environment variables are set correctly using a list of repositories"""
expected_result = (
"",
["org/repo1", "org2/repo2"],
12345,
678910,
b"hello",
False,
"",
"",
["repo4", "repo5"],
True,
TITLE,
BODY,
COMMIT_MESSAGE,
False,
True,
)
result = get_env_vars(True)
self.assertEqual(result, expected_result)
@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
"GH_APP_ID": "12345",
"GH_APP_INSTALLATION_ID": "",
"GH_APP_PRIVATE_KEY": "",
"GH_TOKEN": "",
},
clear=True,
)
def test_get_env_vars_auth_with_github_app_installation_missing_inputs(self):
"""Test that an error is raised there are missing inputs for the gh app"""
with self.assertRaises(ValueError) as context_manager:
get_env_vars(True)
the_exception = context_manager.exception
self.assertEqual(
str(the_exception),
"GH_APP_ID set and GH_APP_INSTALLATION_ID or GH_APP_PRIVATE_KEY variable not set",
)
@patch.dict(
os.environ,
{
"BODY": BODY,
"COMMIT_MESSAGE": COMMIT_MESSAGE,
"DRY_RUN": "true",
"EXEMPT_REPOS": "repo4,repo5",
"GH_APP_ID": "",
"GH_ENTERPRISE_URL": "",
"GH_APP_INSTALLATION_ID": "",
"GH_APP_PRIVATE_KEY": "",
"GH_TOKEN": TOKEN,
"ORGANIZATION": "",
"REPOSITORY": "org/repo1,org2/repo2",
"TITLE": TITLE,
},
clear=True,
)
def test_get_env_vars_with_token_and_repos(self):
"""Test that all environment variables are set correctly using a list of repositories"""
expected_result = (
"",
["org/repo1", "org2/repo2"],
None,
None,
b"",
False,
TOKEN,
"",
["repo4", "repo5"],
True,
TITLE,
BODY,
COMMIT_MESSAGE,
False,
True,
)
result = get_env_vars(True)
self.assertEqual(result, expected_result)
@patch.dict(
os.environ,
{
"BODY": BODY,
"COMMIT_MESSAGE": COMMIT_MESSAGE,
"GH_APP_ID": "",
"GH_APP_INSTALLATION_ID": "",
"GH_APP_PRIVATE_KEY": "",
"GH_TOKEN": TOKEN,
"ORGANIZATION": ORGANIZATION,
"TITLE": TITLE,
"ISSUE_REPORT": "true",
},
)
def test_get_env_vars_optional_values(self):
"""Test that optional values are set to their default values if not provided"""
expected_result = (
ORGANIZATION,
[],
None,
None,
b"",
False,
TOKEN,
"",
[],
False,
TITLE,
BODY,
COMMIT_MESSAGE,
True,
True,
)
result = get_env_vars(True)
self.assertEqual(result, expected_result)
@patch.dict(
os.environ,
{
"GH_APP_ID": "",
"GH_APP_INSTALLATION_ID": "",
"GH_APP_PRIVATE_KEY": "",
"GH_TOKEN": TOKEN,
"ORGANIZATION": ORGANIZATION,
"ENABLE_GITHUB_ACTIONS_STEP_SUMMARY": "false",
},
clear=True,
)
def test_get_env_vars_with_step_summary_disabled(self):
"""Test that ENABLE_GITHUB_ACTIONS_STEP_SUMMARY can be explicitly disabled"""
expected_result = (
ORGANIZATION,
[],
None,
None,
b"",
False,
TOKEN,
"",
[],
False,
"Clean up CODEOWNERS file",
"Consider these updates to the CODEOWNERS file to remove users no longer in this organization.",
"Remove users no longer in this organization from CODEOWNERS file",
False,
False,
)
result = get_env_vars(True)
self.assertEqual(result, expected_result)
@patch.dict(os.environ, {})
def test_get_env_vars_missing_org_or_repo(self):
"""Test that an error is raised if required environment variables are not set"""
with self.assertRaises(ValueError):
get_env_vars(True)
@patch.dict(
os.environ,
{
"ORGANIZATION": ORGANIZATION,
},
clear=True,
)
def test_get_env_vars_missing_token(self):
"""Test that an error is raised if required environment variables are not set"""
with self.assertRaises(ValueError):
get_env_vars(True)
@patch.dict(
os.environ,
{
"GH_APP_ID": "",
"GH_APP_INSTALLATION_ID": "",
"GH_APP_PRIVATE_KEY": "",
"GH_TOKEN": TOKEN,
"ORGANIZATION": ORGANIZATION,
},
clear=True,
)
def test_get_env_vars_with_repos_no_dry_run(self):
"""Test that all environment variables are set correctly when DRY_RUN is false"""
expected_result = (
ORGANIZATION,
[],
None,
None,
b"",
False,
TOKEN,
"",
[],
False,
"Clean up CODEOWNERS file",
"Consider these updates to the CODEOWNERS file to remove users no longer in this organization.",
"Remove users no longer in this organization from CODEOWNERS file",
False,
True,
)
result = get_env_vars(True)
self.assertEqual(result, expected_result)
def test_get_int_env_var_returns_none_for_non_integer(self):
"""Test that get_int_env_var returns None when the value is not an integer."""
with patch.dict(os.environ, {"TEST_VAR": "not_a_number"}):
result = get_int_env_var("TEST_VAR")
self.assertIsNone(result)
@patch("env.load_dotenv")
@patch.dict(
os.environ,
{
"GH_TOKEN": TOKEN,
"ORGANIZATION": ORGANIZATION,
},
clear=True,
)
def test_get_env_vars_loads_dotenv_when_not_test(self, mock_load_dotenv):
"""Test that get_env_vars loads from .env file when test=False."""
result = get_env_vars(False)
self.assertEqual(result[0], ORGANIZATION)
mock_load_dotenv.assert_called_once()
@patch.dict(
os.environ,
{
"GH_TOKEN": TOKEN,
"REPOSITORY": "/invalid-repo",
},
clear=True,
)
def test_get_env_vars_raises_for_repository_starting_with_slash(self):
"""Test that REPOSITORY starting with / raises ValueError."""
with self.assertRaises(ValueError):
get_env_vars(True)
@patch.dict(
os.environ,
{
"GH_TOKEN": TOKEN,
"ORGANIZATION": ORGANIZATION,
"TITLE": "x" * 71,
},
clear=True,
)
def test_get_env_vars_raises_for_title_too_long(self):
"""Test that TITLE longer than 70 characters raises ValueError."""
with self.assertRaises(ValueError):
get_env_vars(True)
@patch.dict(
os.environ,
{
"GH_TOKEN": TOKEN,
"ORGANIZATION": ORGANIZATION,
"BODY": "x" * 65537,
},
clear=True,
)
def test_get_env_vars_raises_for_body_too_long(self):
"""Test that BODY longer than 65536 characters raises ValueError."""
with self.assertRaises(ValueError):
get_env_vars(True)
@patch.dict(
os.environ,
{
"GH_TOKEN": TOKEN,
"ORGANIZATION": ORGANIZATION,
"COMMIT_MESSAGE": "x" * 65537,
},
clear=True,
)
def test_get_env_vars_raises_for_commit_message_too_long(self):
"""Test that COMMIT_MESSAGE longer than 65536 characters raises ValueError."""
with self.assertRaises(ValueError):
get_env_vars(True)
if __name__ == "__main__":
unittest.main()