You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
3.0 KiB
93 lines
3.0 KiB
7 years ago
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
3 years ago
|
package issues_test
|
||
7 years ago
|
|
||
|
import (
|
||
|
"testing"
|
||
|
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
3 years ago
|
issues_model "code.gitea.io/gitea/models/issues"
|
||
3 years ago
|
"code.gitea.io/gitea/models/unittest"
|
||
3 years ago
|
user_model "code.gitea.io/gitea/models/user"
|
||
3 years ago
|
|
||
7 years ago
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestUpdateAssignee(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
7 years ago
|
|
||
|
// Fake issue with assignees
|
||
3 years ago
|
issue, err := issues_model.GetIssueWithAttrsByID(1)
|
||
7 years ago
|
assert.NoError(t, err)
|
||
|
|
||
|
// Assign multiple users
|
||
3 years ago
|
user2, err := user_model.GetUserByID(2)
|
||
7 years ago
|
assert.NoError(t, err)
|
||
3 years ago
|
_, _, err = issues_model.ToggleIssueAssignee(issue, &user_model.User{ID: 1}, user2.ID)
|
||
7 years ago
|
assert.NoError(t, err)
|
||
|
|
||
3 years ago
|
user3, err := user_model.GetUserByID(3)
|
||
7 years ago
|
assert.NoError(t, err)
|
||
3 years ago
|
_, _, err = issues_model.ToggleIssueAssignee(issue, &user_model.User{ID: 1}, user3.ID)
|
||
7 years ago
|
assert.NoError(t, err)
|
||
|
|
||
3 years ago
|
user1, err := user_model.GetUserByID(1) // This user is already assigned (see the definition in fixtures), so running UpdateAssignee should unassign him
|
||
7 years ago
|
assert.NoError(t, err)
|
||
3 years ago
|
_, _, err = issues_model.ToggleIssueAssignee(issue, &user_model.User{ID: 1}, user1.ID)
|
||
7 years ago
|
assert.NoError(t, err)
|
||
|
|
||
|
// Check if he got removed
|
||
3 years ago
|
isAssigned, err := issues_model.IsUserAssignedToIssue(db.DefaultContext, issue, user1)
|
||
7 years ago
|
assert.NoError(t, err)
|
||
|
assert.False(t, isAssigned)
|
||
|
|
||
|
// Check if they're all there
|
||
3 years ago
|
err = issue.LoadAssignees(db.DefaultContext)
|
||
7 years ago
|
assert.NoError(t, err)
|
||
|
|
||
3 years ago
|
var expectedAssignees []*user_model.User
|
||
6 years ago
|
expectedAssignees = append(expectedAssignees, user2, user3)
|
||
7 years ago
|
|
||
3 years ago
|
for in, assignee := range issue.Assignees {
|
||
7 years ago
|
assert.Equal(t, assignee.ID, expectedAssignees[in].ID)
|
||
|
}
|
||
|
|
||
|
// Check if the user is assigned
|
||
3 years ago
|
isAssigned, err = issues_model.IsUserAssignedToIssue(db.DefaultContext, issue, user2)
|
||
7 years ago
|
assert.NoError(t, err)
|
||
|
assert.True(t, isAssigned)
|
||
|
|
||
|
// This user should not be assigned
|
||
3 years ago
|
isAssigned, err = issues_model.IsUserAssignedToIssue(db.DefaultContext, issue, &user_model.User{ID: 4})
|
||
7 years ago
|
assert.NoError(t, err)
|
||
|
assert.False(t, isAssigned)
|
||
|
}
|
||
5 years ago
|
|
||
|
func TestMakeIDsFromAPIAssigneesToAdd(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
4 years ago
|
|
||
3 years ago
|
_ = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
||
|
_ = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
||
4 years ago
|
|
||
3 years ago
|
IDs, err := issues_model.MakeIDsFromAPIAssigneesToAdd("", []string{""})
|
||
5 years ago
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, []int64{}, IDs)
|
||
|
|
||
3 years ago
|
_, err = issues_model.MakeIDsFromAPIAssigneesToAdd("", []string{"none_existing_user"})
|
||
5 years ago
|
assert.Error(t, err)
|
||
|
|
||
3 years ago
|
IDs, err = issues_model.MakeIDsFromAPIAssigneesToAdd("user1", []string{"user1"})
|
||
5 years ago
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, []int64{1}, IDs)
|
||
|
|
||
3 years ago
|
IDs, err = issues_model.MakeIDsFromAPIAssigneesToAdd("user2", []string{""})
|
||
5 years ago
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, []int64{2}, IDs)
|
||
|
|
||
3 years ago
|
IDs, err = issues_model.MakeIDsFromAPIAssigneesToAdd("", []string{"user1", "user2"})
|
||
5 years ago
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, []int64{1, 2}, IDs)
|
||
|
}
|