Check parameters are strings on SWF endpoints

SWF endpoints raise a 400 Bad Request for non-string types, and boto
doesn't enforce it as of today, so better have some safety nets in moto
to avoid this common mistake.

Example exception raised by Boto:

    SWFResponseError: SWFResponseError: 400 Bad Request
    {u'Message': u'class java.lang.Short can not be converted to an String',
    u'__type': u'com.amazon.coral.service#SerializationException'}
This commit is contained in:
Jean-Baptiste Barth 2015-09-30 11:16:02 +02:00
parent 8e3fd6c7de
commit 5392978eaf
3 changed files with 44 additions and 6 deletions

View File

@ -30,3 +30,13 @@ class SWFDomainDeprecatedFault(SWFClientError):
super(SWFDomainDeprecatedFault, self).__init__(
domain_name,
"com.amazonaws.swf.base.model#DomainDeprecatedFault")
class SWFSerializationException(JSONResponseError):
def __init__(self):
message = "class java.lang.Foo can not be converted to an String (not a real SWF exception)"
__type = "com.amazonaws.swf.base.model#SerializationException"
super(SWFSerializationException, self).__init__(
400, "Bad Request",
body={"Message": message, "__type": __type}
)

View File

@ -7,6 +7,7 @@ from .exceptions import (
SWFUnknownResourceFault,
SWFDomainAlreadyExistsFault,
SWFDomainDeprecatedFault,
SWFSerializationException,
)
@ -40,12 +41,21 @@ class SWFBackend(BaseBackend):
return matching[0]
return None
def _check_string(self, parameter):
if not isinstance(parameter, basestring):
raise SWFSerializationException()
def list_domains(self, status):
self._check_string(status)
return [domain for domain in self.domains
if domain.status == status]
def register_domain(self, name, workflow_execution_retention_period_in_days,
description=None):
self._check_string(name)
self._check_string(workflow_execution_retention_period_in_days)
if description:
self._check_string(description)
if self._get_domain(name, ignore_empty=True):
raise SWFDomainAlreadyExistsFault(name)
domain = Domain(name, workflow_execution_retention_period_in_days,
@ -53,12 +63,14 @@ class SWFBackend(BaseBackend):
self.domains.append(domain)
def deprecate_domain(self, name):
self._check_string(name)
domain = self._get_domain(name)
if domain.status == "DEPRECATED":
raise SWFDomainDeprecatedFault(name)
domain.status = "DEPRECATED"
def describe_domain(self, name):
self._check_string(name)
return self._get_domain(name)

View File

@ -7,6 +7,7 @@ from moto.swf.exceptions import (
SWFUnknownResourceFault,
SWFDomainAlreadyExistsFault,
SWFDomainDeprecatedFault,
SWFSerializationException,
)
@ -15,7 +16,7 @@ from moto.swf.exceptions import (
@mock_swf
def test_register_domain():
conn = boto.connect_swf("the_key", "the_secret")
conn.register_domain("test-domain", 60, description="A test domain")
conn.register_domain("test-domain", "60", description="A test domain")
all_domains = conn.list_domains("REGISTERED")
domain = all_domains["domainInfos"][0]
@ -27,10 +28,10 @@ def test_register_domain():
@mock_swf
def test_register_already_existing_domain():
conn = boto.connect_swf("the_key", "the_secret")
conn.register_domain("test-domain", 60, description="A test domain")
conn.register_domain("test-domain", "60", description="A test domain")
with assert_raises(SWFDomainAlreadyExistsFault) as err:
conn.register_domain("test-domain", 60, description="A test domain")
conn.register_domain("test-domain", "60", description="A test domain")
ex = err.exception
ex.status.should.equal(400)
@ -40,12 +41,27 @@ def test_register_already_existing_domain():
"message": "test-domain"
})
@mock_swf
def test_register_with_wrong_parameter_type():
conn = boto.connect_swf("the_key", "the_secret")
with assert_raises(SWFSerializationException) as err:
conn.register_domain("test-domain", 60, description="A test domain")
ex = err.exception
ex.status.should.equal(400)
ex.error_code.should.equal("SerializationException")
ex.body.should.equal({
"__type": "com.amazonaws.swf.base.model#SerializationException",
"Message": "class java.lang.Foo can not be converted to an String (not a real SWF exception)"
})
# DeprecateDomain endpoint
@mock_swf
def test_deprecate_domain():
conn = boto.connect_swf("the_key", "the_secret")
conn.register_domain("test-domain", 60, description="A test domain")
conn.register_domain("test-domain", "60", description="A test domain")
conn.deprecate_domain("test-domain")
all_domains = conn.list_domains("DEPRECATED")
@ -56,7 +72,7 @@ def test_deprecate_domain():
@mock_swf
def test_deprecate_already_deprecated_domain():
conn = boto.connect_swf("the_key", "the_secret")
conn.register_domain("test-domain", 60, description="A test domain")
conn.register_domain("test-domain", "60", description="A test domain")
conn.deprecate_domain("test-domain")
with assert_raises(SWFDomainDeprecatedFault) as err:
@ -89,7 +105,7 @@ def test_deprecate_non_existent_domain():
@mock_swf
def test_describe_domain():
conn = boto.connect_swf("the_key", "the_secret")
conn.register_domain("test-domain", 60, description="A test domain")
conn.register_domain("test-domain", "60", description="A test domain")
domain = conn.describe_domain("test-domain")
domain["configuration"]["workflowExecutionRetentionPeriodInDays"].should.equal("60")