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.

71 lines
1.9 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 = UpdateAssignee(issue, &User{ID: 1}, user2.ID)
  18. assert.NoError(t, err)
  19. user3, err := GetUserByID(3)
  20. assert.NoError(t, err)
  21. err = UpdateAssignee(issue, &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 = UpdateAssignee(issue, &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)
  36. expectedAssignees = append(expectedAssignees, user3)
  37. for in, assignee := range assignees {
  38. assert.Equal(t, assignee.ID, expectedAssignees[in].ID)
  39. }
  40. // Check if the user is assigned
  41. isAssigned, err = IsUserAssignedToIssue(issue, user2)
  42. assert.NoError(t, err)
  43. assert.True(t, isAssigned)
  44. // This user should not be assigned
  45. isAssigned, err = IsUserAssignedToIssue(issue, &User{ID: 4})
  46. assert.NoError(t, err)
  47. assert.False(t, isAssigned)
  48. // Clean everyone
  49. err = DeleteNotPassedAssignee(issue, user1, []*User{})
  50. assert.NoError(t, err)
  51. // Check they're gone
  52. assignees, err = GetAssigneesByIssue(issue)
  53. assert.NoError(t, err)
  54. assert.Equal(t, 0, len(assignees))
  55. }