forked from chaoss/grimoirelab-perceval
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_bugzilla.py
1174 lines (956 loc) · 44.3 KB
/
test_bugzilla.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015-2020 Bitergia
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Santiago Dueñas <[email protected]>
# Stephan Barth <[email protected]>
# Valerio Cosentino <[email protected]>
# Miguel Ángel Fernández <[email protected]>
# Harshal Mittal <[email protected]>
#
import copy
import datetime
import os
import shutil
import unittest
import httpretty
import requests
from perceval.backend import BackendCommandArgumentParser
from perceval.errors import BackendError, ParseError
from perceval.utils import DEFAULT_DATETIME
from perceval.backends.core.bugzilla import (Bugzilla,
BugzillaCommand,
BugzillaClient)
from base import TestCaseBackendArchive
BUGZILLA_SERVER_URL = 'http://example.com'
BUGZILLA_LOGIN_URL = BUGZILLA_SERVER_URL + '/index.cgi'
BUGZILLA_METADATA_URL = BUGZILLA_SERVER_URL + '/show_bug.cgi'
BUGZILLA_BUGLIST_URL = BUGZILLA_SERVER_URL + '/buglist.cgi'
BUGZILLA_BUG_URL = BUGZILLA_SERVER_URL + '/show_bug.cgi'
BUGZILLA_BUG_ACTIVITY_URL = BUGZILLA_SERVER_URL + '/show_activity.cgi'
def read_file(filename, mode='r'):
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), filename), mode) as f:
content = f.read()
return content
class TestBugzillaBackend(unittest.TestCase):
"""Bugzilla backend tests"""
def test_initialization(self):
"""Test whether attributes are initializated"""
bg = Bugzilla(BUGZILLA_SERVER_URL, tag='test',
max_bugs=5)
self.assertEqual(bg.url, BUGZILLA_SERVER_URL)
self.assertEqual(bg.origin, BUGZILLA_SERVER_URL)
self.assertEqual(bg.tag, 'test')
self.assertEqual(bg.max_bugs, 5)
self.assertIsNone(bg.client)
self.assertTrue(bg.ssl_verify)
# When tag is empty or None it will be set to
# the value in the origin (URL)
bg = Bugzilla(BUGZILLA_SERVER_URL)
self.assertEqual(bg.url, BUGZILLA_SERVER_URL)
self.assertEqual(bg.origin, BUGZILLA_SERVER_URL)
self.assertEqual(bg.tag, BUGZILLA_SERVER_URL)
bg = Bugzilla(BUGZILLA_SERVER_URL, tag='', ssl_verify=False)
self.assertEqual(bg.url, BUGZILLA_SERVER_URL)
self.assertEqual(bg.origin, BUGZILLA_SERVER_URL)
self.assertEqual(bg.tag, BUGZILLA_SERVER_URL)
self.assertFalse(bg.ssl_verify)
def test_has_archiving(self):
"""Test if it returns True when has_archiving is called"""
self.assertEqual(Bugzilla.has_archiving(), True)
def test_has_resuming(self):
"""Test if it returns True when has_resuming is called"""
self.assertEqual(Bugzilla.has_resuming(), True)
@httpretty.activate
def test_fetch(self):
"""Test whether a list of bugs is returned"""
requests = []
bodies_csv = [read_file('data/bugzilla/bugzilla_buglist.csv'),
read_file('data/bugzilla/bugzilla_buglist_next.csv'),
""]
bodies_xml = [read_file('data/bugzilla/bugzilla_version.xml', mode='rb'),
read_file('data/bugzilla/bugzilla_bugs_details.xml', mode='rb'),
read_file('data/bugzilla/bugzilla_bugs_details_next.xml', mode='rb')]
bodies_html = [read_file('data/bugzilla/bugzilla_bug_activity.html', mode='rb'),
read_file('data/bugzilla/bugzilla_bug_activity_empty.html', mode='rb')]
def request_callback(method, uri, headers):
if uri.startswith(BUGZILLA_BUGLIST_URL):
body = bodies_csv.pop(0)
elif uri.startswith(BUGZILLA_BUG_URL):
body = bodies_xml.pop(0)
else:
body = bodies_html[len(requests) % 2]
requests.append(httpretty.last_request())
return (200, headers, body)
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUGLIST_URL,
responses=[
httpretty.Response(body=request_callback)
for _ in range(3)
])
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUG_URL,
responses=[
httpretty.Response(body=request_callback)
for _ in range(2)
])
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUG_ACTIVITY_URL,
responses=[
httpretty.Response(body=request_callback)
for _ in range(7)
])
bg = Bugzilla(BUGZILLA_SERVER_URL,
max_bugs=5, max_bugs_csv=500)
bugs = [bug for bug in bg.fetch()]
self.assertEqual(len(bugs), 7)
self.assertEqual(bugs[0]['data']['bug_id'][0]['__text__'], '15')
self.assertEqual(len(bugs[0]['data']['activity']), 0)
self.assertEqual(bugs[0]['origin'], BUGZILLA_SERVER_URL)
self.assertEqual(bugs[0]['uuid'], '5a8a1e25dfda86b961b4146050883cbfc928f8ec')
self.assertEqual(bugs[0]['updated_on'], 1248276445.0)
self.assertEqual(bugs[0]['category'], 'bug')
self.assertEqual(bugs[0]['tag'], BUGZILLA_SERVER_URL)
self.assertEqual(bugs[6]['data']['bug_id'][0]['__text__'], '888')
self.assertEqual(len(bugs[6]['data']['activity']), 14)
self.assertEqual(bugs[6]['origin'], BUGZILLA_SERVER_URL)
self.assertEqual(bugs[6]['uuid'], 'b4009442d38f4241a4e22e3e61b7cd8ef5ced35c')
self.assertEqual(bugs[6]['updated_on'], 1439404330.0)
self.assertEqual(bugs[6]['category'], 'bug')
self.assertEqual(bugs[6]['tag'], BUGZILLA_SERVER_URL)
# Check requests
expected = [
{
'ctype': ['xml']
},
{
'ctype': ['csv'],
'limit': ['500'],
'order': ['changeddate'],
'chfieldfrom': ['1970-01-01 00:00:00']
},
{
'ctype': ['csv'],
'limit': ['500'],
'order': ['changeddate'],
'chfieldfrom': ['2009-07-30 11:35:33']
},
{
'ctype': ['csv'],
'limit': ['500'],
'order': ['changeddate'],
'chfieldfrom': ['2015-08-12 18:32:11']
},
{
'ctype': ['xml'],
'id': ['15', '18', '17', '20', '19'],
'excludefield': ['attachmentdata']
},
{
'id': ['15']
},
{
'id': ['18']
},
{
'id': ['17']
},
{
'id': ['20']
},
{
'id': ['19']
},
{
'ctype': ['xml'],
'id': ['30', '888'],
'excludefield': ['attachmentdata']
},
{
'id': ['30']
},
{
'id': ['888']
}
]
self.assertEqual(len(requests), len(expected))
for i in range(len(expected)):
self.assertDictEqual(requests[i].querystring, expected[i])
@httpretty.activate
def test_search_fields(self):
"""Test whether the search_fields is properly set"""
requests = []
bodies_csv = [read_file('data/bugzilla/bugzilla_buglist.csv'),
read_file('data/bugzilla/bugzilla_buglist_next.csv'),
""]
bodies_xml = [read_file('data/bugzilla/bugzilla_version.xml', mode='rb'),
read_file('data/bugzilla/bugzilla_bugs_details.xml', mode='rb'),
read_file('data/bugzilla/bugzilla_bugs_details_next.xml', mode='rb')]
bodies_html = [read_file('data/bugzilla/bugzilla_bug_activity.html', mode='rb'),
read_file('data/bugzilla/bugzilla_bug_activity_empty.html', mode='rb')]
def request_callback(method, uri, headers):
if uri.startswith(BUGZILLA_BUGLIST_URL):
body = bodies_csv.pop(0)
elif uri.startswith(BUGZILLA_BUG_URL):
body = bodies_xml.pop(0)
else:
body = bodies_html[len(requests) % 2]
requests.append(httpretty.last_request())
return 200, headers, body
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUGLIST_URL,
responses=[
httpretty.Response(body=request_callback)
for _ in range(3)
])
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUG_URL,
responses=[
httpretty.Response(body=request_callback)
for _ in range(2)
])
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUG_ACTIVITY_URL,
responses=[
httpretty.Response(body=request_callback)
for _ in range(7)
])
bg = Bugzilla(BUGZILLA_SERVER_URL,
max_bugs=5, max_bugs_csv=500)
bugs = [bug for bug in bg.fetch()]
self.assertEqual(len(bugs), 7)
bug = bugs[0]
self.assertEqual(bg.metadata_id(bug['data']), bug['search_fields']['item_id'])
self.assertEqual(bug['data']['product'][0]['__text__'], 'LibreGeoSocial (Android)')
self.assertEqual(bug['data']['product'][0]['__text__'], bug['search_fields']['product'])
self.assertEqual(bug['data']['component'][0]['__text__'], 'general')
self.assertEqual(bug['data']['component'][0]['__text__'], bug['search_fields']['component'])
bug = bugs[1]
self.assertEqual(bg.metadata_id(bug['data']), bug['search_fields']['item_id'])
self.assertEqual(bug['data']['product'][0]['__text__'], 'LibreGeoSocial (Android)')
self.assertEqual(bug['data']['product'][0]['__text__'], bug['search_fields']['product'])
self.assertEqual(bug['data']['component'][0]['__text__'], 'general')
self.assertEqual(bug['data']['component'][0]['__text__'], bug['search_fields']['component'])
bug = bugs[2]
self.assertEqual(bg.metadata_id(bug['data']), bug['search_fields']['item_id'])
self.assertEqual(bug['data']['product'][0]['__text__'], 'Bicho')
self.assertEqual(bug['data']['product'][0]['__text__'], bug['search_fields']['product'])
self.assertEqual(bug['data']['component'][0]['__text__'], 'General')
self.assertEqual(bug['data']['component'][0]['__text__'], bug['search_fields']['component'])
bug = bugs[3]
self.assertEqual(bg.metadata_id(bug['data']), bug['search_fields']['item_id'])
self.assertEqual(bug['data']['product'][0]['__text__'], 'LibreGeoSocial (server)')
self.assertEqual(bug['data']['product'][0]['__text__'], bug['search_fields']['product'])
self.assertEqual(bug['data']['component'][0]['__text__'], 'general')
self.assertEqual(bug['data']['component'][0]['__text__'], bug['search_fields']['component'])
bug = bugs[4]
self.assertEqual(bg.metadata_id(bug['data']), bug['search_fields']['item_id'])
self.assertEqual(bug['data']['product'][0]['__text__'], 'CVSAnalY')
self.assertEqual(bug['data']['product'][0]['__text__'], bug['search_fields']['product'])
self.assertEqual(bug['data']['component'][0]['__text__'], 'general')
self.assertEqual(bug['data']['component'][0]['__text__'], bug['search_fields']['component'])
bug = bugs[5]
self.assertEqual(bg.metadata_id(bug['data']), bug['search_fields']['item_id'])
self.assertEqual(bug['data']['product'][0]['__text__'], 'Bicho')
self.assertEqual(bug['data']['product'][0]['__text__'], bug['search_fields']['product'])
self.assertEqual(bug['data']['component'][0]['__text__'], 'General')
self.assertEqual(bug['data']['component'][0]['__text__'], bug['search_fields']['component'])
bug = bugs[6]
self.assertEqual(bg.metadata_id(bug['data']), bug['search_fields']['item_id'])
self.assertEqual(bug['data']['product'][0]['__text__'], 'CVSAnalY')
self.assertEqual(bug['data']['product'][0]['__text__'], bug['search_fields']['product'])
self.assertEqual(bug['data']['component'][0]['__text__'], 'general')
self.assertEqual(bug['data']['component'][0]['__text__'], bug['search_fields']['component'])
@httpretty.activate
def test_fetch_from_date(self):
"""Test whether a list of bugs is returned from a given date"""
requests = []
bodies_csv = [read_file('data/bugzilla/bugzilla_buglist_next.csv'),
""]
bodies_xml = [read_file('data/bugzilla/bugzilla_version.xml', mode='rb'),
read_file('data/bugzilla/bugzilla_bugs_details_next.xml', mode='rb')]
bodies_html = [read_file('data/bugzilla/bugzilla_bug_activity.html', mode='rb'),
read_file('data/bugzilla/bugzilla_bug_activity_empty.html', mode='rb')]
def request_callback(method, uri, headers):
if uri.startswith(BUGZILLA_BUGLIST_URL):
body = bodies_csv.pop(0)
elif uri.startswith(BUGZILLA_BUG_URL):
body = bodies_xml.pop(0)
else:
body = bodies_html[len(requests) % 2]
requests.append(httpretty.last_request())
return (200, headers, body)
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUGLIST_URL,
responses=[
httpretty.Response(body=request_callback)
for _ in range(2)
])
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUG_URL,
responses=[
httpretty.Response(body=request_callback)
])
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUG_ACTIVITY_URL,
responses=[
httpretty.Response(body=request_callback)
for _ in range(2)
])
from_date = datetime.datetime(2015, 1, 1)
bg = Bugzilla(BUGZILLA_SERVER_URL)
bugs = [bug for bug in bg.fetch(from_date=from_date)]
self.assertEqual(len(bugs), 2)
self.assertEqual(bugs[0]['data']['bug_id'][0]['__text__'], '30')
self.assertEqual(len(bugs[0]['data']['activity']), 14)
self.assertEqual(bugs[0]['origin'], BUGZILLA_SERVER_URL)
self.assertEqual(bugs[0]['uuid'], '4b166308f205121bc57704032acdc81b6c9bb8b1')
self.assertEqual(bugs[0]['updated_on'], 1426868155.0)
self.assertEqual(bugs[0]['category'], 'bug')
self.assertEqual(bugs[0]['tag'], BUGZILLA_SERVER_URL)
self.assertEqual(bugs[1]['data']['bug_id'][0]['__text__'], '888')
self.assertEqual(len(bugs[1]['data']['activity']), 0)
self.assertEqual(bugs[1]['origin'], BUGZILLA_SERVER_URL)
self.assertEqual(bugs[1]['uuid'], 'b4009442d38f4241a4e22e3e61b7cd8ef5ced35c')
self.assertEqual(bugs[1]['updated_on'], 1439404330.0)
self.assertEqual(bugs[1]['category'], 'bug')
self.assertEqual(bugs[1]['tag'], BUGZILLA_SERVER_URL)
# Check requests
expected = [
{
'ctype': ['xml']
},
{
'ctype': ['csv'],
'limit': ['10000'],
'order': ['changeddate'],
'chfieldfrom': ['2015-01-01 00:00:00']
},
{
'ctype': ['csv'],
'limit': ['10000'],
'order': ['changeddate'],
'chfieldfrom': ['2015-08-12 18:32:11']
},
{
'ctype': ['xml'],
'id': ['30', '888'],
'excludefield': ['attachmentdata']
},
{
'id': ['30']
},
{
'id': ['888']
}
]
self.assertEqual(len(requests), len(expected))
for i in range(len(expected)):
self.assertDictEqual(requests[i].querystring, expected[i])
@httpretty.activate
def test_fetch_empty(self):
"""Test whether it works when no bugs are fetched"""
body = read_file('data/bugzilla/bugzilla_version.xml')
httpretty.register_uri(httpretty.GET,
BUGZILLA_METADATA_URL,
body=body, status=200)
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUGLIST_URL,
body="", status=200)
from_date = datetime.datetime(2100, 1, 1)
bg = Bugzilla(BUGZILLA_SERVER_URL)
bugs = [bug for bug in bg.fetch(from_date=from_date)]
self.assertEqual(len(bugs), 0)
# Check request
expected = {
'ctype': ['csv'],
'limit': ['10000'],
'order': ['changeddate'],
'chfieldfrom': ['2100-01-01 00:00:00']
}
req = httpretty.last_request()
self.assertDictEqual(req.querystring, expected)
@httpretty.activate
def test_fetch_auth(self):
"""Test whether authentication works"""
requests = []
bodies_csv = [read_file('data/bugzilla/bugzilla_buglist_next.csv'),
""]
bodies_xml = [read_file('data/bugzilla/bugzilla_version.xml', mode='rb'),
read_file('data/bugzilla/bugzilla_bugs_details_next.xml', mode='rb')]
bodies_html = [read_file('data/bugzilla/bugzilla_bug_activity.html', mode='rb'),
read_file('data/bugzilla/bugzilla_bug_activity_empty.html', mode='rb')]
def request_callback(method, uri, headers):
if uri.startswith(BUGZILLA_LOGIN_URL):
body = "index.cgi?logout=1"
elif uri.startswith(BUGZILLA_BUGLIST_URL):
body = bodies_csv.pop(0)
elif uri.startswith(BUGZILLA_BUG_URL):
body = bodies_xml.pop(0)
else:
body = bodies_html[(len(requests) + 1) % 2]
requests.append(httpretty.last_request())
return (200, headers, body)
httpretty.register_uri(httpretty.POST,
BUGZILLA_LOGIN_URL,
responses=[
httpretty.Response(body=request_callback)
])
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUGLIST_URL,
responses=[
httpretty.Response(body=request_callback)
for _ in range(2)
])
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUG_URL,
responses=[
httpretty.Response(body=request_callback)
])
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUG_ACTIVITY_URL,
responses=[
httpretty.Response(body=request_callback)
for _ in range(2)
])
from_date = datetime.datetime(2015, 1, 1)
bg = Bugzilla(BUGZILLA_SERVER_URL,
user='[email protected]',
password='1234')
bugs = [bug for bug in bg.fetch(from_date=from_date)]
self.assertEqual(len(bugs), 2)
self.assertEqual(bugs[0]['data']['bug_id'][0]['__text__'], '30')
self.assertEqual(len(bugs[0]['data']['activity']), 14)
self.assertEqual(bugs[0]['origin'], BUGZILLA_SERVER_URL)
self.assertEqual(bugs[0]['uuid'], '4b166308f205121bc57704032acdc81b6c9bb8b1')
self.assertEqual(bugs[0]['updated_on'], 1426868155.0)
self.assertEqual(bugs[0]['category'], 'bug')
self.assertEqual(bugs[0]['tag'], BUGZILLA_SERVER_URL)
self.assertEqual(bugs[1]['data']['bug_id'][0]['__text__'], '888')
self.assertEqual(len(bugs[1]['data']['activity']), 0)
self.assertEqual(bugs[1]['origin'], BUGZILLA_SERVER_URL)
self.assertEqual(bugs[1]['uuid'], 'b4009442d38f4241a4e22e3e61b7cd8ef5ced35c')
self.assertEqual(bugs[1]['updated_on'], 1439404330.0)
self.assertEqual(bugs[1]['category'], 'bug')
self.assertEqual(bugs[1]['tag'], BUGZILLA_SERVER_URL)
# Check requests
auth_expected = {
'Bugzilla_login': ['[email protected]'],
'Bugzilla_password': ['1234'],
'GoAheadAndLogIn': ['Log in']
}
expected = [
{
'ctype': ['xml']
},
{
'ctype': ['csv'],
'limit': ['10000'],
'order': ['changeddate'],
'chfieldfrom': ['2015-01-01 00:00:00']
},
{
'ctype': ['csv'],
'limit': ['10000'],
'order': ['changeddate'],
'chfieldfrom': ['2015-08-12 18:32:11']
},
{
'ctype': ['xml'],
'id': ['30', '888'],
'excludefield': ['attachmentdata']
},
{
'id': ['30']
},
{
'id': ['888']
}
]
# Check authentication request
auth_req = requests.pop(0)
self.assertDictEqual(auth_req.parsed_body, auth_expected)
# Check the rests of the headers
self.assertEqual(len(requests), len(expected))
for i in range(len(expected)):
self.assertDictEqual(requests[i].querystring, expected[i])
class TestBugzillaBackendArchive(TestCaseBackendArchive):
"""Bugzilla backend tests using an archive"""
def setUp(self):
super().setUp()
self.backend_write_archive = Bugzilla(BUGZILLA_SERVER_URL,
user='[email protected]', password='1234',
max_bugs=5, max_bugs_csv=500,
archive=self.archive)
self.backend_read_archive = Bugzilla(BUGZILLA_SERVER_URL,
user='[email protected]', password='5678',
max_bugs=5, max_bugs_csv=500,
archive=self.archive)
def tearDown(self):
shutil.rmtree(self.test_path)
@httpretty.activate
def test_fetch_from_archive(self):
"""Test whether a list of bugs is returned from the archive"""
requests = []
bodies_csv = [read_file('data/bugzilla/bugzilla_buglist.csv'),
read_file('data/bugzilla/bugzilla_buglist_next.csv'),
""]
bodies_xml = [read_file('data/bugzilla/bugzilla_version.xml', mode='rb'),
read_file('data/bugzilla/bugzilla_bugs_details.xml', mode='rb'),
read_file('data/bugzilla/bugzilla_bugs_details_next.xml', mode='rb')]
bodies_html = [read_file('data/bugzilla/bugzilla_bug_activity.html', mode='rb'),
read_file('data/bugzilla/bugzilla_bug_activity_empty.html', mode='rb')]
def request_callback(method, uri, headers):
if uri.startswith(BUGZILLA_BUGLIST_URL):
body = bodies_csv.pop(0)
elif uri.startswith(BUGZILLA_BUG_URL):
body = bodies_xml.pop(0)
else:
body = bodies_html[len(requests) % 2]
requests.append(httpretty.last_request())
return (200, headers, body)
httpretty.register_uri(httpretty.POST,
BUGZILLA_LOGIN_URL,
body="index.cgi?logout=1",
status=200)
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUGLIST_URL,
responses=[
httpretty.Response(body=request_callback)
for _ in range(3)
])
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUG_URL,
responses=[
httpretty.Response(body=request_callback)
for _ in range(2)
])
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUG_ACTIVITY_URL,
responses=[
httpretty.Response(body=request_callback)
for _ in range(7)
])
self._test_fetch_from_archive(from_date=None)
@httpretty.activate
def test_fetch_from_date_from_archive(self):
"""Test whether a list of bugs is returned from a given date from archive"""
requests = []
bodies_csv = [read_file('data/bugzilla/bugzilla_buglist_next.csv'),
""]
bodies_xml = [read_file('data/bugzilla/bugzilla_version.xml', mode='rb'),
read_file('data/bugzilla/bugzilla_bugs_details_next.xml', mode='rb')]
bodies_html = [read_file('data/bugzilla/bugzilla_bug_activity.html', mode='rb'),
read_file('data/bugzilla/bugzilla_bug_activity_empty.html', mode='rb')]
def request_callback(method, uri, headers):
if uri.startswith(BUGZILLA_BUGLIST_URL):
body = bodies_csv.pop(0)
elif uri.startswith(BUGZILLA_BUG_URL):
body = bodies_xml.pop(0)
else:
body = bodies_html[len(requests) % 2]
requests.append(httpretty.last_request())
return (200, headers, body)
httpretty.register_uri(httpretty.POST,
BUGZILLA_LOGIN_URL,
body="index.cgi?logout=1",
status=200)
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUGLIST_URL,
responses=[
httpretty.Response(body=request_callback)
for _ in range(2)
])
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUG_URL,
responses=[
httpretty.Response(body=request_callback)
])
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUG_ACTIVITY_URL,
responses=[
httpretty.Response(body=request_callback)
for _ in range(2)
])
from_date = datetime.datetime(2015, 1, 1)
self._test_fetch_from_archive(from_date=from_date)
@httpretty.activate
def test_fetch_empty_from_archive(self):
"""Test whether it works when no bugs are fetched from archive"""
body = read_file('data/bugzilla/bugzilla_version.xml')
httpretty.register_uri(httpretty.POST,
BUGZILLA_LOGIN_URL,
body="index.cgi?logout=1",
status=200)
httpretty.register_uri(httpretty.GET,
BUGZILLA_METADATA_URL,
body=body, status=200)
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUGLIST_URL,
body="", status=200)
from_date = datetime.datetime(2100, 1, 1)
self._test_fetch_from_archive(from_date=from_date)
class TestBugzillaBackendParsers(unittest.TestCase):
"""Bugzilla backend parsers tests"""
def test_parse_buglist(self):
"""Test buglist parsing"""
raw_csv = read_file('data/bugzilla/bugzilla_buglist.csv')
bugs = Bugzilla.parse_buglist(raw_csv)
result = [bug for bug in bugs]
self.assertEqual(len(result), 5)
self.assertEqual(result[0]['bug_id'], '15')
self.assertEqual(result[4]['bug_id'], '19')
def test_parse_bugs_details(self):
"""Test bugs details parsing"""
raw_xml = read_file('data/bugzilla/bugzilla_bugs_details.xml')
bugs = Bugzilla.parse_bugs_details(raw_xml)
result = [bug for bug in bugs]
self.assertEqual(len(result), 5)
bug_ids = [bug['bug_id'][0]['__text__'] for bug in result]
expected = ['15', '18', '17', '20', '19']
self.assertListEqual(bug_ids, expected)
raw_xml = read_file('data/bugzilla/bugzilla_bugs_details_next.xml')
bugs = Bugzilla.parse_bugs_details(raw_xml)
result = [bug for bug in bugs]
def test_parse_invalid_bug_details(self):
"""Test whether it fails parsing an invalid XML with no bugs"""
raw_xml = read_file('data/bugzilla/bugzilla_bugs_details_not_valid.xml')
with self.assertRaises(ParseError):
bugs = Bugzilla.parse_bugs_details(raw_xml)
_ = [bug for bug in bugs]
def test_parse_activity(self):
"""Test activity bug parsing"""
raw_html = read_file('data/bugzilla/bugzilla_bug_activity.html')
activity = Bugzilla.parse_bug_activity(raw_html)
result = [event for event in activity]
self.assertEqual(len(result), 14)
expected = {
'Who': '[email protected]',
'When': '2013-06-25 11:57:23 CEST',
'What': 'Attachment #172 Attachment is obsolete',
'Removed': '0',
'Added': '1'
}
self.assertDictEqual(result[0], expected)
expected = {
'Who': '[email protected]',
'When': '2013-06-25 11:59:07 CEST',
'What': 'Depends on',
'Removed': '350',
'Added': ''
}
self.assertDictEqual(result[6], expected)
def test_parse_empty_activity(self):
"""Test the parser when the activity table is empty"""
# There are two possible cases for empty tables.
# The first case includes the term 'bug' while the second
# one replaces it by 'issue'.
raw_html = read_file('data/bugzilla/bugzilla_bug_activity_empty.html')
activity = Bugzilla.parse_bug_activity(raw_html)
result = [event for event in activity]
self.assertEqual(len(result), 0)
raw_html = read_file('data/bugzilla/bugzilla_bug_activity_empty_alt.html')
activity = Bugzilla.parse_bug_activity(raw_html)
result = [event for event in activity]
self.assertEqual(len(result), 0)
def test_parse_activity_no_table(self):
"""Test if it raises an exception the activity table is not found"""
raw_html = read_file('data/bugzilla/bugzilla_bug_activity_not_valid.html')
with self.assertRaises(ParseError):
activity = Bugzilla.parse_bug_activity(raw_html)
_ = [event for event in activity]
class TestBugzillaCommand(unittest.TestCase):
"""BugzillaCommand unit tests"""
def test_backend_class(self):
"""Test if the backend class is Bugzilla"""
self.assertIs(BugzillaCommand.BACKEND, Bugzilla)
def test_setup_cmd_parser(self):
"""Test if it parser object is correctly initialized"""
parser = BugzillaCommand.setup_cmd_parser()
self.assertIsInstance(parser, BackendCommandArgumentParser)
self.assertEqual(parser._backend, Bugzilla)
args = ['--backend-user', '[email protected]',
'--backend-password', '1234',
'--max-bugs', '10', '--max-bugs-csv', '5',
'--tag', 'test',
'--from-date', '1970-01-01',
'--no-archive',
BUGZILLA_SERVER_URL]
parsed_args = parser.parse(*args)
self.assertEqual(parsed_args.user, '[email protected]')
self.assertEqual(parsed_args.password, '1234')
self.assertEqual(parsed_args.max_bugs, 10)
self.assertEqual(parsed_args.max_bugs_csv, 5)
self.assertEqual(parsed_args.tag, 'test')
self.assertTrue(parsed_args.no_archive)
self.assertTrue(parsed_args.ssl_verify)
self.assertEqual(parsed_args.url, BUGZILLA_SERVER_URL)
self.assertEqual(parsed_args.from_date, DEFAULT_DATETIME)
args = ['--backend-user', '[email protected]',
'--backend-password', '1234',
'--max-bugs', '10', '--max-bugs-csv', '5',
'--tag', 'test',
'--from-date', '1970-01-01',
'--no-ssl-verify',
BUGZILLA_SERVER_URL]
parsed_args = parser.parse(*args)
self.assertEqual(parsed_args.user, '[email protected]')
self.assertEqual(parsed_args.password, '1234')
self.assertEqual(parsed_args.max_bugs, 10)
self.assertEqual(parsed_args.max_bugs_csv, 5)
self.assertEqual(parsed_args.tag, 'test')
self.assertFalse(parsed_args.ssl_verify)
self.assertEqual(parsed_args.url, BUGZILLA_SERVER_URL)
self.assertEqual(parsed_args.from_date, DEFAULT_DATETIME)
class TestBugzillaClient(unittest.TestCase):
"""Bugzilla API client tests
These tests not check the body of the response, only if the call
was well formed and if a response was obtained. Due to this, take
into account that the body returned on each request might not
match with the parameters from the request.
"""
@httpretty.activate
def test_init(self):
"""Test initialization"""
client = BugzillaClient(BUGZILLA_SERVER_URL)
self.assertEqual(client.version, None)
self.assertTrue(client.ssl_verify)
self.assertIsInstance(client.session, requests.Session)
client = BugzillaClient(BUGZILLA_SERVER_URL, ssl_verify=False)
self.assertEqual(client.version, None)
self.assertFalse(client.ssl_verify)
self.assertIsInstance(client.session, requests.Session)
@httpretty.activate
def test_init_auth(self):
"""Test initialization with authentication"""
# Set up a mock HTTP server
httpretty.register_uri(httpretty.POST,
BUGZILLA_LOGIN_URL,
body="index.cgi?logout=1",
status=200)
_ = BugzillaClient(BUGZILLA_SERVER_URL,
user='[email protected]',
password='1234')
# Check request params
expected = {
'Bugzilla_login': ['[email protected]'],
'Bugzilla_password': ['1234'],
'GoAheadAndLogIn': ['Log in']
}
req = httpretty.last_request()
self.assertEqual(req.method, 'POST')
self.assertRegex(req.path, '/index.cgi')
self.assertEqual(req.parsed_body, expected)
@httpretty.activate
def test_logout(self):
"""Test whether the logout is properly completed"""
# Set up a mock HTTP server
httpretty.register_uri(httpretty.GET,
BUGZILLA_LOGIN_URL,
body="index.cgi?logout=1",
status=200)
client = BugzillaClient(BUGZILLA_SERVER_URL)
client.logout()
req = httpretty.last_request()
self.assertEqual(req.close_connection, True)
@httpretty.activate
def test_invalid_auth(self):
"""Test whether it fails when the authentication goes wrong"""
# Set up a mock HTTP server
httpretty.register_uri(httpretty.POST,
BUGZILLA_LOGIN_URL,
body="",
status=200)
with self.assertRaises(BackendError):
_ = BugzillaClient(BUGZILLA_SERVER_URL,
user='[email protected]',
password='1234')
@httpretty.activate
def test_not_found_version(self):
"""Test if it fails when the server version is not found"""
# Set up a mock HTTP server
body = read_file('data/bugzilla/bugzilla_no_version.xml')
httpretty.register_uri(httpretty.GET,
BUGZILLA_METADATA_URL,
body=body, status=200)
with self.assertRaises(BackendError):
client = BugzillaClient(BUGZILLA_SERVER_URL)
client.buglist()
@httpretty.activate
def test_metadata(self):
"""Test metadata API call"""
# Set up a mock HTTP server
body = read_file('data/bugzilla/bugzilla_version.xml')
httpretty.register_uri(httpretty.GET,
BUGZILLA_METADATA_URL,
body=body, status=200)
# Call API
client = BugzillaClient(BUGZILLA_SERVER_URL)
response = client.metadata()
self.assertEqual(response, body)
# Check request params
expected = {'ctype': ['xml']}
req = httpretty.last_request()
self.assertEqual(req.method, 'GET')
self.assertRegex(req.path, '/show_bug.cgi')
self.assertDictEqual(req.querystring, expected)
@httpretty.activate
def test_buglist(self):
"""Test buglist API call"""
# Set up a mock HTTP server
body = read_file('data/bugzilla/bugzilla_version.xml')
httpretty.register_uri(httpretty.GET,
BUGZILLA_METADATA_URL,
body=body, status=200)
body = read_file('data/bugzilla/bugzilla_buglist.csv')
httpretty.register_uri(httpretty.GET,
BUGZILLA_BUGLIST_URL,
body=body, status=200)
# Call API without args