-
Notifications
You must be signed in to change notification settings - Fork 443
/
Copy pathtest_util.py
154 lines (135 loc) · 5.59 KB
/
test_util.py
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
import sys
from collections import namedtuple
import stripe
from stripe import util
LogTestCase = namedtuple("LogTestCase", "env flag should_output")
FmtTestCase = namedtuple("FmtTestCase", "props expected")
class TestUtil(object):
DUMMY_REQ_ID = "req_qsxPhqHyLxcoaM"
def test_test_apikey(self, mocker):
mocker.patch("stripe.api_key", "sk_test_KOWobxXidxNlIx")
link = util.dashboard_link(self.DUMMY_REQ_ID)
assert (
link
== "https://dashboard.stripe.com/test/logs/" + self.DUMMY_REQ_ID
)
def test_live_apikey(self, mocker):
mocker.patch("stripe.api_key", "sk_live_axwITqZSgTUXSN")
link = util.dashboard_link(self.DUMMY_REQ_ID)
assert (
link
== "https://dashboard.stripe.com/live/logs/" + self.DUMMY_REQ_ID
)
def test_no_apikey(self, mocker):
mocker.patch("stripe.api_key", None)
link = util.dashboard_link(self.DUMMY_REQ_ID)
assert (
link
== "https://dashboard.stripe.com/test/logs/" + self.DUMMY_REQ_ID
)
def test_old_apikey(self, mocker):
mocker.patch("stripe.api_key", "axwITqZSgTUXSN")
link = util.dashboard_link(self.DUMMY_REQ_ID)
assert (
link
== "https://dashboard.stripe.com/test/logs/" + self.DUMMY_REQ_ID
)
def log_test_loop(self, test_cases, logging_func, logger_name, mocker):
for case in test_cases:
try:
logger_mock = mocker.patch(logger_name)
print_mock = mocker.patch("builtins.print")
mocker.patch("stripe.log", case.flag)
mocker.patch("stripe._util.STRIPE_LOG", case.env)
logging_func("foo \nbar", y=3) # function under test
if case.should_output:
print_mock.assert_called_once_with(
"message='foo \\nbar' y=3", file=sys.stderr
)
else:
print_mock.assert_not_called()
logger_mock.assert_called_once_with("message='foo \\nbar' y=3")
finally:
mocker.stopall()
def test_log_debug(self, mocker):
# (STRIPE_LOG, stripe.log): should_output?
test_cases = [
LogTestCase(env=None, flag=None, should_output=False),
LogTestCase(env=None, flag="debug", should_output=True),
LogTestCase(env=None, flag="info", should_output=False),
LogTestCase(env="debug", flag=None, should_output=True),
LogTestCase(env="debug", flag="debug", should_output=True),
LogTestCase(env="debug", flag="info", should_output=False),
LogTestCase(env="info", flag=None, should_output=False),
LogTestCase(env="info", flag="debug", should_output=True),
LogTestCase(env="info", flag="info", should_output=False),
]
self.log_test_loop(
test_cases,
logging_func=util.log_debug,
logger_name="stripe.util.logger.debug",
mocker=mocker,
)
def test_log_info(self, mocker):
# (STRIPE_LOG, stripe.log): should_output?
test_cases = [
LogTestCase(env=None, flag=None, should_output=False),
LogTestCase(env=None, flag="debug", should_output=True),
LogTestCase(env=None, flag="info", should_output=True),
LogTestCase(env="debug", flag=None, should_output=True),
LogTestCase(env="debug", flag="debug", should_output=True),
LogTestCase(env="debug", flag="info", should_output=True),
LogTestCase(env="info", flag=None, should_output=True),
LogTestCase(env="info", flag="debug", should_output=True),
LogTestCase(env="info", flag="info", should_output=True),
]
self.log_test_loop(
test_cases,
logging_func=util.log_info,
logger_name="stripe.util.logger.info",
mocker=mocker,
)
def test_logfmt(self):
cases = [
FmtTestCase(
props={"foo": "bar", "message": "yes"},
expected="foo=bar message=yes",
),
FmtTestCase(props={"foo": "bar baz"}, expected="foo='bar baz'"),
FmtTestCase(
props={"b": 2, "a": 1, "c": 3}, expected="a=1 b=2 c=3"
),
FmtTestCase(
props={"key with space": True},
expected="'key with space'=True",
),
]
for case in cases:
result = util.logfmt(case.props)
assert result == case.expected
def test_convert_to_stripe_object_and_back(self):
resp = {
"object": "balance",
"available": [
{
"amount": 234,
"currency": "usd",
"source_types": {"card": 234},
}
],
"livemode": False,
}
obj = util.convert_to_stripe_object(resp, api_mode="V1")
assert isinstance(obj, stripe.Balance)
assert isinstance(obj.available, list)
assert isinstance(obj.available[0], stripe.stripe_object.StripeObject)
d = util.convert_to_dict(obj)
assert isinstance(d, dict)
assert isinstance(d["available"], list)
assert isinstance(d["available"][0], dict)
assert d == resp
def test_sanitize_id(self):
sanitized_id = util.sanitize_id("cu %x 123")
if isinstance(sanitized_id, bytes):
sanitized_id = sanitized_id.decode("utf-8", "strict")
assert sanitized_id == "cu++%25x+123"