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
|
# mypy: ignore-errors
"""Additional tests for response serialization.
While there are compliance tests in ``test_protocols.py``, where the majority
of the response serialization is tested, this file contains additional tests
that go above and beyond the specification(s) for a number of reasons:
* We are testing Python-specific behavior that doesn't make sense as a
compliance test.
* We are testing behavior that is not strictly part of the specification.
These may result in a coverage gap that would otherwise be untested.
"""
import time
from xmltodict import parse
from moto.core.exceptions import ServiceException
from moto.core.model import ServiceModel, ShapeResolver
from moto.core.serialize import (
AttributePickerContext,
JSONSerializer,
QuerySerializer,
XFormedAttributePicker,
never_return,
return_if_not_empty,
url_encode,
)
def test_aws_query_compatible_error() -> None:
model = {
"metadata": {
"protocol": "json",
"apiVersion": "2014-01-01",
"awsQueryCompatible": {},
},
"documentation": "",
"operations": {
"TestOperation": {
"name": "TestOperation",
"http": {
"method": "POST",
"requestUri": "/",
},
}
},
"shapes": {
"QueryCompatibleError": {
"type": "structure",
"members": {
"message": {"shape": "StringType"},
},
"error": {
"code": "PreservedErrorCode",
"httpStatusCode": 409,
"senderFault": True,
},
"exception": True,
},
"StringType": {
"type": "string",
},
},
}
class TestError(ServiceException):
pass
service_model = ServiceModel(model)
operation_model = service_model.operation_model("TestOperation")
serializer = JSONSerializer(operation_model)
serialized = serializer.serialize(TestError("PreservedErrorCode", "test-message"))
assert serialized["status_code"] == 409
headers = serialized["headers"]
assert headers.get("x-amzn-query-error") == "PreservedErrorCode;Sender"
def test_serialize_from_object() -> None:
model = {
"metadata": {"protocol": "query", "apiVersion": "2014-01-01"},
"documentation": "",
"operations": {
"TestOperation": {
"name": "TestOperation",
"http": {
"method": "POST",
"requestUri": "/",
},
"output": {"shape": "OutputShape"},
}
},
"shapes": {
"OutputShape": {
"type": "structure",
"members": {
"string": {"shape": "StringType"},
},
},
"StringType": {
"type": "string",
},
},
}
class TestObject:
string = "test-string"
service_model = ServiceModel(model)
operation_model = service_model.operation_model("TestOperation")
serializer = QuerySerializer(operation_model)
serialized = serializer.serialize(TestObject())
assert serialized
def test_datetime_with_microseconds() -> None:
model = {
"metadata": {"protocol": "query", "apiVersion": "2014-01-01"},
"documentation": "",
"operations": {
"TestOperation": {
"name": "TestOperation",
"http": {
"method": "POST",
"requestUri": "/",
},
"output": {"shape": "OutputShape"},
}
},
"shapes": {
"OutputShape": {
"type": "structure",
"members": {
"microseconds": {"shape": "TimestampType"},
},
},
"TimestampType": {
"type": "timestamp",
},
},
}
class TestObject:
microseconds = time.time()
service_model = ServiceModel(model)
operation_model = service_model.operation_model("TestOperation")
serializer = QuerySerializer(operation_model)
serialized = serializer.serialize(TestObject())
assert serialized
parsed = parse(serialized["body"])
time_str = parsed["TestOperationResponse"]["OutputShapeResult"]["microseconds"]
assert "." in time_str
assert time_str[-1] == "Z"
def test_pretty_print_with_short_elements_and_list() -> None:
model = {
"metadata": {"protocol": "query", "apiVersion": "2014-01-01"},
"documentation": "",
"operations": {
"TestOperation": {
"name": "TestOperation",
"http": {
"method": "POST",
"requestUri": "/",
},
"output": {"shape": "OutputShape"},
}
},
"shapes": {
"OutputShape": {
"type": "structure",
"members": {
"DBInstances": {
"shape": "DBInstanceList",
}
},
},
"DBInstanceList": {
"type": "list",
"member": {"shape": "DBInstance", "locationName": "DBInstance"},
},
"DBInstance": {
"type": "structure",
"members": {
"DBInstanceIdentifier": {
"shape": "String",
}
},
},
"String": {"type": "string"},
},
}
service_model = ServiceModel(model)
operation_model = service_model.operation_model("TestOperation")
serializer = QuerySerializer(operation_model, **{"pretty_print": True})
empty_list_to_serialize = {"DBInstances": []}
serialized = serializer.serialize(empty_list_to_serialize)
expected_shortened_list_element = "<DBInstances/>"
assert expected_shortened_list_element in serialized["body"]
class TestXFormedAttributePicker:
picker = XFormedAttributePicker()
def test_missing_attribute(self):
obj = {}
ctx = AttributePickerContext(obj, "Attribute", None)
value = self.picker(ctx)
assert value is None
def test_serialization_key(self):
shape_map = {
"Foo": {
"type": "structure",
"members": {
"Baz": {"shape": "StringType", "locationName": "other"},
},
},
"StringType": {"type": "string"},
}
resolver = ShapeResolver(shape_map)
shape = resolver.resolve_shape_ref(
{"shape": "StringType", "locationName": "other"}
)
assert shape
obj = {"other": "found me"}
ctx = AttributePickerContext(obj, "StringType", shape)
value = self.picker(ctx)
assert value == "found me"
def test_short_key(self):
class Role:
id = "unique-identifier"
ctx = AttributePickerContext(Role(), "RoleId", None)
value = self.picker(ctx)
assert value == "unique-identifier"
def test_transformed_key(self):
class DBInstance:
db_instance_class = "t2.medium"
ctx = AttributePickerContext(DBInstance(), "DBInstanceClass", None)
value = self.picker(ctx)
assert value == "t2.medium"
def test_untransformed_key(self):
obj = {"PascalCasedAttr": True}
ctx = AttributePickerContext(obj, "PascalCasedAttr", None)
value = self.picker(ctx)
assert value is True
def test_key_path_traversal(self):
child = {"Child": True}
parent = {"Parent": child}
ctx = AttributePickerContext(parent, "Parent.Child", None)
value = self.picker(ctx)
assert value is True
def test_explicit_key_alias(self):
class DBInstance:
class Meta:
serialization_aliases = {"DBInstanceClass": "db_instance_klass"}
db_instance_klass = "t2.medium"
ctx = AttributePickerContext(DBInstance(), "DBInstanceClass", None)
value = self.picker(ctx)
assert value == "t2.medium"
class TestResponseAttributeTransformers:
def test_never_return(self):
assert never_return(None) is None
assert never_return(True) is None
assert never_return(False) is None
assert never_return("Test") is None
def test_return_if_not_empty(self):
assert return_if_not_empty([]) is None
assert return_if_not_empty(None) is None
assert return_if_not_empty("Test") == "Test"
assert return_if_not_empty({}) is None
assert return_if_not_empty("") is None
assert return_if_not_empty({"key": "value"}) == {"key": "value"}
assert return_if_not_empty([0, 1, 2]) == [0, 1, 2]
def test_url_encode(self):
assert url_encode(None) is None
assert url_encode("Test") == "Test"
assert url_encode("Test String") == "Test%20String"
|