-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathtest_api_misc.py
More file actions
317 lines (247 loc) · 9.24 KB
/
test_api_misc.py
File metadata and controls
317 lines (247 loc) · 9.24 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
#
# Copyright Red Hat, Inc. 2012
#
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.
#
"""
Test miscellaneous API bits
"""
import pytest
import bugzilla
import tests
import tests.mockbackend
def test_mock_rhbz():
fakebz = tests.mockbackend.make_bz(rhbz=True)
assert fakebz.__class__ == bugzilla.RHBugzilla
def test_file_imports():
# Ensure historically stable import paths continue to work
# pylint: disable=unused-import
from bugzilla.rhbugzilla import RHBugzilla
from bugzilla.bug import Bug
from bugzilla.base import Bugzilla
def testUserAgent():
b3 = tests.mockbackend.make_bz(version="3.0.0")
assert "python-bugzilla" in b3.user_agent
def test_fixurl():
assert (bugzilla.Bugzilla.fix_url("example.com") ==
"https://example.com/xmlrpc.cgi")
assert (bugzilla.Bugzilla.fix_url("example.com", force_rest=True) ==
"https://example.com/rest/")
assert (bugzilla.Bugzilla.fix_url("example.com/xmlrpc.cgi") ==
"https://example.com/xmlrpc.cgi")
assert (bugzilla.Bugzilla.fix_url("http://example.com/somepath.cgi") ==
"http://example.com/somepath.cgi")
assert bugzilla.Bugzilla.fix_url("http:///foo") == "http:///foo"
def testPostTranslation():
def _testPostCompare(bz, indict, outexpect):
outdict = indict.copy()
bz.post_translation({}, outdict)
assert outdict == outexpect
# Make sure multiple calls don't change anything
bz.post_translation({}, outdict)
assert outdict == outexpect
bug3 = tests.mockbackend.make_bz(version="3.4.0")
rhbz = tests.mockbackend.make_bz(version="4.4.0", rhbz=True)
test1 = {
"component": ["comp1"],
"version": ["ver1", "ver2"],
'flags': [{
'is_active': 1,
'name': 'qe_test_coverage',
'setter': '[email protected]',
'status': '?',
}, {
'is_active': 1,
'name': 'rhel-6.4.0',
'setter': '[email protected]',
'status': '+',
}],
'alias': ["FOO", "BAR"],
'blocks': [782183, 840699, 923128],
'keywords': ['Security'],
'groups': ['redhat'],
}
out_simple = test1.copy()
out_simple["components"] = out_simple["component"]
out_simple["component"] = out_simple["components"][0]
out_simple["versions"] = out_simple["version"]
out_simple["version"] = out_simple["versions"][0]
_testPostCompare(bug3, test1, test1)
_testPostCompare(rhbz, test1, out_simple)
def test_rhbz_pre_translation():
bz = tests.mockbackend.make_bz(rhbz=True)
input_query = {
"bug_id": "12345,6789",
"component": "comp1,comp2",
"column_list": ["field1", "field8"],
}
bz.pre_translation(input_query)
output_query = {
'component': ['comp1', 'comp2'],
'id': ['12345', '6789'],
'include_fields': ['field1', 'field8', 'id'],
}
assert output_query == input_query
def testUpdateFailures():
# sub_component without component also passed
bz = tests.mockbackend.make_bz(version="4.4.0", rhbz=True)
with pytest.raises(ValueError):
bz.build_update(sub_component="some sub component")
# Trying to update value that only rhbz supports
bz = tests.mockbackend.make_bz()
with pytest.raises(ValueError):
bz.build_update(fixed_in="some fixedin value")
def testCreatebugFieldConversion():
bz4 = tests.mockbackend.make_bz(version="4.0.0")
vc = bz4._validate_createbug # pylint: disable=protected-access
out = vc(product="foo", component="bar",
version="12", description="foo", short_desc="bar",
check_args=False)
assert out == {
'component': 'bar', 'description': 'foo', 'product': 'foo',
'summary': 'bar', 'version': '12'}
def testURLSavedSearch():
bz4 = tests.mockbackend.make_bz(version="4.0.0")
url = ("https://bugzilla.redhat.com/buglist.cgi?"
"cmdtype=dorem&list_id=2342312&namedcmd="
"RHEL7%20new%20assigned%20virt-maint&remaction=run&"
"sharer_id=321167")
query = {
'sharer_id': '321167',
'savedsearch': 'RHEL7 new assigned virt-maint'
}
assert bz4.url_to_query(url) == query
def testStandardQuery():
bz4 = tests.mockbackend.make_bz(version="4.0.0")
url = ("https://bugzilla.redhat.com/buglist.cgi?"
"component=virt-manager&query_format=advanced&classification="
"Fedora&product=Fedora&bug_status=NEW&bug_status=ASSIGNED&"
"bug_status=MODIFIED&bug_status=ON_DEV&bug_status=ON_QA&"
"bug_status=VERIFIED&bug_status=FAILS_QA&bug_status="
"RELEASE_PENDING&bug_status=POST&order=bug_status%2Cbug_id")
query = {
'product': 'Fedora',
'query_format': 'advanced',
'bug_status': ['NEW', 'ASSIGNED', 'MODIFIED', 'ON_DEV',
'ON_QA', 'VERIFIED', 'FAILS_QA', 'RELEASE_PENDING', 'POST'],
'classification': 'Fedora',
'component': 'virt-manager',
'order': 'bug_status,bug_id'
}
assert bz4.url_to_query(url) == query
# pylint: disable=use-implicit-booleaness-not-comparison
# Test with unknown URL
assert bz4.url_to_query("https://example.com") == {}
def test_api_login():
with pytest.raises(TypeError):
# Missing explicit URL
bugzilla.Bugzilla()
with pytest.raises(Exception):
# Calling connect() with passed in URL
bugzilla.Bugzilla(url="https:///FAKEURL")
bz = tests.mockbackend.make_bz()
with pytest.raises(ValueError):
# Errors on missing user
bz.login()
bz.user = "FOO"
with pytest.raises(ValueError):
# Errors on missing pass
bz.login()
bz.password = "BAR"
bz.api_key = "WIBBLE"
with pytest.raises(ValueError):
# Errors on api_key + login()
bz.login()
# Hit default api_key code path
bz = tests.mockbackend.make_bz(
bz_kwargs={"api_key": "FAKE_KEY"},
user_login_args="data/mockargs/test_api_login.txt",
user_login_return={})
# Try reconnect, with RHBZ testing
bz.connect("https:///fake/bugzilla.redhat.com")
bz.connect()
# Test auto login if user/password is set
bz = tests.mockbackend.make_bz(
bz_kwargs={"user": "FOO", "password": "BAR"},
user_login_args="data/mockargs/test_api_login2.txt",
user_login_return={},
user_logout_args=None,
user_logout_return={})
# Test logout
bz.logout()
def test_version_bad():
# Hit version error handling
bz = tests.mockbackend.make_bz(version="badversion")
assert bz.bz_ver_major == 5
assert bz.bz_ver_minor == 0
# pylint: disable=protected-access
assert bz._get_version() == 5.0
def test_extensions_bad():
# Hit bad extensions error handling
tests.mockbackend.make_bz(extensions="BADEXTENSIONS")
def test_bad_scheme():
bz = tests.mockbackend.make_bz()
try:
bz.connect("ftp://example.com")
except Exception as e:
assert "Invalid URL scheme: ftp" in str(e)
def test_update_flags():
# update_flags is just a compat wrapper for update_bugs
bz = tests.mockbackend.make_bz(
bug_update_args="data/mockargs/test_update_flags.txt",
bug_update_return={})
bz.update_flags([12345, 6789], {"name": "needinfo", "status": "?"})
def test_bugs_history_raw():
# Stub test for bugs_history_raw
ids = ["12345", 567]
bz = tests.mockbackend.make_bz(
bug_history_args=(ids, {}),
bug_history_return={})
bz.bugs_history_raw(ids)
def test_get_comments():
# Stub test for get_commands
ids = ["12345", 567]
bz = tests.mockbackend.make_bz(
bug_comments_args=(ids, {}),
bug_comments_return={})
bz.get_comments(ids)
def test_get_xmlrpc_proxy():
# Ensure _proxy goes to a backend API
bz = tests.mockbackend.make_bz()
with pytest.raises(NotImplementedError):
dummy = bz._proxy # pylint: disable=protected-access
assert bz.is_xmlrpc() is False
assert bz.is_rest() is False
assert hasattr(bz.get_requests_session(), "request")
def test_requests_session_passthrough():
import requests
session = requests.Session()
bz = tests.mockbackend.make_bz(
bz_kwargs={"requests_session": session, "sslverify": False})
assert bz.get_requests_session() is session
assert session.verify is False
def test_query_url_fail():
# test some handling of query from_url errors
query = {"query_format": "advanced", "product": "FOO"}
checkstr = "does not appear to support"
exc = bugzilla.BugzillaError("FAKEERROR query_format", code=123)
bz = tests.mockbackend.make_bz(version="4.0.0",
bug_search_args=None, bug_search_return=exc)
try:
bz.query(query)
except Exception as e:
assert checkstr in str(e)
bz = tests.mockbackend.make_bz(version="5.1.0",
bug_search_args=None, bug_search_return=exc)
try:
bz.query(query)
except Exception as e:
assert checkstr not in str(e)
def test_query_return_extra():
bz = tests.mockbackend.make_bz(version="5.1.0",
bug_search_args=None,
bug_search_return="data/mockreturn/test_query1.txt")
dummy, extra = bz.query_return_extra({})
assert extra['limit'] == 0
assert extra['FOOFAKEVALUE'] == "hello"