Skip to content
Newer
Older
100644 236 lines (185 sloc) 8.07 KB
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
1 import copy
2 import datetime
3 import json
4 import mock
5 from mock import patch
6 import unittest
7
04881f2 @alexjg Added cancel method to subscription
alexjg authored Apr 24, 2012
8 import fixtures
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
9 import gocardless
1950fd3 @alexjg Added create_bill method to PreAuthorization
alexjg authored Apr 24, 2012
10 from gocardless.resources import Resource, Subscription, Bill, PreAuthorization
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
11
3c12186 @hmarr pep8
hmarr authored Jul 9, 2012
12
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
13 class TestResource(Resource):
14 endpoint = "/testendpoint/:id"
15
16 def __init__(self, attrs, client):
17 attrs = create_mock_attrs(attrs)
18 Resource.__init__(self, attrs, client)
19
3c12186 @hmarr pep8
hmarr authored Jul 9, 2012
20
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
21 class TestSubResource(Resource):
22 endpoint = "/subresource/:id"
23
3c12186 @hmarr pep8
hmarr authored Jul 9, 2012
24
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
25 class OtherTestSubResource(Resource):
26 endpoint = "/subresource2/:id"
27
3c12186 @hmarr pep8
hmarr authored Jul 9, 2012
28
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
29 def create_mock_attrs(to_merge):
30 """
3c12186 @hmarr pep8
hmarr authored Jul 9, 2012
31 Creats an attribute set for creating a resource from,
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
32 includes the basic created, modified and id keys. Merges
33 that with to_merge
34 """
35 attrs = {
3c12186 @hmarr pep8
hmarr authored Jul 9, 2012
36 "created_at": "2012-04-18T17:53:12Z",
37 "id": "1",
38 "merchant_id": "amerchantid"
39 }
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
40 attrs.update(to_merge)
41 return attrs
42
3c12186 @hmarr pep8
hmarr authored Jul 9, 2012
43
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
44 class ResourceTestCase(unittest.TestCase):
45
46 def test_endpoint_declared_by_class(self):
47 resource = TestResource({"id":"1"}, None)
48 self.assertEqual(resource.get_endpoint(), "/testendpoint/1")
49
50 def test_resource_attributes(self):
51 attrs = {"key1":"one",
52 "key2":"two",
53 "key3":"three",
54 "id":"1"}
55 res = TestResource(attrs.copy(), None)
56 for key, value in attrs.items():
57 self.assertEqual(getattr(res, key), value)
58
a75cbe9 @alexjg Added date and reference fields to resources
alexjg authored Apr 23, 2012
59 def test_resource_created_at_is_date(self):
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
60 created = datetime.datetime.strptime('2012-04-18T17:53:12Z',\
61 "%Y-%m-%dT%H:%M:%SZ")
62 attrs = create_mock_attrs({"created_at":'2012-04-18T17:53:12Z',
63 "id":"1"})
64 res = TestResource(attrs, None)
65 self.assertEqual(res.created_at, created)
66
04f2452 @alexjg Added a create_bill method on client for billing preauths
alexjg authored Apr 24, 2012
67 def test_resources_with_equal_attrs_are_equal(self):
68 attrs = create_mock_attrs({
69 "someattr":"someval"
70 })
71 res1 = TestResource(attrs, None)
72 res2 = TestResource(attrs, None)
73 self.assertEqual(res1, res2)
74 self.assertEqual(hash(res1), hash(res2))
75
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
76 class ResourceSubresourceTestCase(unittest.TestCase):
77
78 def setUp(self):
3c12186 @hmarr pep8
hmarr authored Jul 9, 2012
79 self.resource = TestResource({"sub_resource_uris":
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
80 {"test_sub_resources":
81 "https://gocardless.com/api/v1/merchants/WOQRUJU9OH2HH1/bills?\
82 source_id=1580",
3c12186 @hmarr pep8
hmarr authored Jul 9, 2012
83 "other_test_sub_resources": "aurl"},
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
84 "id":"1"},
85 None)
86
87 def test_resource_lists_subresources(self):
1950fd3 @alexjg Added create_bill method to PreAuthorization
alexjg authored Apr 24, 2012
88 self.assertTrue(hasattr(self.resource, "test_sub_resources"))
89 self.assertTrue(callable(getattr(self.resource, "test_sub_resources")))
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
90
91 def test_resource_subresource_returns_subresource_instances(self):
92 mock_return = map(create_mock_attrs, [{"id":1},{"id":2}])
93 mock_client = mock.Mock()
94 mock_client.api_get.return_value = mock_return
95 self.resource.client = mock_client
1950fd3 @alexjg Added create_bill method to PreAuthorization
alexjg authored Apr 24, 2012
96 result = self.resource.test_sub_resources()
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
97 for res in result:
98 self.assertIsInstance(res, TestSubResource)
99 self.assertEqual(set([1,2]), set([item.id for item in result]))
100
101 def test_resource_is_correct_instance(self):
102 """
103 Expose an issue where the closure which creates sub_resource functions
104 in the `Resource` constructor does not close over the class name
105 correctly and thus every sub_resource function ends up referencing
106 the same class.
107 """
108 mock_client = mock.Mock()
109 mock_client.api_get.return_value = [create_mock_attrs({"id":"1"})]
110 self.resource.client = mock_client
1950fd3 @alexjg Added create_bill method to PreAuthorization
alexjg authored Apr 24, 2012
111 result = self.resource.test_sub_resources()
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
112 self.assertIsInstance(result[0], TestSubResource)
3c12186 @hmarr pep8
hmarr authored Jul 9, 2012
113
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
114
115 class FindResourceTestCase(unittest.TestCase):
116
117 def test_find_resource_by_id_with_client(self):
118 client = mock.Mock()
119 client.api_get.return_value = {"id":"1"}
120 resource = TestResource.find_with_client("1", client)
121 self.assertEqual(resource.id, "1")
122
123 def test_find_resource_without_details_throws_clienterror(self):
525b3a3 @alexjg Fixing http auth
alexjg authored Apr 26, 2012
124 old_client = gocardless.client
125 gocardless.client = None
e1d4362 @alexjg Implemented methods on client for OAuth flow
alexjg authored Apr 23, 2012
126 self.assertRaises(gocardless.exceptions.ClientError, TestResource.find, "1")
525b3a3 @alexjg Fixing http auth
alexjg authored Apr 26, 2012
127 gocardless.client = old_client
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
128
9c9a247 @alexjg Changed gocardless.global_client to gocardless.client
alexjg authored Apr 24, 2012
129 @patch('gocardless.client')
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
130 def test_find_resource_without_client(self, mock_client):
131 mock_client.api_get.return_value = {"id":"1"}
132 self.assertEqual(TestResource.find("1").id, "1")
133
134
135
a75cbe9 @alexjg Added date and reference fields to resources
alexjg authored Apr 23, 2012
136 class TestDateResource(Resource):
137 endpoint = "/dates"
138 date_fields = ["modified", "activated"]
139
140
04f2452 @alexjg Added a create_bill method on client for billing preauths
alexjg authored Apr 24, 2012
141 class DateResourceFieldTestCase(unittest.TestCase):
a75cbe9 @alexjg Added date and reference fields to resources
alexjg authored Apr 23, 2012
142
143 def test_date_fields_are_converted(self):
144 mod_date = datetime.datetime.strptime("2020-10-10T01:01:00", "%Y-%m-%dT%H:%M:%S")
145 act_date = datetime.datetime.strptime("2020-10-10T01:01:03", "%Y-%m-%dT%H:%M:%S")
146 params = {
3c12186 @hmarr pep8
hmarr authored Jul 9, 2012
147 "modified":mod_date.isoformat() + "Z",
148 "activated":act_date.isoformat() + "Z"
149 }
a75cbe9 @alexjg Added date and reference fields to resources
alexjg authored Apr 23, 2012
150 res = TestDateResource(create_mock_attrs(params), None)
151 self.assertEqual(res.modified, mod_date)
152 self.assertEqual(res.activated, act_date)
153
154
155 class TestReferenceResource(Resource):
156 endpoint = "/referencing"
04f2452 @alexjg Added a create_bill method on client for billing preauths
alexjg authored Apr 24, 2012
157 reference_fields = ["test_resource_id"]
158 date_fields = []
a75cbe9 @alexjg Added date and reference fields to resources
alexjg authored Apr 23, 2012
159
160 class ReferenceResourceTestCase(unittest.TestCase):
3c12186 @hmarr pep8
hmarr authored Jul 9, 2012
161
a75cbe9 @alexjg Added date and reference fields to resources
alexjg authored Apr 23, 2012
162 def test_reference_fields_are_converted(self):
163 params = create_mock_attrs({"test_resource_id":"2345"})
164 res = TestReferenceResource(params, None)
165 self.assertTrue(hasattr(res, "test_resource"))
166 self.assertTrue(callable, res.test_resource)
3c12186 @hmarr pep8
hmarr authored Jul 9, 2012
167
04f2452 @alexjg Added a create_bill method on client for billing preauths
alexjg authored Apr 24, 2012
168 def test_reference_function_calls_resource(self):
169 params = create_mock_attrs({"test_resource_id":"2345"})
170 res = TestReferenceResource(params, None)
3c12186 @hmarr pep8
hmarr authored Jul 9, 2012
171 with patch.object(TestResource,
04f2452 @alexjg Added a create_bill method on client for billing preauths
alexjg authored Apr 24, 2012
172 'find_with_client') as mock_res:
173 mock_res.return_value = "1234"
174 self.assertEqual("1234", res.test_resource())
175 mock_res.assert_called_with("2345", None)
176
177 def test_date_fields_inherited(self):
178 params = create_mock_attrs({"test_resource_id":"123"})
179 res = TestReferenceResource(params, None)
180 self.assertIsInstance(res.created_at, datetime.datetime)
a75cbe9 @alexjg Added date and reference fields to resources
alexjg authored Apr 23, 2012
181
04f2452 @alexjg Added a create_bill method on client for billing preauths
alexjg authored Apr 24, 2012
182 def test_date_with_null_attr_does_not_throw(self):
183 params = create_mock_attrs({"modified_at":None})
3c12186 @hmarr pep8
hmarr authored Jul 9, 2012
184 testclass = type("TestModResource", (Resource,),
04f2452 @alexjg Added a create_bill method on client for billing preauths
alexjg authored Apr 24, 2012
185 {"date_fields":["modified_at"]})
186 res = testclass(params, None)
a75cbe9 @alexjg Added date and reference fields to resources
alexjg authored Apr 23, 2012
187
c7e8c93 @alexjg Basic merchant functionality implemented
alexjg authored Apr 23, 2012
188
ec0b56b @alexjg Merging with devel
alexjg authored Apr 24, 2012
189 class SubscriptionCancelTestCase(unittest.TestCase):
190
191 def test_cancel_puts(self):
04881f2 @alexjg Added cancel method to subscription
alexjg authored Apr 24, 2012
192 client = mock.Mock()
193 sub = Subscription(fixtures.subscription_json, client)
194 sub.cancel()
195 client.api_put.assert_called_with("/subscriptions/{0}/cancel".format(
196 fixtures.subscription_json["id"]))
ec0b56b @alexjg Merging with devel
alexjg authored Apr 24, 2012
197
6e5b219 @alexjg Added cancel method to pre-auth and subscription resources
alexjg authored Aug 6, 2012
198 class PreAuthCancelTestCase(unittest.TestCase):
199
200 def test_cancel_puts(self):
201 client = mock.Mock()
202 preauth= PreAuthorization(fixtures.preauth_json, client)
203 preauth.cancel()
204 client.api_put.assert_called_with(
205 "/pre_authorizations/{0}/cancel".format(
206 fixtures.preauth_json["id"]))
207
ec0b56b @alexjg Merging with devel
alexjg authored Apr 24, 2012
208
1950fd3 @alexjg Added create_bill method to PreAuthorization
alexjg authored Apr 24, 2012
209 class PreAuthBillCreationTestCase(unittest.TestCase):
3c12186 @hmarr pep8
hmarr authored Jul 9, 2012
210
1950fd3 @alexjg Added create_bill method to PreAuthorization
alexjg authored Apr 24, 2012
211 def test_create_bill_calls_client_api_post(self):
212 client = mock.Mock()
213 client.api_post.return_value = fixtures.bill_json
214 result = Bill.create_under_preauth(10, "1234", client, name="aname",
215 description="adesc")
216 self.assertIsInstance(result, Bill)
217 expected_params = {
218 "bill":{
219 "amount":10,
220 "pre_authorization_id":"1234",
221 "name":"aname",
222 "description":"adesc"
223 }
224 }
225 client.api_post.assert_called_with("/bills", expected_params)
226
227 @patch('gocardless.resources.Bill')
228 def test_preauth_create_calls_bill_create(self, mock_bill_class):
229 pre_auth = PreAuthorization(fixtures.preauth_json, None)
230 pre_auth.create_bill(10, name="aname", description="adesc")
231 mock_bill_class.create_under_preauth.assert_called_with(10,
232 pre_auth.id, None, name="aname",
233 description="adesc")
234
235
Something went wrong with that request. Please try again.