|
| 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