Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix error on load_dump when json is a list #171

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions taiga/export_import/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import codecs
import uuid
import gzip
import logging


from django.utils.decorators import method_decorator
from django.utils.translation import gettext as _
Expand Down Expand Up @@ -41,6 +43,7 @@

from taiga.base.api.utils import get_object_or_error

logger = logging.getLogger(__name__)

class ProjectExporterViewSet(mixins.ImportThrottlingPolicyMixin, GenericViewSet):
model = Project
Expand Down Expand Up @@ -333,9 +336,14 @@ def load_dump(self, request):

try:
dump = json.load(reader(dump))

except Exception:
raise exc.WrongArguments(_("Invalid dump format"))

if not isinstance(dump, dict):
logger.error("trying a load_dump with a different format than dict: {0}, from user {1}".format(dump, request.user))
raise exc.WrongArguments(_("Invalid dump format"))

slug = dump.get('slug', None)
if slug is not None and Project.objects.filter(slug=slug).exists():
del dump['slug']
Expand Down
20 changes: 20 additions & 0 deletions tests/integration/test_importer_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,26 @@ def test_valid_dump_import_error_without_enough_public_projects_slots(client, se
assert Project.objects.filter(slug="public-project-without-slots").count() == 0


def test_invalid_dump_json_list(client, settings):
user = f.UserFactory.create(max_public_projects=0)
client.login(user)

url = reverse("importer-load-dump")

data = ContentFile(bytes(json.dumps([{
"slug": "public-project-without-slots",
"name": "Valid project",
"description": "Valid project desc",
"is_private": False
}]), "utf-8"))
data.name = "test"

response = client.post(url, {'dump': data})
assert response.status_code == 400




def test_valid_dump_import_error_without_enough_private_projects_slots(client, settings):
user = f.UserFactory.create(max_private_projects=0)
client.login(user)
Expand Down
Loading