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