1
0
Fork 0

fix: store.GetEnclosure() should return nil if no rows are returned

This commit is contained in:
Frédéric Guillot 2024-08-18 12:07:57 -07:00
parent 810b351772
commit eb4bca6eb7
5 changed files with 28 additions and 25 deletions

View file

@ -613,17 +613,15 @@ func (c *Client) Icon(iconID int64) (*FeedIcon, error) {
return feedIcon, nil return feedIcon, nil
} }
// Enclosure fetches a specific enclosure.
func (c *Client) Enclosure(enclosureID int64) (*Enclosure, error) { func (c *Client) Enclosure(enclosureID int64) (*Enclosure, error) {
body, err := c.request.Get(fmt.Sprintf("/v1/enclosures/%d", enclosureID)) body, err := c.request.Get(fmt.Sprintf("/v1/enclosures/%d", enclosureID))
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer body.Close() defer body.Close()
var enclosure *Enclosure var enclosure *Enclosure
if err := json.NewDecoder(body).Decode(&enclosure); err != nil { if err := json.NewDecoder(body).Decode(&enclosure); err != nil {
return nil, fmt.Errorf("miniflux: response error(%v)", err) return nil, fmt.Errorf("miniflux: response error(%v)", err)
} }
@ -631,9 +629,9 @@ func (c *Client) Enclosure(enclosureID int64) (*Enclosure, error) {
return enclosure, nil return enclosure, nil
} }
// UpdateEnclosure updates an enclosure.
func (c *Client) UpdateEnclosure(enclosureID int64, enclosureUpdate *EnclosureUpdateRequest) error { func (c *Client) UpdateEnclosure(enclosureID int64, enclosureUpdate *EnclosureUpdateRequest) error {
_, err := c.request.Put(fmt.Sprintf("/v1/enclosures/%d", enclosureID), enclosureUpdate) _, err := c.request.Put(fmt.Sprintf("/v1/enclosures/%d", enclosureID), enclosureUpdate)
return err return err
} }

View file

@ -72,8 +72,8 @@ func Serve(router *mux.Router, store *storage.Storage, pool *worker.Pool) {
sr.HandleFunc("/entries/{entryID}/fetch-content", handler.fetchContent).Methods(http.MethodGet) sr.HandleFunc("/entries/{entryID}/fetch-content", handler.fetchContent).Methods(http.MethodGet)
sr.HandleFunc("/flush-history", handler.flushHistory).Methods(http.MethodPut, http.MethodDelete) sr.HandleFunc("/flush-history", handler.flushHistory).Methods(http.MethodPut, http.MethodDelete)
sr.HandleFunc("/icons/{iconID}", handler.getIconByIconID).Methods(http.MethodGet) sr.HandleFunc("/icons/{iconID}", handler.getIconByIconID).Methods(http.MethodGet)
sr.HandleFunc("/enclosures/{enclosureID}", handler.getEnclosureById).Methods(http.MethodGet) sr.HandleFunc("/enclosures/{enclosureID}", handler.getEnclosureByID).Methods(http.MethodGet)
sr.HandleFunc("/enclosures/{enclosureID}", handler.updateEnclosureById).Methods(http.MethodPut) sr.HandleFunc("/enclosures/{enclosureID}", handler.updateEnclosureByID).Methods(http.MethodPut)
sr.HandleFunc("/version", handler.versionHandler).Methods(http.MethodGet) sr.HandleFunc("/version", handler.versionHandler).Methods(http.MethodGet)
} }

View file

@ -67,10 +67,14 @@ func TestIncorrectEndpoint(t *testing.T) {
} }
client := miniflux.NewClient("incorrect url") client := miniflux.NewClient("incorrect url")
_, err := client.Users() if _, err := client.Users(); err == nil {
if err == nil {
t.Fatal(`Using an incorrect URL should raise an error`) t.Fatal(`Using an incorrect URL should raise an error`)
} }
client = miniflux.NewClient("")
if _, err := client.Users(); err == nil {
t.Fatal(`Using an empty URL should raise an error`)
}
} }
func TestHealthcheckEndpoint(t *testing.T) { func TestHealthcheckEndpoint(t *testing.T) {
@ -2074,7 +2078,6 @@ func TestUpdateEnclosureEndpoint(t *testing.T) {
} }
var enclosure *miniflux.Enclosure var enclosure *miniflux.Enclosure
for _, entry := range result.Entries { for _, entry := range result.Entries {
if len(entry.Enclosures) > 0 { if len(entry.Enclosures) > 0 {
enclosure = entry.Enclosures[0] enclosure = entry.Enclosures[0]
@ -2089,13 +2092,11 @@ func TestUpdateEnclosureEndpoint(t *testing.T) {
err = regularUserClient.UpdateEnclosure(enclosure.ID, &miniflux.EnclosureUpdateRequest{ err = regularUserClient.UpdateEnclosure(enclosure.ID, &miniflux.EnclosureUpdateRequest{
MediaProgression: 20, MediaProgression: 20,
}) })
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
updatedEnclosure, err := regularUserClient.Enclosure(enclosure.ID) updatedEnclosure, err := regularUserClient.Enclosure(enclosure.ID)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -2134,7 +2135,6 @@ func TestGetEnclosureEndpoint(t *testing.T) {
} }
var expectedEnclosure *miniflux.Enclosure var expectedEnclosure *miniflux.Enclosure
for _, entry := range result.Entries { for _, entry := range result.Entries {
if len(entry.Enclosures) > 0 { if len(entry.Enclosures) > 0 {
expectedEnclosure = entry.Enclosures[0] expectedEnclosure = entry.Enclosures[0]
@ -2147,7 +2147,6 @@ func TestGetEnclosureEndpoint(t *testing.T) {
} }
enclosure, err := regularUserClient.Enclosure(expectedEnclosure.ID) enclosure, err := regularUserClient.Enclosure(expectedEnclosure.ID)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -2155,7 +2154,12 @@ func TestGetEnclosureEndpoint(t *testing.T) {
if enclosure.ID != expectedEnclosure.ID { if enclosure.ID != expectedEnclosure.ID {
t.Fatalf(`Invalid enclosureID, got %d while expecting %d`, enclosure.ID, expectedEnclosure.ID) t.Fatalf(`Invalid enclosureID, got %d while expecting %d`, enclosure.ID, expectedEnclosure.ID)
} }
if _, err = regularUserClient.Enclosure(99999); err == nil {
t.Fatalf(`Fetching an inexisting enclosure should raise an error`)
}
} }
func TestGetEntryEndpoints(t *testing.T) { func TestGetEntryEndpoints(t *testing.T) {
testConfig := newIntegrationTestConfig() testConfig := newIntegrationTestConfig()
if !testConfig.isConfigured() { if !testConfig.isConfigured() {

View file

@ -13,18 +13,21 @@ import (
"miniflux.app/v2/internal/validator" "miniflux.app/v2/internal/validator"
) )
func (h *handler) getEnclosureById(w http.ResponseWriter, r *http.Request) { func (h *handler) getEnclosureByID(w http.ResponseWriter, r *http.Request) {
enclosureID := request.RouteInt64Param(r, "enclosureID") enclosureID := request.RouteInt64Param(r, "enclosureID")
enclosure, err := h.store.GetEnclosure(enclosureID) enclosure, err := h.store.GetEnclosure(enclosureID)
if err != nil { if err != nil {
json.ServerError(w, r, err)
return
}
if enclosure == nil {
json.NotFound(w, r) json.NotFound(w, r)
return return
} }
userID := request.UserID(r) userID := request.UserID(r)
if enclosure.UserID != userID { if enclosure.UserID != userID {
json.NotFound(w, r) json.NotFound(w, r)
return return
@ -35,11 +38,10 @@ func (h *handler) getEnclosureById(w http.ResponseWriter, r *http.Request) {
json.OK(w, r, enclosure) json.OK(w, r, enclosure)
} }
func (h *handler) updateEnclosureById(w http.ResponseWriter, r *http.Request) { func (h *handler) updateEnclosureByID(w http.ResponseWriter, r *http.Request) {
enclosureID := request.RouteInt64Param(r, "enclosureID") enclosureID := request.RouteInt64Param(r, "enclosureID")
var enclosureUpdateRequest model.EnclosureUpdateRequest var enclosureUpdateRequest model.EnclosureUpdateRequest
if err := json_parser.NewDecoder(r.Body).Decode(&enclosureUpdateRequest); err != nil { if err := json_parser.NewDecoder(r.Body).Decode(&enclosureUpdateRequest); err != nil {
json.BadRequest(w, r, err) json.BadRequest(w, r, err)
return return
@ -51,9 +53,8 @@ func (h *handler) updateEnclosureById(w http.ResponseWriter, r *http.Request) {
} }
enclosure, err := h.store.GetEnclosure(enclosureID) enclosure, err := h.store.GetEnclosure(enclosureID)
if err != nil { if err != nil {
json.BadRequest(w, r, err) json.ServerError(w, r, err)
return return
} }
@ -63,14 +64,12 @@ func (h *handler) updateEnclosureById(w http.ResponseWriter, r *http.Request) {
} }
userID := request.UserID(r) userID := request.UserID(r)
if enclosure.UserID != userID { if enclosure.UserID != userID {
json.NotFound(w, r) json.NotFound(w, r)
return return
} }
enclosure.MediaProgression = enclosureUpdateRequest.MediaProgression enclosure.MediaProgression = enclosureUpdateRequest.MediaProgression
if err := h.store.UpdateEnclosure(enclosure); err != nil { if err := h.store.UpdateEnclosure(enclosure); err != nil {
json.ServerError(w, r, err) json.ServerError(w, r, err)
return return

View file

@ -90,7 +90,9 @@ func (s *Storage) GetEnclosure(enclosureID int64) (*model.Enclosure, error) {
&enclosure.MediaProgression, &enclosure.MediaProgression,
) )
if err != nil { if err == sql.ErrNoRows {
return nil, nil
} else if err != nil {
return nil, fmt.Errorf(`store: unable to fetch enclosure row: %v`, err) return nil, fmt.Errorf(`store: unable to fetch enclosure row: %v`, err)
} }
@ -164,8 +166,8 @@ func (s *Storage) UpdateEnclosure(enclosure *model.Enclosure) error {
url=$1, url=$1,
size=$2, size=$2,
mime_type=$3, mime_type=$3,
entry_id=$4, entry_id=$4,
user_id=$5, user_id=$5,
media_progression=$6 media_progression=$6
WHERE WHERE
id=$7 id=$7