|
1
|
|
|
try: # pragma: no cover |
|
2
|
|
|
from unittest.mock import patch |
|
3
|
|
|
except ImportError: # pragma: no cover |
|
4
|
|
|
from mock import patch |
|
5
|
|
|
|
|
6
|
|
|
import pytest |
|
7
|
|
|
|
|
8
|
|
|
import besepa |
|
9
|
|
|
from besepa.resource import Create, Delete, Find, List, Post, Resource, Update |
|
10
|
|
|
|
|
11
|
|
|
besepa.configure(api_key='dummy') |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
class TestResource(object): |
|
15
|
|
|
def test_getter(self): |
|
16
|
|
|
data = { |
|
17
|
|
|
'name': 'testing', |
|
18
|
|
|
'amount': 10.0, |
|
19
|
|
|
'transaction': {'description': 'testing'}, |
|
20
|
|
|
'items': [{'name': 'testing'}] |
|
21
|
|
|
} |
|
22
|
|
|
resource = Resource(data) |
|
23
|
|
|
assert resource.name == 'testing' |
|
24
|
|
|
assert resource['name'] == 'testing' |
|
25
|
|
|
assert resource.amount == 10.0 |
|
26
|
|
|
assert resource.items[0].__class__ == Resource |
|
27
|
|
|
assert resource.items[0].name == 'testing' |
|
28
|
|
|
assert resource.items[0]['name'] == 'testing' |
|
29
|
|
|
assert resource.unknown is None |
|
30
|
|
|
with pytest.raises(KeyError): |
|
31
|
|
|
resource['unknown'] |
|
32
|
|
|
|
|
33
|
|
|
def test_setter(self): |
|
34
|
|
|
data = {'name': 'testing'} |
|
35
|
|
|
resource = Resource(data) |
|
36
|
|
|
assert resource.name == 'testing' |
|
37
|
|
|
resource.name = 'changed' |
|
38
|
|
|
assert resource.name == 'changed' |
|
39
|
|
|
resource['name'] = 'again-changed' |
|
40
|
|
|
assert resource.name == 'again-changed' |
|
41
|
|
|
resource.transaction = {'description': 'testing'} |
|
42
|
|
|
assert resource.transaction.__class__ == Resource |
|
43
|
|
|
assert resource.transaction.description == 'testing' |
|
44
|
|
|
|
|
45
|
|
|
def test_to_dict(self): |
|
46
|
|
|
data = { |
|
47
|
|
|
"intent": "sale", |
|
48
|
|
|
"payer": { |
|
49
|
|
|
"payment_method": "credit_card", |
|
50
|
|
|
"funding_instruments": [{ |
|
51
|
|
|
"credit_card": { |
|
52
|
|
|
"type": "visa", |
|
53
|
|
|
"number": "4417119669820331", |
|
54
|
|
|
"expire_month": "11", |
|
55
|
|
|
"expire_year": "2018", |
|
56
|
|
|
"cvv2": "874", |
|
57
|
|
|
"first_name": "Joe", |
|
58
|
|
|
"last_name": "Shopper" |
|
59
|
|
|
} |
|
60
|
|
|
}] |
|
61
|
|
|
}, "transactions": [{ |
|
62
|
|
|
"item_list": { |
|
63
|
|
|
"items": [{ |
|
64
|
|
|
"name": "item", |
|
65
|
|
|
"sku": "item", |
|
66
|
|
|
"price": "1.00", |
|
67
|
|
|
"currency": "USD", |
|
68
|
|
|
"quantity": 1 |
|
69
|
|
|
}] |
|
70
|
|
|
}, "amount": { |
|
71
|
|
|
"total": "1.00", |
|
72
|
|
|
"currency": "USD" |
|
73
|
|
|
}, "description": "This is the payment transaction description." |
|
74
|
|
|
}] |
|
75
|
|
|
} |
|
76
|
|
|
resource = Resource(data) |
|
77
|
|
|
assert resource.to_dict() == data |
|
78
|
|
|
|
|
79
|
|
|
def test_http_headers(self): |
|
80
|
|
|
data = { |
|
81
|
|
|
'name': 'testing', |
|
82
|
|
|
'header': {'My-Header': 'testing'} |
|
83
|
|
|
} |
|
84
|
|
|
resource = Resource(data) |
|
85
|
|
|
assert resource.header == {'My-Header': 'testing'} |
|
86
|
|
|
assert resource.http_headers() == {'My-Header': 'testing'} |
|
87
|
|
|
|
|
88
|
|
|
def test_passing_api(self): |
|
89
|
|
|
""" |
|
90
|
|
|
Check that api objects are passed on to new resources when given |
|
91
|
|
|
""" |
|
92
|
|
|
|
|
93
|
|
|
class DummyAPI(object): |
|
94
|
|
|
def post(self, *a, **k): pass |
|
95
|
|
|
|
|
96
|
|
|
def get(self, *a, **k): pass |
|
97
|
|
|
|
|
98
|
|
|
api = DummyAPI() |
|
99
|
|
|
|
|
100
|
|
|
# Conversion |
|
101
|
|
|
resource = Resource({'name': 'testing', }, api=api) |
|
102
|
|
|
assert resource.api == api |
|
103
|
|
|
convert_ret = resource.convert('test', {}) |
|
104
|
|
|
assert convert_ret.api == api |
|
105
|
|
|
|
|
106
|
|
|
class TestResource(Find, List, Post): |
|
107
|
|
|
path = '/' |
|
108
|
|
|
|
|
109
|
|
|
# Find |
|
110
|
|
|
find = TestResource.find('resourceid', api=api) |
|
111
|
|
|
assert find.api == api |
|
112
|
|
|
|
|
113
|
|
|
# List |
|
114
|
|
|
list_ = TestResource.all(api=api) |
|
115
|
|
|
assert list_.api == api |
|
116
|
|
|
|
|
117
|
|
|
# Post |
|
118
|
|
|
post = TestResource({'id': 'id'}, api=api) |
|
119
|
|
|
post_ret = post.post('test') |
|
120
|
|
|
assert post_ret.api == api |
|
121
|
|
|
|
|
122
|
|
|
def test_default_resource(self): |
|
123
|
|
|
from besepa import api |
|
124
|
|
|
original = api.__api__ |
|
125
|
|
|
|
|
126
|
|
|
class DummyAPI(object): |
|
127
|
|
|
def post(self, *a, **k): pass |
|
128
|
|
|
|
|
129
|
|
|
def get(self, *a, **k): pass |
|
130
|
|
|
|
|
131
|
|
|
# Make default api object a dummy api object |
|
132
|
|
|
default = api.__api__ = DummyAPI() |
|
133
|
|
|
|
|
134
|
|
|
resource = Resource({}) |
|
135
|
|
|
assert resource.api == default |
|
136
|
|
|
|
|
137
|
|
|
class TestResource(Find, List, Post): |
|
138
|
|
|
path = '/' |
|
139
|
|
|
|
|
140
|
|
|
# Find |
|
141
|
|
|
find = TestResource.find('resourceid') |
|
142
|
|
|
assert find.api == default |
|
143
|
|
|
|
|
144
|
|
|
# List |
|
145
|
|
|
list_ = TestResource.all() |
|
146
|
|
|
assert list_.api == default |
|
147
|
|
|
|
|
148
|
|
|
api.__api__ = original # Restore original api object |
|
149
|
|
|
|
|
150
|
|
|
def test_contains(self): |
|
151
|
|
|
resource = Resource({'name': 'testing'}) |
|
152
|
|
|
assert True == ('name' in resource) |
|
153
|
|
|
assert False == ('testing' in resource) |
|
154
|
|
|
|
|
155
|
|
|
def test_representation(self): |
|
156
|
|
|
assert str(Resource({'name': 'testing'})) == str({'name': 'testing'}) |
|
157
|
|
|
assert repr(Resource({'name': 'testing'})) == str({'name': 'testing'}) |
|
158
|
|
|
|
|
159
|
|
|
def test_success(self): |
|
160
|
|
|
resource = Resource() |
|
161
|
|
|
assert resource.success() is True |
|
162
|
|
|
resource.error = 'Error' |
|
163
|
|
|
assert resource.success() is False |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
class TestCreate(object): |
|
167
|
|
|
@patch('resource_test.besepa.Api.post', autospec=True) |
|
168
|
|
|
def test_create(self, mock): |
|
169
|
|
|
class TestResource(Create): |
|
170
|
|
|
path = '/' |
|
171
|
|
|
|
|
172
|
|
|
attributes = {'name': 'Ender Wiggin', 'taxid': '68571053A', 'reference': '1'} |
|
173
|
|
|
resource = TestResource(attributes) |
|
174
|
|
|
response = resource.create() |
|
175
|
|
|
|
|
176
|
|
|
mock.assert_called_once_with(resource.api, '/', {'testresource': attributes}, {}) |
|
177
|
|
|
assert True is response |
|
178
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
class TestList(object): |
|
181
|
|
|
@patch('resource_test.besepa.Api.get', autospec=True) |
|
182
|
|
|
def test_all(self, mock): |
|
183
|
|
|
class TestResource(List): |
|
184
|
|
|
path = '/' |
|
185
|
|
|
|
|
186
|
|
|
TestResource.convert_resources['response'] = TestResource |
|
187
|
|
|
mock.return_value = { |
|
188
|
|
|
'count': 1, 'response': [{'id': '1', 'name': 'Ender Wiggin', 'taxid': '68571053A', 'reference': '1'}]} |
|
189
|
|
|
response = TestResource.all() |
|
190
|
|
|
|
|
191
|
|
|
mock.assert_called_once_with(response.api, '/') |
|
192
|
|
|
assert response.count == 1 |
|
193
|
|
|
assert isinstance(response.response[0], TestResource) |
|
194
|
|
|
|
|
195
|
|
|
@patch('resource_test.besepa.Api.get', autospec=True) |
|
196
|
|
|
def test_all_return_list(self, mock): |
|
197
|
|
|
class TestResource(List): |
|
198
|
|
|
path = '/' |
|
199
|
|
|
|
|
200
|
|
|
mock.return_value = [{'id': '1', 'name': 'Ender Wiggin', 'taxid': '68571053A', 'reference': '1'}] |
|
201
|
|
|
response = TestResource.all() |
|
202
|
|
|
|
|
203
|
|
|
mock.assert_called_once_with(response[0].api, '/') |
|
204
|
|
|
assert len(response) == 1 |
|
205
|
|
|
assert isinstance(response[0], Resource) |
|
206
|
|
|
|
|
207
|
|
|
|
|
208
|
|
|
class TestFind(object): |
|
209
|
|
|
@patch('resource_test.besepa.Api.get', autospec=True) |
|
210
|
|
|
def test_find(self, mock): |
|
211
|
|
|
class TestResource(Find): |
|
212
|
|
|
path = '/' |
|
213
|
|
|
|
|
214
|
|
|
test_resource = TestResource.find('1') |
|
215
|
|
|
|
|
216
|
|
|
mock.assert_called_once_with(test_resource.api, '/1') |
|
217
|
|
|
assert isinstance(test_resource, TestResource) |
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
class TestUpdate(object): |
|
221
|
|
|
@patch('resource_test.besepa.Api.patch', autospec=True) |
|
222
|
|
|
def test_update(self, mock): |
|
223
|
|
|
class TestResource(Update): |
|
224
|
|
|
path = '/' |
|
225
|
|
|
|
|
226
|
|
|
updated_attributes = {'id': '1', 'name': 'Andrew Wiggin', 'taxid': '68571053A', 'reference': '1'} |
|
227
|
|
|
mock.return_value = updated_attributes |
|
228
|
|
|
test_resource = TestResource({'id': '1', 'name': 'Ender Wiggin', 'taxid': '68571053A', 'reference': '1'}) |
|
229
|
|
|
|
|
230
|
|
|
response = test_resource.update({'name': 'Andrew Wiggin'}) |
|
231
|
|
|
|
|
232
|
|
|
mock.assert_called_once_with(test_resource.api, '/1', {'name': 'Andrew Wiggin'}, {}) |
|
233
|
|
|
assert True is response |
|
234
|
|
|
assert test_resource.to_dict() == updated_attributes |
|
235
|
|
|
|
|
236
|
|
|
|
|
237
|
|
|
class TestDelete(object): |
|
238
|
|
|
@patch('resource_test.besepa.Api.delete', autospec=True) |
|
239
|
|
|
def test_delete(self, mock): |
|
240
|
|
|
class TestResource(Delete): |
|
241
|
|
|
path = '/' |
|
242
|
|
|
|
|
243
|
|
|
test_resource = TestResource({'id': '1', 'name': 'Ender Wiggin', 'taxid': '68571053A', 'reference': '1'}) |
|
244
|
|
|
response = test_resource.delete() |
|
245
|
|
|
|
|
246
|
|
|
mock.assert_called_once_with(test_resource.api, '/1') |
|
247
|
|
|
assert True is response |
|
248
|
|
|
|
|
249
|
|
|
|
|
250
|
|
|
class TestPost(object): |
|
251
|
|
|
@patch('resource_test.besepa.Api.post', autospec=True) |
|
252
|
|
|
def test_post(self, mock): |
|
253
|
|
|
class TestResource(Post): |
|
254
|
|
|
path = '/' |
|
255
|
|
|
|
|
256
|
|
|
def test(self, attributes): |
|
257
|
|
|
return self.post('test', attributes, self) |
|
258
|
|
|
|
|
259
|
|
|
attributes = {'name': 'Ender Wiggin', 'taxid': '68571053A', 'reference': '1'} |
|
260
|
|
|
resource = TestResource({'id': '1'}) |
|
261
|
|
|
response = resource.test(attributes) |
|
262
|
|
|
|
|
263
|
|
|
mock.assert_called_once_with(resource.api, '/1/test', attributes, {}) |
|
264
|
|
|
assert True is response |
|
265
|
|
|
|