Skip to content

Commit 3d6608c

Browse files
authored
feat(api/v2): add task duplicate action (#2815)
1 parent fd10300 commit 3d6608c

4 files changed

Lines changed: 145 additions & 1 deletion

File tree

pkg/models/task_duplicate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type TaskDuplicate struct {
3232
TaskID int64 `json:"-" param:"projecttask"`
3333

3434
// The duplicated task
35-
Task *Task `json:"duplicated_task,omitempty"`
35+
Task *Task `json:"duplicated_task,omitempty" readOnly:"true" doc:"The newly created duplicate task, populated by the server in the response."`
3636

3737
web.Permissions `json:"-"`
3838
web.CRUDable `json:"-"`
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Vikunja is a to-do list application to facilitate your life.
2+
// Copyright 2018-present Vikunja and contributors. All rights reserved.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
package apiv2
18+
19+
import (
20+
"context"
21+
"net/http"
22+
23+
"code.vikunja.io/api/pkg/models"
24+
"code.vikunja.io/api/pkg/web/handler"
25+
26+
"github.com/danielgtaylor/huma/v2"
27+
)
28+
29+
// RegisterTaskDuplicateRoutes wires the task-duplicate action onto the Huma API.
30+
// TaskDuplicate is a CRUDable Create, so the handler reuses handler.DoCreate
31+
// (its CanCreate enforces read-source + write-project); the only custom part is
32+
// taking TaskID from the path rather than a request body.
33+
func RegisterTaskDuplicateRoutes(api huma.API) {
34+
tags := []string{"tasks"}
35+
36+
Register(api, huma.Operation{
37+
OperationID: "tasks-duplicate",
38+
Summary: "Duplicate a task",
39+
Description: "Copies a task — including its labels, assignees, attachments and reminders — into the same project, and records a \"copied from\" relation back to the original. The authenticated user needs read access to the source task and write access to its project. Returns the newly created duplicate.",
40+
Method: http.MethodPost,
41+
Path: "/tasks/{projecttask}/duplicate",
42+
Tags: tags,
43+
}, tasksDuplicate)
44+
}
45+
46+
func tasksDuplicate(ctx context.Context, in *struct {
47+
TaskID int64 `path:"projecttask" doc:"The numeric id of the task to duplicate."`
48+
}) (*singleBody[models.TaskDuplicate], error) {
49+
a, err := authFromCtx(ctx)
50+
if err != nil {
51+
return nil, err
52+
}
53+
td := &models.TaskDuplicate{TaskID: in.TaskID}
54+
if err := handler.DoCreate(ctx, td, a); err != nil {
55+
return nil, translateDomainError(err)
56+
}
57+
return &singleBody[models.TaskDuplicate]{Body: td}, nil
58+
}

pkg/routes/routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ func registerAPIRoutesV2(e *echo.Echo, a *echo.Group) {
395395

396396
// Resource registrations.
397397
apiv2.RegisterLabelRoutes(api)
398+
apiv2.RegisterTaskDuplicateRoutes(api)
398399

399400
// AutoPatch must run AFTER all GET/PUT pairs are registered so it can
400401
// synthesize their PATCH counterparts.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Vikunja is a to-do list application to facilitate your life.
2+
// Copyright 2018-present Vikunja and contributors. All rights reserved.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
package webtests
18+
19+
import (
20+
"encoding/json"
21+
"net/http"
22+
"testing"
23+
24+
"github.com/stretchr/testify/assert"
25+
"github.com/stretchr/testify/require"
26+
)
27+
28+
// TestTaskDuplicateV2 covers POST /tasks/{projecttask}/duplicate. It drives the
29+
// Echo+Huma stack directly (humaRequest/humaTokenFor) because webHandlerTestV2's
30+
// buildURL only models base[/{id}] paths, not action sub-paths.
31+
func TestTaskDuplicateV2(t *testing.T) {
32+
t.Run("duplicates an accessible task", func(t *testing.T) {
33+
e, err := setupTestEnv()
34+
require.NoError(t, err)
35+
token := humaTokenFor(t, &testuser1)
36+
37+
// Task 2 lives in project 1, which testuser1 owns.
38+
const sourceTaskID int64 = 2
39+
rec := humaRequest(t, e, http.MethodPost, "/api/v2/tasks/2/duplicate", ``, token, "")
40+
require.Equal(t, http.StatusCreated, rec.Code, "body: %s", rec.Body.String())
41+
assert.Contains(t, rec.Body.String(), `"duplicated_task"`)
42+
assert.Contains(t, rec.Body.String(), `"title":"task #2 done"`)
43+
44+
// A returned original task would also pass the title check above; assert a new id.
45+
var resp struct {
46+
DuplicatedTask struct {
47+
ID int64 `json:"id"`
48+
} `json:"duplicated_task"`
49+
}
50+
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &resp))
51+
assert.NotZero(t, resp.DuplicatedTask.ID, "duplicated task should have an id")
52+
assert.NotEqual(t, sourceTaskID, resp.DuplicatedTask.ID, "duplicated task must have a new id, not the source task's")
53+
})
54+
55+
t.Run("nonexistent source task", func(t *testing.T) {
56+
e, err := setupTestEnv()
57+
require.NoError(t, err)
58+
token := humaTokenFor(t, &testuser1)
59+
60+
rec := humaRequest(t, e, http.MethodPost, "/api/v2/tasks/99999/duplicate", `{}`, token, "")
61+
// Missing source task yields ErrTaskDoesNotExist (404), not the 403 of the permission cases below.
62+
require.Equal(t, http.StatusNotFound, rec.Code, "body: %s", rec.Body.String())
63+
})
64+
65+
t.Run("no read on source task is forbidden", func(t *testing.T) {
66+
e, err := setupTestEnv()
67+
require.NoError(t, err)
68+
// testuser15 cannot read task 1 (project 1, owned by testuser1).
69+
token := humaTokenFor(t, &testuser15)
70+
71+
rec := humaRequest(t, e, http.MethodPost, "/api/v2/tasks/1/duplicate", `{}`, token, "")
72+
require.Equal(t, http.StatusForbidden, rec.Code, "body: %s", rec.Body.String())
73+
})
74+
75+
t.Run("read but no write on source project is forbidden", func(t *testing.T) {
76+
e, err := setupTestEnv()
77+
require.NoError(t, err)
78+
// Task 32 lives in project 3, on which testuser1 has read-only access:
79+
// CanRead passes, CanUpdate on the project fails, so CanCreate denies.
80+
token := humaTokenFor(t, &testuser1)
81+
82+
rec := humaRequest(t, e, http.MethodPost, "/api/v2/tasks/32/duplicate", `{}`, token, "")
83+
require.Equal(t, http.StatusForbidden, rec.Code, "body: %s", rec.Body.String())
84+
})
85+
}

0 commit comments

Comments
 (0)