Projet

Général

Profil

0001-toulouse_smart-validate-uuid-parameter-format-58992.patch

Nicolas Roche, 25 novembre 2021 18:19

Télécharger (3,17 ko)

Voir les différences:

Subject: [PATCH] toulouse_smart: validate uuid parameter format (#58992)

 passerelle/contrib/toulouse_smart/models.py | 3 +++
 tests/test_toulouse_smart.py                | 6 ++++++
 2 files changed, 9 insertions(+)
passerelle/contrib/toulouse_smart/models.py
17 17
import base64
18 18
import datetime
19 19
import json
20 20
from uuid import uuid4
21 21

  
22 22
import lxml.etree as ET
23 23
from django.conf import settings
24 24
from django.contrib.postgres.fields import JSONField
25
from django.core.exceptions import ValidationError
25 26
from django.core.files.base import ContentFile
26 27
from django.db import models
27 28
from django.db.transaction import atomic
28 29
from django.urls import reverse
29 30
from django.utils.six.moves.urllib import parse as urlparse
30 31
from django.utils.text import slugify
31 32
from django.utils.timezone import now
32 33
from django.utils.translation import ugettext_lazy as _
......
320 321
        parameters={
321 322
            'uuid': {'description': _('Notification identifier')},
322 323
        },
323 324
        post={'request_body': {'schema': {'application/json': schemas.UPDATE_SCHEMA}}},
324 325
    )
325 326
    def update_intervention(self, request, uuid, post_data):
326 327
        try:
327 328
            wcs_request = self.wcs_requests.get(uuid=uuid)
329
        except ValidationError as e:
330
            raise APIError(str(e), http_status=400)
328 331
        except WcsRequest.DoesNotExist:
329 332
            raise APIError("Cannot find intervention '%s'" % uuid, http_status=400)
330 333
        smart_request = wcs_request.smart_requests.create(payload=post_data)
331 334
        self.add_job(
332 335
            'update_intervention_job',
333 336
            id=smart_request.id,
334 337
            natural_id='smart-request-%s' % smart_request.id,
335 338
        )
tests/test_toulouse_smart.py
607 607
    smart_request = smart.wcs_requests.get(uuid=UUID).smart_requests.get()
608 608
    assert smart_request.result == {'err': 0, 'url': None}
609 609

  
610 610

  
611 611
def test_update_intervention_wrong_uuid(app, smart):
612 612
    with pytest.raises(WcsRequest.DoesNotExist):
613 613
        smart.wcs_requests.get(uuid=UUID)
614 614

  
615
    url = URL + 'update-intervention?uuid=0123456789'
616
    resp = app.post_json(url, params=UPDATE_INTERVENTION_PAYLOAD, status=400)
617
    assert resp.json['err']
618
    assert "'0123456789' is not a valid UUID." in resp.json['err_desc']
619
    assert SmartRequest.objects.count() == 0
620

  
615 621
    url = URL + 'update-intervention?uuid=%s' % str(UUID)
616 622
    resp = app.post_json(url, params=UPDATE_INTERVENTION_PAYLOAD, status=400)
617 623
    assert resp.json['err']
618 624
    assert 'Cannot find intervention' in resp.json['err_desc']
619 625
    assert SmartRequest.objects.count() == 0
620 626

  
621 627

  
622 628
@mock_response(
623
-