Skip to content

Commit

Permalink
* Correct handling of SOAP-requests (issue #7)
Browse files Browse the repository at this point in the history
* Reworked test configuration file
  • Loading branch information
dploeger committed Sep 11, 2014
1 parent 39ae006 commit b2d0a80
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 84 deletions.
24 changes: 13 additions & 11 deletions pythonzimbra/communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,24 @@ def send_request(self, request, response):
urllib2.HTTPError
"""

server_request = urllib2.urlopen(
self.url,
request.get_request(),
self.timeout
)

try:

server_response = server_request.read()
server_request = urllib2.urlopen(
self.url,
request.get_request(),
self.timeout
)

response.set_response(server_request.read())

except urllib2.HTTPError as e:

# Return the exception to the caller on HTTPerrors
if e.code == 500:

# 500 codes normally returns a SoapFault, that we can use

raise e
response.set_response(e.fp.read())

# Find the response for
else:

response.set_response(server_response)
raise e
3 changes: 2 additions & 1 deletion pythonzimbra/response.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Zimbra response access. """
import re


class Response(object):
Expand Down Expand Up @@ -106,7 +107,7 @@ def is_fault(self):

my_response = self.get_response()

if my_response.keys()[0] == 'Fault':
if my_response.keys()[0] == "Fault":

return True

Expand Down
31 changes: 23 additions & 8 deletions pythonzimbra/tools/xmlserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,19 @@ def dom_to_dict(root_node):
:rtype: dict
"""

# Remove namespaces from tagname

tag = root_node.tagName

if ":" in tag:

tag = tag.split(":")[1]

root_dict = {
root_node.tagName: {}
tag: {}
}

node_dict = root_dict[root_node.tagName]
node_dict = root_dict[tag]

# Set attributes

Expand All @@ -89,20 +97,27 @@ def dom_to_dict(root_node):
else:

subnode_dict = dom_to_dict(child)
new_val = subnode_dict[child.tagName]

child_tag = child.tagName

if ":" in child_tag:

child_tag = child_tag.split(":")[1]

new_val = subnode_dict[child_tag]

# If we have several child with same name, put them in a list.

if node_dict.has_key(child.tagName):
prev_val = node_dict[child.tagName]
if node_dict.has_key(child_tag):
prev_val = node_dict[child_tag]

if type(prev_val) != list:
node_dict[child.tagName] = [prev_val]
node_dict[child_tag] = [prev_val]

node_dict[child.tagName].append(new_val)
node_dict[child_tag].append(new_val)

else:
node_dict[child.tagName] = new_val
node_dict[child_tag] = new_val

return root_dict

Expand Down
75 changes: 33 additions & 42 deletions tests/config.ini.dist
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
# Additional configuration for tests

# Testing user authentication using preauth key
# These default values are used in every section if not overridden

[auth_test]
[DEFAULT]

# The URL of the zimbra soap service
url=None

# The account to be logged in using preauth
# The user account to be logged in using preauth or password auth
account=None

# The password of the user
password=None

# The preauth key of the domain the account is member of
preauthkey=None

# What is in "account"? A name, an ID, a ForeignPrincipal?
account_by=name

# The URL of the zimbra admin-soap service
admin_url=None

# The admin account to be logged in
admin_account=None

# The password of the admin account
admin_password=None

# What is in admin "account"? A name, an ID, a ForeignPrincipal?
admin_account_by=name

# Testing user authentication using preauth key

[auth_test]

# When does the token expire?
expires=0

Expand All @@ -30,18 +49,6 @@ enabled=False

[auth_by_password_test]

# The URL of the zimbra soap service
url=None

# The account to be logged in using password based authentication
account=None

# The password of the account
password=None

# What is in "account"? A name, an ID, a ForeignPrincipal?
account_by=name

# Is the auth testing enabled? Set this to true if you have configured the
# other values!
enabled=False
Expand All @@ -50,18 +57,6 @@ enabled=False

[admin_auth_test]

# The URL of the zimbra admin-soap service
url=None

# The admin account to be logged in
account=None

# The password of the admin account
password=None

# What is in "account"? A name, an ID, a ForeignPrincipal?
account_by=name

# Is the auth testing enabled? Set this to true if you have configured the
# other values!
enabled=False
Expand All @@ -70,27 +65,23 @@ enabled=False

[admin_request_test]

# The URL of the zimbra admin-soap service
url=None

# The admin account to be logged in
account=None

# The password of the admin account
password=None

# What is in "account"? A name, an ID, a ForeignPrincipal?
account_by=name

# What URL to use for testing the authentication of the new user
user_url=None

# What account can be created and removed for testing purposes?
test_account=None

# The password of that account
test_password=None

# Is the auth testing enabled? Set this to true if you have configured the
# other values!
enabled=False

# Testing a fault

[fault_test]

# Non-existent folder
folder=None

# Is the auth testing enabled? Set this to true if you have configured the
# other values!
enabled=False
14 changes: 7 additions & 7 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def run_admin_test(self, request_type):
# Run only if enabled

token = authenticate(
config.get("admin_request_test", "url"),
config.get("admin_request_test", "account"),
config.get("admin_request_test", "password"),
config.get("admin_request_test", "account_by"),
config.get("admin_request_test", "admin_url"),
config.get("admin_request_test", "admin_account"),
config.get("admin_request_test", "admin_password"),
config.get("admin_request_test", "admin_account_by"),
admin_auth=True,
request_type=request_type
)
Expand All @@ -39,7 +39,7 @@ def run_admin_test(self, request_type):

# Create an account

comm = Communication(config.get("admin_request_test", "url"))
comm = Communication(config.get("admin_request_test", "admin_url"))

if request_type == "xml":

Expand Down Expand Up @@ -85,7 +85,7 @@ def run_admin_test(self, request_type):
# Try to log in as the new account

user_token = authenticate(
config.get("admin_request_test", "user_url"),
config.get("admin_request_test", "url"),
config.get("admin_request_test", "test_account"),
config.get("admin_request_test", "test_password"),
"name",
Expand Down Expand Up @@ -133,4 +133,4 @@ def test_admin_json(self):
it afterwards. Assumes, that this works correctly (in json format)
"""

self.run_admin_test("xml")
self.run_admin_test("json")
16 changes: 8 additions & 8 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ def test_admin_auth_xml(self):
# Run only if enabled

response = authenticate(
config.get("admin_auth_test", "url"),
config.get("admin_auth_test", "account"),
config.get("admin_auth_test", "password"),
config.get("admin_auth_test", "account_by"),
config.get("admin_auth_test", "admin_url"),
config.get("admin_auth_test", "admin_account"),
config.get("admin_auth_test", "admin_password"),
config.get("admin_auth_test", "admin_account_by"),
admin_auth=True,
request_type="xml"
)
Expand All @@ -173,10 +173,10 @@ def test_admin_auth_json(self):
# Run only if enabled

response = authenticate(
config.get("admin_auth_test", "url"),
config.get("admin_auth_test", "account"),
config.get("admin_auth_test", "password"),
config.get("admin_auth_test", "account_by"),
config.get("admin_auth_test", "admin_url"),
config.get("admin_auth_test", "admin_account"),
config.get("admin_auth_test", "admin_password"),
config.get("admin_auth_test", "admin_account_by"),
admin_auth=True,
request_type="json"
)
Expand Down
Loading

0 comments on commit b2d0a80

Please sign in to comment.