File: test_custom_method.py

package info (click to toggle)
python-stripe 12.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,864 kB
  • sloc: python: 157,573; makefile: 13; sh: 9
file content (303 lines) | stat: -rw-r--r-- 10,404 bytes parent folder | download
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
import stripe
from stripe import util


class TestCustomMethod(object):
    @stripe.api_resources.abstract.custom_method(
        "do_stuff", http_verb="post", http_path="do_the_thing"
    )
    @stripe.api_resources.abstract.custom_method(
        "do_list_stuff", http_verb="get", http_path="do_the_list_thing"
    )
    @stripe.api_resources.abstract.custom_method(
        "do_stream_stuff",
        http_verb="post",
        http_path="do_the_stream_thing",
        is_streaming=True,
    )
    class MyResource(stripe.api_resources.abstract.APIResource):
        OBJECT_NAME = "myresource"

        def do_stuff(self, idempotency_key=None, **params):
            url = self.instance_url() + "/do_the_thing"
            self._request_and_refresh(
                "post", url, {**params, "idempotency_key": idempotency_key}
            )
            return self

        def do_stream_stuff(self, idempotency_key=None, **params):
            url = self.instance_url() + "/do_the_stream_thing"
            return self._request_stream(
                "post", url, {**params, "idempotency_key": idempotency_key}
            )

        @classmethod
        def _cls_do_stuff_new_codegen(cls, id, **params):
            return cls._static_request(
                "post",
                "/v1/myresources/{id}/do_the_thing".format(
                    id=util.sanitize_id(id)
                ),
                params=params,
            )

        @util.class_method_variant("_cls_do_stuff_new_codegen")
        def do_stuff_new_codegen(self, **params):
            return self._request(
                "post",
                "/v1/myresources/{id}/do_the_thing".format(
                    id=util.sanitize_id(self.get("id"))
                ),
                params=params,
            )

    def test_call_custom_method_class(self, http_client_mock):
        http_client_mock.stub_request(
            "post",
            path="/v1/myresources/mid/do_the_thing",
            rbody='{"id": "mid", "thing_done": true}',
            rheaders={"request-id": "req_id"},
        )

        obj = self.MyResource.do_stuff("mid", foo="bar")

        http_client_mock.assert_requested(
            "post",
            path="/v1/myresources/mid/do_the_thing",
            post_data="foo=bar",
        )
        assert obj.thing_done is True

    def test_call_custom_list_method_class_paginates(self, http_client_mock):
        http_client_mock.stub_request(
            "get",
            path="/v1/myresources/mid/do_the_list_thing",
            query_string="param1=abc&param2=def",
            rbody='{"object": "list", "url": "/v1/myresources/mid/do_the_list_thing", "has_more": true, "data": [{"id": "cus_1", "object": "customer"}, {"id": "cus_2", "object": "customer"}]}',
            rheaders={"request-id": "req_123"},
        )

        resp = self.MyResource.do_list_stuff("mid", param1="abc", param2="def")

        http_client_mock.assert_requested(
            "get",
            path="/v1/myresources/mid/do_the_list_thing",
            query_string="param1=abc&param2=def",
        )

        # Stub out the second request which will happen automatically.
        http_client_mock.stub_request(
            "get",
            path="/v1/myresources/mid/do_the_list_thing",
            query_string="param1=abc&param2=def&starting_after=cus_2",
            rbody='{"object": "list", "url": "/v1/myresources/mid/do_the_list_thing", "has_more": false, "data": [{"id": "cus_3", "object": "customer"}]}',
            rheaders={"request-id": "req_123"},
        )

        # Iterate through entire content
        ids = []
        for i in resp.auto_paging_iter():
            ids.append(i.id)

        # Explicitly assert that the pagination parameter were kept for the
        # second request along with the starting_after param.
        http_client_mock.assert_requested(
            "get",
            path="/v1/myresources/mid/do_the_list_thing",
            query_string="param1=abc&param2=def&starting_after=cus_2",
        )

        assert ids == ["cus_1", "cus_2", "cus_3"]

    def test_call_custom_stream_method_class(self, http_client_mock):
        http_client_mock.stub_request(
            "post",
            path="/v1/myresources/mid/do_the_stream_thing",
            rbody=util.io.BytesIO(str.encode("response body")),
            rheaders={"request-id": "req_id"},
        )

        resp = self.MyResource.do_stream_stuff("mid", foo="bar")

        http_client_mock.assert_requested(
            "post",
            path="/v1/myresources/mid/do_the_stream_thing",
            post_data="foo=bar",
        )

        body_content = resp.io.read()
        if hasattr(body_content, "decode"):
            body_content = body_content.decode("utf-8")

        assert body_content == "response body"

    def test_call_custom_method_class_with_object(self, http_client_mock):
        http_client_mock.stub_request(
            "post",
            path="/v1/myresources/mid/do_the_thing",
            rbody='{"id": "mid", "thing_done": true}',
            rheaders={"request-id": "req_id"},
        )

        obj = self.MyResource.construct_from({"id": "mid"}, "mykey")
        self.MyResource.do_stuff(obj, foo="bar")

        http_client_mock.assert_requested(
            "post",
            path="/v1/myresources/mid/do_the_thing",
            post_data="foo=bar",
        )
        assert obj.thing_done is True

    def test_call_custom_stream_method_class_with_object(
        self, http_client_mock
    ):
        http_client_mock.stub_request(
            "post",
            path="/v1/myresources/mid/do_the_stream_thing",
            rbody=util.io.BytesIO(str.encode("response body")),
            rheaders={"request-id": "req_id"},
        )

        obj = self.MyResource.construct_from({"id": "mid"}, "mykey")
        resp = self.MyResource.do_stream_stuff(obj, foo="bar")

        http_client_mock.assert_requested(
            "post",
            path="/v1/myresources/mid/do_the_stream_thing",
            post_data="foo=bar",
        )

        body_content = resp.io.read()
        if hasattr(body_content, "decode"):
            body_content = body_content.decode("utf-8")

        assert body_content == "response body"

    def test_call_custom_method_instance(self, http_client_mock):
        http_client_mock.stub_request(
            "post",
            path="/v1/myresources/mid/do_the_thing",
            rbody='{"id": "mid", "thing_done": true}',
            rheaders={"request-id": "req_id"},
        )

        obj = self.MyResource.construct_from({"id": "mid"}, "mykey")
        obj.do_stuff(foo="bar")

        http_client_mock.assert_requested(
            "post",
            path="/v1/myresources/mid/do_the_thing",
            post_data="foo=bar",
        )
        assert obj.thing_done is True

    def test_call_custom_stream_method_instance(self, http_client_mock):
        http_client_mock.stub_request(
            "post",
            path="/v1/myresources/mid/do_the_stream_thing",
            rbody=util.io.BytesIO(str.encode("response body")),
            rheaders={"request-id": "req_id"},
        )

        obj = self.MyResource.construct_from({"id": "mid"}, "mykey")
        resp = obj.do_stream_stuff(foo="bar")

        http_client_mock.assert_requested(
            "post",
            path="/v1/myresources/mid/do_the_stream_thing",
            post_data="foo=bar",
        )

        body_content = resp.io.read()
        if hasattr(body_content, "decode"):
            body_content = body_content.decode("utf-8")

        assert body_content == "response body"

    def test_call_custom_method_class_special_fields(self, http_client_mock):
        http_client_mock.stub_request(
            "post",
            path="/v1/myresources/mid/do_the_thing",
            rbody='{"id": "mid", "thing_done": true}',
            rheaders={"request-id": "req_id"},
        )

        self.MyResource.do_stuff(
            "mid",
            foo="bar",
            stripe_version="2017-08-15",
            api_key="APIKEY",
            idempotency_key="IdempotencyKey",
            stripe_account="Acc",
        )

        http_client_mock.assert_requested(
            "post",
            path="/v1/myresources/mid/do_the_thing",
            post_data="foo=bar",
            api_key="APIKEY",
            stripe_version="2017-08-15",
            stripe_account="Acc",
            idempotency_key="IdempotencyKey",
        )

    def test_call_custom_method_class_newcodegen_special_fields(
        self, http_client_mock
    ):
        http_client_mock.stub_request(
            "post",
            path="/v1/myresources/mid/do_the_thing",
            rbody='{"id": "mid", "thing_done": true}',
            rheaders={"request-id": "req_id"},
        )

        self.MyResource.do_stuff_new_codegen(
            "mid",
            foo="bar",
            stripe_version="2017-08-15",
            api_key="APIKEY",
            idempotency_key="IdempotencyKey",
            stripe_account="Acc",
        )

        http_client_mock.assert_requested(
            "post",
            path="/v1/myresources/mid/do_the_thing",
            post_data="foo=bar",
            api_key="APIKEY",
            stripe_version="2017-08-15",
            stripe_account="Acc",
            idempotency_key="IdempotencyKey",
        )

    def test_call_custom_method_instance_newcodegen_special_fields(
        self, http_client_mock
    ):
        http_client_mock.stub_request(
            "post",
            path="/v1/myresources/mid/do_the_thing",
            rbody='{"id": "mid", "thing_done": true}',
            rheaders={"request-id": "req_id"},
        )

        obj = self.MyResource.construct_from({"id": "mid"}, "mykey")
        obj.do_stuff_new_codegen(
            foo="bar",
            stripe_version="2017-08-15",
            api_key="APIKEY",
            idempotency_key="IdempotencyKey",
            stripe_account="Acc",
            headers={"extra_header": "val"},
        )

        http_client_mock.assert_requested(
            "post",
            path="/v1/myresources/mid/do_the_thing",
            post_data="foo=bar",
            api_key="APIKEY",
            stripe_version="2017-08-15",
            stripe_account="Acc",
            idempotency_key="IdempotencyKey",
            extra_headers={"extra_header": "val"},
        )