Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 🐛 item の code が空文字なことを許容する #549

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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