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.

164 lines
4.7 KiB

  1. // Copyright 2017 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 repo
  5. import (
  6. "net/http"
  7. "strconv"
  8. "testing"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/auth"
  11. "code.gitea.io/gitea/modules/test"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func int64SliceToCommaSeparated(a []int64) string {
  15. s := ""
  16. for i, n := range a {
  17. if i > 0 {
  18. s += ","
  19. }
  20. s += strconv.Itoa(int(n))
  21. }
  22. return s
  23. }
  24. func TestInitializeLabels(t *testing.T) {
  25. models.PrepareTestEnv(t)
  26. ctx := test.MockContext(t, "user2/repo1/labels/initialize")
  27. test.LoadUser(t, ctx, 2)
  28. test.LoadRepo(t, ctx, 2)
  29. InitializeLabels(ctx, auth.InitializeLabelsForm{"Default"})
  30. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  31. models.AssertExistsAndLoadBean(t, &models.Label{
  32. RepoID: 2,
  33. Name: "enhancement",
  34. Color: "#84b6eb",
  35. })
  36. assert.Equal(t, "/user2/repo2/labels", test.RedirectURL(ctx.Resp))
  37. }
  38. func TestRetrieveLabels(t *testing.T) {
  39. models.PrepareTestEnv(t)
  40. for _, testCase := range []struct {
  41. RepoID int64
  42. Sort string
  43. ExpectedLabelIDs []int64
  44. }{
  45. {1, "", []int64{1, 2}},
  46. {1, "leastissues", []int64{2, 1}},
  47. {2, "", []int64{}},
  48. } {
  49. ctx := test.MockContext(t, "user/repo/issues")
  50. test.LoadUser(t, ctx, 2)
  51. test.LoadRepo(t, ctx, testCase.RepoID)
  52. ctx.Req.Form.Set("sort", testCase.Sort)
  53. RetrieveLabels(ctx)
  54. assert.False(t, ctx.Written())
  55. labels, ok := ctx.Data["Labels"].([]*models.Label)
  56. assert.True(t, ok)
  57. if assert.Len(t, labels, len(testCase.ExpectedLabelIDs)) {
  58. for i, label := range labels {
  59. assert.EqualValues(t, testCase.ExpectedLabelIDs[i], label.ID)
  60. }
  61. }
  62. }
  63. }
  64. func TestNewLabel(t *testing.T) {
  65. models.PrepareTestEnv(t)
  66. ctx := test.MockContext(t, "user2/repo1/labels/edit")
  67. test.LoadUser(t, ctx, 2)
  68. test.LoadRepo(t, ctx, 1)
  69. NewLabel(ctx, auth.CreateLabelForm{
  70. Title: "newlabel",
  71. Color: "#abcdef",
  72. })
  73. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  74. models.AssertExistsAndLoadBean(t, &models.Label{
  75. Name: "newlabel",
  76. Color: "#abcdef",
  77. })
  78. assert.Equal(t, "/user2/repo1/labels", test.RedirectURL(ctx.Resp))
  79. }
  80. func TestUpdateLabel(t *testing.T) {
  81. models.PrepareTestEnv(t)
  82. ctx := test.MockContext(t, "user2/repo1/labels/edit")
  83. test.LoadUser(t, ctx, 2)
  84. test.LoadRepo(t, ctx, 1)
  85. UpdateLabel(ctx, auth.CreateLabelForm{
  86. ID: 2,
  87. Title: "newnameforlabel",
  88. Color: "#abcdef",
  89. })
  90. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  91. models.AssertExistsAndLoadBean(t, &models.Label{
  92. ID: 2,
  93. Name: "newnameforlabel",
  94. Color: "#abcdef",
  95. })
  96. assert.Equal(t, "/user2/repo1/labels", test.RedirectURL(ctx.Resp))
  97. }
  98. func TestDeleteLabel(t *testing.T) {
  99. models.PrepareTestEnv(t)
  100. ctx := test.MockContext(t, "user2/repo1/labels/delete")
  101. test.LoadUser(t, ctx, 2)
  102. test.LoadRepo(t, ctx, 1)
  103. ctx.Req.Form.Set("id", "2")
  104. DeleteLabel(ctx)
  105. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  106. models.AssertNotExistsBean(t, &models.Label{ID: 2})
  107. models.AssertNotExistsBean(t, &models.IssueLabel{LabelID: 2})
  108. assert.Equal(t, ctx.Tr("repo.issues.label_deletion_success"), ctx.Flash.SuccessMsg)
  109. }
  110. func TestUpdateIssueLabel_Clear(t *testing.T) {
  111. models.PrepareTestEnv(t)
  112. ctx := test.MockContext(t, "user2/repo1/issues/labels")
  113. test.LoadUser(t, ctx, 2)
  114. test.LoadRepo(t, ctx, 1)
  115. ctx.Req.Form.Set("issue_ids", "1,3")
  116. ctx.Req.Form.Set("action", "clear")
  117. UpdateIssueLabel(ctx)
  118. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  119. models.AssertNotExistsBean(t, &models.IssueLabel{IssueID: 1})
  120. models.AssertNotExistsBean(t, &models.IssueLabel{IssueID: 3})
  121. models.CheckConsistencyFor(t, &models.Label{})
  122. }
  123. func TestUpdateIssueLabel_Toggle(t *testing.T) {
  124. for _, testCase := range []struct {
  125. Action string
  126. IssueIDs []int64
  127. LabelID int64
  128. ExpectedAdd bool // whether we expect the label to be added to the issues
  129. }{
  130. {"attach", []int64{1, 3}, 1, true},
  131. {"detach", []int64{1, 3}, 1, false},
  132. {"toggle", []int64{1, 3}, 1, false},
  133. {"toggle", []int64{1, 2}, 2, true},
  134. } {
  135. models.PrepareTestEnv(t)
  136. ctx := test.MockContext(t, "user2/repo1/issues/labels")
  137. test.LoadUser(t, ctx, 2)
  138. test.LoadRepo(t, ctx, 1)
  139. ctx.Req.Form.Set("issue_ids", int64SliceToCommaSeparated(testCase.IssueIDs))
  140. ctx.Req.Form.Set("action", testCase.Action)
  141. ctx.Req.Form.Set("id", strconv.Itoa(int(testCase.LabelID)))
  142. UpdateIssueLabel(ctx)
  143. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  144. for _, issueID := range testCase.IssueIDs {
  145. models.AssertExistsIf(t, testCase.ExpectedAdd, &models.IssueLabel{
  146. IssueID: issueID,
  147. LabelID: testCase.LabelID,
  148. })
  149. }
  150. models.CheckConsistencyFor(t, &models.Label{})
  151. }
  152. }