@@ -6,10 +6,12 @@ package repo
66import (
77 "net/http"
88 "strconv"
9+ "strings"
910 "testing"
1011
1112 issues_model "code.gitea.io/gitea/models/issues"
1213 "code.gitea.io/gitea/models/unittest"
14+ "code.gitea.io/gitea/modules/base"
1315 "code.gitea.io/gitea/modules/repository"
1416 "code.gitea.io/gitea/modules/test"
1517 "code.gitea.io/gitea/modules/web"
@@ -19,19 +21,20 @@ import (
1921 "github.com/stretchr/testify/assert"
2022)
2123
22- func int64SliceToCommaSeparated (a []int64 ) string {
23- s := ""
24- for i , n := range a {
25- if i > 0 {
26- s += ","
27- }
28- s += strconv .Itoa (int (n ))
29- }
30- return s
24+ func TestIssueLabel (t * testing.T ) {
25+ unittest .PrepareTestEnv (t )
26+ t .Run ("RetrieveLabels" , testRetrieveLabels )
27+ t .Run ("NewLabel" , testNewLabel )
28+ t .Run ("NewLabelInvalidColor" , testNewLabelInvalidColor )
29+ t .Run ("UpdateLabel" , testUpdateLabel )
30+ t .Run ("UpdateLabelInvalidColor" , testUpdateLabelInvalidColor )
31+ t .Run ("UpdateIssueLabel_Clear" , testUpdateIssueLabel_Clear )
32+ t .Run ("UpdateIssueLabel_Toggle" , testUpdateIssueLabel_Toggle )
33+ t .Run ("InitializeLabels" , testInitializeLabels )
34+ t .Run ("DeleteLabel" , testDeleteLabel )
3135}
3236
33- func TestInitializeLabels (t * testing.T ) {
34- unittest .PrepareTestEnv (t )
37+ func testInitializeLabels (t * testing.T ) {
3538 assert .NoError (t , repository .LoadRepoConfig ())
3639 ctx , _ := contexttest .MockContext (t , "user2/repo1/labels/initialize" )
3740 contexttest .LoadUser (t , ctx , 2 )
@@ -47,8 +50,7 @@ func TestInitializeLabels(t *testing.T) {
4750 assert .Equal (t , "/user2/repo2/labels" , test .RedirectURL (ctx .Resp ))
4851}
4952
50- func TestRetrieveLabels (t * testing.T ) {
51- unittest .PrepareTestEnv (t )
53+ func testRetrieveLabels (t * testing.T ) {
5254 for _ , testCase := range []struct {
5355 RepoID int64
5456 Sort string
@@ -74,45 +76,41 @@ func TestRetrieveLabels(t *testing.T) {
7476 }
7577}
7678
77- func TestNewLabel (t * testing.T ) {
78- unittest .PrepareTestEnv (t )
79- ctx , _ := contexttest .MockContext (t , "user2/repo1/labels/edit" )
79+ func testNewLabel (t * testing.T ) {
80+ ctx , respWriter := contexttest .MockContext (t , "user2/repo1/labels/edit" )
8081 contexttest .LoadUser (t , ctx , 2 )
8182 contexttest .LoadRepo (t , ctx , 1 )
8283 web .SetForm (ctx , & forms.CreateLabelForm {
8384 Title : "newlabel" ,
8485 Color : "#abcdef" ,
8586 })
8687 NewLabel (ctx )
87- assert .Equal (t , http .StatusSeeOther , ctx .Resp .WrittenStatus ())
88+ assert .Equal (t , http .StatusOK , ctx .Resp .WrittenStatus ())
8889 unittest .AssertExistsAndLoadBean (t , & issues_model.Label {
8990 Name : "newlabel" ,
9091 Color : "#abcdef" ,
9192 })
92- assert .Equal (t , "/user2/repo1/labels" , test .RedirectURL (ctx . Resp ))
93+ assert .Equal (t , "/user2/repo1/labels" , test .RedirectURL (respWriter ))
9394}
9495
95- func TestNewLabelGivenInvalidLabelCode (t * testing.T ) {
96- unittest .PrepareTestEnv (t )
97- ctx , _ := contexttest .MockContext (t , "user2/repo1/labels/edit" )
96+ func testNewLabelInvalidColor (t * testing.T ) {
97+ ctx , respWriter := contexttest .MockContext (t , "user2/repo1/labels/edit" )
9898 contexttest .LoadUser (t , ctx , 2 )
9999 contexttest .LoadRepo (t , ctx , 1 )
100100 web .SetForm (ctx , & forms.CreateLabelForm {
101- Title : "newlabel" ,
101+ Title : "newlabel-x " ,
102102 Color : "bad-label-code" ,
103103 })
104104 NewLabel (ctx )
105- assert .Equal (t , http .StatusSeeOther , ctx .Resp .WrittenStatus ())
106- assert .Equal (t , "/user2/repo1/labels" , test .RedirectURL (ctx .Resp ))
107- assert .True (t , ctx .Flash .Has ("error" ))
105+ assert .Equal (t , http .StatusBadRequest , ctx .Resp .WrittenStatus ())
106+ assert .Equal (t , "repo.issues.label_color_invalid" , test .ParseJSONError (respWriter .Body .Bytes ()).ErrorMessage )
108107 unittest .AssertNotExistsBean (t , & issues_model.Label {
109- Name : "newlabel" ,
108+ Name : "newlabel-x " ,
110109 })
111110}
112111
113- func TestUpdateLabel (t * testing.T ) {
114- unittest .PrepareTestEnv (t )
115- ctx , _ := contexttest .MockContext (t , "user2/repo1/labels/edit" )
112+ func testUpdateLabel (t * testing.T ) {
113+ ctx , respWriter := contexttest .MockContext (t , "user2/repo1/labels/edit" )
116114 contexttest .LoadUser (t , ctx , 2 )
117115 contexttest .LoadRepo (t , ctx , 1 )
118116 web .SetForm (ctx , & forms.CreateLabelForm {
@@ -122,18 +120,17 @@ func TestUpdateLabel(t *testing.T) {
122120 IsArchived : true ,
123121 })
124122 UpdateLabel (ctx )
125- assert .Equal (t , http .StatusSeeOther , ctx .Resp .WrittenStatus ())
123+ assert .Equal (t , http .StatusOK , ctx .Resp .WrittenStatus ())
126124 unittest .AssertExistsAndLoadBean (t , & issues_model.Label {
127125 ID : 2 ,
128126 Name : "newnameforlabel" ,
129127 Color : "#abcdef" ,
130128 })
131- assert .Equal (t , "/user2/repo1/labels" , test .RedirectURL (ctx . Resp ))
129+ assert .Equal (t , "/user2/repo1/labels" , test .RedirectURL (respWriter ))
132130}
133131
134- func TestUpdateLabelGivenInvalidLabelCode (t * testing.T ) {
135- unittest .PrepareTestEnv (t )
136- ctx , _ := contexttest .MockContext (t , "user2/repo1/labels/edit" )
132+ func testUpdateLabelInvalidColor (t * testing.T ) {
133+ ctx , respWriter := contexttest .MockContext (t , "user2/repo1/labels/edit" )
137134 contexttest .LoadUser (t , ctx , 2 )
138135 contexttest .LoadRepo (t , ctx , 1 )
139136 web .SetForm (ctx , & forms.CreateLabelForm {
@@ -144,18 +141,16 @@ func TestUpdateLabelGivenInvalidLabelCode(t *testing.T) {
144141
145142 UpdateLabel (ctx )
146143
147- assert .Equal (t , http .StatusSeeOther , ctx .Resp .WrittenStatus ())
148- assert .Equal (t , "/user2/repo1/labels" , test .RedirectURL (ctx .Resp ))
149- assert .True (t , ctx .Flash .Has ("error" ))
144+ assert .Equal (t , http .StatusBadRequest , ctx .Resp .WrittenStatus ())
145+ assert .Equal (t , "repo.issues.label_color_invalid" , test .ParseJSONError (respWriter .Body .Bytes ()).ErrorMessage )
150146 unittest .AssertExistsAndLoadBean (t , & issues_model.Label {
151147 ID : 1 ,
152148 Name : "label1" ,
153149 Color : "#abcdef" ,
154150 })
155151}
156152
157- func TestDeleteLabel (t * testing.T ) {
158- unittest .PrepareTestEnv (t )
153+ func testDeleteLabel (t * testing.T ) {
159154 ctx , _ := contexttest .MockContext (t , "user2/repo1/labels/delete" )
160155 contexttest .LoadUser (t , ctx , 2 )
161156 contexttest .LoadRepo (t , ctx , 1 )
@@ -167,8 +162,7 @@ func TestDeleteLabel(t *testing.T) {
167162 assert .EqualValues (t , ctx .Tr ("repo.issues.label_deletion_success" ), ctx .Flash .SuccessMsg )
168163}
169164
170- func TestUpdateIssueLabel_Clear (t * testing.T ) {
171- unittest .PrepareTestEnv (t )
165+ func testUpdateIssueLabel_Clear (t * testing.T ) {
172166 ctx , _ := contexttest .MockContext (t , "user2/repo1/issues/labels" )
173167 contexttest .LoadUser (t , ctx , 2 )
174168 contexttest .LoadRepo (t , ctx , 1 )
@@ -181,7 +175,7 @@ func TestUpdateIssueLabel_Clear(t *testing.T) {
181175 unittest .CheckConsistencyFor (t , & issues_model.Label {})
182176}
183177
184- func TestUpdateIssueLabel_Toggle (t * testing.T ) {
178+ func testUpdateIssueLabel_Toggle (t * testing.T ) {
185179 for _ , testCase := range []struct {
186180 Action string
187181 IssueIDs []int64
@@ -197,7 +191,8 @@ func TestUpdateIssueLabel_Toggle(t *testing.T) {
197191 ctx , _ := contexttest .MockContext (t , "user2/repo1/issues/labels" )
198192 contexttest .LoadUser (t , ctx , 2 )
199193 contexttest .LoadRepo (t , ctx , 1 )
200- ctx .Req .Form .Set ("issue_ids" , int64SliceToCommaSeparated (testCase .IssueIDs ))
194+
195+ ctx .Req .Form .Set ("issue_ids" , strings .Join (base .Int64sToStrings (testCase .IssueIDs ), "," ))
201196 ctx .Req .Form .Set ("action" , testCase .Action )
202197 ctx .Req .Form .Set ("id" , strconv .Itoa (int (testCase .LabelID )))
203198 UpdateIssueLabel (ctx )
0 commit comments