0001-mpdh13-validate-date-syntax-31186.patch
passerelle/contrib/mdph13/models.py | ||
---|---|---|
31 | 31 |
from passerelle.base.models import BaseResource, HTTPResource |
32 | 32 | |
33 | 33 | |
34 |
def json_walker(value, func, path=None): |
|
35 |
path = path or [] |
|
36 |
if isinstance(value, dict): |
|
37 |
for key in value: |
|
38 |
json_walker(value[key], func, path + [key]) |
|
39 |
elif isinstance(value, list): |
|
40 |
for i, v in enumerate(value): |
|
41 |
json_walker(v, func, path + ['[%s]' % i]) |
|
42 |
else: |
|
43 |
func(value, path) |
|
44 | ||
45 | ||
34 | 46 |
class MDPH13Resource(BaseResource, HTTPResource): |
35 | 47 |
category = _('Business Process Connectors') |
36 | 48 | |
37 | 49 |
webservice_base_url = models.URLField(_('Webservice Base URL')) |
38 | 50 | |
39 | 51 |
EMAIL_RE = re.compile(r'^[^@\s]+@[^@\s]+\.[^@\s]+$') |
52 |
DATE_RE = re.compile(r'^\d{4}-\d{2}-\d{2}$') |
|
40 | 53 | |
41 | 54 |
class Meta: |
42 | 55 |
verbose_name = _('MDPH CD13') |
... | ... | |
155 | 168 |
[] |
156 | 169 |
).append(demande) |
157 | 170 |
data['demandes'] = new_demandes |
171 | ||
172 |
# Check some syntaxes |
|
173 |
errors = [] |
|
174 | ||
175 |
def check(value, path): |
|
176 |
if path[-1].startswith('date_'): |
|
177 |
if (not isinstance(value, six.text_type) |
|
178 |
or not self.DATE_RE.match(value)): |
|
179 |
errors.append('%s is not a date string' % '.'.join(path)) |
|
180 |
json_walker(data, check) |
|
181 |
if errors: |
|
182 |
raise APIError('invalid-response-format', data={'errors': errors}) |
|
183 | ||
158 | 184 |
return data |
159 | 185 | |
160 | 186 |
def check_status(self): |
tests/test_mdph13.py | ||
---|---|---|
473 | 473 |
assert response['data'][0]['err'] == 0 |
474 | 474 |
assert response['data'][1]['id'] == str(link2.pk) |
475 | 475 |
assert response['data'][1]['err'] == 1 |
476 | ||
477 | ||
478 |
def test_dossier_bad_date(mdph13, mock_http): |
|
479 |
link = Link.objects.create( |
|
480 |
resource=mdph13, |
|
481 |
name_id=NAME_ID, |
|
482 |
file_number=FILE_NUMBER, |
|
483 |
secret=SECRET, |
|
484 |
dob=DOB) |
|
485 |
INVALID_RESPONSE = json.loads(VALID_RESPONSE) |
|
486 |
INVALID_RESPONSE['data']['demandes'][0]['date_demande'] = 'xxx' |
|
487 |
mock_http.add_response(json.dumps(INVALID_RESPONSE)) |
|
488 |
with pytest.raises(APIError) as exc_info: |
|
489 |
mdph13.dossiers(None, NAME_ID, EMAIL, link_id=str(link.pk)) |
|
490 |
assert str(exc_info.value) == 'invalid-response-format' |
|
476 |
- |