diff --git a/model/items.go b/model/items.go index 433ea2f..72f1866 100644 --- a/model/items.go +++ b/model/items.go @@ -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), ) diff --git a/router/items_test.go b/router/items_test.go index bcfa34c..c040246 100644 --- a/router/items_test.go +++ b/router/items_test.go @@ -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: "", @@ -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, @@ -67,7 +70,7 @@ func TestPostItems(t *testing.T) { Name: "testPostInvalidItem5", Type: 0, Code: "3575736936335", - Description: "これはバリデーションのテスト5です", + Description: "これはバリデーションのテスト4です", ImgURL: "not a url", }, } @@ -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) {