Files
vikunja/pkg/models/reaction_test.go
kolaente a7086e5e49 fix: prevent session leaks and visibility issues in model tests
Two categories of fixes:

1. Use defer s.Close() instead of explicit s.Close() to prevent session
   leaks when require.FailNow() triggers runtime.Goexit(), which skips
   explicit close calls but runs deferred functions. Leaked sessions
   hold SQLite write locks that block all subsequent fixture loading.

2. Add s.Commit() before db.AssertExists/db.AssertMissing calls. These
   assertion helpers query via the global engine (not the test session),
   so they cannot see uncommitted data from the session's transaction.

For block-scoped sessions (kanban_task_bucket_test.go), wrap each block
in an anonymous function so defer runs at block boundary rather than
deferring to the enclosing test function.
2026-02-25 11:03:02 +01:00

219 lines
5.1 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/user"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestReaction_ReadAll(t *testing.T) {
t.Run("normal", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 1,
EntityKindString: "tasks",
}
reactions, _, _, err := r.ReadAll(s, u, "", 0, 0)
require.NoError(t, err)
assert.IsType(t, ReactionMap{}, reactions)
reactionMap := reactions.(ReactionMap)
assert.Len(t, reactionMap["👋"], 1)
assert.Equal(t, int64(1), reactionMap["👋"][0].ID)
})
t.Run("invalid entity", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 1,
EntityKindString: "loremipsum",
}
_, _, _, err := r.ReadAll(s, u, "", 0, 0)
require.Error(t, err)
assert.ErrorIs(t, err, ErrInvalidReactionEntityKind{Kind: "loremipsum"})
})
t.Run("no access to task", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 34,
EntityKindString: "tasks",
}
_, _, _, err := r.ReadAll(s, u, "", 0, 0)
require.Error(t, err)
})
t.Run("nonexistant task", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 9999999,
EntityKindString: "tasks",
}
_, _, _, err := r.ReadAll(s, u, "", 0, 0)
require.Error(t, err)
assert.ErrorIs(t, err, ErrTaskDoesNotExist{ID: r.EntityID})
})
t.Run("no access to comment", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 18,
EntityKindString: "comments",
}
_, _, _, err := r.ReadAll(s, u, "", 0, 0)
require.Error(t, err)
})
t.Run("nonexistant comment", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 9999999,
EntityKindString: "comments",
}
_, _, _, err := r.ReadAll(s, u, "", 0, 0)
require.Error(t, err)
assert.ErrorIs(t, err, ErrTaskCommentDoesNotExist{ID: r.EntityID})
})
}
func TestReaction_Create(t *testing.T) {
t.Run("normal", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 1,
EntityKindString: "tasks",
Value: "🦙",
}
can, err := r.CanCreate(s, u)
require.NoError(t, err)
assert.True(t, can)
err = r.Create(s, u)
require.NoError(t, err)
err = s.Commit()
require.NoError(t, err)
db.AssertExists(t, "reactions", map[string]interface{}{
"entity_id": r.EntityID,
"entity_kind": ReactionKindTask,
"user_id": u.ID,
"value": r.Value,
}, false)
})
t.Run("no permission to access task", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 34,
EntityKindString: "tasks",
Value: "🦙",
}
can, err := r.CanCreate(s, u)
require.NoError(t, err)
assert.False(t, can)
})
t.Run("no permission to access comment", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 18,
EntityKindString: "comments",
Value: "🦙",
}
can, err := r.CanCreate(s, u)
require.NoError(t, err)
assert.False(t, can)
})
}
func TestReaction_Delete(t *testing.T) {
t.Run("normal", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 1,
EntityKindString: "tasks",
Value: "👋",
}
can, err := r.CanDelete(s, u)
require.NoError(t, err)
assert.True(t, can)
err = r.Delete(s, u)
require.NoError(t, err)
require.NoError(t, s.Commit())
db.AssertMissing(t, "reactions", map[string]interface{}{
"entity_id": r.EntityID,
"entity_kind": ReactionKindTask,
"value": "👋",
})
})
}