Make sure buckets are properly attributed to link shares

This commit is contained in:
kolaente 2021-04-06 15:23:04 +02:00
parent df21e04daf
commit 9a686efb5b
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
5 changed files with 62 additions and 9 deletions

View File

@ -209,3 +209,9 @@
created_by_id: 1
created: 2020-04-18 21:13:52
updated: 2020-04-18 21:13:52
- id: 35
title: testbucket35
list_id: 23
created_by_id: -2
created: 2020-04-18 21:13:52
updated: 2020-04-18 21:13:52

View File

@ -17,6 +17,7 @@
package integrations
import (
"code.vikunja.io/api/pkg/db"
"testing"
"code.vikunja.io/api/pkg/models"
@ -33,6 +34,20 @@ func TestBucket(t *testing.T) {
},
t: t,
}
testHandlerLinkShareWrite := webHandlerTest{
linkShare: &models.LinkSharing{
ID: 2,
Hash: "test2",
ListID: 2,
Right: models.RightWrite,
SharingType: models.SharingTypeWithoutPassword,
SharedByID: 1,
},
strFunc: func() handler.CObject {
return &models.Bucket{}
},
t: t,
}
t.Run("ReadAll", func(t *testing.T) {
t.Run("Normal", func(t *testing.T) {
rec, err := testHandler.testReadAllWithUser(nil, map[string]string{"list": "1"})
@ -297,5 +312,15 @@ func TestBucket(t *testing.T) {
assert.Contains(t, rec.Body.String(), `"title":"Lorem Ipsum"`)
})
})
t.Run("Link Share", func(t *testing.T) {
rec, err := testHandlerLinkShareWrite.testCreateWithLinkShare(nil, map[string]string{"list": "2"}, `{"title":"Lorem Ipsum"}`)
assert.NoError(t, err)
assert.Contains(t, rec.Body.String(), `"title":"Lorem Ipsum"`)
db.AssertExists(t, "buckets", map[string]interface{}{
"list_id": 2,
"created_by_id": -2,
"title": "Lorem Ipsum",
}, false)
})
})
}

View File

@ -135,12 +135,9 @@ func (b *Bucket) ReadAll(s *xorm.Session, auth web.Auth, search string, page int
}
// Get all users
users := make(map[int64]*user.User)
if len(userIDs) > 0 {
err = s.In("id", userIDs).Find(&users)
if err != nil {
return
}
users, err := getUsersOrLinkSharesFromIDs(s, userIDs)
if err != nil {
return
}
for _, bb := range buckets {
@ -234,7 +231,11 @@ func (b *Bucket) ReadAll(s *xorm.Session, auth web.Auth, search string, page int
// @Failure 500 {object} models.Message "Internal error"
// @Router /lists/{id}/buckets [put]
func (b *Bucket) Create(s *xorm.Session, a web.Auth) (err error) {
b.CreatedByID = a.GetID()
b.CreatedBy, err = getUserOrLinkShareUser(s, a)
if err != nil {
return
}
b.CreatedByID = b.CreatedBy.ID
_, err = s.Insert(b)
return

View File

@ -89,6 +89,20 @@ func TestBucket_ReadAll(t *testing.T) {
assert.Len(t, buckets, 3)
assert.Equal(t, int64(2), buckets[0].Tasks[0].ID)
})
t.Run("link share", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
testuser := &user.User{ID: 1}
b := &Bucket{ListID: 23}
result, _, _, err := b.ReadAll(s, testuser, "", 0, 0)
assert.NoError(t, err)
buckets, _ := result.([]*Bucket)
assert.Len(t, buckets, 1)
assert.NotNil(t, buckets[0].CreatedBy)
assert.Equal(t, int64(-2), buckets[0].CreatedByID)
})
}
func TestBucket_Delete(t *testing.T) {

View File

@ -43,6 +43,7 @@ func getUserOrLinkShareUser(s *xorm.Session, a web.Auth) (uu *user.User, err err
// Returns all users or pseudo link shares from a slice of ids. ids < 0 are considered to be a link share in that case.
func getUsersOrLinkSharesFromIDs(s *xorm.Session, ids []int64) (users map[int64]*user.User, err error) {
users = make(map[int64]*user.User)
var userIDs []int64
var linkShareIDs []int64
for _, id := range ids {
@ -54,8 +55,14 @@ func getUsersOrLinkSharesFromIDs(s *xorm.Session, ids []int64) (users map[int64]
userIDs = append(userIDs, id)
}
users, err = user.GetUsersByIDs(s, userIDs)
if err != nil {
if len(userIDs) > 0 {
users, err = user.GetUsersByIDs(s, userIDs)
if err != nil {
return
}
}
if len(linkShareIDs) == 0 {
return
}