code.gitea.io/gitea@v1.21.7/models/issues/assignees_test.go (about) 1 // Copyright 2018 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package issues_test 5 6 import ( 7 "testing" 8 9 "code.gitea.io/gitea/models/db" 10 issues_model "code.gitea.io/gitea/models/issues" 11 "code.gitea.io/gitea/models/unittest" 12 user_model "code.gitea.io/gitea/models/user" 13 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestUpdateAssignee(t *testing.T) { 18 assert.NoError(t, unittest.PrepareTestDatabase()) 19 20 // Fake issue with assignees 21 issue, err := issues_model.GetIssueWithAttrsByID(1) 22 assert.NoError(t, err) 23 24 // Assign multiple users 25 user2, err := user_model.GetUserByID(db.DefaultContext, 2) 26 assert.NoError(t, err) 27 _, _, err = issues_model.ToggleIssueAssignee(db.DefaultContext, issue, &user_model.User{ID: 1}, user2.ID) 28 assert.NoError(t, err) 29 30 org3, err := user_model.GetUserByID(db.DefaultContext, 3) 31 assert.NoError(t, err) 32 _, _, err = issues_model.ToggleIssueAssignee(db.DefaultContext, issue, &user_model.User{ID: 1}, org3.ID) 33 assert.NoError(t, err) 34 35 user1, err := user_model.GetUserByID(db.DefaultContext, 1) // This user is already assigned (see the definition in fixtures), so running UpdateAssignee should unassign him 36 assert.NoError(t, err) 37 _, _, err = issues_model.ToggleIssueAssignee(db.DefaultContext, issue, &user_model.User{ID: 1}, user1.ID) 38 assert.NoError(t, err) 39 40 // Check if he got removed 41 isAssigned, err := issues_model.IsUserAssignedToIssue(db.DefaultContext, issue, user1) 42 assert.NoError(t, err) 43 assert.False(t, isAssigned) 44 45 // Check if they're all there 46 err = issue.LoadAssignees(db.DefaultContext) 47 assert.NoError(t, err) 48 49 var expectedAssignees []*user_model.User 50 expectedAssignees = append(expectedAssignees, user2, org3) 51 52 for in, assignee := range issue.Assignees { 53 assert.Equal(t, assignee.ID, expectedAssignees[in].ID) 54 } 55 56 // Check if the user is assigned 57 isAssigned, err = issues_model.IsUserAssignedToIssue(db.DefaultContext, issue, user2) 58 assert.NoError(t, err) 59 assert.True(t, isAssigned) 60 61 // This user should not be assigned 62 isAssigned, err = issues_model.IsUserAssignedToIssue(db.DefaultContext, issue, &user_model.User{ID: 4}) 63 assert.NoError(t, err) 64 assert.False(t, isAssigned) 65 } 66 67 func TestMakeIDsFromAPIAssigneesToAdd(t *testing.T) { 68 assert.NoError(t, unittest.PrepareTestDatabase()) 69 70 _ = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) 71 _ = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 72 73 IDs, err := issues_model.MakeIDsFromAPIAssigneesToAdd(db.DefaultContext, "", []string{""}) 74 assert.NoError(t, err) 75 assert.Equal(t, []int64{}, IDs) 76 77 _, err = issues_model.MakeIDsFromAPIAssigneesToAdd(db.DefaultContext, "", []string{"none_existing_user"}) 78 assert.Error(t, err) 79 80 IDs, err = issues_model.MakeIDsFromAPIAssigneesToAdd(db.DefaultContext, "user1", []string{"user1"}) 81 assert.NoError(t, err) 82 assert.Equal(t, []int64{1}, IDs) 83 84 IDs, err = issues_model.MakeIDsFromAPIAssigneesToAdd(db.DefaultContext, "user2", []string{""}) 85 assert.NoError(t, err) 86 assert.Equal(t, []int64{2}, IDs) 87 88 IDs, err = issues_model.MakeIDsFromAPIAssigneesToAdd(db.DefaultContext, "", []string{"user1", "user2"}) 89 assert.NoError(t, err) 90 assert.Equal(t, []int64{1, 2}, IDs) 91 }