forked from python-bugzilla/python-bugzilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cli_misc.py
148 lines (114 loc) · 4.26 KB
/
test_cli_misc.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
#
# 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 CLI bits to get build out our code coverage
"""
import base64
import datetime
import json
import xmlrpc.client
import pytest
import requests
import bugzilla
import tests
import tests.mockbackend
def testHelp(run_cli):
out = run_cli("bugzilla --help", None)
assert len(out.splitlines()) > 18
def testCmdHelp(run_cli):
out = run_cli("bugzilla query --help", None)
assert len(out.splitlines()) > 40
def testVersion(run_cli):
out = run_cli("bugzilla --version", None)
assert out.strip() == bugzilla.__version__
def testCookiefileDeprecated(run_cli):
with pytest.raises(TypeError) as e:
run_cli("bugzilla --cookiefile foobar login",
None, expectfail=True)
assert "cookiefile is deprecated" in str(e)
def testPositionalArgs(run_cli):
# Make sure cli correctly rejects ambiguous positional args
out = run_cli("bugzilla login --xbadarg foo",
None, expectfail=True)
assert "unrecognized arguments: --xbadarg" in out
out = run_cli("bugzilla modify 123456 --foobar --status NEW",
None, expectfail=True)
assert "unrecognized arguments: --foobar" in out
def testDebug(run_cli):
# Coverage testing for debug options
run_cli("bugzilla --bugzilla https:///BADURI --verbose login",
None, expectfail=True)
run_cli("bugzilla --bugzilla https:///BADURI --debug login",
None, expectfail=True)
def testExceptions(run_cli):
"""
Test exception handling around main()
"""
fakebz = tests.mockbackend.make_bz(
bug_search_args=None,
bug_search_return=KeyboardInterrupt())
out = run_cli("bugzilla query --bug_id 1", fakebz, expectfail=True)
assert "user request" in out
fakebz = tests.mockbackend.make_bz(
bug_search_args=None,
bug_search_return=bugzilla.BugzillaError("foo"))
out = run_cli("bugzilla query --bug_id 1", fakebz, expectfail=True)
assert "Server error:" in out
fakebz = tests.mockbackend.make_bz(
bug_search_args=None,
bug_search_return=requests.exceptions.SSLError())
out = run_cli("bugzilla query --bug_id 1", fakebz, expectfail=True)
assert "trust the remote server" in out
fakebz = tests.mockbackend.make_bz(
bug_search_args=None,
bug_search_return=requests.exceptions.ConnectionError())
out = run_cli("bugzilla query --bug_id 1", fakebz, expectfail=True)
assert "Connection lost" in out
def testManualURL(run_cli):
"""
Test passing a manual URL, to hit those non-testsuite code paths
"""
try:
cmd = "bugzilla --bztype foobar "
cmd += "--bugzilla https:///FAKEURL query --bug_id 1"
run_cli(cmd, None)
except Exception as e:
assert "No host supplied" in str(e)
def test_json_xmlrpc(run_cli):
# Test --json output with XMLRPC type conversion
cmd = "bugzilla query --json --id 1165434"
timestr = '20181209T19:12:12'
dateobj = datetime.datetime.strptime(timestr, '%Y%m%dT%H:%M:%S')
attachfile = tests.utils.tests_path("data/bz-attach-get1.txt")
attachdata = open(attachfile, "rb").read()
bugid = 1165434
data = {"bugs": [{
'id': bugid,
'timetest': xmlrpc.client.DateTime(dateobj),
'binarytest': xmlrpc.client.Binary(attachdata),
}]}
fakebz = tests.mockbackend.make_bz(
bug_search_args=None,
bug_search_return={"bugs": [{"id": bugid}]},
bug_get_args=None,
bug_get_return=data)
out = run_cli(cmd, fakebz)
tests.utils.diff_compare(tests.utils.sanitize_json(out),
"data/clioutput/test_json_xmlrpc.txt")
retdata = json.loads(out)["bugs"][0]
assert (base64.b64decode(retdata["binarytest"]) ==
attachdata)
assert retdata["timetest"] == dateobj.isoformat() + "Z"
# Test an error case, json converter can't handle Exception class
data["bugs"][0]["foo"] = Exception("foo")
fakebz = tests.mockbackend.make_bz(
bug_search_args=None,
bug_search_return={"bugs": [{"id": bugid}]},
bug_get_args=None,
bug_get_return=data)
with pytest.raises(RuntimeError):
run_cli(cmd, fakebz, expectfail=True)