Skip to content

Commit 09c5e4f

Browse files
committed
* Corrected handling of faults in authenticate (closes #9)
* Added tests about this * Some refactoring inside the tests
1 parent 95a2cfc commit 09c5e4f

File tree

4 files changed

+147
-41
lines changed

4 files changed

+147
-41
lines changed

pythonzimbra/tools/auth.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
def authenticate(url, account, key, by='name', expires=0, timestamp=None,
1616
timeout=None, request_type="xml", admin_auth=False,
17-
use_password=False):
17+
use_password=False, raise_on_error=False):
1818

1919
""" Authenticate to the Zimbra server
2020
@@ -35,7 +35,9 @@ def authenticate(url, account, key, by='name', expires=0, timestamp=None,
3535
use_password)
3636
:param use_password: The "key"-parameter holds a password. Do a password-
3737
based user authentication.
38-
:return: The authentication token
38+
:param raise_on_error: Should I raise an exception when an authentication
39+
error occurs or just return None?
40+
:return: The authentication token or None
3941
:rtype: str or None or unicode
4042
"""
4143

@@ -99,15 +101,19 @@ def authenticate(url, account, key, by='name', expires=0, timestamp=None,
99101

100102
response = ResponseJson()
101103

102-
try:
104+
server.send_request(auth_request, response)
103105

104-
server.send_request(auth_request, response)
106+
if response.is_fault():
105107

106-
except HTTPError:
108+
if raise_on_error:
107109

108-
# A HTTPError (which is an AuthError in most cases) occured. Simply
109-
# return nothing
110+
raise Exception(
111+
"Cannot authenticate user: (%s) %s" % (
112+
response.get_fault_code(),
113+
response.get_fault_message()
114+
)
115+
)
110116

111117
return None
112118

113-
return response.get_response()['AuthResponse']['authToken']['_content']
119+
return response.get_response()['AuthResponse']['authToken']['_content']

tests/test_admin.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ def run_admin_test(self, request_type):
7676
if response.is_fault():
7777

7878
self.fail(
79-
"CreateAccount faulted. %s" % (response.get_response())
79+
"CreateAccount faulted. (%s) %s" % (
80+
response.get_fault_code(),
81+
response.get_fault_message()
82+
)
8083
)
8184

8285
account_id = response.get_response(
@@ -116,7 +119,10 @@ def run_admin_test(self, request_type):
116119
if response.is_fault():
117120

118121
self.fail(
119-
"Cannot remove test account. %s" % response.get_response()
122+
"Cannot remove test account: (%s) %s" % (
123+
response.get_fault_code(),
124+
response.get_fault_message()
125+
)
120126
)
121127

122128
def test_admin_xml(self):

tests/test_auth.py

+115-22
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,52 @@ def test_auth_xml(self):
3939
timestamp
4040
)
4141

42-
if response is None:
42+
self.assertNotEqual(
43+
response,
44+
None,
45+
"Authentication with the configured settings "
46+
"was not successful"
47+
)
48+
49+
def test_auth_failure_xml(self):
50+
51+
""" Send a configured auth request with a wrong password in XML format
52+
and check the result
53+
"""
54+
55+
config = get_config()
56+
57+
if config.getboolean('auth_test', 'enabled'):
58+
59+
# Run only if enabled
60+
61+
try:
62+
63+
timestamp = config.getint('auth_test', 'timestamp')
64+
65+
except ValueError:
66+
67+
# If timestamp is set to a none-integer, we'll just assume
68+
# that it's unset
4369

44-
self.fail("Authentication with the configured settings "
45-
"was not successful")
70+
timestamp = None
71+
72+
response = authenticate(
73+
config.get('auth_test', 'url'),
74+
config.get('auth_test', 'account'),
75+
config.get('auth_test', 'preauthkey') + "1234",
76+
config.get('auth_test', 'account_by'),
77+
config.getint('auth_test', 'expires'),
78+
timestamp
79+
)
80+
81+
self.assertEqual(
82+
response,
83+
None,
84+
"Authentication did not return 'None', but %s instead." % (
85+
response
86+
)
87+
)
4688

4789
def test_auth_json(self):
4890

@@ -77,10 +119,53 @@ def test_auth_json(self):
77119
request_type='json'
78120
)
79121

80-
if response is None:
122+
self.assertNotEqual(
123+
response,
124+
None,
125+
"Authentication with the configured settings "
126+
"was not successful"
127+
)
128+
129+
def test_auth_failure_json(self):
130+
131+
""" Send a configured auth request with a wrong password in json
132+
format and check the result
133+
"""
134+
135+
config = get_config()
136+
137+
if config.getboolean('auth_test', 'enabled'):
138+
139+
# Run only if enabled
140+
141+
try:
142+
143+
timestamp = config.getint('auth_test', 'timestamp')
144+
145+
except ValueError:
146+
147+
# If timestamp is set to a none-integer, we'll just assume
148+
# that it's unset
149+
150+
timestamp = None
151+
152+
response = authenticate(
153+
config.get('auth_test', 'url'),
154+
config.get('auth_test', 'account'),
155+
config.get('auth_test', 'preauthkey') + "1234",
156+
config.get('auth_test', 'account_by'),
157+
config.getint('auth_test', 'expires'),
158+
timestamp,
159+
request_type='json'
160+
)
81161

82-
self.fail("Authentication with the configured settings "
83-
"was not successful")
162+
self.assertEqual(
163+
response,
164+
None,
165+
"Authentication did not return 'None', but %s instead." % (
166+
response
167+
)
168+
)
84169

85170
def test_password_auth_xml(self):
86171

@@ -103,10 +188,12 @@ def test_password_auth_xml(self):
103188
request_type="xml"
104189
)
105190

106-
if response is None:
107-
108-
self.fail("Authentication with the configured settings "
109-
"was not ssuccessful")
191+
self.assertNotEqual(
192+
response,
193+
None,
194+
"Authentication with the configured settings "
195+
"was not ssuccessful"
196+
)
110197

111198
def test_password_auth_json(self):
112199

@@ -129,10 +216,12 @@ def test_password_auth_json(self):
129216
request_type="json"
130217
)
131218

132-
if response is None:
133-
134-
self.fail("Authentication with the configured settings "
135-
"was not ssuccessful")
219+
self.assertNotEqual(
220+
response,
221+
None,
222+
"Authentication with the configured settings "
223+
"was not ssuccessful"
224+
)
136225

137226
def test_admin_auth_xml(self):
138227

@@ -155,10 +244,12 @@ def test_admin_auth_xml(self):
155244
request_type="xml"
156245
)
157246

158-
if response is None:
159-
160-
self.fail("Authentication with the configured settings "
161-
"was not successful")
247+
self.assertNotEqual(
248+
response,
249+
None,
250+
"Authentication with the configured settings "
251+
"was not successful"
252+
)
162253

163254
def test_admin_auth_json(self):
164255

@@ -181,7 +272,9 @@ def test_admin_auth_json(self):
181272
request_type="json"
182273
)
183274

184-
if response is None:
185-
186-
self.fail("Authentication with the configured settings "
187-
"was not successful")
275+
self.assertNotEqual(
276+
response,
277+
None,
278+
"Authentication with the configured settings "
279+
"was not successful"
280+
)

tests/test_fault.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def test_fault_non_existing_folder_json(self):
6060

6161
comm.send_request(request, response)
6262

63-
self.checkResponse(
63+
self.check_response(
6464
response
6565
)
6666

@@ -111,7 +111,7 @@ def test_fault_non_existing_folder_batch_json(self):
111111

112112
comm.send_request(request, response)
113113

114-
self.checkResponse(
114+
self.check_response(
115115
response
116116
)
117117

@@ -148,7 +148,7 @@ def test_fault_non_existing_folder_xml(self):
148148

149149
comm.send_request(request, response)
150150

151-
self.checkResponse(
151+
self.check_response(
152152
response
153153
)
154154

@@ -199,18 +199,19 @@ def test_fault_non_existing_folder_batch_xml(self):
199199

200200
comm.send_request(request, response)
201201

202-
self.checkResponse(
202+
self.check_response(
203203
response
204204
)
205205

206-
def checkResponse(self, response):
206+
def check_response(self, response):
207207

208208
# Should be a fault
209209

210-
self.assertEqual(
211-
True,
212-
response.is_fault()
213-
)
210+
if not response.is_fault():
211+
212+
self.fail(
213+
"Response wasn't a fault: %s" % (response.get_response())
214+
)
214215

215216
config = get_config()
216217

0 commit comments

Comments
 (0)