Debian Codegraph @

tests/api_resources/abstract/test_api_resource.py

raw
1 from unittest import mock
2 import pytest
3
4 import stripe
5 from stripe._util import convert_to_stripe_object
6 from stripe._stripe_object import StripeObject
7 from stripe._error import InvalidRequestError
8
9
10 class TestAPIResource(object):
11 class MyResource(stripe.APIResource):
12 OBJECT_NAME = "myresource"
13
14 class MyDeletableResource(stripe.DeletableAPIResource):
15 OBJECT_NAME = "myresource"
16
17 @classmethod
18 def my_method(cls, **params):
19 return cls._static_request(
20 "post",
21 cls.class_url(),
22 params=params,
23 )
24
25 @classmethod
26 async def my_method_async(cls, **params):
27 return cls.construct_from(
28 await cls._static_request_async(
29 "post",
30 cls.class_url(),
31 params=params,
32 ),
33 stripe.api_key,
34 )
35
36 def test_retrieve_and_refresh(self, http_client_mock):
37 path = "/v1/myresources/foo%2A"
38 query_string = "myparam=5"
39 key = "sk_test_123"
40 stripe_version = "2018-02-28"
41 stripe_account = "acct_foo"
42 http_client_mock.stub_request(
43 "get",
44 path,
45 query_string,
46 '{"id": "foo2", "bobble": "scrobble"}',
47 rheaders={"request-id": "req_id"},
48 )
49
50 res = self.MyResource.retrieve(
51 "foo*",
52 myparam=5,
53 stripe_version="2018-02-28",
54 stripe_account="acct_foo",
55 )
56
57 http_client_mock.assert_requested(
58 "get",
59 path=path,
60 query_string=query_string,
61 api_key=key,
62 stripe_version=stripe_version,
63 stripe_account=stripe_account,
64 )
65 assert res.bobble == "scrobble"
66 assert res.id == "foo2"
67 assert res.api_key == key
68 assert res.stripe_version == stripe_version
69 assert res.stripe_account == stripe_account
70
71 assert res.last_response is not None
72 assert res.last_response.request_id == "req_id"
73
74 path = "/v1/myresources/foo2"
75 query_string = "myparam=5"
76 http_client_mock.stub_request(
77 "get", path, query_string, '{"frobble": 5}'
78 )
79
80 res = res.refresh()
81
82 http_client_mock.assert_requested(
83 "get",
84 path=path,
85 query_string=query_string,
86 api_key=key,
87 stripe_version=stripe_version,
88 stripe_account=stripe_account,
89 )
90 assert res.frobble == 5
91 with pytest.raises(KeyError):
92 res["bobble"]
93
94 def test_convert_to_stripe_object(self):
95 sample = {
96 "foo": "bar",
97 "adict": {"object": "charge", "id": 42, "amount": 7},
98 "alist": [{"object": "customer", "name": "chilango"}],
99 }
100
101 converted = convert_to_stripe_object(
102 sample, "akey", None, None, api_mode="V1"
103 )
104
105 # Types
106 assert isinstance(converted, StripeObject)
107 assert isinstance(converted.adict, stripe.Charge)
108 assert len(converted.alist) == 1
109 assert isinstance(converted.alist[0], stripe.Customer)
110
111 # Values
112 assert converted.foo == "bar"
113 assert converted.adict.id == 42
114 assert converted.alist[0].name == "chilango"
115
116 # Stripping
117 # TODO: We should probably be stripping out this property
118 # self.assertRaises(AttributeError, getattr, converted.adict, 'object')
119
120 def test_raise_on_incorrect_id_type(self):
121 for obj in [None, 1, 3.14, dict(), list(), set(), tuple(), object()]:
122 with pytest.raises(InvalidRequestError):
123 self.MyResource.retrieve(obj)
124
125 def test_class_methods_use_global_options(self, http_client_mock):
126 key = "newkey"
127 stripe_version = "2023-01-01"
128 stripe_account = "acct_foo"
129
130 resource = self.MyDeletableResource.construct_from(
131 {"id": "foo"},
132 key=key,
133 stripe_version=stripe_version,
134 stripe_account=stripe_account,
135 )
136
137 http_client_mock.stub_request(
138 "get",
139 "/v1/myresources/foo",
140 rbody='{"id": "foo"}',
141 )
142
143 resource.retrieve("foo")
144
145 http_client_mock.assert_requested(
146 "get",
147 path="/v1/myresources/foo",
148 api_key=stripe.api_key,
149 stripe_version=stripe.api_version,
150 extra_headers={"Stripe-Account": None},
151 )
152
153 http_client_mock.stub_request(
154 "post",
155 "/v1/myresources",
156 rbody='{"id": "foo", "object": "myresource"}',
157 )
158
159 self.MyDeletableResource.my_method()
160
161 http_client_mock.assert_requested(
162 "post",
163 path="/v1/myresources",
164 api_key=stripe.api_key,
165 stripe_version=stripe.api_version,
166 extra_headers={"Stripe-Account": None},
167 )
168
169 def test_class_method_prefers_method_arguments(self, http_client_mock):
170 http_client_mock.stub_request(
171 "get",
172 "/v1/myresources/foo",
173 rbody='{"id": "foo"}',
174 )
175
176 resource = self.MyResource.construct_from(
177 {"id": "foo"},
178 key="oldkey",
179 stripe_version="2000-01-01",
180 stripe_account="acct_foo",
181 )
182
183 resource.retrieve(
184 "foo",
185 api_key="newkey",
186 stripe_version="2023-01-01",
187 stripe_account="acct_bar",
188 )
189
190 http_client_mock.assert_requested(
191 "get",
192 path="/v1/myresources/foo",
193 api_key="newkey",
194 stripe_version="2023-01-01",
195 stripe_account="acct_bar",
196 )
197
198 def test_retrieve_forwards_options(self, http_client_mock):
199 http_client_mock.stub_request(
200 "get",
201 "/v1/myresources/foo",
202 rbody='{"id": "foo"}',
203 )
204
205 res = self.MyDeletableResource.retrieve(
206 "foo",
207 api_key="newkey",
208 stripe_version="2023-01-01",
209 stripe_account="foo",
210 )
211
212 http_client_mock.assert_requested(
213 "get",
214 path="/v1/myresources/foo",
215 api_key="newkey",
216 stripe_version="2023-01-01",
217 stripe_account="foo",
218 )
219
220 http_client_mock.stub_request(
221 "delete",
222 "/v1/myresources/foo",
223 )
224
225 res.delete()
226
227 http_client_mock.assert_requested(
228 "delete",
229 path="/v1/myresources/foo",
230 api_key="newkey",
231 stripe_version="2023-01-01",
232 stripe_account="foo",
233 )
234
235 @mock.patch("stripe._object_classes.get_object_class")
236 def test_class_method_forwards_options(
237 self, mock_get_object_class, http_client_mock
238 ):
239 mock_get_object_class.return_value = self.MyDeletableResource
240
241 http_client_mock.stub_request(
242 "post",
243 "/v1/myresources",
244 rbody='{"id": "foo", "object": "myresource"}',
245 )
246
247 res = self.MyDeletableResource.my_method(
248 api_key="newkey", stripe_version="2023-01-01", stripe_account="foo"
249 )
250
251 http_client_mock.assert_requested(
252 "post",
253 path="/v1/myresources",
254 api_key="newkey",
255 stripe_version="2023-01-01",
256 stripe_account="foo",
257 )
258
259 http_client_mock.stub_request(
260 "delete",
261 "/v1/myresources/foo",
262 )
263
264 res.delete()
265
266 http_client_mock.assert_requested(
267 "delete",
268 path="/v1/myresources/foo",
269 api_key="newkey",
270 stripe_version="2023-01-01",
271 stripe_account="foo",
272 )
273
274 def test_instance_method_forwards_options(self, http_client_mock):
275 http_client_mock.stub_request(
276 "get",
277 "/v1/myresources/foo",
278 rbody='{"id": "foo"}',
279 )
280
281 key = "newkey"
282 stripe_version = "2023-01-01"
283 stripe_account = "acct_foo"
284
285 resource = self.MyResource.construct_from(
286 {"id": "foo"},
287 key=key,
288 stripe_version=stripe_version,
289 stripe_account=stripe_account,
290 )
291
292 resource.refresh()
293
294 http_client_mock.assert_requested(
295 "get",
296 path="/v1/myresources/foo",
297 api_key=key,
298 stripe_version=stripe_version,
299 stripe_account=stripe_account,
300 )
301
302 def test_instance_method_prefers_method_arguments(self, http_client_mock):
303 class MyDeletableResource(stripe.DeletableAPIResource):
304 OBJECT_NAME = "mydeletableresource"
305
306 http_client_mock.stub_request(
307 "delete",
308 "/v1/mydeletableresources/foo",
309 rbody='{"id": "foo"}',
310 )
311
312 resource = MyDeletableResource.construct_from(
313 {"id": "foo"},
314 key="oldkey",
315 stripe_version="2000-01-01",
316 stripe_account="acct_foo",
317 )
318
319 resource.delete(
320 api_key="newkey",
321 stripe_version="2023-01-01",
322 stripe_account="acct_bar",
323 )
324
325 http_client_mock.assert_requested(
326 "delete",
327 path="/v1/mydeletableresources/foo",
328 api_key="newkey",
329 stripe_version="2023-01-01",
330 stripe_account="acct_bar",
331 )
332
333 @pytest.mark.anyio
334 async def test_async_methods_succeed(self, http_client_mock):
335 http_client_mock.stub_request(
336 "post",
337 "/v1/myresources",
338 rbody='{"id": "foo", "object": "myresource"}',
339 )
340 resource = await self.MyDeletableResource.my_method_async()
341 http_client_mock.assert_requested(
342 "post",
343 path="/v1/myresources",
344 )
345
346 http_client_mock.stub_request(
347 "get",
348 "/v1/myresources/foo",
349 rbody='{"id": "foo", "object": "myresource"}',
350 )
351 await resource.refresh_async()
352 http_client_mock.assert_requested(
353 "get",
354 path="/v1/myresources/foo",
355 )