44 |
44 |
|
45 |
45 |
|
46 |
46 |
def mock_response(*path_contents):
|
47 |
47 |
def decorator(func):
|
48 |
48 |
@httmock.urlmatch()
|
49 |
49 |
def error(url, request):
|
50 |
50 |
assert False, 'request to %s' % url.geturl()
|
51 |
51 |
|
52 |
|
def register(path, content, status_code=200):
|
|
52 |
def register(path, payload, content, status_code=200):
|
53 |
53 |
@httmock.urlmatch(path=path)
|
54 |
54 |
def handler(url, request):
|
|
55 |
if payload and json.loads(request.body) != payload:
|
|
56 |
assert False, 'wrong payload sent to request to %s' % url.geturl()
|
55 |
57 |
return httmock.response(status_code, content)
|
56 |
58 |
|
57 |
59 |
return handler
|
58 |
60 |
|
59 |
61 |
@functools.wraps(func)
|
60 |
62 |
def wrapper(*args, **kwargs):
|
61 |
63 |
handlers = []
|
62 |
64 |
for row in path_contents:
|
... | ... | |
71 |
73 |
return decorator
|
72 |
74 |
|
73 |
75 |
|
74 |
76 |
def get_xml_file(filename):
|
75 |
77 |
with open(os.path.join(TEST_BASE_DIR, "%s.xml" % filename), 'rb') as desc:
|
76 |
78 |
return desc.read()
|
77 |
79 |
|
78 |
80 |
|
79 |
|
@mock_response(['/v1/type-intervention', b'<List></List>'])
|
|
81 |
@mock_response(['/v1/type-intervention', None, b'<List></List>'])
|
80 |
82 |
def test_empty_intervention_types(smart):
|
81 |
83 |
assert smart.get_intervention_types() == []
|
82 |
84 |
|
83 |
85 |
|
84 |
86 |
INTERVENTION_TYPES = '''<List>
|
85 |
87 |
<item>
|
86 |
88 |
<id>1234</id>
|
87 |
89 |
<name>coin</name>
|
... | ... | |
102 |
104 |
<type>int</type>
|
103 |
105 |
<required>true</required>
|
104 |
106 |
</properties>
|
105 |
107 |
</properties>
|
106 |
108 |
</item>
|
107 |
109 |
</List>'''.encode()
|
108 |
110 |
|
109 |
111 |
|
110 |
|
@mock_response(['/v1/type-intervention', INTERVENTION_TYPES])
|
|
112 |
@mock_response(['/v1/type-intervention', None, INTERVENTION_TYPES])
|
111 |
113 |
def test_model_intervention_types(smart):
|
112 |
114 |
assert smart.get_intervention_types() == [
|
113 |
115 |
{
|
114 |
116 |
'id': '1234',
|
115 |
117 |
'name': 'coin',
|
116 |
118 |
'order': 1,
|
117 |
119 |
'properties': [
|
118 |
120 |
{
|
... | ... | |
126 |
128 |
],
|
127 |
129 |
},
|
128 |
130 |
]
|
129 |
131 |
|
130 |
132 |
|
131 |
133 |
URL = '/toulouse-smart/test/'
|
132 |
134 |
|
133 |
135 |
|
134 |
|
@mock_response(['/v1/type-intervention', INTERVENTION_TYPES])
|
|
136 |
@mock_response(['/v1/type-intervention', None, INTERVENTION_TYPES])
|
135 |
137 |
def test_endpoint_intervention_types(app, smart):
|
136 |
138 |
resp = app.get(URL + 'type-intervention')
|
137 |
139 |
assert resp.json == {'data': [{'id': 'coin', 'text': 'coin', 'uuid': '1234'}], 'err': 0}
|
138 |
140 |
|
139 |
141 |
|
140 |
142 |
@mock_response()
|
141 |
143 |
def test_endpoint_intervention_types_unavailable(app, smart):
|
142 |
144 |
resp = app.get(URL + 'type-intervention')
|
143 |
145 |
assert resp.json == {'data': [{'id': '', 'text': 'Service is unavailable', 'disabled': True}], 'err': 0}
|
144 |
146 |
|
145 |
147 |
|
146 |
|
@mock_response(['/v1/type-intervention', INTERVENTION_TYPES])
|
|
148 |
@mock_response(['/v1/type-intervention', None, INTERVENTION_TYPES])
|
147 |
149 |
def test_manage_intervention_types(app, smart, admin_user):
|
148 |
150 |
login(app)
|
149 |
151 |
resp = app.get('/manage' + URL + 'type-intervention/')
|
150 |
152 |
assert [[td.text for td in tr.cssselect('td,th')] for tr in resp.pyquery('tr')] == [
|
151 |
153 |
["Nom du type d'intervention", 'Nom', 'Type', 'Requis', 'Valeur par défaut'],
|
152 |
154 |
['1 - coin'],
|
153 |
155 |
[None, 'FIELD1', 'item («Candélabre», «Mât», «Ne sait pas»)', '✘', 'Ne sait pas'],
|
154 |
156 |
[None, 'FIELD2', 'int', '✔', None],
|
... | ... | |
196 |
198 |
</field>
|
197 |
199 |
</fields>
|
198 |
200 |
</block>
|
199 |
201 |
'''
|
200 |
202 |
)
|
201 |
203 |
|
202 |
204 |
|
203 |
205 |
@mock_response(
|
204 |
|
['/v1/intervention', get_xml_file('get_intervention')],
|
|
206 |
['/v1/intervention', None, get_xml_file('get_intervention')],
|
205 |
207 |
)
|
206 |
208 |
def test_get_intervention(app, smart):
|
207 |
209 |
resp = app.get(URL + 'get-intervention?id=3f0558bd-7d85-49a8-97e4-d07bc7f8dc9b')
|
208 |
210 |
assert not resp.json['err']
|
209 |
211 |
assert resp.json['data']['id'] == '3f0558bd-7d85-49a8-97e4-d07bc7f8dc9b'
|
210 |
212 |
assert resp.json['data']['state'] == ["e844e67f-5382-4c0f-94d8-56f618263485", "Nouveau", "false"]
|
211 |
213 |
|
212 |
214 |
|
213 |
215 |
@mock_response(
|
214 |
|
['/v1/intervention', None, 500],
|
|
216 |
['/v1/intervention', None, None, 500],
|
215 |
217 |
)
|
216 |
218 |
def test_get_intervention_error_status(app, smart):
|
217 |
219 |
resp = app.get(URL + 'get-intervention?id=3f0558bd-7d85-49a8-97e4-d07bc7f8dc9b')
|
218 |
220 |
assert resp.json['err']
|
219 |
221 |
assert 'failed to get' in resp.json['err_desc']
|
220 |
222 |
|
221 |
223 |
|
222 |
224 |
@mock_response(
|
223 |
|
['/v1/intervention', None, 404],
|
|
225 |
['/v1/intervention', None, None, 404],
|
224 |
226 |
)
|
225 |
227 |
def test_get_intervention_wrond_id(app, smart):
|
226 |
228 |
resp = app.get(URL + 'get-intervention?id=3f0558bd-7d85-49a8-97e4-d07bc7f8dc9b')
|
227 |
229 |
assert resp.json['err']
|
228 |
230 |
assert 'failed to get' in resp.json['err_desc']
|
229 |
231 |
assert '404' in resp.json['err_desc']
|
230 |
|
-
|