-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtest_downstream_issue.py
More file actions
1660 lines (1483 loc) · 63.3 KB
/
test_downstream_issue.py
File metadata and controls
1660 lines (1483 loc) · 63.3 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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from datetime import datetime, timezone
from typing import Any, Optional
import unittest
import unittest.mock as mock
from unittest.mock import MagicMock
from jira import JIRAError
import jira.client
import sync2jira.downstream_issue as d
from sync2jira.downstream_issue import remove_diacritics
from sync2jira.intermediary import Issue
PATH = "sync2jira.downstream_issue."
class TestDownstreamIssue(unittest.TestCase):
"""
This class tests the downstream_issue.py file under sync2jira
"""
def setUp(self):
"""
Setting up the testing environment
"""
# Mock Config dict
self.mock_config = {
"sync2jira": {
"default_jira_instance": "another_jira_instance",
"jira_username": "mock_user",
"default_jira_fields": {"storypoints": "customfield_12310243"},
"jira": {
"mock_jira_instance": {"mock_jira": "mock_jira"},
"another_jira_instance": {
"token_auth": "mock_token",
"options": {"server": "mock_server"},
},
},
"testing": {},
"legacy_matching": False,
"admins": [{"mock_admin": "mock_email"}],
"develop": False,
},
}
# Mock sync2jira.intermediary.Issue
self.mock_issue = MagicMock()
self.mock_issue.assignee = [
{"fullname": "mock_user", "login": "mock_user_login"}
]
self.mock_issue.downstream = {
"project": "mock_project",
"custom_fields": {"somecustumfield": "somecustumvalue"},
"qa-contact": "dummy@dummy.com",
"epic-link": "DUMMY-1234",
"EXD-Service": {"guild": "EXD-Project", "value": "EXD-Value"},
"issue_updates": [
"comments",
{"tags": {"overwrite": False}},
{"fixVersion": {"overwrite": False}},
{"assignee": {"overwrite": True}},
"description",
"title",
{"transition": "CUSTOM TRANSITION"},
{"on_close": {"apply_labels": ["closed-upstream"]}},
],
"owner": "mock_owner",
}
self.mock_issue.content = "mock_content"
self.mock_issue.reporter = {"fullname": "mock_user"}
self.mock_issue.url = "mock_url"
self.mock_issue.title = "mock_title"
self.mock_issue.comments = "mock_comments"
self.mock_issue.tags = ["tag1", "tag2"]
self.mock_issue.fixVersion = ["fixVersion3", "fixVersion4"]
self.mock_issue.fixVersion = ["fixVersion3", "fixVersion4"]
self.mock_issue.assignee = [
{"fullname": "mock_assignee", "login": "mock_assignee_login"}
]
self.mock_issue.status = "Open"
self.mock_issue.id = "1234"
self.mock_issue.storypoints = 2
self.mock_issue.priority = "P1"
self.mock_issue.issue_type = None
# Mock issue updates
self.mock_updates = [
"comments",
{"tags": {"overwrite": False}},
{"fixVersion": {"overwrite": False}},
{"assignee": {"overwrite": True}},
"description",
"title",
{"transition": "CUSTOM TRANSITION"},
{"on_close": {"apply_labels": ["closed-upstream"]}},
]
# Mock Jira transition
self.mock_transition = [{"name": "custom_closed_status", "id": 1234}]
# Mock jira.resources.Issue
self.mock_downstream = MagicMock()
self.mock_downstream.id = 1234
self.mock_downstream.fields.labels = ["tag3", "tag4"]
self.mock_downstream.key = "downstream_issue_key"
mock_version1 = MagicMock()
mock_version1.name = "fixVersion3"
mock_version2 = MagicMock()
mock_version2.name = "fixVersion4"
self.mock_downstream.fields.fixVersions = [mock_version1, mock_version2]
self.mock_downstream.update.return_value = True
self.mock_downstream.fields.description = "This is an existing description"
# Mock datetime.today()
self.mock_today = MagicMock()
self.mock_today.strftime.return_value = "mock_today"
@mock.patch("jira.client.JIRA")
def test_get_jira_client_not_issue(self, mock_client):
"""
This tests 'get_jira_client' function where the passed in
argument is not an Issue instance
"""
# Call the function
with self.assertRaises(Exception):
d.get_jira_client(issue="string", config=self.mock_config)
# Assert everything was called correctly
mock_client.assert_not_called()
@mock.patch("jira.client.JIRA")
def test_get_jira_client_not_instance(self, mock_client):
"""
This tests 'get_jira_client' function there is no JIRA instance
"""
# Set up return values
self.mock_issue.downstream = {}
# Call the function
with self.assertRaises(Exception):
d.get_jira_client(issue=self.mock_issue, config=self.mock_config)
# Assert everything was called correctly
mock_client.assert_not_called()
@mock.patch("jira.client.JIRA")
def test_get_jira_client(self, mock_client):
"""
This tests 'get_jira_client' function where everything goes smoothly
"""
# Set up return values
mock_issue = MagicMock(spec=Issue)
mock_issue.downstream = {"jira_instance": "mock_jira_instance"}
mock_client.return_value = "Successful call!"
# Call the function
response = d.get_jira_client(issue=mock_issue, config=self.mock_config)
# Assert everything was called correctly
mock_client.assert_called_with(mock_jira="mock_jira")
self.assertEqual("Successful call!", response)
@mock.patch("jira.client.JIRA")
def test_get_existing_legacy(self, client):
"""
This tests '_get_existing_jira_issue_legacy' function
"""
class MockIssue(object):
downstream = {"key": "value"}
url = "wat"
issue = MockIssue()
# Ensure that we get results back from the jira client.
target1 = "target1"
client.return_value.search_issues = mock.MagicMock(return_value=[target1])
result = d._get_existing_jira_issue_legacy(jira.client.JIRA(), issue)
assert result == target1
client.return_value.search_issues.assert_called_once_with(
"'External issue URL'='wat' AND 'key'='value' AND "
"(resolution is null OR resolution = Duplicate)",
)
@mock.patch("jira.client.JIRA")
def test_get_existing_newstyle(self, client):
config = self.mock_config
issue = MagicMock()
issue.downstream = {"key": "value"}
issue.title = "A title, a title..."
issue.url = "http://threebean.org"
mock_results_of_query = MagicMock()
mock_results_of_query.fields.summary = "A title, a title..."
client.return_value.search_issues.return_value = [mock_results_of_query]
result = d._get_existing_jira_issue(jira.client.JIRA(), issue, config)
# Ensure that we get the mock_result_of_query as a result
self.assertEqual(result, mock_results_of_query)
client.return_value.search_issues.assert_called_once_with(
'issueFunction in linkedIssuesOfRemote("Upstream issue") and '
'issueFunction in linkedIssuesOfRemote("http://threebean.org")'
)
@mock.patch("jira.client.JIRA")
def test_upgrade_oldstyle_jira_issue(self, client):
config = self.mock_config
class MockIssue(object):
downstream = {"key": "value"}
title = "A title, a title..."
url = "http://threebean.org"
downstream = mock.MagicMock()
issue = MockIssue()
client_obj = mock.MagicMock()
client.return_value = client_obj
d._upgrade_jira_issue(jira.client.JIRA(), downstream, issue, config)
remote = {
"url": "http://threebean.org",
"title": "Upstream issue",
}
client_obj.add_remote_link.assert_called_once_with(downstream.id, remote)
@mock.patch("Rover_Lookup.github_username_to_emails")
@mock.patch("jira.client.JIRA")
def test_assign_user(self, mock_client, mock_rover_lookup):
"""
Test `assign_user()` when the downstream user matches the upstream user.
"""
# Set up return values
mock_user = MagicMock()
mock_user.displayName = "mock_assignee"
mock_user.key = "mock_user_key"
mock_client.search_assignable_users_for_issues.return_value = [mock_user]
mock_client.assign_issue.return_value = True
mock_rover_lookup.return_value = ["mock_user@redhat.com"]
# Call the assign user function
d.assign_user(
issue=self.mock_issue, downstream=self.mock_downstream, client=mock_client
)
# Assert that all calls mocked were called properly
self.mock_downstream.update.assert_called_with(
{"assignee": {"name": mock_user.name}}
)
mock_client.search_assignable_users_for_issues.assert_called_with(
query="mock_user@redhat.com", issueKey=self.mock_downstream.key
)
@mock.patch("Rover_Lookup.github_username_to_emails")
@mock.patch("jira.client.JIRA")
def test_assign_user_diacritics(self, mock_client, mock_rover_lookup):
"""
Test `assign_user()` when the downstream user matches the upstream user
only when the diacritic characters are replaced.
"""
# Set up return values
mock_user = MagicMock()
mock_user.displayName = "mock_assignee"
mock_user.key = "mock_user_key"
mock_client.search_assignable_users_for_issues.return_value = [mock_user]
mock_client.assign_issue.return_value = True
mock_rover_lookup.return_value = ["mock_user@redhat.com"]
self.mock_issue.assignee = [
{"fullname": "ḿòćḱ_ášśìǵńèé", "login": "mock_user_diacritics"}
]
# Call the assign user function
d.assign_user(
issue=self.mock_issue, downstream=self.mock_downstream, client=mock_client
)
# Assert that all calls mocked were called properly
self.mock_downstream.update.assert_called_with(
{"assignee": {"name": mock_user.name}}
)
mock_client.search_assignable_users_for_issues.assert_called_with(
query="mock_user@redhat.com", issueKey=self.mock_downstream.key
)
@mock.patch("Rover_Lookup.github_username_to_emails")
@mock.patch("jira.client.JIRA")
def test_assign_user_multiple(self, mock_client, mock_rover_lookup):
"""
Test `assign_user()` when the upstream assignee field contains a list
in which most entries aren't useful.
"""
# Set up return values
mock_user = MagicMock()
mock_user.displayName = "mock_assignee"
mock_user.name = "mock_assignee_name"
mock_user.emailAddress = "mock_user@redhat.com"
mock_user.key = "mock_user_key"
mock_user2 = MagicMock()
mock_user2.displayName = "mock_assignee2"
mock_user2.name = "mock_assignee2_name"
mock_user2.emailAddress = "wrong_mock_user@redhat.com"
mock_user2.key = "mock_user2_key"
mock_client.search_assignable_users_for_issues.return_value = [
mock_user,
mock_user2,
]
mock_client.assign_issue.return_value = True
self.mock_issue.assignee = [
{"fullname": None, "login": "login1"},
{"fullname": "", "login": "login2"},
{"fullname": "not_a_match", "login": "login3"},
{"fullname": "ḿòćḱ_ášśìǵńèé", "login": "login4"},
# Should not match this next -- should match the previous.
{"fullname": "mock_assignee2", "login": "login5"},
]
rlu = {
"login1": [],
"login2": [],
"login3": ["not_a_match@redhat.com"],
"login4": [mock_user.emailAddress],
"login5": [mock_user2.emailAddress],
}
mock_rover_lookup.side_effect = lambda un: rlu.get(
un, AssertionError("Test bug! Missing assignee login")
)
# Call the assign user function
d.assign_user(
issue=self.mock_issue, downstream=self.mock_downstream, client=mock_client
)
# Assert that all calls mocked were called properly
self.mock_downstream.update.assert_called_with(
{"assignee": {"name": mock_user.name}}
)
mock_client.search_assignable_users_for_issues.assert_called_with(
query="mock_user@redhat.com", issueKey=self.mock_downstream.key
)
@mock.patch("Rover_Lookup.github_username_to_emails")
@mock.patch("jira.client.JIRA")
def test_assign_user_with_owner_no_upstream(self, mock_client, mock_rover_lookup):
"""
Test `assign_user()` to show that, when no downstream user is
available, the issue is assigned to the configured owner.
"""
# Set up return values
mock_user = MagicMock()
mock_user.displayName = "mock_assignee"
mock_user.key = "mock_user_key"
mock_client.search_assignable_users_for_issues.return_value = []
mock_client.assign_issue.return_value = True
mock_rover_lookup.return_value = []
# Call the assign user function
d.assign_user(
issue=self.mock_issue, downstream=self.mock_downstream, client=mock_client
)
# Assert that all calls mocked were called properly
mock_client.assign_issue.assert_called_with(1234, "mock_owner")
mock_client.search_assignable_users_for_issues.assert_not_called()
@mock.patch("Rover_Lookup.github_username_to_emails")
@mock.patch("jira.client.JIRA")
def test_assign_user_with_owner_no_match(self, mock_client, mock_rover_lookup):
"""
Test `assign_user()` to show that, when no downstream user is
available, the issue is assigned to the configured owner.
"""
# Set up return values
mock_user = MagicMock()
mock_user.displayName = "mock_assignee"
mock_user.key = "mock_user_key"
mock_client.search_assignable_users_for_issues.return_value = []
mock_client.assign_issue.return_value = True
mock_rover_lookup.return_value = ["no_match@redhat.com"]
# Call the assign user function
d.assign_user(
issue=self.mock_issue, downstream=self.mock_downstream, client=mock_client
)
# Assert that all calls mocked were called properly
mock_client.assign_issue.assert_called_with(1234, "mock_owner")
mock_client.search_assignable_users_for_issues.assert_called_with(
query="no_match@redhat.com", issueKey=self.mock_downstream.key
)
@mock.patch("Rover_Lookup.github_username_to_emails")
@mock.patch("jira.client.JIRA")
def test_assign_user_without_owner(self, mock_client, mock_rover_lookup):
"""
Test `assign_user()` when no downstream user is available and there is
no configured owner for the project.
"""
# Set up return values
mock_user = MagicMock()
mock_user.displayName = "mock_assignee"
mock_user.key = "mock_user_key"
mock_client.search_assignable_users_for_issues.return_value = []
mock_client.assign_issue.return_value = True
mock_rover_lookup.return_value = []
self.mock_issue.downstream.pop("owner")
# Call the assign user function
d.assign_user(
issue=self.mock_issue, downstream=self.mock_downstream, client=mock_client
)
# Assert that all calls mocked were called properly
mock_client.assign_issue.assert_not_called()
mock_client.search_assignable_users_for_issues.assert_not_called()
@mock.patch(PATH + "match_user")
@mock.patch("jira.client.JIRA")
def test_assign_user_none(self, mock_client, mock_match_user):
"""
Test `assign_user()` when no upstream user is available and there is
no configured owner for the project.
"""
# Set up return values
self.mock_issue.assignee = []
self.mock_issue.downstream.pop("owner")
# Call the assign user function
d.assign_user(
issue=self.mock_issue, downstream=self.mock_downstream, client=mock_client
)
# Assert that all calls mocked were called properly
mock_match_user.assert_not_called()
mock_client.search_assignable_users_for_issues.assert_not_called()
self.mock_downstream.update.assert_not_called()
mock_client.assign_issue.assert_not_called()
@mock.patch("jira.client.JIRA")
def test_assign_user_remove_all(self, mock_client):
"""
Test 'assign_user' function when the `remove_all` flag is True
"""
# Call the assign user function
d.assign_user(
issue=self.mock_issue,
downstream=self.mock_downstream,
client=mock_client,
remove_all=True,
)
# Assert that all calls mocked were called properly
self.mock_downstream.update.assert_called_with(assignee={"name": ""})
mock_client.assign_issue.assert_not_called()
mock_client.search_assignable_users_for_issues.assert_not_called()
def common_test_create_jira_issue(
self, mock_attach_link, mock_client, mock_update_jira_issue
):
"""Common code for testing _create_jira_issue"""
# Set up return values
mock_client.create_issue.return_value = self.mock_downstream
mock_client.fields.return_value = [
{"name": "Epic Link", "id": "customfield_1"},
{"name": "QA Contact", "id": "customfield_2"},
{"name": "EXD-Service", "id": "customfield_3"},
]
# Call the function
response = d._create_jira_issue(
client=mock_client, issue=self.mock_issue, config=self.mock_config
)
# Assert everything was called correctly
mock_client.create_issue.assert_called_with(
issuetype={"name": "Bug"},
project={"key": "mock_project"},
somecustumfield="somecustumvalue",
description=(
"[1234] Upstream Reporter: mock_user\n"
"Upstream issue status: Open\n"
"Upstream description: {quote}mock_content{quote}"
),
summary="mock_title",
)
mock_attach_link.assert_called_with(
mock_client,
self.mock_downstream,
{"url": "mock_url", "title": "Upstream issue"},
)
mock_update_jira_issue.assert_called_with(
self.mock_downstream, self.mock_issue, mock_client, self.mock_config
)
self.mock_downstream.update.assert_any_call({"customfield_1": "DUMMY-1234"})
self.mock_downstream.update.assert_any_call(
{"customfield_2": "dummy@dummy.com"}
)
self.mock_downstream.update.assert_any_call(
{"customfield_3": {"value": "EXD-Project", "child": {"value": "EXD-Value"}}}
)
self.assertEqual(response, self.mock_downstream)
@mock.patch(PATH + "_update_jira_issue")
@mock.patch(PATH + "attach_link")
@mock.patch("jira.client.JIRA")
def test_create_jira_issue(
self, mock_client, mock_attach_link, mock_update_jira_issue
):
"""
Tests '_create_jira_issue' function normal success case
"""
self.common_test_create_jira_issue(
mock_attach_link, mock_client, mock_update_jira_issue
)
mock_client.add_comment.assert_not_called()
@mock.patch(PATH + "_update_jira_issue")
@mock.patch(PATH + "attach_link")
@mock.patch("jira.client.JIRA")
def test_create_jira_issue_failed_epic_link(
self, mock_client, mock_attach_link, mock_update_jira_issue
):
"""
Tests '_create_jira_issue' function when we fail while updating the epic link
"""
# Set up return values
self.mock_downstream.update.side_effect = [JIRAError, "success", "success"]
self.common_test_create_jira_issue(
mock_attach_link, mock_client, mock_update_jira_issue
)
mock_client.add_comment.assert_called_with(
self.mock_downstream, f"Error adding Epic-Link: DUMMY-1234"
)
@mock.patch(PATH + "_update_jira_issue")
@mock.patch(PATH + "attach_link")
@mock.patch("jira.client.JIRA")
def test_create_jira_issue_failed_exd_service(
self, mock_client, mock_attach_link, mock_update_jira_issue
):
"""
Tests '_create_jira_issue' function when we fail while updating the
EXD-Service field
"""
# Set up return values
self.mock_downstream.update.side_effect = ["success", "success", JIRAError]
self.common_test_create_jira_issue(
mock_attach_link, mock_client, mock_update_jira_issue
)
mock_client.add_comment.assert_called_with(
self.mock_downstream,
f"Error adding EXD-Service field.\n"
f"Project: {self.mock_issue.downstream['EXD-Service']['guild']}\n"
f"Value: {self.mock_issue.downstream['EXD-Service']['value']}",
)
@mock.patch(PATH + "_update_jira_issue")
@mock.patch(PATH + "attach_link")
@mock.patch(PATH + "_get_preferred_issue_types")
@mock.patch("jira.client.JIRA")
def test_create_jira_issue_multiple_types(
self,
mock_client,
mock_get_preferred_issue_types,
mock_attach_link,
mock_update_jira_issue,
):
"""
Tests '_create_jira_issue' function when multiple possible issue types are found
"""
# Set up return values
issue_types = ["Bug", "Feature", "Outcome", "Story"]
mock_get_preferred_issue_types.return_value = issue_types
self.common_test_create_jira_issue(
mock_attach_link, mock_client, mock_update_jira_issue
)
mock_client.add_comment.assert_called_with(
self.mock_downstream,
f"Some labels look like issue types but were not considered: {issue_types[1:]}",
)
@mock.patch(PATH + "_update_jira_issue")
@mock.patch(PATH + "attach_link")
@mock.patch("jira.client.JIRA")
def test_create_jira_issue_no_updates(
self, mock_client, mock_attach_link, mock_update_jira_issue
):
"""
Tests '_create_jira_issue' function where we have
no updates
"""
# Set up return values
mock_client.create_issue.return_value = self.mock_downstream
self.mock_issue.downstream["issue_updates"] = []
# Call the function
response = d._create_jira_issue(
client=mock_client, issue=self.mock_issue, config=self.mock_config
)
# Assert everything was called correctly
mock_client.create_issue.assert_called_with(
issuetype={"name": "Bug"},
project={"key": "mock_project"},
somecustumfield="somecustumvalue",
description="[1234] Upstream Reporter: mock_user\n",
summary="mock_title",
)
mock_attach_link.assert_called_with(
mock_client,
self.mock_downstream,
{"url": "mock_url", "title": "Upstream issue"},
)
mock_update_jira_issue.assert_called_with(
self.mock_downstream, self.mock_issue, mock_client, self.mock_config
)
self.assertEqual(response, self.mock_downstream)
mock_client.add_comment.assert_not_called()
def test_get_preferred_issue_types(self):
"""
Tests '_get_preferred_issue_types' function
Scenarios:
- configuration has type mappings
- first mapping matches
- second mapping matches
- multiple mappings match
- no mapping matches
- configuration has a default type
- upstream issue has a type
- "RFE" in issue title
- None of the above.
"""
conf = {
"issue_types": {
"tag1": "mapped_type_C", # In reverse-sorted order to test sorting
"tag2": "mapped_type_B",
"tag3": "mapped_type_A",
},
"type": "S2J_type",
}
self.mock_config["sync2jira"]["map"] = {
"github": {self.mock_issue.upstream: conf}
}
self.mock_issue.issue_type = "GH_type"
self.mock_issue.tags = ["tag1"]
self.mock_issue.title = "RFE: Mock Issue Title"
def update_state(
issue_field: Optional[dict[str, Any]] = None, config: Optional[str] = None
):
if issue_field:
for k, v in issue_field.items():
self.mock_issue.__setattr__(k, v)
if config:
del conf[config]
# List of scenarios: each entry includes a callable which modifies
# either the mock issue or the mock configuration prior to invoking the
# CUT and the resulting value which is expected to be returned; the
# test iterates over the list, calling each setup function, calling the
# CUT, and then comparing the result to the expected value.
scenarios = (
# The issue label matches the first in the configured type map.
(lambda: None, ["mapped_type_C"]),
# The issue label matches the second in the configured type map.
(
lambda: update_state(issue_field={"tags": ["tag2"]}),
["mapped_type_B"],
),
# The issue label has multiple matches in the configured type map.
(
lambda: update_state(issue_field={"tags": ["tag2", "tag1"]}),
["mapped_type_B", "mapped_type_C"],
),
# The issue label has no matches in the configured type map.
(lambda: update_state(issue_field={"tags": ["fred"]}), ["S2J_type"]),
# There is no type map, but the configuration specifies a default type.
(lambda: update_state(config="issue_types"), ["S2J_type"]),
# No type in the configuration, but the upstream issue has a type.
(lambda: update_state(config="type"), ["GH_type"]),
# No type from config or upstream, but there is "RFE" in issue title.
(lambda: update_state(issue_field={"issue_type": None}), ["Story"]),
# Default fallback
(
lambda: update_state(issue_field={"title": "Plain Issue Title"}),
["Bug"],
),
)
for scenario, (setup_action, expected) in enumerate(scenarios):
setup_action()
actual = d._get_preferred_issue_types(self.mock_config, self.mock_issue)
self.assertEqual(actual, expected, f"In scenario {scenario}")
@mock.patch(PATH + "get_jira_client")
@mock.patch(PATH + "_get_existing_jira_issue")
@mock.patch(PATH + "_update_jira_issue")
@mock.patch(PATH + "_create_jira_issue")
@mock.patch("jira.client.JIRA")
@mock.patch(PATH + "_get_existing_jira_issue_legacy")
@mock.patch(PATH + "check_jira_status")
def test_sync_with_jira_matching(
self,
mock_check_jira_status,
mock_existing_jira_issue_legacy,
mock_client,
mock_create_jira_issue,
mock_update_jira_issue,
mock_existing_jira_issue,
mock_get_jira_client,
):
"""
Tests 'sync_with_jira' function where we do find a matching issue
This assumes we're not using the legacy matching anymore
"""
# Set up return values
mock_get_jira_client.return_value = mock_client
mock_existing_jira_issue.return_value = self.mock_downstream
mock_check_jira_status.return_value = True
# Call the function
d.sync_with_jira(issue=self.mock_issue, config=self.mock_config)
# Assert all calls were made correctly
mock_get_jira_client.assert_called_with(self.mock_issue, self.mock_config)
mock_update_jira_issue.assert_called_with(
self.mock_downstream, self.mock_issue, mock_client, self.mock_config
)
mock_create_jira_issue.assert_not_called()
mock_existing_jira_issue_legacy.assert_not_called()
@mock.patch(PATH + "get_jira_client")
@mock.patch(PATH + "_get_existing_jira_issue")
@mock.patch(PATH + "_update_jira_issue")
@mock.patch(PATH + "_create_jira_issue")
@mock.patch("jira.client.JIRA")
@mock.patch(PATH + "_get_existing_jira_issue_legacy")
@mock.patch(PATH + "check_jira_status")
def test_sync_with_jira_down(
self,
mock_check_jira_status,
mock_existing_jira_issue_legacy,
mock_client,
mock_create_jira_issue,
mock_update_jira_issue,
mock_existing_jira_issue,
mock_get_jira_client,
):
"""
Tests 'sync_with_jira' function where the JIRA scriptrunner is down
"""
# Set up return values
mock_get_jira_client.return_value = mock_client
mock_existing_jira_issue.return_value = self.mock_downstream
mock_check_jira_status.return_value = False
# Call the function
with self.assertRaises(RuntimeError):
d.sync_with_jira(issue=self.mock_issue, config=self.mock_config)
# Assert all calls were made correctly
mock_get_jira_client.assert_called_with(self.mock_issue, self.mock_config)
mock_update_jira_issue.assert_not_called()
mock_create_jira_issue.assert_not_called()
mock_existing_jira_issue_legacy.assert_not_called()
@mock.patch(PATH + "get_jira_client")
@mock.patch(PATH + "_get_existing_jira_issue")
@mock.patch(PATH + "_update_jira_issue")
@mock.patch(PATH + "_create_jira_issue")
@mock.patch("jira.client.JIRA")
@mock.patch(PATH + "_get_existing_jira_issue_legacy")
@mock.patch(PATH + "check_jira_status")
def test_sync_with_jira_no_matching(
self,
mock_check_jira_status,
mock_existing_jira_issue_legacy,
mock_client,
mock_create_jira_issue,
mock_update_jira_issue,
mock_existing_jira_issue,
mock_get_jira_client,
):
"""
Tests 'sync_with_jira' function where we do NOT find a matching issue
This assumes we're not using the legacy matching anymore
"""
# Set up return values
mock_get_jira_client.return_value = mock_client
mock_existing_jira_issue.return_value = None
mock_check_jira_status.return_value = True
# Call the function
d.sync_with_jira(issue=self.mock_issue, config=self.mock_config)
# Assert all calls were made correctly
mock_get_jira_client.assert_called_with(self.mock_issue, self.mock_config)
mock_update_jira_issue.assert_not_called()
mock_create_jira_issue.assert_called_with(
mock_client, self.mock_issue, self.mock_config
)
mock_existing_jira_issue_legacy.assert_not_called()
@mock.patch(PATH + "_update_title")
@mock.patch(PATH + "_update_description")
@mock.patch(PATH + "_update_comments")
@mock.patch(PATH + "_update_tags")
@mock.patch(PATH + "_update_fixVersion")
@mock.patch(PATH + "_update_transition")
@mock.patch(PATH + "_update_assignee")
@mock.patch(PATH + "_update_on_close")
@mock.patch("jira.client.JIRA")
def test_update_jira_issue(
self,
mock_client,
mock_update_on_close,
mock_update_assignee,
mock_update_transition,
mock_update_fixVersion,
mock_update_tags,
mock_update_comments,
mock_update_description,
mock_update_title,
):
"""
This tests '_update_jira_issue' function
"""
# Call the function
d._update_jira_issue(
existing=self.mock_downstream,
issue=self.mock_issue,
client=mock_client,
config=self.mock_config,
)
# Assert all calls were made correctly
mock_update_comments.assert_called_with(
mock_client, self.mock_downstream, self.mock_issue
)
mock_update_tags.assert_called_with(
self.mock_updates, self.mock_downstream, self.mock_issue
)
mock_update_fixVersion.assert_called_with(
self.mock_updates,
self.mock_downstream,
self.mock_issue,
mock_client,
)
mock_update_description.assert_called_with(
self.mock_downstream, self.mock_issue
)
mock_update_title.assert_called_with(self.mock_issue, self.mock_downstream)
mock_update_transition.assert_called_with(
mock_client, self.mock_downstream, self.mock_issue
)
mock_update_on_close.assert_called_once()
@mock.patch("jira.client.JIRA")
def test_update_transition_JIRAError(self, mock_client):
"""
This function tests the '_update_transition' function where Upstream issue status
s not in existing.fields.description and transitioning the issue throws an error
"""
# Set up return values
self.mock_issue.status = "Closed"
self.mock_downstream.fields.description = ""
mock_client.transitions.return_value = [
{"name": "CUSTOM TRANSITION", "id": "1234"}
]
mock_client.transition_issue.side_effect = JIRAError
# Call the function
d._update_transition(
client=mock_client, existing=self.mock_downstream, issue=self.mock_issue
)
# Assert all calls were made correctly
mock_client.transitions.assert_called_with(self.mock_downstream)
mock_client.transition_issue.assert_called_with(self.mock_downstream, 1234)
@mock.patch("jira.client.JIRA")
def test_update_transition_not_found(self, mock_client):
"""
This function tests the '_update_transition' function when the Upstream
issue status is not in the existing.fields.description value and we
can't find the appropriate closed status
"""
# Set up return values
self.mock_issue.status = "Closed"
self.mock_issue.downstream["transition"] = "bad_transition"
self.mock_downstream.fields.description = ""
mock_client.transitions.return_value = [
{"name": "CUSTOM TRANSITION", "id": "1234"}
]
# Call the function
d._update_transition(
client=mock_client, existing=self.mock_downstream, issue=self.mock_issue
)
# Assert all calls were made correctly
mock_client.transitions.assert_called_with(self.mock_downstream)
mock_client.transition_issue.assert_called_with(self.mock_downstream, 1234)
@mock.patch("jira.client.JIRA")
def test_update_transition_successful(self, mock_client):
"""
This function tests the '_update_transition' function where everything goes smoothly!
"""
# Set up return values
self.mock_issue.status = "Closed"
self.mock_downstream.fields.description = "[test] Upstream issue status: Open"
mock_client.transitions.return_value = [
{"name": "CUSTOM TRANSITION", "id": "1234"}
]
# Call the function
d._update_transition(
client=mock_client, existing=self.mock_downstream, issue=self.mock_issue
)
# Assert all calls were made correctly
mock_client.transitions.assert_called_with(self.mock_downstream)
mock_client.transition_issue.assert_called_with(self.mock_downstream, 1234)
@mock.patch(PATH + "_comment_format")
@mock.patch(PATH + "_comment_matching")
@mock.patch("jira.client.JIRA")
def test_update_comments(
self, mock_client, mock_comment_matching, mock_comment_format
):
"""
This function tests the 'update_comments' function
"""
# Set up return values
mock_client.comments.return_value = "mock_comments"
mock_comment_matching.return_value = ["mock_comments_d"]
mock_comment_format.return_value = "mock_comment_body"
# Call the function
d._update_comments(
client=mock_client, existing=self.mock_downstream, issue=self.mock_issue
)
# Assert all calls were made correctly
mock_client.comments.assert_called_with(self.mock_downstream)
mock_comment_matching.assert_called_with(
self.mock_issue.comments, "mock_comments"
)
mock_comment_format.assert_called_with("mock_comments_d")
mock_client.add_comment.assert_called_with(
self.mock_downstream, "mock_comment_body"
)
def test_update_fixVersion_JIRAError(self):
"""
This function tests the 'update_fixVersion' function where updating the downstream
issue throws an error
"""
# Set up return values
self.mock_downstream.update.side_effect = JIRAError
self.mock_downstream.fields.fixVersions = []
mock_client = MagicMock()
# Call the function
d._update_fixVersion(
updates=self.mock_updates,
existing=self.mock_downstream,
issue=self.mock_issue,
client=mock_client,
)
# Assert all calls were made correctly
self.mock_downstream.update.assert_called_with(
{"fixVersions": [{"name": "fixVersion3"}, {"name": "fixVersion4"}]}
)
mock_client.add_comment(
self.mock_downstream,
f"Error updating fixVersion: {self.mock_issue.fixVersion}",
)
def test_update_fixVersion_no_api_call(self):
"""
This function tests the 'update_fixVersion' function existing labels are the same
and thus no API call should be made
"""
# Set up return values
self.mock_downstream.update.side_effect = JIRAError
mock_client = MagicMock()
# Call the function
d._update_fixVersion(
updates=self.mock_updates,