export_import: post bundle (#89036)
gitea/lingo/pipeline/head This commit looks good Details

This commit is contained in:
Lauréline Guérin 2024-04-04 13:49:10 +02:00
parent 887d532c10
commit b8b0da6809
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 35 additions and 16 deletions

View File

@ -14,7 +14,6 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import io
import json
import tarfile
@ -221,7 +220,7 @@ def component_redirect(request, component_type, slug):
class BundleCheck(GenericAPIView):
permission_classes = (permissions.IsAdminUser,)
def put(self, request, *args, **kwargs):
def post(self, request, *args, **kwargs):
return Response({'err': 0, 'data': {}})
@ -232,11 +231,11 @@ class BundleImport(GenericAPIView):
permission_classes = (permissions.IsAdminUser,)
install = True
def put(self, request, *args, **kwargs):
tar_io = io.BytesIO(request.read())
def post(self, request, *args, **kwargs):
bundle = request.FILES['bundle']
components = {}
try:
with tarfile.open(fileobj=tar_io) as tar:
with tarfile.open(fileobj=bundle) as tar:
try:
manifest = json.loads(tar.extractfile('manifest.json').read().decode())
except KeyError:

View File

@ -703,7 +703,7 @@ def test_bundle_import(mock_refresh, app, admin_user):
Agenda.objects.all().delete()
agenda = Agenda.objects.create(label='Foo Bar Agenda') # created by agenda refresh
resp = app.put('/api/export-import/bundle-import/', bundles[0])
resp = app.post('/api/export-import/bundle-import/', upload_files=[('bundle', 'bundle.tar', bundles[0])])
assert Pricing.objects.all().count() == 1
assert resp.json['err'] == 0
assert Application.objects.count() == 1
@ -745,7 +745,7 @@ def test_bundle_import(mock_refresh, app, admin_user):
)
# check update
resp = app.put('/api/export-import/bundle-import/', bundles[1])
resp = app.post('/api/export-import/bundle-import/', upload_files=[('bundle', 'bundle.tar', bundles[1])])
assert Pricing.objects.all().count() == 1
assert resp.json['err'] == 0
assert Application.objects.count() == 1
@ -762,7 +762,9 @@ def test_bundle_import(mock_refresh, app, admin_user):
)
# bad file format
resp = app.put('/api/export-import/bundle-import/', b'garbage', status=400)
resp = app.post(
'/api/export-import/bundle-import/', upload_files=[('bundle', 'bundle.tar', b'garbage')], status=400
)
assert resp.json['err']
assert resp.json['err_desc'] == 'Invalid tar file'
@ -773,7 +775,11 @@ def test_bundle_import(mock_refresh, 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(), status=400)
resp = app.post(
'/api/export-import/bundle-import/',
upload_files=[('bundle', 'bundle.tar', tar_io.getvalue())],
status=400,
)
assert resp.json['err']
assert resp.json['err_desc'] == 'Invalid tar file, missing manifest'
@ -789,7 +795,11 @@ def test_bundle_import(mock_refresh, 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(), status=400)
resp = app.post(
'/api/export-import/bundle-import/',
upload_files=[('bundle', 'bundle.tar', tar_io.getvalue())],
status=400,
)
assert resp.json['err']
assert resp.json['err_desc'] == 'Invalid tar file, missing component pricings/foo'
@ -798,7 +808,7 @@ def test_bundle_declare(app, admin_user):
app.authorization = ('Basic', ('admin', 'admin'))
bundle = create_bundle(app, admin_user, visible=False)
resp = app.put('/api/export-import/bundle-declare/', bundle)
resp = app.post('/api/export-import/bundle-declare/', upload_files=[('bundle', 'bundle.tar', bundle)])
assert Pricing.objects.all().count() == 1
assert resp.json['err'] == 0
assert Application.objects.count() == 1
@ -825,14 +835,16 @@ def test_bundle_declare(app, admin_user):
# and remove regie to have unkown references in manifest
Regie.objects.all().delete()
resp = app.put('/api/export-import/bundle-declare/', bundle)
resp = app.post('/api/export-import/bundle-declare/', upload_files=[('bundle', 'bundle.tar', bundle)])
assert Application.objects.count() == 1
application = Application.objects.latest('pk')
assert application.visible is True
assert ApplicationElement.objects.count() == 5 # pricing, categorie, agenda, group, payer
# bad file format
resp = app.put('/api/export-import/bundle-declare/', b'garbage', status=400)
resp = app.post(
'/api/export-import/bundle-declare/', upload_files=[('bundle', 'bundle.tar', b'garbage')], status=400
)
assert resp.json['err']
assert resp.json['err_desc'] == 'Invalid tar file'
@ -843,7 +855,11 @@ 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(), status=400)
resp = app.post(
'/api/export-import/bundle-declare/',
upload_files=[('bundle', 'bundle.tar', tar_io.getvalue())],
status=400,
)
assert resp.json['err']
assert resp.json['err_desc'] == 'Invalid tar file, missing manifest'
@ -859,7 +875,11 @@ 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(), status=400)
resp = app.post(
'/api/export-import/bundle-declare/',
upload_files=[('bundle', 'bundle.tar', tar_io.getvalue())],
status=400,
)
assert resp.json['err']
assert resp.json['err_desc'] == 'Invalid tar file, missing component pricings/foo'
@ -921,4 +941,4 @@ def test_bundle_unlink(app, admin_user, bundle):
def test_bundle_check(app, admin_user):
app.authorization = ('Basic', ('admin', 'admin'))
assert app.put('/api/export-import/bundle-check/').json == {'err': 0, 'data': {}}
assert app.post('/api/export-import/bundle-check/').json == {'err': 0, 'data': {}}