Skip to content

Commit

Permalink
Merge pull request #549 from traPtitech/fix/validation.allow.empty.it…
Browse files Browse the repository at this point in the history
…em.code

fix: 🐛 item の code が空文字なことを許容する
  • Loading branch information
cp-20 authored Dec 18, 2023
2 parents 69b5126 + 93ba154 commit 4bd03d2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion model/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (item Item) Validate() error {
return validation.ValidateStruct(&item,
validation.Field(&item.Name, validation.Required),
validation.Field(&item.Type, validation.By(checkItemType)),
validation.Field(&item.Code, validation.Required),
validation.Field(&item.Code, validation.NotNil),
validation.Field(&item.Description, validation.Required),
validation.Field(&item.ImgURL, is.URL),
)
Expand Down
34 changes: 26 additions & 8 deletions router/items_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ func TestPostItems(t *testing.T) {
ImgURL: "http://example.com/testKojin.jpg",
}

testValidBodies := []model.Item{
{
Name: "testPostInvalidItem3",
Type: 0,
Code: "",
Description: "これはバリデーションのテスト3です",
ImgURL: "http://example.com/testInvalid3.jpg",
},
}

testInvalidBodies := []model.Item{
{
Name: "",
Expand All @@ -49,13 +59,6 @@ func TestPostItems(t *testing.T) {
Description: "これはバリデーションのテスト2です",
ImgURL: "http://example.com/testInvalid2.jpg",
},
{
Name: "testPostInvalidItem3",
Type: 0,
Code: "",
Description: "これはバリデーションのテスト3です",
ImgURL: "http://example.com/testInvalid3.jpg",
},
{
Name: "testPostInvalidItem4",
Type: 0,
Expand All @@ -67,7 +70,7 @@ func TestPostItems(t *testing.T) {
Name: "testPostInvalidItem5",
Type: 0,
Code: "3575736936335",
Description: "これはバリデーションのテスト5です",
Description: "これはバリデーションのテスト4です",
ImgURL: "not a url",
},
}
Expand Down Expand Up @@ -138,6 +141,21 @@ func TestPostItems(t *testing.T) {
assert.Equal(http.StatusBadRequest, rec.Code)
})
}

for _, body := range testValidBodies {
t.Run("admin user/validation pass", func(t *testing.T) {
assert := assert.New(t)
e := echoSetupWithAdminUser()

reqBody, _ := json.Marshal(body)
req := httptest.NewRequest(echo.POST, "/api/items", bytes.NewReader(reqBody))
req.Header.Set("Content-Type", "application/json")
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)

assert.Equal(http.StatusCreated, rec.Code)
})
}
}

func TestPutItem(t *testing.T) {
Expand Down

0 comments on commit 4bd03d2

Please sign in to comment.