-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_aeat.py
146 lines (108 loc) · 4.81 KB
/
test_aeat.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
from unittest.mock import Mock, PropertyMock, patch
import pytest
from tests import factories
from zeep import xsd
from zeep import exceptions as zeep_exceptions
from aeat import Config, Controller, wsdl
def test_config_as_str():
config = Config('ens_presentation', test_mode=True)
assert 'Servicio de Presentación ENS V4.0' in config.__str__()
@patch('aeat.Client')
def test_controller_is_built_from_config_obj(client):
config = Mock(signed=True)
ctrl = Controller.build_from_config(config, Mock(), Mock())
assert isinstance(ctrl, Controller)
assert ctrl.config.signed
@pytest.mark.parametrize('test_mode,expected_port', [
(True, 'IE315V4Pruebas'),
(False, 'IE315V4'),
])
def test_config_is_built_from_service_name(test_mode, expected_port):
config = Config('ens_presentation', test_mode=test_mode)
assert config.wsdl.endswith('IE315V4.wsdl')
assert 'IE315V4' == config.operation
assert 'IE315V4Service' == config.service
assert expected_port == config.port
@patch('aeat.Controller.operation', new_callable=PropertyMock)
def test_controller_with_valid_response(operation_patch, zeep_response):
response = zeep_response(
'wsdl_ens_query_ConsENSV3.wsdl', 'ens_query_valid.xml', 'ConsENSV3'
)
operation_patch.return_value = lambda **kwargs: response
ctrl = Controller(Mock(), Mock())
result = ctrl.request(factories.ENSQueryFactory())
assert result.valid
assert 2 == len(result.data['IMPOPE'])
@patch('aeat.Controller.operation')
def test_controller_marks_signature_as_skip_if_config_is_signed(operation_patch):
config = Mock(signed=True)
ctrl = Controller(Mock(), config)
ctrl.request({'arg': 'x'})
operation_patch.assert_called_with(arg='x', Signature=xsd.SkipValue)
@patch('aeat.Controller.operation', new_callable=PropertyMock)
def test_controller_with_99999_error(operation_patch, zeep_response):
def response():
return zeep_response('wsdl_ens_presentation_IE315V4.wsdl',
'ens_presentation_error_99999.xml', 'IE313V4')
operation_patch.return_value = lambda **kwargs: response()
ctrl = Controller(Mock(), Mock())
result = ctrl.request(factories.ENSQueryFactory())
assert not result.valid
assert 'Mensaje REENVIABLE. Codigo[99999].' == result.error
assert result.data is None
@patch('aeat.Controller.operation', new_callable=PropertyMock)
def test_controller_with_html_error(operation_patch, zeep_response):
def response():
return zeep_response('wsdl_ens_query_ConsENSV3.wsdl', 'unknown_certificate.html',
'ConsENSV3')
operation_patch.return_value = lambda **kwargs: response()
ctrl = Controller(Mock(), Mock())
result = ctrl.request(factories.ENSQueryFactory())
assert not result.valid
assert 'Unknown AEAT error' == result.error
assert result.data is None
def test_controller_operation():
service = Mock()
service.myoperation = Mock()
ctrl = Controller(Mock(service=service), Mock(operation='myoperation'))
assert service.myoperation == ctrl.operation
@pytest.mark.parametrize('service_name', [
name for name, _ in wsdl.ADUANET_SERVICES.items()
])
def test_aduanet_services_configuration(service_name):
config = Config(service_name, True)
assert isinstance(config, Config)
assert config.verbose_name is not None
assert config.wsdl is not None
assert config.operation is not None
assert config.port is not None
assert config.service is not None
assert config.signed is not None
assert config.port.endswith('Pruebas')
@patch('aeat.Controller.operation', new_callable=PropertyMock)
def test_controller_with_xmlerr805(operation_patch, zeep_response):
def response():
return zeep_response('wsdl_ens_presentation_IE315V4.wsdl',
'ens_presentation_XMLERR805.xml', 'IE313V4')
operation_patch.return_value = lambda **kwargs: response()
ctrl = Controller(Mock(), Mock())
result = ctrl.request(factories.ENSPresentationFactory())
assert not result.valid
assert result.data is None
msg = 'Validation error. CC315A,DatOfPreMES9: Element too long (length constraint)'
assert msg == result.error
@pytest.mark.parametrize('detail,exception_cls', [
('Unknown AEAT error', zeep_exceptions.XMLSyntaxError),
('Validation error', zeep_exceptions.ValidationError),
('Unknown error', Exception),
])
@patch('aeat.Controller.operation', new_callable=PropertyMock)
def test_controller_operation_request_exception_handling(operation_patch, detail, exception_cls):
def operation(arg, Signature):
raise exception_cls
operation_patch.return_value = operation
config = Mock(signed=True)
ctrl = Controller(Mock(), config)
result = ctrl.request({'arg': 'x'})
assert not result.valid
assert detail == result.error