Skip to content

Commit 10bd974

Browse files
committed
Update backend changes + add web route tests
1 parent 3d39baf commit 10bd974

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

modules/label/label.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type ErrInvalidLabelColor struct {
2626
}
2727

2828
func (e *ErrInvalidLabelColor) Error() string {
29-
return fmt.Sprintf("invalid label color: %s", e.Color)
29+
return "invalid label color: " + e.Color
3030
}
3131

3232
func IsErrInvalidLabelColor(err error) bool {

routers/web/repo/issue_label.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,11 @@ func NewLabel(ctx *context.Context) {
120120
}
121121
if err := issues_model.NewLabel(ctx, l); err != nil {
122122
if label.IsErrInvalidLabelColor(err) {
123-
ctx.Flash.Error(err.Error())
124-
ctx.Redirect(ctx.Repo.RepoLink + "/labels")
123+
ctx.Flash.Error("NewLabel: " + err.Error())
125124
} else {
126125
ctx.ServerError("NewLabel", err)
126+
return
127127
}
128-
return
129128
}
130129
ctx.Redirect(ctx.Repo.RepoLink + "/labels")
131130
}
@@ -152,12 +151,11 @@ func UpdateLabel(ctx *context.Context) {
152151
l.SetArchived(form.IsArchived)
153152
if err := issues_model.UpdateLabel(ctx, l); err != nil {
154153
if label.IsErrInvalidLabelColor(err) {
155-
ctx.Flash.Error(err.Error())
156-
ctx.Redirect(ctx.Repo.RepoLink + "/labels")
154+
ctx.Flash.Error("UpdateLabel: " + err.Error())
157155
} else {
158156
ctx.ServerError("UpdateLabel", err)
157+
return
159158
}
160-
return
161159
}
162160
ctx.Redirect(ctx.Repo.RepoLink + "/labels")
163161
}

routers/web/repo/issue_label_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@ func TestNewLabel(t *testing.T) {
9292
assert.Equal(t, "/user2/repo1/labels", test.RedirectURL(ctx.Resp))
9393
}
9494

95+
func TestNewLabelGivenInvalidLabelCode(t *testing.T) {
96+
unittest.PrepareTestEnv(t)
97+
ctx, _ := contexttest.MockContext(t, "user2/repo1/labels/edit")
98+
contexttest.LoadUser(t, ctx, 2)
99+
contexttest.LoadRepo(t, ctx, 1)
100+
web.SetForm(ctx, &forms.CreateLabelForm{
101+
Title: "newlabel",
102+
Color: "bad-label-code",
103+
})
104+
NewLabel(ctx)
105+
assert.Equal(t, http.StatusSeeOther, ctx.Resp.WrittenStatus())
106+
assert.True(t, ctx.Flash.Has("error"))
107+
unittest.AssertNotExistsBean(t, &issues_model.Label{
108+
Name: "newlabel",
109+
})
110+
}
111+
95112
func TestUpdateLabel(t *testing.T) {
96113
unittest.PrepareTestEnv(t)
97114
ctx, _ := contexttest.MockContext(t, "user2/repo1/labels/edit")
@@ -113,6 +130,29 @@ func TestUpdateLabel(t *testing.T) {
113130
assert.Equal(t, "/user2/repo1/labels", test.RedirectURL(ctx.Resp))
114131
}
115132

133+
func TestUpdateLabelGivenInvalidLabelCode(t *testing.T) {
134+
unittest.PrepareTestEnv(t)
135+
ctx, _ := contexttest.MockContext(t, "user2/repo1/labels/edit")
136+
contexttest.LoadUser(t, ctx, 2)
137+
contexttest.LoadRepo(t, ctx, 1)
138+
web.SetForm(ctx, &forms.CreateLabelForm{
139+
ID: 1,
140+
Title: "label1",
141+
Color: "bad-label-code",
142+
})
143+
144+
UpdateLabel(ctx)
145+
146+
assert.Equal(t, http.StatusSeeOther, ctx.Resp.WrittenStatus())
147+
assert.Equal(t, "/user2/repo1/labels", test.RedirectURL(ctx.Resp))
148+
assert.True(t, ctx.Flash.Has("error"))
149+
unittest.AssertExistsAndLoadBean(t, &issues_model.Label{
150+
ID: 1,
151+
Name: "label1",
152+
Color: "#abcdef",
153+
})
154+
}
155+
116156
func TestDeleteLabel(t *testing.T) {
117157
unittest.PrepareTestEnv(t)
118158
ctx, _ := contexttest.MockContext(t, "user2/repo1/labels/delete")

0 commit comments

Comments
 (0)