Projet

Général

Profil

0001-applications-report-scan-errors-68017.patch

Lauréline Guérin, 03 novembre 2022 09:31

Télécharger (5,16 ko)

Voir les différences:

Subject: [PATCH] applications: report scan errors (#68017)

 hobo/applications/models.py                   | 19 ++++++-
 .../templates/hobo/applications/job.html      |  1 +
 tests/test_application.py                     | 53 ++++++++++++++++++-
 3 files changed, 70 insertions(+), 3 deletions(-)
hobo/applications/models.py
37 37
requests = Requests()
38 38

  
39 39

  
40
class DeploymentError(Exception):
40
class ApplicationError(Exception):
41 41
    def __init__(self, msg):
42 42
        self.msg = msg
43 43

  
44 44

  
45
class ScanError(ApplicationError):
46
    pass
47

  
48

  
49
class DeploymentError(ApplicationError):
50
    pass
51

  
52

  
45 53
class Application(models.Model):
46 54
    SUPPORTED_MODULES = ('wcs',)
47 55

  
......
89 97
                if not dependencies_url:
90 98
                    continue
91 99
                response = requests.get(dependencies_url)
100
                if not response.ok:
101
                    raise ScanError(
102
                        _(
103
                            'Failed to scan "%s" (type %s, slug %s) dependencies (%s)'
104
                            % (el.name, el.type, el.slug, response.status_code)
105
                        )
106
                    )
92 107
                for dependency in response.json()['data']:
93 108
                    if (dependency['type'], dependency['id']) in elements:
94 109
                        continue
......
289 304
                self.version.create_bundle()
290 305
            elif self.action == 'deploy':
291 306
                self.version.deploy()
292
        except DeploymentError as e:
307
        except ApplicationError as e:
293 308
            self.status = 'failed'
294 309
            self.exception = e.msg
295 310
            if self.raise_exception:
hobo/applications/templates/hobo/applications/job.html
9 9
  {% if object.status == 'failed' %}
10 10
    <div class="pk-error">
11 11
      <p>{% trans "Error running the job." %}</p>
12
      {% if object.exception %}<pre>{{ object.exception }}</pre>{% endif %}
12 13
      <p><a class="pk-button" href="{{ view.get_redirect_url }}">{% trans "Back" %}</a></p>
13 14
    </div>
14 15
  {% else %}
tests/test_application.py
10 10
from test_manager import login
11 11
from webtest import Upload
12 12

  
13
from hobo.applications.models import Application, AsyncJob, DeploymentError, Element, Relation, Version
13
from hobo.applications.models import (
14
    Application,
15
    AsyncJob,
16
    DeploymentError,
17
    Element,
18
    Relation,
19
    ScanError,
20
    Version,
21
)
14 22
from hobo.environment.models import Authentic, Wcs
15 23

  
16 24
pytestmark = pytest.mark.django_db
......
333 341
        app.get('/applications/manifest/test/delete/%s/' % application.relation_set.first().pk, status=404)
334 342

  
335 343

  
344
def test_scandeps_on_unknown_element(app, admin_user, settings):
345
    Wcs.objects.create(base_url='https://wcs.example.invalid', slug='foobar', title='Foobar')
346

  
347
    settings.KNOWN_SERVICES = {
348
        'wcs': {
349
            'foobar': {
350
                'title': 'Foobar',
351
                'url': 'https://wcs.example.invalid/',
352
                'orig': 'example.org',
353
                'secret': 'xxx',
354
            }
355
        }
356
    }
357

  
358
    application = Application.objects.create(name='Test', slug='test')
359
    element = Element.objects.create(
360
        type='forms',
361
        slug='unknown',
362
        name='Unknown',
363
        cache={
364
            'urls': {
365
                "dependencies": "https://wcs.example.invalid/api/export-import/forms/unknown/dependencies/",
366
            }
367
        },
368
    )
369
    Relation.objects.create(application=application, element=element)
370

  
371
    def response_content(url, request):
372
        if url.path == '/api/export-import/forms/unknown/dependencies/':
373
            return {'status_code': 404}
374
        return mocked_http(url, request)
375

  
376
    login(app)
377

  
378
    with HTTMock(response_content):
379
        with pytest.raises(ScanError) as e:
380
            app.get('/applications/manifest/test/scandeps/').follow()
381
        assert str(e.value) == 'Failed to scan "Unknown" (type forms, slug unknown) dependencies (404)'
382
        job = AsyncJob.objects.latest('pk')
383
        assert job.status == 'failed'
384
        assert job.exception == 'Failed to scan "Unknown" (type forms, slug unknown) dependencies (404)'
385

  
386

  
336 387
@pytest.mark.parametrize('editable', [True, False])
337 388
def test_redirect_application_element(app, admin_user, settings, editable):
338 389
    Wcs.objects.create(base_url='https://wcs.example.invalid', slug='foobar', title='Foobar')
339
-