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.

82 lines
2.3 KiB

  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestUpdateAssignee(t *testing.T) {
  10. assert.NoError(t, PrepareTestDatabase())
  11. // Fake issue with assignees
  12. issue, err := GetIssueWithAttrsByID(1)
  13. assert.NoError(t, err)
  14. // Assign multiple users
  15. user2, err := GetUserByID(2)
  16. assert.NoError(t, err)
  17. _, _, err = issue.ToggleAssignee(&User{ID: 1}, user2.ID)
  18. assert.NoError(t, err)
  19. user3, err := GetUserByID(3)
  20. assert.NoError(t, err)
  21. _, _, err = issue.ToggleAssignee(&User{ID: 1}, user3.ID)
  22. assert.NoError(t, err)
  23. user1, err := GetUserByID(1) // This user is already assigned (see the definition in fixtures), so running UpdateAssignee should unassign him
  24. assert.NoError(t, err)
  25. _, _, err = issue.ToggleAssignee(&User{ID: 1}, user1.ID)
  26. assert.NoError(t, err)
  27. // Check if he got removed
  28. isAssigned, err := IsUserAssignedToIssue(issue, user1)
  29. assert.NoError(t, err)
  30. assert.False(t, isAssigned)
  31. // Check if they're all there
  32. assignees, err := GetAssigneesByIssue(issue)
  33. assert.NoError(t, err)
  34. var expectedAssignees []*User
  35. expectedAssignees = append(expectedAssignees, user2, user3)
  36. for in, assignee := range assignees {
  37. assert.Equal(t, assignee.ID, expectedAssignees[in].ID)
  38. }
  39. // Check if the user is assigned
  40. isAssigned, err = IsUserAssignedToIssue(issue, user2)
  41. assert.NoError(t, err)
  42. assert.True(t, isAssigned)
  43. // This user should not be assigned
  44. isAssigned, err = IsUserAssignedToIssue(issue, &User{ID: 4})
  45. assert.NoError(t, err)
  46. assert.False(t, isAssigned)
  47. }
  48. func TestMakeIDsFromAPIAssigneesToAdd(t *testing.T) {
  49. IDs, err := MakeIDsFromAPIAssigneesToAdd("", []string{""})
  50. assert.NoError(t, err)
  51. assert.Equal(t, []int64{}, IDs)
  52. IDs, err = MakeIDsFromAPIAssigneesToAdd("", []string{"none_existing_user"})
  53. assert.Error(t, err)
  54. IDs, err = MakeIDsFromAPIAssigneesToAdd("user1", []string{"user1"})
  55. assert.NoError(t, err)
  56. assert.Equal(t, []int64{1}, IDs)
  57. IDs, err = MakeIDsFromAPIAssigneesToAdd("user2", []string{""})
  58. assert.NoError(t, err)
  59. assert.Equal(t, []int64{2}, IDs)
  60. IDs, err = MakeIDsFromAPIAssigneesToAdd("", []string{"user1", "user2"})
  61. assert.NoError(t, err)
  62. assert.Equal(t, []int64{1, 2}, IDs)
  63. }