|
1
|
|
|
import json |
|
2
|
|
|
import os |
|
3
|
|
|
from collections import namedtuple |
|
4
|
|
|
|
|
5
|
|
|
import pytest |
|
6
|
|
|
|
|
7
|
|
|
import besepa |
|
8
|
|
|
|
|
9
|
|
|
try: # pragma: no cover |
|
10
|
|
|
from unittest.mock import Mock, patch |
|
11
|
|
|
except ImportError: # pragma: no cover |
|
12
|
|
|
from mock import Mock, patch |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
@pytest.fixture |
|
16
|
|
|
def api(): |
|
17
|
|
|
import besepa |
|
18
|
|
|
return besepa.Api(api_key='dummy') |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
@pytest.fixture |
|
22
|
|
|
def request_mock(api): |
|
23
|
|
|
api.request = Mock() |
|
24
|
|
|
return api |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
@pytest.fixture |
|
28
|
|
|
def http_call_mock(api): |
|
29
|
|
|
api.http_call = Mock() |
|
30
|
|
|
return api |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
class TestApi(object): |
|
34
|
|
|
|
|
35
|
|
|
@pytest.mark.parametrize('mode, expected_enpoint', [ |
|
36
|
|
|
('live', 'https://api.besepa.com'), ('sandbox', 'https://sandbox.besepa.com') |
|
37
|
|
|
]) |
|
38
|
|
|
def test_endpoint(self, mode, expected_enpoint): |
|
39
|
|
|
new_api = besepa.Api(mode=mode, api_key='dummy') |
|
40
|
|
|
|
|
41
|
|
|
assert new_api.endpoint == expected_enpoint |
|
42
|
|
|
|
|
43
|
|
|
def test_bad_mode(self): |
|
44
|
|
|
with pytest.raises(besepa.exceptions.InvalidConfig): |
|
45
|
|
|
besepa.Api(mode='bad', api_key='dummy') |
|
46
|
|
|
|
|
47
|
|
|
@patch('besepa.api.requests.request', return_value=Mock) |
|
48
|
|
|
def test_http_call(self, requests_mock, api): |
|
49
|
|
|
Response = namedtuple('Response', 'status_code reason headers content') |
|
50
|
|
|
requests_mock.return_value = Response(200, 'Failed', {}, 'Test'.encode()) |
|
51
|
|
|
api.handle_response = Mock() |
|
52
|
|
|
api.http_call('https://sandbox.besepa.com/api/1/customers', 'GET') |
|
53
|
|
|
requests_mock.assert_called_once_with('GET', 'https://sandbox.besepa.com/api/1/customers', proxies=None) |
|
54
|
|
|
api.handle_response.assert_called_once_with(requests_mock.return_value, 'Test') |
|
55
|
|
|
|
|
56
|
|
|
def test_bad_request(self, http_call_mock): |
|
57
|
|
|
http_call_mock.http_call.side_effect = besepa.exceptions.BadRequest('error', '""') |
|
58
|
|
|
|
|
59
|
|
|
customer = http_call_mock.request('https://sandbox.besepa.com/api/1/customers', 'GET') |
|
60
|
|
|
|
|
61
|
|
|
http_call_mock.http_call.assert_called_once_with( |
|
62
|
|
|
'https://sandbox.besepa.com/api/1/customers', 'GET', json=None, headers=http_call_mock.headers()) |
|
63
|
|
|
assert customer.get('error') is not None |
|
64
|
|
|
|
|
65
|
|
|
def test_get(self, request_mock): |
|
66
|
|
|
request_mock.get('api/1/customers?page=1') |
|
67
|
|
|
|
|
68
|
|
|
request_mock.request.assert_called_once_with( |
|
69
|
|
|
'https://sandbox.besepa.com/api/1/customers?page=1', 'GET', headers={}) |
|
70
|
|
|
|
|
71
|
|
|
def test_post(self, request_mock): |
|
72
|
|
|
request_mock.request.return_value = {'id': 'test'} |
|
73
|
|
|
customer_attributes = {'name': 'Ender Wiggin', 'taxid': '68571053A', 'reference': '1'} |
|
74
|
|
|
|
|
75
|
|
|
customer = request_mock.post('api/1/customers', customer_attributes) |
|
76
|
|
|
|
|
77
|
|
|
request_mock.request.assert_called_once_with( |
|
78
|
|
|
'https://sandbox.besepa.com/api/1/customers', 'POST', body=customer_attributes, headers={}) |
|
79
|
|
|
assert customer.get('error') is None |
|
80
|
|
|
assert customer.get('id') is not None |
|
81
|
|
|
|
|
82
|
|
|
def test_patch(self, request_mock): |
|
83
|
|
|
request_mock.request.return_value = {'id': 'test'} |
|
84
|
|
|
customer_attributes = {'name': 'Andrew Wiggin'} |
|
85
|
|
|
|
|
86
|
|
|
customer = request_mock.patch('api/1/customers/1', customer_attributes) |
|
87
|
|
|
|
|
88
|
|
|
request_mock.request.assert_called_once_with( |
|
89
|
|
|
'https://sandbox.besepa.com/api/1/customers/1', 'PATCH', body=customer_attributes, headers={}) |
|
90
|
|
|
assert customer.get('error') is None |
|
91
|
|
|
assert customer.get('id') is not None |
|
92
|
|
|
|
|
93
|
|
|
def test_delete(self, request_mock): |
|
94
|
|
|
request_mock.delete('api/1/customers/1') |
|
95
|
|
|
|
|
96
|
|
|
request_mock.request.assert_called_once_with( |
|
97
|
|
|
'https://sandbox.besepa.com/api/1/customers/1', 'DELETE', headers={}) |
|
98
|
|
|
|
|
99
|
|
|
def test_not_found(self, request_mock): |
|
100
|
|
|
request_mock.request.side_effect = besepa.ResourceNotFound('error') |
|
101
|
|
|
|
|
102
|
|
|
with pytest.raises(besepa.ResourceNotFound): |
|
103
|
|
|
request_mock.get('api/1/customers/2') |
|
104
|
|
|
|
|
105
|
|
|
def test_hanlde_response(self, api): |
|
106
|
|
|
response = Mock() |
|
107
|
|
|
response.status_code = 200 |
|
108
|
|
|
assert api.handle_response(response, json.dumps({'response': ''})) == "" |
|
109
|
|
|
assert api.handle_response(response, None) == {} |
|
110
|
|
|
|
|
111
|
|
|
@pytest.mark.parametrize('code, expected_exception', [ |
|
112
|
|
|
(301, besepa.exceptions.Redirection), |
|
113
|
|
|
(302, besepa.exceptions.Redirection), |
|
114
|
|
|
(400, besepa.exceptions.BadRequest), |
|
115
|
|
|
(401, besepa.exceptions.UnauthorizedAccess), |
|
116
|
|
|
(403, besepa.exceptions.ForbiddenAccess), |
|
117
|
|
|
(404, besepa.exceptions.ResourceNotFound), |
|
118
|
|
|
(405, besepa.exceptions.MethodNotAllowed), |
|
119
|
|
|
(409, besepa.exceptions.ResourceConflict), |
|
120
|
|
|
(410, besepa.exceptions.ResourceGone), |
|
121
|
|
|
(422, besepa.exceptions.ResourceInvalid), |
|
122
|
|
|
(402, besepa.exceptions.ClientError), |
|
123
|
|
|
(500, besepa.exceptions.ServerError), |
|
124
|
|
|
(600, besepa.exceptions.ConnectionError), |
|
125
|
|
|
]) |
|
126
|
|
|
def test_hanlde_response_exception(self, code, expected_exception, api): |
|
127
|
|
|
response = Mock() |
|
128
|
|
|
response.status_code = code |
|
129
|
|
|
with pytest.raises(expected_exception): |
|
130
|
|
|
api.handle_response(response, None) |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
def test_default_configuration(): |
|
134
|
|
|
default = besepa.api.__api__ |
|
135
|
|
|
besepa.api.__api__ = None |
|
136
|
|
|
os.environ["BESEPA_API_KEY"] = "EBWKjlELKMYqRNQ6sYvFo64FtaRLRR5BdHEESmha49TM" |
|
137
|
|
|
|
|
138
|
|
|
assert isinstance(besepa.api.default(), besepa.Api) |
|
139
|
|
|
assert besepa.api.default().mode == "sandbox" |
|
140
|
|
|
assert besepa.api.default().api_key == "EBWKjlELKMYqRNQ6sYvFo64FtaRLRR5BdHEESmha49TM" |
|
141
|
|
|
|
|
142
|
|
|
del os.environ["BESEPA_API_KEY"] |
|
143
|
|
|
besepa.api.__api__ = default |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
def test_default_configuration_missing_api_key(): |
|
147
|
|
|
default = besepa.api.__api__ |
|
148
|
|
|
besepa.api.__api__ = None |
|
149
|
|
|
with pytest.raises(besepa.exceptions.MissingConfig): |
|
150
|
|
|
besepa.api.default() |
|
151
|
|
|
besepa.api.__api__ = default |
|
152
|
|
|
|