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/request"
|
||||||
"miniflux.app/http/response/json"
|
"miniflux.app/http/response/json"
|
||||||
|
"miniflux.app/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *handler) createCategory(w http.ResponseWriter, r *http.Request) {
|
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 {
|
if err != nil {
|
||||||
json.BadRequest(w, r, err)
|
json.BadRequest(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
userID := request.UserID(r)
|
category := &model.Category{UserID: userID, Title: categoryRequest.Title}
|
||||||
category.UserID = userID
|
|
||||||
if err := category.ValidateCategoryCreation(); err != nil {
|
if err := category.ValidateCategoryCreation(); err != nil {
|
||||||
json.BadRequest(w, r, err)
|
json.BadRequest(w, r, err)
|
||||||
return
|
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) {
|
func (h *handler) updateCategory(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID := request.UserID(r)
|
||||||
categoryID := request.RouteInt64Param(r, "categoryID")
|
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 {
|
if err != nil {
|
||||||
json.BadRequest(w, r, err)
|
json.BadRequest(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
category.UserID = request.UserID(r)
|
category.Title = categoryRequest.Title
|
||||||
category.ID = categoryID
|
|
||||||
if err := category.ValidateCategoryModification(); err != nil {
|
if err := category.ValidateCategoryModification(); err != nil {
|
||||||
json.BadRequest(w, r, err)
|
json.BadRequest(w, r, err)
|
||||||
return
|
return
|
||||||
|
|
|
@ -236,14 +236,18 @@ func decodeFeedModificationPayload(r io.ReadCloser) (*feedModification, error) {
|
||||||
return &feed, nil
|
return &feed, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func decodeCategoryPayload(r io.ReadCloser) (*model.Category, error) {
|
type categoryRequest struct {
|
||||||
var category model.Category
|
Title string `json:"title"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeCategoryRequest(r io.ReadCloser) (*categoryRequest, error) {
|
||||||
|
var payload categoryRequest
|
||||||
|
|
||||||
decoder := json.NewDecoder(r)
|
decoder := json.NewDecoder(r)
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
if err := decoder.Decode(&category); err != nil {
|
if err := decoder.Decode(&payload); err != nil {
|
||||||
return nil, fmt.Errorf("Unable to decode category JSON object: %v", err)
|
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 {
|
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…
Add table
Reference in a new issue