forked from release-engineering/Sync2Jira
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_main.py
More file actions
484 lines (420 loc) · 17.2 KB
/
Copy pathtest_main.py
File metadata and controls
484 lines (420 loc) · 17.2 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
import unittest
import unittest.mock as mock
from unittest.mock import MagicMock, patch
import requests
import sync2jira.jira_auth as jira_auth_module
from sync2jira.jira_auth import build_jira_client_kwargs
import sync2jira.main as m
PATH = "sync2jira.main."
class MockMessage(object):
def __init__(self, msg_id, body, topic):
self.id = msg_id
self.body = body
self.topic = topic
class TestMain(unittest.TestCase):
"""
This class tests the main.py file under sync2jira
"""
def setUp(self):
"""
Set up the testing environment
"""
# Mock Config dict
self.mock_config = {
"sync2jira": {
"jira": {"mock_jira_instance": {"mock_jira": "mock_jira"}},
"testing": {},
"legacy_matching": False,
"map": {"github": {"key_github": {"sync": ["issue", "pullrequest"]}}},
"initialize": True,
"listen": True,
"develop": False,
},
}
# Mock Fedmsg Message
self.mock_message_body = {"issue": "mock_issue"}
self.old_style_mock_message = MockMessage(
msg_id="mock_id",
body=self.mock_message_body,
topic=None,
)
self.new_style_mock_message = MockMessage(
msg_id="mock_id",
body={"body": self.mock_message_body},
topic=None,
)
def _check_for_exception(self, loader, target, exc=ValueError):
try:
m.load_config(loader)
assert False, "Exception expected."
except exc as e:
self.assertIn(target, repr(e))
def test_config_validate_empty(self):
loader = lambda: {}
self._check_for_exception(loader, "No sync2jira section")
def test_config_validate_missing_map(self):
loader = lambda: {"sync2jira": {}}
self._check_for_exception(loader, "No sync2jira.map section")
def test_config_validate_misspelled_mappings(self):
loader = lambda: {"sync2jira": {"map": {"githob": {}}}, "jira": {}}
self._check_for_exception(loader, 'Specified handlers: "githob", must')
def test_config_validate_missing_jira(self):
loader = lambda: {"sync2jira": {"map": {"github": {}}}}
self._check_for_exception(loader, "No sync2jira.jira section")
def test_config_validate_all_good(self):
loader = lambda: {"sync2jira": {"map": {"github": {}}, "jira": {}}}
m.load_config(loader) # Should succeed without an exception.
@mock.patch(PATH + "report_failure")
@mock.patch(PATH + "INITIALIZE", 1)
@mock.patch(PATH + "initialize_issues")
@mock.patch(PATH + "initialize_pr")
@mock.patch(PATH + "load_config")
@mock.patch(PATH + "listen")
def test_main_initialize(
self,
mock_listen,
mock_load_config,
mock_initialize_pr,
mock_initialize_issues,
mock_report_failure,
):
"""
This tests the 'main' function
"""
# Set up return values
mock_load_config.return_value = self.mock_config
# Call the function
m.main()
# Assert everything was called correctly
mock_load_config.assert_called_once()
mock_listen.assert_called_with(self.mock_config)
mock_listen.assert_called_with(self.mock_config)
mock_initialize_issues.assert_called_with(self.mock_config)
mock_initialize_pr.assert_called_with(self.mock_config)
mock_report_failure.assert_not_called()
@mock.patch(PATH + "report_failure")
@mock.patch(PATH + "INITIALIZE", 0)
@mock.patch(PATH + "initialize_issues")
@mock.patch(PATH + "initialize_pr")
@mock.patch(PATH + "load_config")
@mock.patch(PATH + "listen")
def test_main_no_initialize(
self,
mock_listen,
mock_load_config,
mock_initialize_pr,
mock_initialize_issues,
mock_report_failure,
):
"""
This tests the 'main' function
"""
# Set up return values
mock_load_config.return_value = self.mock_config
# Call the function
m.main()
# Assert everything was called correctly
mock_load_config.assert_called_once()
mock_listen.assert_called_with(self.mock_config)
mock_listen.assert_called_with(self.mock_config)
mock_initialize_issues.assert_not_called()
mock_initialize_pr.assert_not_called()
mock_report_failure.assert_not_called()
@mock.patch(PATH + "u_issue")
@mock.patch(PATH + "d_issue")
def test_initialize(self, mock_d, mock_u):
"""
This tests 'initialize' function where everything goes smoothly!
"""
# Set up return values
mock_u.github_issues.return_value = ["mock_issue_github"]
# Call the function
m.initialize_issues(self.mock_config)
# Assert everything was called correctly
mock_u.github_issues.assert_called_with("key_github", self.mock_config)
mock_d.sync_with_jira.assert_any_call("mock_issue_github", self.mock_config)
@mock.patch(PATH + "u_issue")
@mock.patch(PATH + "d_issue")
def test_initialize_repo_name_github(self, mock_d, mock_u):
"""
This tests 'initialize' function where we want to sync an individual repo for GitHub
"""
# Set up return values
mock_u.github_issues.return_value = ["mock_issue_github"]
# Call the function
m.initialize_issues(self.mock_config, repo_name="key_github")
# Assert everything was called correctly
mock_u.github_issues.assert_called_with("key_github", self.mock_config)
mock_d.sync_with_jira.assert_called_with("mock_issue_github", self.mock_config)
@mock.patch(PATH + "u_issue")
@mock.patch(PATH + "d_issue")
def test_initialize_errors(self, mock_d, mock_u):
"""
This tests 'initialize' function where syncing with JIRA throws an exception
"""
# Set up return values
mock_u.github_issues.return_value = ["mock_issue_github"]
mock_d.sync_with_jira.side_effect = Exception()
# Call the function
with self.assertRaises(Exception):
m.initialize_issues(self.mock_config)
# Assert everything was called correctly
mock_u.github_issues.assert_called_with("key_github", self.mock_config)
mock_d.sync_with_jira.assert_any_call("mock_issue_github", self.mock_config)
@mock.patch(PATH + "u_issue")
@mock.patch(PATH + "d_issue")
@mock.patch(PATH + "sleep")
@mock.patch(PATH + "report_failure")
def test_initialize_api_limit(
self, mock_report_failure, mock_sleep, mock_d, mock_u
):
"""
This tests 'initialize' where we get an GitHub API limit error.
"""
# Set up return values
mock_error = MagicMock(side_effect=Exception("API rate limit exceeded"))
mock_u.github_issues.side_effect = mock_error
# Call the function
m.initialize_issues(self.mock_config, testing=True)
# Assert everything was called correctly
mock_u.github_issues.assert_called_with("key_github", self.mock_config)
mock_d.sync_with_jira.assert_not_called()
mock_sleep.assert_called_with(3600)
mock_report_failure.assert_not_called()
@mock.patch(PATH + "u_issue")
@mock.patch(PATH + "d_issue")
@mock.patch(PATH + "sleep")
@mock.patch(PATH + "report_failure")
def test_initialize_github_error(
self, mock_report_failure, mock_sleep, mock_d, mock_u
):
"""
This tests 'initialize' where we get a GitHub API (not limit) error.
"""
# Set up return values
mock_error = MagicMock(side_effect=Exception("Random Error"))
mock_u.github_issues.side_effect = mock_error
# Call the function
with self.assertRaises(Exception):
m.initialize_issues(self.mock_config, testing=True)
# Assert everything was called correctly
mock_u.github_issues.assert_called_with("key_github", self.mock_config)
mock_d.sync_with_jira.assert_not_called()
mock_sleep.assert_not_called()
mock_report_failure.assert_called_with(self.mock_config)
@mock.patch(PATH + "handle_msg")
@mock.patch(PATH + "load_config")
def test_listen_no_handlers(self, mock_load_config, mock_handle_msg):
"""
Test 'listen' function where suffix is not in handlers
"""
# Set up return values
mock_load_config.return_value = self.mock_config
# Call the function
self.old_style_mock_message.topic = "d.d.d.github.issue.no_handlers_match_this"
m.callback(self.old_style_mock_message)
# Assert everything was called correctly
mock_handle_msg.assert_not_called()
@mock.patch.dict(
PATH + "issue_handlers", {"github.issue.comment": lambda msg, c: "dummy_issue"}
)
@mock.patch(PATH + "handle_msg")
@mock.patch(PATH + "load_config")
def test_listen(self, mock_load_config, mock_handle_msg):
"""
Test 'listen' function where everything goes smoothly
"""
# Set up return values
mock_load_config.return_value = self.mock_config
# Call the function once with the old style
self.old_style_mock_message.topic = "d.d.d.github.issue.comment"
m.callback(self.old_style_mock_message)
# ... and again with the new style
self.new_style_mock_message.topic = "d.d.d.github.issue.comment"
m.callback(self.new_style_mock_message)
# Assert everything was called correctly
# It should be called twice, once for the old style message and once for the new.
mock_handle_msg.assert_has_calls(
[
mock.call(
self.mock_message_body, "github.issue.comment", self.mock_config
),
mock.call(
self.mock_message_body, "github.issue.comment", self.mock_config
),
]
)
@mock.patch(PATH + "send_mail")
@mock.patch(PATH + "jinja2")
def test_report_failure(self, mock_jinja2, mock_send_mail):
"""
Tests 'report_failure' function
"""
# Set up return values
mock_template_loader = MagicMock()
mock_template_env = MagicMock()
mock_template = MagicMock()
mock_template.render.return_value = "mock_html"
mock_template_env.get_template.return_value = mock_template
mock_jinja2.FileSystemLoader.return_value = mock_template_loader
mock_jinja2.Environment.return_value = mock_template_env
# Call the function
m.report_failure({"sync2jira": {"mailing-list": "mock_email"}})
# Assert everything was called correctly
mock_send_mail.assert_called_with(
cc=None,
recipients=["mock_email"],
subject="Sync2Jira Has Failed!",
text="mock_html",
)
@mock.patch(PATH + "u_issue")
@mock.patch(PATH + "d_issue")
def test_handle_msg_no_handlers(self, mock_d, mock_u):
"""
Tests 'handle_msg' function where there are no handlers
"""
# Call the function
m.handle_msg(
body=self.mock_message_body, suffix="no_handler", config=self.mock_config
)
# Assert everything was called correctly
mock_d.sync_with_jira.assert_not_called()
mock_u.handle_github_message.assert_not_called()
@mock.patch.dict(
PATH + "issue_handlers", {"github.issue.comment": lambda msg, c: None}
)
@mock.patch(PATH + "u_issue")
@mock.patch(PATH + "d_issue")
def test_handle_msg_no_issue(self, mock_d, mock_u):
"""
Tests 'handle_msg' function where there is no issue
"""
# Call the function
m.handle_msg(
body=self.mock_message_body,
suffix="github.issue.comment",
config=self.mock_config,
)
# Assert everything was called correctly
mock_d.sync_with_jira.assert_not_called()
mock_u.handle_github_message.assert_not_called()
@mock.patch.dict(
PATH + "issue_handlers", {"github.issue.comment": lambda msg, c: "dummy_issue"}
)
@mock.patch(PATH + "u_issue")
@mock.patch(PATH + "d_issue")
def test_handle_msg(self, mock_d, mock_u):
"""
Tests 'handle_msg' function
"""
# Set up return values
mock_u.handle_github_message.return_value = "dummy_issue"
# Call the function
m.handle_msg(
body=self.mock_message_body,
suffix="github.issue.comment",
config=self.mock_config,
)
# Assert everything was called correctly
mock_d.sync_with_jira.assert_called_with("dummy_issue", self.mock_config)
class TestJiraAuth(unittest.TestCase):
"""Tests for Jira auth: PAT and OAuth2 (build_jira_client_kwargs)."""
def setUp(self):
"""Clear OAuth2 token cache so tests don't reuse tokens from other tests."""
jira_auth_module._oauth2_token_cache.clear()
def test_jira_auth_pat_with_basic_auth(self):
"""PAT with basic_auth succeeds."""
config = {
"options": {"server": "https://jira.example.com", "verify": True},
"basic_auth": ("user", "pass"),
}
kwargs = build_jira_client_kwargs(config)
self.assertEqual(kwargs["basic_auth"], ("user", "pass"))
self.assertEqual(kwargs["options"], config["options"])
def test_jira_auth_pat_missing_basic_auth(self):
"""PAT without basic_auth raises ValueError."""
config = {
"options": {"server": "https://jira.example.com"},
}
with self.assertRaises(ValueError) as ctx:
build_jira_client_kwargs(config)
self.assertIn("basic_auth", str(ctx.exception))
@patch("sync2jira.jira_auth.requests.post")
def test_jira_auth_oauth2_cache(self, mock_post):
"""OAuth2 second call reuses cached token (no second request)."""
mock_post.return_value = MagicMock(
status_code=200,
json=lambda: {
"access_token": "cached_token",
"expires_in": 3600,
},
raise_for_status=MagicMock(),
)
config = {
"options": {"server": "https://site.atlassian.net"},
"auth_method": "oauth2",
"oauth2": {"client_id": "cid", "client_secret": "csecret"},
}
kwargs1 = build_jira_client_kwargs(config)
kwargs2 = build_jira_client_kwargs(config)
self.assertEqual(kwargs1["token_auth"], "cached_token")
self.assertEqual(kwargs2["token_auth"], "cached_token")
mock_post.assert_called_once()
@patch("sync2jira.jira_auth.time.time")
@patch("sync2jira.jira_auth.requests.post")
def test_jira_auth_oauth2_refresh(self, mock_post, mock_time):
"""OAuth2 expired token triggers new token fetch; second token is used."""
mock_time.return_value = 1000.0
# Return different tokens per call so we can verify the second call's result is used
def make_response(access_token, expires_in=60):
return MagicMock(
status_code=200,
json=lambda t=access_token, e=expires_in: {
"access_token": t,
"expires_in": e,
},
raise_for_status=MagicMock(),
)
mock_post.side_effect = [
make_response("first_token"),
make_response("refreshed_token"),
]
config = {
"options": {"server": "https://site.atlassian.net"},
"auth_method": "oauth2",
"oauth2": {"client_id": "cid", "client_secret": "csecret"},
}
# First call populates cache (expires at 1000 + 60 = 1060)
build_jira_client_kwargs(config)
# Advance time past expiry (e.g. 2000)
mock_time.return_value = 2000.0
kwargs = build_jira_client_kwargs(config)
self.assertEqual(kwargs["token_auth"], "refreshed_token")
self.assertEqual(mock_post.call_count, 2)
def test_jira_auth_oauth2_missing_credentials(self):
"""OAuth2 missing client_id or client_secret raises ValueError."""
base = {
"options": {"server": "https://site.atlassian.net"},
"auth_method": "oauth2",
}
for oauth2_cfg in [
{},
{"client_id": "cid"},
{"client_secret": "csecret"},
]:
config = base | {"oauth2": oauth2_cfg}
with self.assertRaises(ValueError) as ctx:
build_jira_client_kwargs(config)
self.assertIn("client_id and oauth2.client_secret", str(ctx.exception))
@patch("sync2jira.jira_auth.requests.post")
def test_jira_auth_oauth2_request_failure(self, mock_post):
"""OAuth2 token request failure propagates requests.RequestException."""
mock_post.side_effect = requests.RequestException("network error")
config = {
"options": {"server": "https://site.atlassian.net"},
"auth_method": "oauth2",
"oauth2": {"client_id": "cid", "client_secret": "csecret"},
}
with self.assertRaises(requests.RequestException) as ctx:
build_jira_client_kwargs(config)
self.assertIn("network error", str(ctx.exception))