-
Notifications
You must be signed in to change notification settings - Fork 20
/
test_chargify.py
204 lines (162 loc) · 8.45 KB
/
test_chargify.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
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
import unittest
from chargify import Chargify, ChargifyError
class ChargifyHttpClientStub(object):
def make_request(self, url, method, data, api_key):
return url, method, data
class ChargifyTestCase(unittest.TestCase):
def setUp(self):
self.chargify = Chargify('api_key','subdomain',client=ChargifyHttpClientStub())
def assertResult(self, result, expected_url, expected_method, expected_data):
"""
A little helper method to help verify that the correct URL, HTTP method, and POST data are
being constructed from the Chargify API.
"""
url, method, data = result
print url, method, data
self.assertEqual(url,expected_url)
self.assertEqual(method,expected_method)
self.assertEqual(data,expected_data)
class TestCustomers(ChargifyTestCase):
def test_construct_request(self):
# List
result = self.chargify.customers()
self.assertResult(result,'https://subdomain.chargify.com/customers.json','GET',None)
# Read/show (via chargify id)
result = self.chargify.customers(customer_id=123)
self.assertResult(result,'https://subdomain.chargify.com/customers/123.json','GET',None)
# Read/show (via reference value)
result = self.chargify.customers.lookup(reference=123)
self.assertResult(result,'https://subdomain.chargify.com/customers/lookup.json?reference=123','GET',None)
# Create
result = self.chargify.customers.create(data={
'customer':{
'first_name':'Joe',
'last_name':'Blow',
'email':'joe@example.com'
}
})
self.assertResult(result,'https://subdomain.chargify.com/customers.json','POST',
'{"customer": {"first_name": "Joe", "last_name": "Blow", "email": "joe@example.com"}}')
# Edit/update
result = self.chargify.customers.update(customer_id=123,data={
'customer':{
'email':'joe@example.com'
}
})
self.assertResult(result,'https://subdomain.chargify.com/customers/123.json','PUT',
'{"customer": {"email": "joe@example.com"}}')
# Delete
result = self.chargify.customers.delete(customer_id=123)
self.assertResult(result,'https://subdomain.chargify.com/customers/123.json','DELETE',None)
class TestProducts(ChargifyTestCase):
def test_construct_request(self):
chargify = Chargify('api_key','subdomain')
# List
result = self.chargify.products()
self.assertResult(result,'https://subdomain.chargify.com/products.json','GET',None)
# Read/show (via chargify id)
result = self.chargify.products(product_id=123)
self.assertResult(result,'https://subdomain.chargify.com/products/123.json','GET',None)
# Read/show (via api handle)
result = self.chargify.products.handle(handle='myhandle')
self.assertResult(result,'https://subdomain.chargify.com/products/handle/myhandle.json','GET',None)
class TestSubscriptions(ChargifyTestCase):
def test_construct_request(self):
chargify = Chargify('api_key','subdomain')
# List
result = self.chargify.customers.subscriptions(customer_id=123)
self.assertResult(result,'https://subdomain.chargify.com/customers/123/subscriptions.json','GET',None)
# Read
result = self.chargify.subscriptions(subscription_id=123)
self.assertResult(result,'https://subdomain.chargify.com/subscriptions/123.json','GET',None)
# Create
result = self.chargify.subscriptions.create(data={
'subscription':{
'product_handle':'my_product',
'customer_attributes':{
'first_name':'Joe',
'last_name':'Blow',
'email':'joe@example.com'
},
'credit_card_attributes':{
'full_number':'1',
'expiration_month':'10',
'expiration_year':'2020'
}
}
})
self.assertResult(result,'https://subdomain.chargify.com/subscriptions.json','POST',
'{"subscription": {"product_handle": "my_product", "credit_card_attributes": {"expiration_month": "10", "full_number": "1", "expiration_year": "2020"}, "customer_attributes": {"first_name": "Joe", "last_name": "Blow", "email": "joe@example.com"}}}')
# Update
result = self.chargify.subscriptions.update(subscription_id=123,data={
'subscription':{
'credit_card_attributes':{
'full_number':'2',
'expiration_month':'10',
'expiration_year':'2030'
}
}
})
self.assertResult(result,'https://subdomain.chargify.com/subscriptions/123.json','PUT',
'{"subscription": {"credit_card_attributes": {"expiration_month": "10", "full_number": "2", "expiration_year": "2030"}}}')
# Delete
result = self.chargify.subscriptions.delete(subscription_id=123,data={
'subscription':{
'cancellation_message':'Goodbye!'
}
})
self.assertResult(result,'https://subdomain.chargify.com/subscriptions/123.json','DELETE',
'{"subscription": {"cancellation_message": "Goodbye!"}}')
class TestCharges(ChargifyTestCase):
def test_construct_request(self):
chargify = Chargify('api_key','subdomain')
# Create
result = self.chargify.subscriptions.charges.create(subscription_id=123,data={
'charge':{
'amount':'1.00',
'memo':'This is the description of the one time charge.'
}
})
self.assertResult(result,'https://subdomain.chargify.com/subscriptions/123/charges.json','POST',
'{"charge": {"amount": "1.00", "memo": "This is the description of the one time charge."}}')
class TestComponents(ChargifyTestCase):
def test_construct_request(self):
chargify = Chargify('api_key','subdomain')
# List
result = self.chargify.subscriptions.components.usages(subscription_id=123,component_id=456)
self.assertResult(result,'https://subdomain.chargify.com/subscriptions/123/components/456/usages.json','GET',None)
# Create
result = self.chargify.subscriptions.components.usages.create(subscription_id=123,component_id=456,data={
'usage':{
'quantity':5,
'memo':'My memo'
}
})
self.assertResult(result,'https://subdomain.chargify.com/subscriptions/123/components/456/usages.json','POST',
'{"usage": {"memo": "My memo", "quantity": 5}}')
class TestMigrations(ChargifyTestCase):
def test_construct_request(self):
chargify = Chargify('api_key','subdomain')
# Create
result = self.chargify.subscriptions.migrations.create(subscription_id=123,data={
'product_id':1234
})
self.assertResult(result,'https://subdomain.chargify.com/subscriptions/123/migrations.json','POST',
'{"product_id": 1234}')
class TestReactivate(ChargifyTestCase):
def test_construct_request(self):
chargify = Chargify('api_key','subdomain')
# Reactivate
result = self.chargify.subscriptions.reactivate.update(subscription_id=123)
self.assertResult(result,'https://subdomain.chargify.com/subscriptions/123/reactivate.json','PUT',None)
class TestTransactions(ChargifyTestCase):
def test_construct_request(self):
chargify = Chargify('api_key','subdomain')
# List transactions for a site
result = self.chargify.transactions()
self.assertResult(result,'https://subdomain.chargify.com/transactions.json','GET',None)
# List transactions for a subscription
result = self.chargify.subscriptions.transactions(subscription_id=123)
self.assertResult(result,'https://subdomain.chargify.com/subscriptions/123/transactions.json','GET',None)
if __name__ == "__main__":
unittest.main()