@ -6,10 +6,12 @@ package repo
import (
import (
"net/http"
"net/http"
"strconv"
"strconv"
"strings"
"testing"
"testing"
issues_model "code.gitea.io/gitea/models/issues"
issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/modules/web"
@ -19,19 +21,20 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
)
)
func int64SliceToCommaSeparated ( a [ ] int64 ) string {
func TestIssueLabel ( t * testing . T ) {
s := ""
unittest . PrepareTestEnv ( t )
for i , n := range a {
t . Run ( "RetrieveLabels" , testRetrieveLabels )
if i > 0 {
t . Run ( "NewLabel" , testNewLabel )
s += ","
t . Run ( "NewLabelInvalidColor" , testNewLabelInvalidColor )
}
t . Run ( "UpdateLabel" , testUpdateLabel )
s += strconv . Itoa ( int ( n ) )
t . Run ( "UpdateLabelInvalidColor" , testUpdateLabelInvalidColor )
}
t . Run ( "UpdateIssueLabelClear" , testUpdateIssueLabelClear )
return s
t . Run ( "UpdateIssueLabelToggle" , testUpdateIssueLabelToggle )
t . Run ( "InitializeLabels" , testInitializeLabels )
t . Run ( "DeleteLabel" , testDeleteLabel )
}
}
func TestInitializeLabels ( t * testing . T ) {
func testInitializeLabels ( t * testing . T ) {
unittest . PrepareTestEnv ( t )
assert . NoError ( t , repository . LoadRepoConfig ( ) )
assert . NoError ( t , repository . LoadRepoConfig ( ) )
ctx , _ := contexttest . MockContext ( t , "user2/repo1/labels/initialize" )
ctx , _ := contexttest . MockContext ( t , "user2/repo1/labels/initialize" )
contexttest . LoadUser ( t , ctx , 2 )
contexttest . LoadUser ( t , ctx , 2 )
@ -47,8 +50,7 @@ func TestInitializeLabels(t *testing.T) {
assert . Equal ( t , "/user2/repo2/labels" , test . RedirectURL ( ctx . Resp ) )
assert . Equal ( t , "/user2/repo2/labels" , test . RedirectURL ( ctx . Resp ) )
}
}
func TestRetrieveLabels ( t * testing . T ) {
func testRetrieveLabels ( t * testing . T ) {
unittest . PrepareTestEnv ( t )
for _ , testCase := range [ ] struct {
for _ , testCase := range [ ] struct {
RepoID int64
RepoID int64
Sort string
Sort string
@ -74,9 +76,8 @@ func TestRetrieveLabels(t *testing.T) {
}
}
}
}
func TestNewLabel ( t * testing . T ) {
func testNewLabel ( t * testing . T ) {
unittest . PrepareTestEnv ( t )
ctx , respWriter := contexttest . MockContext ( t , "user2/repo1/labels/edit" )
ctx , _ := contexttest . MockContext ( t , "user2/repo1/labels/edit" )
contexttest . LoadUser ( t , ctx , 2 )
contexttest . LoadUser ( t , ctx , 2 )
contexttest . LoadRepo ( t , ctx , 1 )
contexttest . LoadRepo ( t , ctx , 1 )
web . SetForm ( ctx , & forms . CreateLabelForm {
web . SetForm ( ctx , & forms . CreateLabelForm {
@ -84,17 +85,32 @@ func TestNewLabel(t *testing.T) {
Color : "#abcdef" ,
Color : "#abcdef" ,
} )
} )
NewLabel ( ctx )
NewLabel ( ctx )
assert . Equal ( t , http . Status SeeOther , ctx . Resp . WrittenStatus ( ) )
assert . Equal ( t , http . Status OK , ctx . Resp . WrittenStatus ( ) )
unittest . AssertExistsAndLoadBean ( t , & issues_model . Label {
unittest . AssertExistsAndLoadBean ( t , & issues_model . Label {
Name : "newlabel" ,
Name : "newlabel" ,
Color : "#abcdef" ,
Color : "#abcdef" ,
} )
} )
assert . Equal ( t , "/user2/repo1/labels" , test . RedirectURL ( ctx. Resp ) )
assert . Equal ( t , "/user2/repo1/labels" , test . RedirectURL ( respWriter ) )
}
}
func TestUpdateLabel ( t * testing . T ) {
func testNewLabelInvalidColor ( t * testing . T ) {
unittest . PrepareTestEnv ( t )
ctx , respWriter := contexttest . MockContext ( t , "user2/repo1/labels/edit" )
ctx , _ := contexttest . MockContext ( t , "user2/repo1/labels/edit" )
contexttest . LoadUser ( t , ctx , 2 )
contexttest . LoadRepo ( t , ctx , 1 )
web . SetForm ( ctx , & forms . CreateLabelForm {
Title : "newlabel-x" ,
Color : "bad-label-code" ,
} )
NewLabel ( ctx )
assert . Equal ( t , http . StatusBadRequest , ctx . Resp . WrittenStatus ( ) )
assert . Equal ( t , "repo.issues.label_color_invalid" , test . ParseJSONError ( respWriter . Body . Bytes ( ) ) . ErrorMessage )
unittest . AssertNotExistsBean ( t , & issues_model . Label {
Name : "newlabel-x" ,
} )
}
func testUpdateLabel ( t * testing . T ) {
ctx , respWriter := contexttest . MockContext ( t , "user2/repo1/labels/edit" )
contexttest . LoadUser ( t , ctx , 2 )
contexttest . LoadUser ( t , ctx , 2 )
contexttest . LoadRepo ( t , ctx , 1 )
contexttest . LoadRepo ( t , ctx , 1 )
web . SetForm ( ctx , & forms . CreateLabelForm {
web . SetForm ( ctx , & forms . CreateLabelForm {
@ -104,17 +120,37 @@ func TestUpdateLabel(t *testing.T) {
IsArchived : true ,
IsArchived : true ,
} )
} )
UpdateLabel ( ctx )
UpdateLabel ( ctx )
assert . Equal ( t , http . Status SeeOther , ctx . Resp . WrittenStatus ( ) )
assert . Equal ( t , http . Status OK , ctx . Resp . WrittenStatus ( ) )
unittest . AssertExistsAndLoadBean ( t , & issues_model . Label {
unittest . AssertExistsAndLoadBean ( t , & issues_model . Label {
ID : 2 ,
ID : 2 ,
Name : "newnameforlabel" ,
Name : "newnameforlabel" ,
Color : "#abcdef" ,
Color : "#abcdef" ,
} )
} )
assert . Equal ( t , "/user2/repo1/labels" , test . RedirectURL ( ctx. Resp ) )
assert . Equal ( t , "/user2/repo1/labels" , test . RedirectURL ( respWriter ) )
}
}
func TestDeleteLabel ( t * testing . T ) {
func testUpdateLabelInvalidColor ( t * testing . T ) {
unittest . PrepareTestEnv ( t )
ctx , respWriter := contexttest . MockContext ( t , "user2/repo1/labels/edit" )
contexttest . LoadUser ( t , ctx , 2 )
contexttest . LoadRepo ( t , ctx , 1 )
web . SetForm ( ctx , & forms . CreateLabelForm {
ID : 1 ,
Title : "label1" ,
Color : "bad-label-code" ,
} )
UpdateLabel ( ctx )
assert . Equal ( t , http . StatusBadRequest , ctx . Resp . WrittenStatus ( ) )
assert . Equal ( t , "repo.issues.label_color_invalid" , test . ParseJSONError ( respWriter . Body . Bytes ( ) ) . ErrorMessage )
unittest . AssertExistsAndLoadBean ( t , & issues_model . Label {
ID : 1 ,
Name : "label1" ,
Color : "#abcdef" ,
} )
}
func testDeleteLabel ( t * testing . T ) {
ctx , _ := contexttest . MockContext ( t , "user2/repo1/labels/delete" )
ctx , _ := contexttest . MockContext ( t , "user2/repo1/labels/delete" )
contexttest . LoadUser ( t , ctx , 2 )
contexttest . LoadUser ( t , ctx , 2 )
contexttest . LoadRepo ( t , ctx , 1 )
contexttest . LoadRepo ( t , ctx , 1 )
@ -126,8 +162,7 @@ func TestDeleteLabel(t *testing.T) {
assert . EqualValues ( t , ctx . Tr ( "repo.issues.label_deletion_success" ) , ctx . Flash . SuccessMsg )
assert . EqualValues ( t , ctx . Tr ( "repo.issues.label_deletion_success" ) , ctx . Flash . SuccessMsg )
}
}
func TestUpdateIssueLabel_Clear ( t * testing . T ) {
func testUpdateIssueLabelClear ( t * testing . T ) {
unittest . PrepareTestEnv ( t )
ctx , _ := contexttest . MockContext ( t , "user2/repo1/issues/labels" )
ctx , _ := contexttest . MockContext ( t , "user2/repo1/issues/labels" )
contexttest . LoadUser ( t , ctx , 2 )
contexttest . LoadUser ( t , ctx , 2 )
contexttest . LoadRepo ( t , ctx , 1 )
contexttest . LoadRepo ( t , ctx , 1 )
@ -140,7 +175,7 @@ func TestUpdateIssueLabel_Clear(t *testing.T) {
unittest . CheckConsistencyFor ( t , & issues_model . Label { } )
unittest . CheckConsistencyFor ( t , & issues_model . Label { } )
}
}
func TestUpdateIssueLabel_ Toggle( t * testing . T ) {
func testUpdateIssueLabel Toggle( t * testing . T ) {
for _ , testCase := range [ ] struct {
for _ , testCase := range [ ] struct {
Action string
Action string
IssueIDs [ ] int64
IssueIDs [ ] int64
@ -156,7 +191,8 @@ func TestUpdateIssueLabel_Toggle(t *testing.T) {
ctx , _ := contexttest . MockContext ( t , "user2/repo1/issues/labels" )
ctx , _ := contexttest . MockContext ( t , "user2/repo1/issues/labels" )
contexttest . LoadUser ( t , ctx , 2 )
contexttest . LoadUser ( t , ctx , 2 )
contexttest . LoadRepo ( t , ctx , 1 )
contexttest . LoadRepo ( t , ctx , 1 )
ctx . Req . Form . Set ( "issue_ids" , int64SliceToCommaSeparated ( testCase . IssueIDs ) )
ctx . Req . Form . Set ( "issue_ids" , strings . Join ( base . Int64sToStrings ( testCase . IssueIDs ) , "," ) )
ctx . Req . Form . Set ( "action" , testCase . Action )
ctx . Req . Form . Set ( "action" , testCase . Action )
ctx . Req . Form . Set ( "id" , strconv . Itoa ( int ( testCase . LabelID ) ) )
ctx . Req . Form . Set ( "id" , strconv . Itoa ( int ( testCase . LabelID ) ) )
UpdateIssueLabel ( ctx )
UpdateIssueLabel ( ctx )