export_import: replace APIError by APIErrorBadRequest (#88593)
gitea/chrono/pipeline/head This commit looks good Details

This commit is contained in:
Lauréline Guérin 2024-03-25 10:04:28 +01:00
parent bbc7ebfcda
commit c268aada20
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 14 additions and 14 deletions

View File

@ -29,7 +29,7 @@ from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from chrono.agendas.models import Agenda, Category, EventsType, Resource, UnavailabilityCalendar
from chrono.api.utils import APIError
from chrono.api.utils import APIErrorBadRequest
from chrono.apps.export_import.models import Application, ApplicationElement
from chrono.manager.utils import import_site
@ -244,7 +244,7 @@ class BundleCheck(GenericAPIView):
try:
manifest = json.loads(tar.extractfile('manifest.json').read().decode())
except KeyError:
raise APIError(_('Invalid tar file, missing manifest'))
raise APIErrorBadRequest(_('Invalid tar file, missing manifest'))
application_slug = manifest.get('slug')
application_version = manifest.get('version_number')
if not application_slug or not application_version:
@ -334,7 +334,7 @@ class BundleCheck(GenericAPIView):
}
)
except tarfile.TarError:
raise APIError(_('Invalid tar file'))
raise APIErrorBadRequest(_('Invalid tar file'))
return Response(
{
@ -363,7 +363,7 @@ class BundleImport(GenericAPIView):
try:
manifest = json.loads(tar.extractfile('manifest.json').read().decode())
except KeyError:
raise APIError(_('Invalid tar file, missing manifest'))
raise APIErrorBadRequest(_('Invalid tar file, missing manifest'))
self.application = Application.update_or_create_from_manifest(
manifest,
tar,
@ -382,7 +382,7 @@ class BundleImport(GenericAPIView):
tar.extractfile('%s/%s' % (element['type'], element['slug'])).read().decode()
)
except KeyError:
raise APIError(
raise APIErrorBadRequest(
_(
'Invalid tar file, missing component %s/%s'
% (element['type'], element['slug'])
@ -390,7 +390,7 @@ class BundleImport(GenericAPIView):
)
components[component_type].append(json.loads(component_content).get('data'))
except tarfile.TarError:
raise APIError(_('Invalid tar file'))
raise APIErrorBadRequest(_('Invalid tar file'))
# init cache of application elements, from manifest
self.application_elements = set()

View File

@ -598,7 +598,7 @@ def test_bundle_import(app, admin_user):
assert last_snapshot.application_version == '42.1'
# bad file format
resp = app.put('/api/export-import/bundle-import/', b'garbage')
resp = app.put('/api/export-import/bundle-import/', b'garbage', status=400)
assert resp.json['err']
assert resp.json['err_desc'] == 'Invalid tar file'
@ -609,7 +609,7 @@ def test_bundle_import(app, admin_user):
tarinfo = tarfile.TarInfo('foo.json')
tarinfo.size = len(foo_fd.getvalue())
tar.addfile(tarinfo, fileobj=foo_fd)
resp = app.put('/api/export-import/bundle-import/', tar_io.getvalue())
resp = app.put('/api/export-import/bundle-import/', tar_io.getvalue(), status=400)
assert resp.json['err']
assert resp.json['err_desc'] == 'Invalid tar file, missing manifest'
@ -625,7 +625,7 @@ def test_bundle_import(app, admin_user):
tarinfo = tarfile.TarInfo('manifest.json')
tarinfo.size = len(manifest_fd.getvalue())
tar.addfile(tarinfo, fileobj=manifest_fd)
resp = app.put('/api/export-import/bundle-import/', tar_io.getvalue())
resp = app.put('/api/export-import/bundle-import/', tar_io.getvalue(), status=400)
assert resp.json['err']
assert resp.json['err_desc'] == 'Invalid tar file, missing component agendas/foo'
@ -668,7 +668,7 @@ def test_bundle_declare(app, admin_user):
assert ApplicationElement.objects.count() == 4 # category, events_type, unavailability_calendar, resource
# bad file format
resp = app.put('/api/export-import/bundle-declare/', b'garbage')
resp = app.put('/api/export-import/bundle-declare/', b'garbage', status=400)
assert resp.json['err']
assert resp.json['err_desc'] == 'Invalid tar file'
@ -679,7 +679,7 @@ def test_bundle_declare(app, admin_user):
tarinfo = tarfile.TarInfo('foo.json')
tarinfo.size = len(foo_fd.getvalue())
tar.addfile(tarinfo, fileobj=foo_fd)
resp = app.put('/api/export-import/bundle-declare/', tar_io.getvalue())
resp = app.put('/api/export-import/bundle-declare/', tar_io.getvalue(), status=400)
assert resp.json['err']
assert resp.json['err_desc'] == 'Invalid tar file, missing manifest'
@ -695,7 +695,7 @@ def test_bundle_declare(app, admin_user):
tarinfo = tarfile.TarInfo('manifest.json')
tarinfo.size = len(manifest_fd.getvalue())
tar.addfile(tarinfo, fileobj=manifest_fd)
resp = app.put('/api/export-import/bundle-declare/', tar_io.getvalue())
resp = app.put('/api/export-import/bundle-declare/', tar_io.getvalue(), status=400)
assert resp.json['err']
assert resp.json['err_desc'] == 'Invalid tar file, missing component agendas/foo'
@ -1023,7 +1023,7 @@ def test_bundle_check(app, admin_user):
}
# bad file format
resp = app.put('/api/export-import/bundle-check/', b'garbage')
resp = app.put('/api/export-import/bundle-check/', b'garbage', status=400)
assert resp.json['err']
assert resp.json['err_desc'] == 'Invalid tar file'
@ -1034,6 +1034,6 @@ def test_bundle_check(app, admin_user):
tarinfo = tarfile.TarInfo('foo.json')
tarinfo.size = len(foo_fd.getvalue())
tar.addfile(tarinfo, fileobj=foo_fd)
resp = app.put('/api/export-import/bundle-check/', tar_io.getvalue())
resp = app.put('/api/export-import/bundle-check/', tar_io.getvalue(), status=400)
assert resp.json['err']
assert resp.json['err_desc'] == 'Invalid tar file, missing manifest'