mirror of
https://github.com/go-vikunja/vikunja.git
synced 2025-12-05 19:16:51 -06:00
Resolves https://github.com/go-vikunja/vikunja/issues/1745 - [x] Understand the issue from GitHub issue #1745 - [x] Analyze the codebase to locate the bug in `duplicateProjectBackground` function - [x] Fix the bug: return nil explicitly at the end of duplicateProjectBackground - [x] Add test for duplicating a project with an uploaded background (as subtest) - [x] Run tests and verify the fix - [x] Run code review and address any feedback - [x] Run CodeQL security scan ## Summary of Changes ### Problem When duplicating a project with an uploaded (non-Unsplash) background image, users encounter an internal server error (HTTP 500). The backend logs show: `file was not downloaded from unsplash [FileID: X]` ### Root Cause The `duplicateProjectBackground` function in `pkg/models/project_duplicate.go` uses named returns. When `GetUnsplashPhotoByFileID` returns `ErrFileIsNotUnsplashFile` for an uploaded background, the error was intentionally ignored (to proceed with copying the file) but not cleared from the named return variable. This caused the error to be returned at the end of the function via the bare `return` statement, triggering a 500 response. ### Solution Changed the bare `return` at the end of `duplicateProjectBackground` to `return nil` explicitly. ### Changes 1. **`pkg/models/project_duplicate.go`**: Changed bare `return` to `return nil` at the end of `duplicateProjectBackground` 2. **`pkg/models/project_duplicate_test.go`**: Added subtest "duplicate project with uploaded background" to `TestProjectDuplicate` ### Testing - All existing tests pass - Added subtest to `TestProjectDuplicate` for uploaded background scenario (project 35 with non-Unsplash background) ### Security Summary - No security vulnerabilities found by CodeQL - Code review passed <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > # Duplicate project with uploaded background - Implementation Plan > > ## Overview > Users encounter an internal server error when duplicating a project that uses an uploaded background image (non-Unsplash). The b > ackend attempt to copy the background leaves a non-Unsplash error (`ErrFileIsNotUnsplashFile`) in a named return value, causing > the duplication API call to fail even though the error should be ignored. We need to adjust the duplication flow to allow upload > ed backgrounds and add regression tests. > > ## Current State Analysis > - Project duplication calls `duplicateProjectBackground` to copy the background file. The helper tries to copy a downloaded Unsp > lash image and returns `ErrFileIsNotUnsplashFile` for uploaded files. > - In the duplication code, the error variable is not cleared after intentionally ignoring this specific error, so the function s > till returns the error and triggers a 500 response. > - There are no automated regression tests covering project duplication with uploaded backgrounds. > > ### Key Discoveries > - The duplication logic treats Unsplash and uploaded backgrounds differently and only clears the Unsplash download error, leavin > g the non-Unsplash error set. > - The API currently works for Unsplash backgrounds but fails for uploaded backgrounds due to the lingering error value. > > ## Desired End State > - Duplicating a project succeeds for both Unsplash and uploaded backgrounds. > - Uploaded background files (and their metadata) are copied correctly to the new project when possible, or gracefully skipped wi > thout failing duplication. > - Regression tests cover duplication with both background types to prevent future regressions. > > ## What We're NOT Doing > - No changes to the background upload endpoints or UI selection workflow. > - No changes to Unsplash download behavior or quota handling. > - No new migration or database schema changes. > > ## Implementation Approach > 1. Fix backend duplication error handling so uploaded backgrounds do not cause a fatal error. > 2. Add backend tests to cover duplication with uploaded backgrounds and Unsplash backgrounds (success paths) and verify duplicat > ion works without returning 500 errors. > 3. Ensure tests document the expected behavior and guard against regressions. > > ## Phase 1: Fix duplication error handling > ### Overview > Make project duplication tolerate uploaded backgrounds by clearing or not propagating `ErrFileIsNotUnsplashFile` once it has bee > n intentionally ignored. > > ### Changes Required > - **File:** `pkg/models/projects.go` (or relevant duplication helper) > - Adjust `duplicateProjectBackground` (or the calling logic) to reset the named return error after handling `ErrFileIsNotUnspl > ashFile`, ensuring the function returns `nil` when no real error occurs. > - Keep existing behavior for other errors and for Unsplash downloads. > > ### Success Criteria > - Uploaded background duplication no longer returns an internal server error. > - Unsplash background duplication remains functional and still surfaces real errors. > > ## Phase 2: Add regression tests > ### Overview > Add automated tests verifying project duplication works for both uploaded and Unsplash backgrounds. > > ### Changes Required > - **File:** `pkg/models/projects_test.go` (or closest existing test file for project duplication) > - Add a test that sets up a project with an uploaded background file, duplicates the project, and asserts duplication succeeds > and the duplicated project has an appropriate background reference. > - Add/adjust test coverage for Unsplash background duplication to confirm unchanged behavior. > - Use existing fixtures or temporary files as needed for uploaded background setup. > > ### Success Criteria > - Tests fail on current main branch but pass after the fix. > - Tests validate that duplication completes without 500 errors for both background types. > > ## Testing Strategy > - Automated Go tests via `mage test:filter` targeting the new duplication tests. > - Optionally run the broader suite (`mage test:feature`) if time permits to ensure no regressions. > > ## Manual Verification > 1. Create a project and upload a background via the UI; duplicate it; observe duplication succeeds and background is present or > gracefully handled. > 2. Create a project with an Unsplash background; duplicate it; verify duplication succeeds. > 3. Check API responses for duplication calls to ensure no internal server errors. </details> <!-- START COPILOT CODING AGENT TIPS --> --- 💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://gh.io/copilot-coding-agent-survey). --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: kolaente <13721712+kolaente@users.noreply.github.com>
155 lines
5.5 KiB
Go
155 lines
5.5 KiB
Go
// Vikunja is a to-do list application to facilitate your life.
|
|
// Copyright 2018-present Vikunja and contributors. All rights reserved.
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
package models
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"code.vikunja.io/api/pkg/db"
|
|
"code.vikunja.io/api/pkg/files"
|
|
"code.vikunja.io/api/pkg/user"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestProjectDuplicate(t *testing.T) {
|
|
t.Run("duplicate project", func(t *testing.T) {
|
|
testProjectDuplicate(t, 1, 1)
|
|
})
|
|
|
|
t.Run("duplicate project with uploaded background", func(t *testing.T) {
|
|
// Project 35 has a background_file_id of 1, which is NOT an Unsplash photo
|
|
// This tests the fix for issue #1745 where duplicating a project with an uploaded
|
|
// (non-Unsplash) background would fail with an internal server error
|
|
testProjectDuplicate(t, 35, 6)
|
|
})
|
|
}
|
|
|
|
func testProjectDuplicate(t *testing.T, projectID int64, userID int64) {
|
|
files.InitTestFileFixtures(t)
|
|
db.LoadAndAssertFixtures(t)
|
|
s := db.NewSession()
|
|
defer s.Close()
|
|
|
|
u := &user.User{
|
|
ID: userID,
|
|
}
|
|
|
|
l := &ProjectDuplicate{
|
|
ProjectID: projectID,
|
|
}
|
|
can, err := l.CanCreate(s, u)
|
|
require.NoError(t, err)
|
|
assert.True(t, can)
|
|
err = l.Create(s, u)
|
|
require.NoError(t, err)
|
|
|
|
originalProjectID := l.ProjectID
|
|
duplicatedProjectID := l.Project.ID
|
|
|
|
// assert the new project has the same number of buckets as the old one
|
|
numberOfOriginalViews, err := s.Where("project_id = ?", originalProjectID).Count(&ProjectView{})
|
|
require.NoError(t, err)
|
|
numberOfDuplicatedViews, err := s.Where("project_id = ?", duplicatedProjectID).Count(&ProjectView{})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, numberOfOriginalViews, numberOfDuplicatedViews, "duplicated project does not have the same amount of views as the original one")
|
|
|
|
// Check that the duplicated project has the same number of tasks as the original
|
|
numberOfOriginalTasks, err := s.Where("project_id = ?", originalProjectID).Count(&Task{})
|
|
require.NoError(t, err)
|
|
numberOfDuplicatedTasks, err := s.Where("project_id = ?", duplicatedProjectID).Count(&Task{})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, numberOfOriginalTasks, numberOfDuplicatedTasks, "duplicated project does not have the same amount of tasks as the original one")
|
|
|
|
// Check that each view has the same number of task positions between original and duplicated project
|
|
var originalViews []*ProjectView
|
|
err = s.Where("project_id = ?", originalProjectID).
|
|
OrderBy("position").
|
|
Find(&originalViews)
|
|
require.NoError(t, err)
|
|
|
|
var duplicatedViews []*ProjectView
|
|
err = s.Where("project_id = ?", duplicatedProjectID).
|
|
OrderBy("position").
|
|
Find(&duplicatedViews)
|
|
require.NoError(t, err)
|
|
|
|
// Create a map of original view positions to compare with duplicated views
|
|
originalViewPositions := make(map[int64]int64)
|
|
for _, view := range originalViews {
|
|
count, err := s.Where("project_view_id = ?", view.ID).Count(&TaskPosition{})
|
|
require.NoError(t, err)
|
|
originalViewPositions[view.ID] = count
|
|
}
|
|
|
|
// For each duplicated view, check if it has the same number of task positions as its original counterpart
|
|
for i, view := range duplicatedViews {
|
|
taskPositionsCount, err := s.Where("project_view_id = ?", view.ID).Count(&TaskPosition{})
|
|
require.NoError(t, err)
|
|
assert.Equal(
|
|
t,
|
|
originalViewPositions[originalViews[i].ID],
|
|
taskPositionsCount,
|
|
"duplicated view '%s' does not have the same amount of task positions as the original view", view.Title,
|
|
)
|
|
}
|
|
|
|
// Check that each view has the same number of task buckets between original and duplicated project
|
|
originalViewBuckets := make(map[int64]int64)
|
|
for _, view := range originalViews {
|
|
count, err := s.Where("project_view_id = ?", view.ID).Count(&TaskBucket{})
|
|
require.NoError(t, err)
|
|
originalViewBuckets[view.ID] = count
|
|
}
|
|
|
|
// For each duplicated view, check if it has the same number of task buckets as its original counterpart
|
|
for i, view := range duplicatedViews {
|
|
taskBucketsCount, err := s.Where("project_view_id = ?", view.ID).Count(&TaskBucket{})
|
|
require.NoError(t, err)
|
|
assert.Equal(
|
|
t,
|
|
originalViewBuckets[originalViews[i].ID],
|
|
taskBucketsCount,
|
|
"duplicated view '%s' does not have the same amount of task buckets as the original view", view.Title,
|
|
)
|
|
}
|
|
|
|
// Check that the kanban view in the duplicated project has a different default bucket than the original
|
|
// (only if the original kanban view has a default bucket configured)
|
|
var originalKanbanView *ProjectView
|
|
var duplicatedKanbanView *ProjectView
|
|
|
|
for _, view := range originalViews {
|
|
if view.ViewKind == ProjectViewKindKanban {
|
|
originalKanbanView = view
|
|
break
|
|
}
|
|
}
|
|
|
|
for _, view := range duplicatedViews {
|
|
if view.ViewKind == ProjectViewKindKanban {
|
|
duplicatedKanbanView = view
|
|
break
|
|
}
|
|
}
|
|
|
|
if originalKanbanView != nil && duplicatedKanbanView != nil && originalKanbanView.DefaultBucketID != 0 {
|
|
assert.NotEqual(t, originalKanbanView.DefaultBucketID, duplicatedKanbanView.DefaultBucketID)
|
|
}
|
|
}
|