Make sure tasks are properly attributed to link shares

This commit is contained in:
kolaente 2021-04-06 14:54:21 +02:00
parent 9ca9540eba
commit df21e04daf
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
5 changed files with 64 additions and 25 deletions

View File

@ -338,5 +338,11 @@
bucket_id: 20 bucket_id: 20
created: 2018-12-01 01:12:04 created: 2018-12-01 01:12:04
updated: 2018-12-01 01:12:04 updated: 2018-12-01 01:12:04
- id: 37
title: 'task #37'
done: false
created_by_id: -2
list_id: 2
index: 2
created: 2018-12-01 01:12:04
updated: 2018-12-01 01:12:04

View File

@ -295,16 +295,16 @@ func TestTaskComments(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Contains(t, rec.Body.String(), `"comment":"Lorem Ipsum"`) assert.Contains(t, rec.Body.String(), `"comment":"Lorem Ipsum"`)
}) })
t.Run("Link Share", func(t *testing.T) { })
rec, err := testHandlerLinkShareWrite.testCreateWithLinkShare(nil, map[string]string{"task": "13"}, `{"comment":"Lorem Ipsum"}`) t.Run("Link Share", func(t *testing.T) {
assert.NoError(t, err) rec, err := testHandlerLinkShareWrite.testCreateWithLinkShare(nil, map[string]string{"task": "13"}, `{"comment":"Lorem Ipsum"}`)
assert.Contains(t, rec.Body.String(), `"comment":"Lorem Ipsum"`) assert.NoError(t, err)
db.AssertExists(t, "task_comments", map[string]interface{}{ assert.Contains(t, rec.Body.String(), `"comment":"Lorem Ipsum"`)
"task_id": 13, db.AssertExists(t, "task_comments", map[string]interface{}{
"comment": "Lorem Ipsum", "task_id": 13,
"author_id": -2, "comment": "Lorem Ipsum",
}, false) "author_id": -2,
}) }, false)
}) })
}) })
} }

View File

@ -17,6 +17,7 @@
package integrations package integrations
import ( import (
"code.vikunja.io/api/pkg/db"
"testing" "testing"
"code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/models"
@ -33,6 +34,20 @@ func TestTask(t *testing.T) {
}, },
t: 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.Task{}
},
t: t,
}
// Only run specific nested tests: // Only run specific nested tests:
// ^TestTask$/^Update$/^Update_task_items$/^Removing_Assignees_null$ // ^TestTask$/^Update$/^Update_task_items$/^Removing_Assignees_null$
t.Run("Update", func(t *testing.T) { t.Run("Update", func(t *testing.T) {
@ -489,5 +504,15 @@ func TestTask(t *testing.T) {
assertHandlerErrorCode(t, err, models.ErrCodeBucketDoesNotExist) assertHandlerErrorCode(t, err, models.ErrCodeBucketDoesNotExist)
}) })
}) })
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, "tasks", map[string]interface{}{
"list_id": 2,
"title": "Lorem Ipsum",
"created_by_id": -2,
}, false)
})
}) })
} }

View File

@ -670,7 +670,7 @@ func addMoreInfoToTasks(s *xorm.Session, taskMap map[int64]*Task) (err error) {
return return
} }
users, err := user.GetUsersByIDs(s, userIDs) users, err := getUsersOrLinkSharesFromIDs(s, userIDs)
if err != nil { if err != nil {
return return
} }
@ -817,17 +817,11 @@ func createTask(s *xorm.Session, t *Task, a web.Auth, updateAssignees bool) (err
return err return err
} }
if _, is := a.(*LinkSharing); is { createdBy, err := getUserOrLinkShareUser(s, a)
// A negative user id indicates user share links if err != nil {
t.CreatedByID = a.GetID() * -1 return err
} else {
u, err := user.GetUserByID(s, a.GetID())
if err != nil {
return err
}
t.CreatedByID = u.ID
t.CreatedBy = u
} }
t.CreatedByID = createdBy.ID
// Generate a uuid if we don't already have one // Generate a uuid if we don't already have one
if t.UID == "" { if t.UID == "" {
@ -856,6 +850,8 @@ func createTask(s *xorm.Session, t *Task, a web.Auth, updateAssignees bool) (err
return err return err
} }
t.CreatedBy = createdBy
// Update the assignees // Update the assignees
if updateAssignees { if updateAssignees {
if err := t.updateTaskAssignees(s, t.Assignees, a); err != nil { if err := t.updateTaskAssignees(s, t.Assignees, a); err != nil {
@ -870,10 +866,9 @@ func createTask(s *xorm.Session, t *Task, a web.Auth, updateAssignees bool) (err
t.setIdentifier(l) t.setIdentifier(l)
doer, _ := user.GetFromAuth(a)
err = events.Dispatch(&TaskCreatedEvent{ err = events.Dispatch(&TaskCreatedEvent{
Task: t, Task: t,
Doer: doer, Doer: createdBy,
}) })
if err != nil { if err != nil {
return err return err

View File

@ -579,4 +579,17 @@ func TestTask_ReadOne(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, task.Subscription) assert.NotNil(t, task.Subscription)
}) })
t.Run("created by link share", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
task := &Task{ID: 37}
err := task.ReadOne(s, u)
assert.NoError(t, err)
assert.Equal(t, "task #37", task.Title)
assert.Equal(t, int64(-2), task.CreatedByID)
assert.NotNil(t, task.CreatedBy)
assert.Equal(t, int64(-2), task.CreatedBy.ID)
})
} }