Renaming non-existent category via API should returns a 404
This commit is contained in:
parent
70effdc706
commit
5922a7a051
3 changed files with 38 additions and 12 deletions
|
@ -11,17 +11,19 @@ import (
|
|||
|
||||
"miniflux.app/http/request"
|
||||
"miniflux.app/http/response/json"
|
||||
"miniflux.app/model"
|
||||
)
|
||||
|
||||
func (h *handler) createCategory(w http.ResponseWriter, r *http.Request) {
|
||||
category, err := decodeCategoryPayload(r.Body)
|
||||
userID := request.UserID(r)
|
||||
|
||||
categoryRequest, err := decodeCategoryRequest(r.Body)
|
||||
if err != nil {
|
||||
json.BadRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
userID := request.UserID(r)
|
||||
category.UserID = userID
|
||||
category := &model.Category{UserID: userID, Title: categoryRequest.Title}
|
||||
if err := category.ValidateCategoryCreation(); err != nil {
|
||||
json.BadRequest(w, r, err)
|
||||
return
|
||||
|
@ -41,16 +43,27 @@ func (h *handler) createCategory(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (h *handler) updateCategory(w http.ResponseWriter, r *http.Request) {
|
||||
userID := request.UserID(r)
|
||||
categoryID := request.RouteInt64Param(r, "categoryID")
|
||||
|
||||
category, err := decodeCategoryPayload(r.Body)
|
||||
category, err := h.store.Category(userID, categoryID)
|
||||
if err != nil {
|
||||
json.ServerError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
if category == nil {
|
||||
json.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
categoryRequest, err := decodeCategoryRequest(r.Body)
|
||||
if err != nil {
|
||||
json.BadRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
category.UserID = request.UserID(r)
|
||||
category.ID = categoryID
|
||||
category.Title = categoryRequest.Title
|
||||
if err := category.ValidateCategoryModification(); err != nil {
|
||||
json.BadRequest(w, r, err)
|
||||
return
|
||||
|
|
|
@ -236,14 +236,18 @@ func decodeFeedModificationPayload(r io.ReadCloser) (*feedModification, error) {
|
|||
return &feed, nil
|
||||
}
|
||||
|
||||
func decodeCategoryPayload(r io.ReadCloser) (*model.Category, error) {
|
||||
var category model.Category
|
||||
type categoryRequest struct {
|
||||
Title string `json:"title"`
|
||||
}
|
||||
|
||||
func decodeCategoryRequest(r io.ReadCloser) (*categoryRequest, error) {
|
||||
var payload categoryRequest
|
||||
|
||||
decoder := json.NewDecoder(r)
|
||||
defer r.Close()
|
||||
if err := decoder.Decode(&category); err != nil {
|
||||
return nil, fmt.Errorf("Unable to decode category JSON object: %v", err)
|
||||
if err := decoder.Decode(&payload); err != nil {
|
||||
return nil, fmt.Errorf("Unable to decode JSON object: %v", err)
|
||||
}
|
||||
|
||||
return &category, nil
|
||||
return &payload, nil
|
||||
}
|
||||
|
|
|
@ -79,7 +79,16 @@ func TestUpdateCategory(t *testing.T) {
|
|||
}
|
||||
|
||||
if category.Title != categoryName {
|
||||
t.Fatalf(`Invalid title, got "%v" instead of "%v"`, category.Title, categoryName)
|
||||
t.Fatalf(`Invalid title, got %q instead of %q`, category.Title, categoryName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateInexistingCategory(t *testing.T) {
|
||||
client := createClient(t)
|
||||
|
||||
_, err := client.UpdateCategory(4200000, "Test")
|
||||
if err != miniflux.ErrNotFound {
|
||||
t.Errorf(`Updating an inexisting category should returns a 404 instead of %v`, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue