File: test_v2_error.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 (141 lines) | stat: -rw-r--r-- 4,280 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
from __future__ import absolute_import, division, print_function

import json

import pytest

import stripe
from stripe import error
from tests.http_client_mock import HTTPClientMock


class TestV2Error(object):
    @pytest.fixture(scope="function")
    def stripe_client(self, http_client_mock):
        return stripe.StripeClient(
            api_key="keyinfo_test_123",
            http_client=http_client_mock.get_mock_http_client(),
        )

    def test_raises_v2_error(
        self,
        stripe_client: stripe.StripeClient,
        http_client_mock: HTTPClientMock,
    ):
        method = "get"
        path = "/v2/core/events/evt_123"

        error_response = {
            "error": {
                "type": "temporary_session_expired",
                "code": "session_bad",
                "message": "you messed up",
            }
        }
        http_client_mock.stub_request(
            method,
            path=path,
            rbody=json.dumps(error_response),
            rcode=400,
            rheaders={},
        )

        try:
            stripe_client.v2.core.events.retrieve("evt_123")
        except error.TemporarySessionExpiredError as e:
            assert e.code == "session_bad"
            assert e.error.code == "session_bad"
            assert e.error.message == "you messed up"
        else:
            assert False, "Should have raised a TemporarySessionExpiredError"

        http_client_mock.assert_requested(
            method,
            path=path,
            api_key="keyinfo_test_123",
        )

    @pytest.mark.skip("python doesn't have any errors with invalid params yet")
    def test_raises_v2_error_with_field(
        self,
        stripe_client: stripe.StripeClient,
        http_client_mock: HTTPClientMock,
    ):
        method = "post"
        path = "/v2/payment_methods/us_bank_accounts"

        error_response = {
            "error": {
                "type": "invalid_payment_method",
                "code": "invalid_us_bank_account",
                "message": "bank account is invalid",
                "invalid_param": "routing_number",
            }
        }
        http_client_mock.stub_request(
            method,
            path=path,
            rbody=json.dumps(error_response),
            rcode=400,
            rheaders={},
        )

        try:
            stripe_client.v2.payment_methods.us_bank_accounts.create(
                params={"account_number": "123", "routing_number": "456"}
            )
        except error.InvalidPaymentMethodError as e:
            assert e.invalid_param == "routing_number"
            assert e.error.code == "invalid_us_bank_account"
            assert e.error.message == "bank account is invalid"
        else:
            assert False, "Should have raised a InvalidUsBankAccountError"

        http_client_mock.assert_requested(
            method,
            path=path,
            api_key="keyinfo_test_123",
        )

    def test_falls_back_to_v1_error(
        self,
        stripe_client: stripe.StripeClient,
        http_client_mock: HTTPClientMock,
    ):
        method = "post"
        path = "/v2/billing/meter_events"

        error_response = {
            "error": {
                "code": "invalid_request",
                "message": "your request is invalid",
                "param": "invalid_param",
            }
        }
        http_client_mock.stub_request(
            method,
            path=path,
            rbody=json.dumps(error_response),
            rcode=400,
            rheaders={"request-id": "123"},
        )

        try:
            stripe_client.v2.billing.meter_events.create(
                {"event_name": "asdf", "payload": {}}
            )
        except error.InvalidRequestError as e:
            assert e.param == "invalid_param"
            assert repr(e) == (
                "InvalidRequestError(message='your request is invalid', "
                "param='invalid_param', code='invalid_request', "
                "http_status=400, request_id='123')"
            )
        else:
            assert False, "Should have raised a InvalidRequestError"

        http_client_mock.assert_requested(
            method,
            path=path,
            api_key="keyinfo_test_123",
        )