def test_project_update(client, data):
    url = reverse('projects-detail', kwargs={"pk": data.private_project2.pk})
    blocked_url = reverse('projects-detail',
                          kwargs={"pk": data.blocked_project.pk})

    users = [
        None, data.registered_user, data.project_member_with_perms,
        data.project_owner
    ]

    project_data = ProjectSerializer(data.private_project2).data
    # Because in serializer is a dict anin model is a list of lists
    project_data["tags_colors"] = list(
        map(list, project_data["tags_colors"].items()))
    project_data["is_private"] = False

    results = helper_test_http_method(client, 'put', url,
                                      json.dumps(project_data), users)
    assert results == [401, 403, 403, 200]

    project_data = ProjectSerializer(data.blocked_project).data
    # Because in serializer is a dict anin model is a list of lists
    project_data["tags_colors"] = list(
        map(list, project_data["tags_colors"].items()))
    project_data["is_private"] = False

    results = helper_test_http_method(client, 'put', blocked_url,
                                      json.dumps(project_data), users)
    assert results == [401, 403, 403, 451]
예제 #2
0
파일: api.py 프로젝트: shadown/taiga-back
    def load_dump(self, request):
        throttle = throttling.ImportDumpModeRateThrottle()

        if not throttle.allow_request(request, self):
            self.throttled(request, throttle.wait())

        self.check_permissions(request, "load_dump", None)

        dump = request.FILES.get('dump', None)

        if not dump:
            raise exc.WrongArguments(_("Needed dump file"))

        reader = codecs.getreader("utf-8")

        try:
            dump = json.load(reader(dump))
        except Exception:
            raise exc.WrongArguments(_("Invalid dump format"))

        if Project.objects.filter(slug=dump['slug']).exists():
            del dump['slug']

        if settings.CELERY_ENABLED:
            task = tasks.load_project_dump.delay(request.user, dump)
            return Response({"import_id": task.id},
                            status=status.HTTP_202_ACCEPTED)

        project = dump_service.dict_to_project(dump, request.user.email)
        response_data = ProjectSerializer(project).data
        return Response(response_data, status=status.HTTP_201_CREATED)
예제 #3
0
def test_project_update(client, data):
    url = reverse('projects-detail', kwargs={"pk": data.private_project2.pk})
    blocked_url = reverse('projects-detail', kwargs={"pk": data.blocked_project.pk})

    users = [
        None,
        data.registered_user,
        data.project_member_with_perms,
        data.project_owner
    ]

    project_data = ProjectSerializer(data.private_project2).data
    project_data["is_private"] = False
    results = helper_test_http_method(client, 'put', url, json.dumps(project_data), users)
    assert results == [401, 403, 403, 200]

    project_data = ProjectSerializer(data.blocked_project).data
    project_data["is_private"] = False
    results = helper_test_http_method(client, 'put', blocked_url, json.dumps(project_data), users)
    assert results == [401, 403, 403, 451]
예제 #4
0
    def load_dump(self, request):
        throttle = throttling.ImportDumpModeRateThrottle()

        if not throttle.allow_request(request, self):
            self.throttled(request, throttle.wait())

        self.check_permissions(request, "load_dump", None)

        dump = request.FILES.get('dump', None)

        if not dump:
            raise exc.WrongArguments(_("Needed dump file"))

        reader = codecs.getreader("utf-8")

        try:
            dump = json.load(reader(dump))
            is_private = dump.get("is_private", False)
        except Exception:
            raise exc.WrongArguments(_("Invalid dump format"))

        user = request.user
        (enough_slots, not_enough_slots_error) = users_service.has_available_slot_for_project(
            user,
            project=Project(is_private=is_private, id=None)
        )
        if not enough_slots:
            raise exc.BadRequest(not_enough_slots_error)

        if Project.objects.filter(slug=dump['slug']).exists():
            del dump['slug']

        members = len(dump.get("memberships", []))
        (enough_slots, not_enough_slots_error) = users_service.has_available_slot_for_project(
            user,
            project=Project(is_private=is_private, id=None),
            members=max(members, 1)
        )
        if not enough_slots:
            raise exc.BadRequest(not_enough_slots_error)

        if settings.CELERY_ENABLED:
            task = tasks.load_project_dump.delay(user, dump)
            return response.Accepted({"import_id": task.id})

        project = dump_service.dict_to_project(dump, request.user)
        response_data = ProjectSerializer(project).data
        return response.Created(response_data)
예제 #5
0
    def load_dump(self, request):
        throttle = throttling.ImportDumpModeRateThrottle()

        if not throttle.allow_request(request, self):
            self.throttled(request, throttle.wait())

        self.check_permissions(request, "load_dump", None)

        dump = request.FILES.get('dump', None)

        if not dump:
            raise exc.WrongArguments(_("Needed dump file"))

        reader = codecs.getreader("utf-8")

        try:
            dump = json.load(reader(dump))
        except Exception:
            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']

        user = request.user
        dump['owner'] = user.email

        # Validate if the project can be imported
        is_private = dump.get("is_private", False)
        total_memberships = len([
            m for m in dump.get("memberships", [])
            if m.get("email", None) != dump["owner"]
        ])
        total_memberships = total_memberships + 1  # 1 is the owner
        (enough_slots, error_message
         ) = users_service.has_available_slot_for_import_new_project(
             user, is_private, total_memberships)
        if not enough_slots:
            raise exc.NotEnoughSlotsForProject(is_private, total_memberships,
                                               error_message)

        if settings.CELERY_ENABLED:
            task = tasks.load_project_dump.delay(user, dump)
            return response.Accepted({"import_id": task.id})

        project = dump_service.dict_to_project(dump, request.user)
        response_data = ProjectSerializer(project).data
        return response.Created(response_data)
예제 #6
0
    def load_dump(self, request):
        throttle = throttling.ImportDumpModeRateThrottle()

        if not throttle.allow_request(request, self):
            self.throttled(request, throttle.wait())

        self.check_permissions(request, "load_dump", None)

        dump = request.FILES.get('dump', None)

        if not dump:
            raise exc.WrongArguments(_("Needed dump file"))

        if dump.content_type == "application/gzip":
            dump = gzip.GzipFile(fileobj=dump)

        reader = codecs.getreader("utf-8")

        try:
            dump = json.load(reader(dump))
        except Exception:
            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']

        user = request.user
        dump['owner'] = user.email

        # Validate if the project can be imported
        is_private = dump.get("is_private", False)
        total_memberships = len([
            m for m in dump.get("memberships", [])
            if m.get("email", None) != dump["owner"]
        ])
        total_memberships = total_memberships + 1  # 1 is the owner
        (enough_slots,
         error_message) = users_services.has_available_slot_for_new_project(
             user, is_private, total_memberships)
        if not enough_slots:
            raise exc.NotEnoughSlotsForProject(is_private, total_memberships,
                                               error_message)

        # Async mode
        if settings.CELERY_ENABLED:
            task = tasks.load_project_dump.delay(user, dump)
            return response.Accepted({"import_id": task.id})

        # Sync mode
        try:
            project = services.store_project_from_dict(dump, request.user)
        except err.TaigaImportError as e:
            # On Error
            ## remove project
            if e.project:
                e.project.delete_related_content()
                e.project.delete()

            return response.BadRequest({
                "error": e.message,
                "details": e.errors
            })
        else:
            # On Success
            project_from_qs = project_utils.attach_extra_info(
                Project.objects.all()).get(id=project.id)
            response_data = ProjectSerializer(project_from_qs).data

            return response.Created(response_data)