Bläddra i källkod

fix: return 404 when starring a missing entry

Toggling the starred flag of an entry that does not exist or belongs to
another user returned 500 Internal Server Error from both the API and
the web UI. ToggleStarred now returns ErrEntryNotFound, and both
handlers answer with 404 Not Found, like the share endpoints.
Frédéric Guillot 14 timmar sedan
förälder
incheckning
0fa6c919d0

+ 5 - 1
internal/api/entry_handlers.go

@@ -209,7 +209,11 @@ func (h *handler) toggleStarredHandler(w http.ResponseWriter, r *http.Request) {
 		return
 		return
 	}
 	}
 
 
-	if err := h.store.ToggleStarred(request.UserID(r), entryID); err != nil {
+	err := h.store.ToggleStarred(request.UserID(r), entryID)
+	if errors.Is(err, storage.ErrEntryNotFound) {
+		response.JSONNotFound(w, r)
+		return
+	} else if err != nil {
 		response.JSONServerError(w, r, err)
 		response.JSONServerError(w, r, err)
 		return
 		return
 	}
 	}

+ 15 - 0
internal/api/entry_integration_test.go

@@ -648,6 +648,21 @@ func TestToggleStarredEndpoint(t *testing.T) {
 	if entry.Starred {
 	if entry.Starred {
 		t.Fatalf(`The entry should no longer be starred`)
 		t.Fatalf(`The entry should no longer be starred`)
 	}
 	}
+
+	nonExistingEntryID := int64(math.MaxInt64)
+	if err := regularUserClient.ToggleStarred(nonExistingEntryID); !errors.Is(err, miniflux.ErrNotFound) {
+		t.Fatalf(`Expected Not Found when toggling the starred flag of a non existent entry: %v`, err)
+	}
+
+	otherUserClient, _ := newRegularUser(t)
+
+	otherFeedID := fixture.Subscribe(t, otherUserClient)
+
+	result = listFeedEntries(t, otherUserClient, otherFeedID, &miniflux.Filter{Limit: 1})
+
+	if err := regularUserClient.ToggleStarred(result.Entries[0].ID); !errors.Is(err, miniflux.ErrNotFound) {
+		t.Fatalf(`Expected Not Found when toggling the starred flag of another user's entry: %v`, err)
+	}
 }
 }
 
 
 func TestShareEntryEndpoint(t *testing.T) {
 func TestShareEntryEndpoint(t *testing.T) {

+ 1 - 1
internal/storage/entry.go

@@ -480,7 +480,7 @@ func (s *Storage) ToggleStarred(userID int64, entryID int64) error {
 	}
 	}
 
 
 	if count == 0 {
 	if count == 0 {
-		return errors.New(`store: nothing has been updated`)
+		return ErrEntryNotFound
 	}
 	}
 
 
 	return nil
 	return nil

+ 7 - 1
internal/ui/entry_toggle_starred.go

@@ -4,15 +4,21 @@
 package ui // import "miniflux.app/v2/internal/ui"
 package ui // import "miniflux.app/v2/internal/ui"
 
 
 import (
 import (
+	"errors"
 	"net/http"
 	"net/http"
 
 
 	"miniflux.app/v2/internal/http/request"
 	"miniflux.app/v2/internal/http/request"
 	"miniflux.app/v2/internal/http/response"
 	"miniflux.app/v2/internal/http/response"
+	"miniflux.app/v2/internal/storage"
 )
 )
 
 
 func (h *handler) toggleStarred(w http.ResponseWriter, r *http.Request) {
 func (h *handler) toggleStarred(w http.ResponseWriter, r *http.Request) {
 	entryID := request.RouteInt64Param(r, "entryID")
 	entryID := request.RouteInt64Param(r, "entryID")
-	if err := h.store.ToggleStarred(request.UserID(r), entryID); err != nil {
+	err := h.store.ToggleStarred(request.UserID(r), entryID)
+	if errors.Is(err, storage.ErrEntryNotFound) {
+		response.JSONNotFound(w, r)
+		return
+	} else if err != nil {
 		response.JSONServerError(w, r, err)
 		response.JSONServerError(w, r, err)
 		return
 		return
 	}
 	}