-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest_env.py
More file actions
495 lines (460 loc) · 14.7 KB
/
test_env.py
File metadata and controls
495 lines (460 loc) · 14.7 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
"""This is the test module for the env module."""
import os
import unittest
from unittest.mock import patch
import env
class TestEnv(unittest.TestCase):
"""
Test case for the env module.
"""
def setUp(self):
env_keys = [
"DRY_RUN",
"END_DATE",
"GH_APP_ID",
"GH_ENTERPRISE_URL",
"GH_APP_INSTALLATION_ID",
"GH_APP_PRIVATE_KEY",
"GITHUB_APP_ENTERPRISE_ONLY",
"GH_TOKEN",
"ORGANIZATION",
"OUTPUT_FILENAME",
"REPOSITORY",
"START_DATE",
"SHOW_AVATAR",
]
for key in env_keys:
if key in os.environ:
del os.environ[key]
@patch.dict(
os.environ,
{
"ORGANIZATION": "org",
"REPOSITORY": "repo,repo2",
"GH_APP_ID": "",
"GH_APP_INSTALLATION_ID": "",
"GH_APP_PRIVATE_KEY": "",
"GH_TOKEN": "token",
"GH_ENTERPRISE_URL": "",
"START_DATE": "2022-01-01",
"END_DATE": "2022-12-31",
"SPONSOR_INFO": "False",
"LINK_TO_PROFILE": "True",
"SHOW_AVATAR": "False",
},
clear=True,
)
def test_get_env_vars(self):
"""
Test the get_env_vars function when all environment variables are set correctly.
"""
(
organization,
repository_list,
gh_app_id,
gh_app_installation_id,
gh_app_private_key,
gh_app_enterprise_only,
token,
ghe,
start_date,
end_date,
sponsor_info,
link_to_profile,
_output_filename,
_show_avatar,
) = env.get_env_vars()
self.assertEqual(organization, "org")
self.assertEqual(repository_list, ["repo", "repo2"])
self.assertIsNone(gh_app_id)
self.assertIsNone(gh_app_installation_id)
self.assertEqual(gh_app_private_key, b"")
self.assertFalse(gh_app_enterprise_only)
self.assertEqual(token, "token")
self.assertEqual(ghe, "")
self.assertEqual(start_date, "2022-01-01")
self.assertEqual(end_date, "2022-12-31")
self.assertFalse(sponsor_info)
self.assertTrue(link_to_profile)
@patch.dict(
os.environ,
{
"ORGANIZATION": "org",
"REPOSITORY": "repo,repo2",
"GH_APP_ID": "",
"GH_APP_INSTALLATION_ID": "",
"GH_APP_PRIVATE_KEY": "",
"GH_TOKEN": "",
"GH_ENTERPRISE_URL": "",
"START_DATE": "2022-01-01",
"END_DATE": "2022-12-31",
"SPONSOR_INFO": "False",
"LINK_TO_PROFILE": "True",
"SHOW_AVATAR": "False",
},
clear=True,
)
def test_get_env_vars_missing_values(self):
"""
Test the get_env_vars function when none of the environment variables are set.
Expect a ValueError to be raised.
"""
with self.assertRaises(ValueError) as cm:
env.get_env_vars()
the_exception = cm.exception
self.assertEqual(str(the_exception), "GH_TOKEN environment variable not set")
@patch.dict(
os.environ,
{
"ORGANIZATION": "org",
"REPOSITORY": "repo,repo2",
"GH_APP_ID": "",
"GH_APP_INSTALLATION_ID": "",
"GH_APP_PRIVATE_KEY": "",
"GH_TOKEN": "token",
"GH_ENTERPRISE_URL": "",
"START_DATE": "2022/01/01",
"END_DATE": "2022-12-31",
"SPONSOR_INFO": "False",
"LINK_TO_PROFILE": "True",
"SHOW_AVATAR": "False",
},
clear=True,
)
def test_get_env_vars_invalid_start_date(self):
"""
Test the get_env_vars function when invalid start date given.
Expect a ValueError to be raised.
"""
with self.assertRaises(ValueError) as cm:
env.get_env_vars()
the_exception = cm.exception
self.assertEqual(
str(the_exception),
"START_DATE environment variable not in the format YYYY-MM-DD",
)
@patch.dict(
os.environ,
{
"ORGANIZATION": "org",
"REPOSITORY": "repo,repo2",
"GH_APP_ID": "",
"GH_APP_INSTALLATION_ID": "",
"GH_APP_PRIVATE_KEY": "",
"GH_TOKEN": "token",
"GH_ENTERPRISE_URL": "",
"START_DATE": "",
"END_DATE": "",
"SPONSOR_INFO": "False",
"LINK_TO_PROFILE": "True",
"SHOW_AVATAR": "False",
},
clear=True,
)
def test_get_env_vars_no_dates(self):
"""
Test the get_env_vars function when all environment variables are set correctly
and start_date and end_date are not set.
"""
(
organization,
repository_list,
gh_app_id,
gh_app_installation_id,
gh_app_private_key,
gh_app_enterprise_only,
token,
ghe,
start_date,
end_date,
sponsor_info,
link_to_profile,
_output_filename,
_show_avatar,
) = env.get_env_vars()
self.assertEqual(organization, "org")
self.assertEqual(repository_list, ["repo", "repo2"])
self.assertIsNone(gh_app_id)
self.assertIsNone(gh_app_installation_id)
self.assertEqual(gh_app_private_key, b"")
self.assertFalse(gh_app_enterprise_only)
self.assertEqual(token, "token")
self.assertEqual(ghe, "")
self.assertEqual(start_date, "")
self.assertEqual(end_date, "")
self.assertFalse(sponsor_info)
self.assertTrue(link_to_profile)
@patch.dict(
os.environ,
{
"ORGANIZATION": "org",
"REPOSITORY": "repo,repo2",
"GH_APP_ID": "",
"GH_APP_INSTALLATION_ID": "",
"GH_APP_PRIVATE_KEY": "",
"GH_TOKEN": "token",
"GH_ENTERPRISE_URL": "",
"START_DATE": "",
"END_DATE": "",
"SPONSOR_INFO": "False",
"LINK_TO_PROFILE": "True",
"OUTPUT_FILENAME": "custom-report.md",
},
clear=True,
)
def test_get_env_vars_custom_output_filename(self):
"""Test that OUTPUT_FILENAME overrides the default output filename."""
(
_organization,
_repository_list,
_gh_app_id,
_gh_app_installation_id,
_gh_app_private_key,
_gh_app_enterprise_only,
_token,
_ghe,
_start_date,
_end_date,
_sponsor_info,
_link_to_profile,
output_filename,
_show_avatar,
) = env.get_env_vars()
self.assertEqual(output_filename, "custom-report.md")
@patch.dict(
os.environ,
{
"ORGANIZATION": "org",
"GH_TOKEN": "token",
"OUTPUT_FILENAME": "../../../etc/passwd",
},
clear=True,
)
def test_get_env_vars_output_filename_path_traversal_rejected(self):
"""Test that OUTPUT_FILENAME rejects path traversal attempts."""
with self.assertRaises(ValueError):
env.get_env_vars()
@patch.dict(
os.environ,
{
"ORGANIZATION": "org",
"GH_TOKEN": "token",
"OUTPUT_FILENAME": "/tmp/output.md",
},
clear=True,
)
def test_get_env_vars_output_filename_absolute_path_rejected(self):
"""Test that OUTPUT_FILENAME rejects absolute paths."""
with self.assertRaises(ValueError):
env.get_env_vars()
@patch.dict(
os.environ,
{
"ORGANIZATION": "org",
"GH_TOKEN": "token",
"OUTPUT_FILENAME": "reports/output.md",
},
clear=True,
)
def test_get_env_vars_output_filename_directory_separator_rejected(self):
"""Test that OUTPUT_FILENAME rejects filenames with directory separators."""
with self.assertRaises(ValueError):
env.get_env_vars()
@patch.dict(
os.environ,
{
"ORGANIZATION": "org",
"GH_TOKEN": "token",
"OUTPUT_FILENAME": "file;rm -rf /.md",
},
clear=True,
)
def test_get_env_vars_output_filename_special_chars_rejected(self):
"""Test that OUTPUT_FILENAME rejects filenames with special characters."""
with self.assertRaises(ValueError):
env.get_env_vars()
@patch.dict(
os.environ,
{
"ORGANIZATION": "org",
"GH_TOKEN": "token",
"OUTPUT_FILENAME": "",
},
clear=True,
)
def test_get_env_vars_output_filename_empty_defaults(self):
"""Test that empty OUTPUT_FILENAME defaults to contributors.md."""
(
_organization,
_repository_list,
_gh_app_id,
_gh_app_installation_id,
_gh_app_private_key,
_gh_app_enterprise_only,
_token,
_ghe,
_start_date,
_end_date,
_sponsor_info,
_link_to_profile,
output_filename,
_show_avatar,
) = env.get_env_vars()
self.assertEqual(output_filename, "contributors.md")
@patch.dict(
os.environ,
{
"ORGANIZATION": "org",
"GH_TOKEN": "token",
"OUTPUT_FILENAME": " ",
},
clear=True,
)
def test_get_env_vars_output_filename_whitespace_defaults(self):
"""Test that whitespace OUTPUT_FILENAME defaults to contributors.md."""
(
_organization,
_repository_list,
_gh_app_id,
_gh_app_installation_id,
_gh_app_private_key,
_gh_app_enterprise_only,
_token,
_ghe,
_start_date,
_end_date,
_sponsor_info,
_link_to_profile,
output_filename,
_show_avatar,
) = env.get_env_vars()
self.assertEqual(output_filename, "contributors.md")
@patch.dict(
os.environ,
{
"ORGANIZATION": "org",
"GH_TOKEN": "token",
"OUTPUT_FILENAME": "bad@name.md",
},
clear=True,
)
def test_get_env_vars_output_filename_invalid_chars_rejected(self):
"""Test that OUTPUT_FILENAME rejects invalid characters."""
with self.assertRaises(ValueError):
env.get_env_vars()
@patch.dict(os.environ, {}, clear=True)
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) as cm:
env.get_env_vars(test=True)
the_exception = cm.exception
self.assertEqual(
str(the_exception),
"ORGANIZATION and REPOSITORY environment variables were not set. Please set one",
)
@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:
env.get_env_vars()
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,
{
"ORGANIZATION": "org",
"REPOSITORY": "repo",
"GH_TOKEN": "token",
"START_DATE": "2025-01-01",
"END_DATE": "2024-01-01",
},
clear=True,
)
def test_get_env_vars_end_date_before_start_date(self):
"""Test that an error is raised when END_DATE is before START_DATE"""
with self.assertRaises(ValueError) as cm:
env.get_env_vars()
the_exception = cm.exception
self.assertEqual(
str(the_exception),
"END_DATE ('2024-01-01') must be after START_DATE ('2025-01-01')",
)
@patch.dict(
os.environ,
{
"ORGANIZATION": "org",
"REPOSITORY": "repo",
"GH_TOKEN": "token",
"START_DATE": "2024-01-01",
"END_DATE": "2024-01-01",
},
clear=True,
)
def test_get_env_vars_equal_start_and_end_date(self):
"""Test that an error is raised when START_DATE equals END_DATE"""
with self.assertRaises(ValueError) as cm:
env.get_env_vars()
the_exception = cm.exception
self.assertEqual(
str(the_exception),
"END_DATE ('2024-01-01') must be after START_DATE ('2024-01-01')",
)
@patch.dict(
os.environ,
{
"ORGANIZATION": "org",
"REPOSITORY": "repo",
"GH_TOKEN": "token",
"START_DATE": "2024-01-01",
"END_DATE": "2025-01-01",
},
clear=True,
)
def test_get_env_vars_valid_date_range(self):
"""Test that valid date range (START_DATE before END_DATE) is accepted"""
(
_organization,
_repository_list,
_gh_app_id,
_gh_app_installation_id,
_gh_app_private_key,
_gh_app_enterprise_only,
_token,
_ghe,
start_date,
end_date,
_sponsor_info,
_link_to_profile,
_output_filename,
_show_avatar,
) = env.get_env_vars()
self.assertEqual(start_date, "2024-01-01")
self.assertEqual(end_date, "2025-01-01")
@patch.dict(os.environ, {"TEST_INT": "12.34"}, clear=True)
def test_get_int_env_var_returns_none_for_invalid_int(self):
"""Test that invalid integer env values return None."""
self.assertIsNone(env.get_int_env_var("TEST_INT"))
def test_validate_date_range_invalid_date_format_raises(self):
"""Test that invalid date formats raise a ValueError."""
with self.assertRaises(ValueError) as cm:
env.validate_date_range("2024/01/01", "2024-02-01")
the_exception = cm.exception
self.assertEqual(
str(the_exception),
"start_date and end_date must be in the format YYYY-MM-DD",
)
if __name__ == "__main__":
unittest.main()