| 1 |
import pytest |
| 2 |
import json |
| 3 |
|
| 4 |
from stripe._updateable_api_resource import UpdateableAPIResource |
| 5 |
|
| 6 |
|
| 7 |
class TestUpdateableAPIResource(object): |
| 8 |
class MyUpdateable(UpdateableAPIResource): |
| 9 |
OBJECT_NAME = "myupdateable" |
| 10 |
|
| 11 |
@pytest.fixture |
| 12 |
def obj(self, http_client_mock): |
| 13 |
http_client_mock.stub_request( |
| 14 |
"post", |
| 15 |
path="/v1/myupdateables/myid", |
| 16 |
rbody=json.dumps({"id": "myid", "thats": "it"}), |
| 17 |
rheaders={"request-id": "req_id"}, |
| 18 |
) |
| 19 |
|
| 20 |
return self.MyUpdateable.construct_from( |
| 21 |
{ |
| 22 |
"id": "myid", |
| 23 |
"foo": "bar", |
| 24 |
"baz": "boz", |
| 25 |
"metadata": {"size": "l", "score": 4, "height": 10}, |
| 26 |
"object": "obj", |
| 27 |
}, |
| 28 |
"mykey", |
| 29 |
) |
| 30 |
|
| 31 |
def checkSave(self, obj): |
| 32 |
assert obj is obj.save() |
| 33 |
|
| 34 |
assert obj.thats == "it" |
| 35 |
# TODO: Should we force id to be retained? |
| 36 |
# assert obj.id == 'myid' |
| 37 |
with pytest.raises(AttributeError): |
| 38 |
obj.baz |
| 39 |
|
| 40 |
def test_idempotent_save(self, http_client_mock, obj): |
| 41 |
obj.baz = "updated" |
| 42 |
obj.save(idempotency_key="foo") |
| 43 |
|
| 44 |
http_client_mock.assert_requested( |
| 45 |
"post", |
| 46 |
path="/v1/myupdateables/myid", |
| 47 |
post_data="baz=updated", |
| 48 |
idempotency_key="foo", |
| 49 |
) |
| 50 |
|
| 51 |
def test_save(self, http_client_mock, obj): |
| 52 |
obj.baz = "updated" |
| 53 |
obj.other = "newval" |
| 54 |
obj.metadata.size = "m" |
| 55 |
obj.metadata.info = "a2" |
| 56 |
obj.metadata.height = None |
| 57 |
|
| 58 |
self.checkSave(obj) |
| 59 |
|
| 60 |
http_client_mock.assert_requested( |
| 61 |
"post", |
| 62 |
path="/v1/myupdateables/myid", |
| 63 |
post_data="baz=updated&metadata[info]=a2&metadata[size]=m&other=newval", |
| 64 |
) |
| 65 |
|
| 66 |
assert obj.last_response is not None |
| 67 |
assert obj.last_response.request_id == "req_id" |
| 68 |
|
| 69 |
# Saving again should not cause any request. |
| 70 |
http_client_mock.reset_mock() |
| 71 |
self.checkSave(obj) |
| 72 |
http_client_mock.assert_no_request() |
| 73 |
|
| 74 |
# Setting the same value should cause a request. |
| 75 |
http_client_mock.reset_mock() |
| 76 |
http_client_mock.stub_request( |
| 77 |
"post", |
| 78 |
path="/v1/myupdateables/myid", |
| 79 |
rbody=json.dumps({"id": "myid", "thats": "it"}), |
| 80 |
) |
| 81 |
|
| 82 |
obj.thats = "it" |
| 83 |
self.checkSave(obj) |
| 84 |
|
| 85 |
http_client_mock.assert_requested( |
| 86 |
"post", path="/v1/myupdateables/myid", post_data="thats=it" |
| 87 |
) |
| 88 |
|
| 89 |
# Changing the value should cause a request. |
| 90 |
http_client_mock.reset_mock() |
| 91 |
http_client_mock.stub_request( |
| 92 |
"post", |
| 93 |
path="/v1/myupdateables/myid", |
| 94 |
rbody=json.dumps({"id": "myid", "thats": "it"}), |
| 95 |
) |
| 96 |
|
| 97 |
obj.id = "myid" |
| 98 |
obj.thats = "updated" |
| 99 |
self.checkSave(obj) |
| 100 |
|
| 101 |
http_client_mock.assert_requested( |
| 102 |
"post", path="/v1/myupdateables/myid", post_data="thats=updated" |
| 103 |
) |
| 104 |
|
| 105 |
def test_add_key_to_nested_object(self, http_client_mock, obj): |
| 106 |
acct = self.MyUpdateable.construct_from( |
| 107 |
{ |
| 108 |
"id": "myid", |
| 109 |
"legal_entity": {"size": "l", "score": 4, "height": 10}, |
| 110 |
}, |
| 111 |
"mykey", |
| 112 |
) |
| 113 |
|
| 114 |
acct.legal_entity["first_name"] = "bob" |
| 115 |
|
| 116 |
assert acct is acct.save() |
| 117 |
|
| 118 |
http_client_mock.assert_requested( |
| 119 |
"post", |
| 120 |
path="/v1/myupdateables/myid", |
| 121 |
post_data="legal_entity[first_name]=bob", |
| 122 |
) |
| 123 |
|
| 124 |
def test_save_nothing(self, http_client_mock, obj): |
| 125 |
acct = self.MyUpdateable.construct_from( |
| 126 |
{"id": "myid", "metadata": {"key": "value"}}, "mykey" |
| 127 |
) |
| 128 |
|
| 129 |
assert acct is acct.save() |
| 130 |
|
| 131 |
http_client_mock.assert_no_request() |
| 132 |
|
| 133 |
def test_replace_nested_object(self, http_client_mock, obj): |
| 134 |
acct = self.MyUpdateable.construct_from( |
| 135 |
{"id": "myid", "legal_entity": {"last_name": "smith"}}, "mykey" |
| 136 |
) |
| 137 |
|
| 138 |
acct.legal_entity = {"first_name": "bob"} |
| 139 |
|
| 140 |
assert acct is acct.save() |
| 141 |
|
| 142 |
http_client_mock.assert_requested( |
| 143 |
"post", |
| 144 |
path="/v1/myupdateables/myid", |
| 145 |
post_data="legal_entity[first_name]=bob&legal_entity[last_name]=", |
| 146 |
) |
| 147 |
|
| 148 |
def test_array_setting(self, http_client_mock, obj): |
| 149 |
acct = self.MyUpdateable.construct_from( |
| 150 |
{"id": "myid", "legal_entity": {}}, "mykey" |
| 151 |
) |
| 152 |
|
| 153 |
acct.legal_entity.additional_owners = [{"first_name": "Bob"}] |
| 154 |
|
| 155 |
assert acct is acct.save() |
| 156 |
|
| 157 |
http_client_mock.assert_requested( |
| 158 |
"post", |
| 159 |
path="/v1/myupdateables/myid", |
| 160 |
post_data="legal_entity[additional_owners][0][first_name]=Bob", |
| 161 |
) |
| 162 |
|
| 163 |
def test_array_none(self, http_client_mock, obj): |
| 164 |
acct = self.MyUpdateable.construct_from( |
| 165 |
{"id": "myid", "legal_entity": {"additional_owners": None}}, |
| 166 |
"mykey", |
| 167 |
) |
| 168 |
|
| 169 |
acct.foo = "bar" |
| 170 |
|
| 171 |
assert acct is acct.save() |
| 172 |
|
| 173 |
http_client_mock.assert_requested( |
| 174 |
"post", path="/v1/myupdateables/myid", post_data="foo=bar" |
| 175 |
) |
| 176 |
|
| 177 |
def test_array_insertion(self, http_client_mock, obj): |
| 178 |
acct = self.MyUpdateable.construct_from( |
| 179 |
{"id": "myid", "legal_entity": {"additional_owners": []}}, "mykey" |
| 180 |
) |
| 181 |
|
| 182 |
acct.legal_entity.additional_owners.append({"first_name": "Bob"}) |
| 183 |
|
| 184 |
assert acct is acct.save() |
| 185 |
|
| 186 |
http_client_mock.assert_requested( |
| 187 |
"post", |
| 188 |
path="/v1/myupdateables/myid", |
| 189 |
post_data="legal_entity[additional_owners][0][first_name]=Bob", |
| 190 |
) |
| 191 |
|
| 192 |
def test_array_update(self, http_client_mock, obj): |
| 193 |
acct = self.MyUpdateable.construct_from( |
| 194 |
{ |
| 195 |
"id": "myid", |
| 196 |
"legal_entity": { |
| 197 |
"additional_owners": [ |
| 198 |
{"first_name": "Bob"}, |
| 199 |
{"first_name": "Jane"}, |
| 200 |
] |
| 201 |
}, |
| 202 |
}, |
| 203 |
"mykey", |
| 204 |
) |
| 205 |
|
| 206 |
acct.legal_entity.additional_owners[1].first_name = "Janet" |
| 207 |
|
| 208 |
assert acct is acct.save() |
| 209 |
|
| 210 |
http_client_mock.assert_requested( |
| 211 |
"post", |
| 212 |
path="/v1/myupdateables/myid", |
| 213 |
post_data="legal_entity[additional_owners][1][first_name]=Janet", |
| 214 |
) |
| 215 |
|
| 216 |
def test_array_noop(self, http_client_mock, obj): |
| 217 |
acct = self.MyUpdateable.construct_from( |
| 218 |
{ |
| 219 |
"id": "myid", |
| 220 |
"legal_entity": {"additional_owners": [{"first_name": "Bob"}]}, |
| 221 |
"currencies_supported": ["usd", "cad"], |
| 222 |
}, |
| 223 |
"mykey", |
| 224 |
) |
| 225 |
|
| 226 |
assert acct is acct.save() |
| 227 |
|
| 228 |
http_client_mock.assert_requested( |
| 229 |
"post", |
| 230 |
path="/v1/myupdateables/myid", |
| 231 |
post_data="", |
| 232 |
) |
| 233 |
|
| 234 |
def test_hash_noop(self, http_client_mock, obj): |
| 235 |
acct = self.MyUpdateable.construct_from( |
| 236 |
{ |
| 237 |
"id": "myid", |
| 238 |
"legal_entity": {"address": {"line1": "1 Two Three"}}, |
| 239 |
}, |
| 240 |
"mykey", |
| 241 |
) |
| 242 |
|
| 243 |
assert acct is acct.save() |
| 244 |
|
| 245 |
http_client_mock.assert_no_request() |
| 246 |
|
| 247 |
def test_save_replace_metadata_with_number(self, http_client_mock, obj): |
| 248 |
obj.baz = "updated" |
| 249 |
obj.other = "newval" |
| 250 |
obj.metadata = 3 |
| 251 |
|
| 252 |
self.checkSave(obj) |
| 253 |
|
| 254 |
http_client_mock.assert_requested( |
| 255 |
"post", |
| 256 |
path="/v1/myupdateables/myid", |
| 257 |
post_data="baz=updated&metadata=3&other=newval", |
| 258 |
) |
| 259 |
|
| 260 |
def test_save_overwrite_metadata(self, http_client_mock, obj): |
| 261 |
obj.metadata = {} |
| 262 |
self.checkSave(obj) |
| 263 |
|
| 264 |
http_client_mock.assert_requested( |
| 265 |
"post", |
| 266 |
path="/v1/myupdateables/myid", |
| 267 |
post_data="metadata[size]=&metadata[score]=&metadata[height]=", |
| 268 |
) |
| 269 |
|
| 270 |
def test_save_replace_metadata(self, http_client_mock, obj): |
| 271 |
obj.baz = "updated" |
| 272 |
obj.other = "newval" |
| 273 |
obj.metadata = {"size": "m", "info": "a2", "score": 4} |
| 274 |
|
| 275 |
self.checkSave(obj) |
| 276 |
|
| 277 |
http_client_mock.assert_requested( |
| 278 |
"post", |
| 279 |
path="/v1/myupdateables/myid", |
| 280 |
post_data="baz=updated&metadata[info]=a2&metadata[size]=m&metadata[score]=4&other=newval", |
| 281 |
) |
| 282 |
|
| 283 |
def test_save_update_metadata(self, http_client_mock, obj): |
| 284 |
obj.baz = "updated" |
| 285 |
obj.other = "newval" |
| 286 |
obj.metadata.update({"size": "m", "info": "a2", "score": 4}) |
| 287 |
|
| 288 |
self.checkSave(obj) |
| 289 |
|
| 290 |
http_client_mock.assert_requested( |
| 291 |
"post", |
| 292 |
path="/v1/myupdateables/myid", |
| 293 |
post_data="baz=updated&metadata[info]=a2&metadata[size]=m&metadata[score]=4&other=newval", |
| 294 |
) |
| 295 |
|
| 296 |
def test_retrieve_and_update_with_stripe_version( |
| 297 |
self, http_client_mock, obj |
| 298 |
): |
| 299 |
http_client_mock.stub_request( |
| 300 |
"get", |
| 301 |
path="/v1/myupdateables/foo", |
| 302 |
rbody=json.dumps({"id": "foo", "bobble": "scrobble"}), |
| 303 |
) |
| 304 |
|
| 305 |
res = self.MyUpdateable.retrieve("foo", stripe_version="2017-08-15") |
| 306 |
|
| 307 |
http_client_mock.assert_requested(stripe_version="2017-08-15") |
| 308 |
|
| 309 |
http_client_mock.stub_request( |
| 310 |
"post", |
| 311 |
path="/v1/myupdateables/foo", |
| 312 |
rbody=json.dumps({"id": "foo", "bobble": "new_scrobble"}), |
| 313 |
) |
| 314 |
|
| 315 |
res.bobble = "new_scrobble" |
| 316 |
res.save() |
| 317 |
|
| 318 |
http_client_mock.assert_requested(stripe_version="2017-08-15") |
| 319 |
|
| 320 |
def test_modify_with_all_special_fields(self, http_client_mock, obj): |
| 321 |
http_client_mock.stub_request( |
| 322 |
"post", |
| 323 |
path="/v1/myupdateables/foo", |
| 324 |
rbody=json.dumps({"id": "foo", "bobble": "new_scrobble"}), |
| 325 |
rheaders={"Idempotency-Key": "IdempotencyKey"}, |
| 326 |
) |
| 327 |
|
| 328 |
self.MyUpdateable.modify( |
| 329 |
"foo", |
| 330 |
stripe_version="2017-08-15", |
| 331 |
api_key="APIKEY", |
| 332 |
idempotency_key="IdempotencyKey", |
| 333 |
stripe_account="Acc", |
| 334 |
bobble="new_scrobble", |
| 335 |
headers={"extra_header": "val"}, |
| 336 |
) |
| 337 |
|
| 338 |
http_client_mock.assert_requested( |
| 339 |
"post", |
| 340 |
path="/v1/myupdateables/foo", |
| 341 |
post_data="bobble=new_scrobble", |
| 342 |
stripe_version="2017-08-15", |
| 343 |
idempotency_key="IdempotencyKey", |
| 344 |
extra_headers={"extra_header": "val"}, |
| 345 |
) |