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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
import pytest
import stripe
class TestAPIResource(object):
class MyResource(stripe.APIResource):
OBJECT_NAME = "myresource"
class MyDeletableResource(stripe.DeletableAPIResource):
OBJECT_NAME = "myresource"
@classmethod
def my_method(cls, **params):
return cls._static_request(
"post",
cls.class_url(),
params=params,
)
@classmethod
async def my_method_async(cls, **params):
return cls.construct_from(
await cls._static_request_async(
"post",
cls.class_url(),
params=params,
),
stripe.api_key,
)
def test_retrieve_and_refresh(self, http_client_mock):
path = "/v1/myresources/foo%2A"
query_string = "myparam=5"
key = "sk_test_123"
stripe_version = "2018-02-28"
stripe_account = "acct_foo"
http_client_mock.stub_request(
"get",
path,
query_string,
'{"id": "foo2", "bobble": "scrobble"}',
rheaders={"request-id": "req_id"},
)
res = self.MyResource.retrieve(
"foo*",
myparam=5,
stripe_version="2018-02-28",
stripe_account="acct_foo",
)
http_client_mock.assert_requested(
"get",
path=path,
query_string=query_string,
api_key=key,
stripe_version=stripe_version,
stripe_account=stripe_account,
)
assert res.bobble == "scrobble"
assert res.id == "foo2"
assert res.api_key == key
assert res.stripe_version == stripe_version
assert res.stripe_account == stripe_account
assert res.last_response is not None
assert res.last_response.request_id == "req_id"
path = "/v1/myresources/foo2"
query_string = "myparam=5"
http_client_mock.stub_request(
"get", path, query_string, '{"frobble": 5}'
)
res = res.refresh()
http_client_mock.assert_requested(
"get",
path=path,
query_string=query_string,
api_key=key,
stripe_version=stripe_version,
stripe_account=stripe_account,
)
assert res.frobble == 5
with pytest.raises(KeyError):
res["bobble"]
def test_convert_to_stripe_object(self):
sample = {
"foo": "bar",
"adict": {"object": "charge", "id": 42, "amount": 7},
"alist": [{"object": "customer", "name": "chilango"}],
}
converted = stripe.util.convert_to_stripe_object(
sample, "akey", None, None, api_mode="V1"
)
# Types
assert isinstance(converted, stripe.stripe_object.StripeObject)
assert isinstance(converted.adict, stripe.Charge)
assert len(converted.alist) == 1
assert isinstance(converted.alist[0], stripe.Customer)
# Values
assert converted.foo == "bar"
assert converted.adict.id == 42
assert converted.alist[0].name == "chilango"
# Stripping
# TODO: We should probably be stripping out this property
# self.assertRaises(AttributeError, getattr, converted.adict, 'object')
def test_raise_on_incorrect_id_type(self):
for obj in [None, 1, 3.14, dict(), list(), set(), tuple(), object()]:
with pytest.raises(stripe.error.InvalidRequestError):
self.MyResource.retrieve(obj)
def test_class_methods_use_global_options(self, http_client_mock):
key = "newkey"
stripe_version = "2023-01-01"
stripe_account = "acct_foo"
resource = self.MyDeletableResource.construct_from(
{"id": "foo"},
key=key,
stripe_version=stripe_version,
stripe_account=stripe_account,
)
http_client_mock.stub_request(
"get",
"/v1/myresources/foo",
rbody='{"id": "foo"}',
)
resource.retrieve("foo")
http_client_mock.assert_requested(
"get",
path="/v1/myresources/foo",
api_key=stripe.api_key,
stripe_version=stripe.api_version,
extra_headers={"Stripe-Account": None},
)
http_client_mock.stub_request(
"post",
"/v1/myresources",
rbody='{"id": "foo", "object": "myresource"}',
)
self.MyDeletableResource.my_method()
http_client_mock.assert_requested(
"post",
path="/v1/myresources",
api_key=stripe.api_key,
stripe_version=stripe.api_version,
extra_headers={"Stripe-Account": None},
)
def test_class_method_prefers_method_arguments(self, http_client_mock):
http_client_mock.stub_request(
"get",
"/v1/myresources/foo",
rbody='{"id": "foo"}',
)
resource = self.MyResource.construct_from(
{"id": "foo"},
key="oldkey",
stripe_version="2000-01-01",
stripe_account="acct_foo",
)
resource.retrieve(
"foo",
api_key="newkey",
stripe_version="2023-01-01",
stripe_account="acct_bar",
)
http_client_mock.assert_requested(
"get",
path="/v1/myresources/foo",
api_key="newkey",
stripe_version="2023-01-01",
stripe_account="acct_bar",
)
def test_retrieve_forwards_options(self, http_client_mock):
http_client_mock.stub_request(
"get",
"/v1/myresources/foo",
rbody='{"id": "foo"}',
)
res = self.MyDeletableResource.retrieve(
"foo",
api_key="newkey",
stripe_version="2023-01-01",
stripe_account="foo",
)
http_client_mock.assert_requested(
"get",
path="/v1/myresources/foo",
api_key="newkey",
stripe_version="2023-01-01",
stripe_account="foo",
)
http_client_mock.stub_request(
"delete",
"/v1/myresources/foo",
)
res.delete()
http_client_mock.assert_requested(
"delete",
path="/v1/myresources/foo",
api_key="newkey",
stripe_version="2023-01-01",
stripe_account="foo",
)
def test_class_method_forwards_options(self, http_client_mock):
from stripe._object_classes import OBJECT_CLASSES
OBJECT_CLASSES["myresource"] = self.MyDeletableResource
http_client_mock.stub_request(
"post",
"/v1/myresources",
rbody='{"id": "foo", "object": "myresource"}',
)
res = self.MyDeletableResource.my_method(
api_key="newkey", stripe_version="2023-01-01", stripe_account="foo"
)
http_client_mock.assert_requested(
"post",
path="/v1/myresources",
api_key="newkey",
stripe_version="2023-01-01",
stripe_account="foo",
)
http_client_mock.stub_request(
"delete",
"/v1/myresources/foo",
)
res.delete()
http_client_mock.assert_requested(
"delete",
path="/v1/myresources/foo",
api_key="newkey",
stripe_version="2023-01-01",
stripe_account="foo",
)
del OBJECT_CLASSES["myresource"]
def test_instance_method_forwards_options(self, http_client_mock):
http_client_mock.stub_request(
"get",
"/v1/myresources/foo",
rbody='{"id": "foo"}',
)
key = "newkey"
stripe_version = "2023-01-01"
stripe_account = "acct_foo"
resource = self.MyResource.construct_from(
{"id": "foo"},
key=key,
stripe_version=stripe_version,
stripe_account=stripe_account,
)
resource.refresh()
http_client_mock.assert_requested(
"get",
path="/v1/myresources/foo",
api_key=key,
stripe_version=stripe_version,
stripe_account=stripe_account,
)
def test_instance_method_prefers_method_arguments(self, http_client_mock):
class MyDeletableResource(stripe.DeletableAPIResource):
OBJECT_NAME = "mydeletableresource"
http_client_mock.stub_request(
"delete",
"/v1/mydeletableresources/foo",
rbody='{"id": "foo"}',
)
resource = MyDeletableResource.construct_from(
{"id": "foo"},
key="oldkey",
stripe_version="2000-01-01",
stripe_account="acct_foo",
)
resource.delete(
api_key="newkey",
stripe_version="2023-01-01",
stripe_account="acct_bar",
)
http_client_mock.assert_requested(
"delete",
path="/v1/mydeletableresources/foo",
api_key="newkey",
stripe_version="2023-01-01",
stripe_account="acct_bar",
)
@pytest.mark.anyio
async def test_async_methods_succeed(self, http_client_mock):
http_client_mock.stub_request(
"post",
"/v1/myresources",
rbody='{"id": "foo", "object": "myresource"}',
)
resource = await self.MyDeletableResource.my_method_async()
http_client_mock.assert_requested(
"post",
path="/v1/myresources",
)
http_client_mock.stub_request(
"get",
"/v1/myresources/foo",
rbody='{"id": "foo", "object": "myresource"}',
)
await resource.refresh_async()
http_client_mock.assert_requested(
"get",
path="/v1/myresources/foo",
)
|