Просмотр исходного кода

test(api): cover category refresh, bookmark and DELETE flush-history

Refreshing an unknown or another user's category now returns 404, like
the other category endpoints. The flush history test also waits for
the background flush instead of racing it.
Fred 3 часов назад
Родитель
Сommit
647fd20193

+ 10 - 0
internal/api/category_handlers.go

@@ -173,6 +173,16 @@ func (h *handler) refreshCategoryHandler(w http.ResponseWriter, r *http.Request)
 		return
 	}
 
+	exists, err := h.store.CategoryIDExists(userID, categoryID)
+	if err != nil {
+		response.JSONServerError(w, r, err)
+		return
+	}
+	if !exists {
+		response.JSONNotFound(w, r)
+		return
+	}
+
 	jobs, err := h.store.NewBatchBuilder().
 		WithErrorLimit(config.Opts.PollingParsingErrorLimit()).
 		WithoutDisabledFeeds().

+ 36 - 0
internal/api/category_integration_test.go

@@ -386,3 +386,39 @@ func TestMarkCategoryAsReadEndpoint(t *testing.T) {
 		}
 	}
 }
+
+func TestRefreshCategoryEndpoint(t *testing.T) {
+	t.Parallel()
+
+	testConfig := requireIntegrationTestConfig(t)
+	fixture := newFeedFixture(t)
+
+	regularUserClient, _ := testConfig.newRegularUser(t)
+
+	feed, err := regularUserClient.Feed(fixture.subscribe(t, regularUserClient))
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if err := regularUserClient.RefreshCategory(feed.Category.ID); err != nil {
+		t.Fatal(err)
+	}
+
+	if err := regularUserClient.RefreshCategory(0); !errors.Is(err, miniflux.ErrBadRequest) {
+		t.Fatalf(`Refreshing a category with an invalid ID should raise a bad request error, got %v`, err)
+	}
+
+	if err := regularUserClient.RefreshCategory(123456789); !errors.Is(err, miniflux.ErrNotFound) {
+		t.Fatalf(`Refreshing an inexisting category should raise a "not found" error, got %v`, err)
+	}
+
+	otherUserClient, _ := testConfig.newRegularUser(t)
+	otherCategory, err := otherUserClient.CreateCategory("Other category")
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if err := regularUserClient.RefreshCategory(otherCategory.ID); !errors.Is(err, miniflux.ErrNotFound) {
+		t.Fatalf(`Refreshing a category of another user should raise a "not found" error, got %v`, err)
+	}
+}

+ 46 - 6
internal/api/entry_integration_test.go

@@ -5,9 +5,12 @@ package api_test
 
 import (
 	"errors"
+	"fmt"
 	"math"
+	"net/http"
 	"strings"
 	"testing"
+	"time"
 
 	miniflux "miniflux.app/v2/client"
 	"miniflux.app/v2/internal/model"
@@ -621,7 +624,7 @@ func TestToggleStarredEndpoint(t *testing.T) {
 	testConfig := requireIntegrationTestConfig(t)
 	fixture := newFeedFixture(t)
 
-	regularUserClient, _ := testConfig.newRegularUser(t)
+	regularUserClient, regularTestUser := testConfig.newRegularUser(t)
 
 	feedID := fixture.subscribe(t, regularUserClient)
 
@@ -639,6 +642,21 @@ func TestToggleStarredEndpoint(t *testing.T) {
 	if !entry.Starred {
 		t.Fatalf(`The entry should be starred`)
 	}
+
+	// The bookmark route is an alias of the star route.
+	path := fmt.Sprintf("/v1/entries/%d/bookmark", entry.ID)
+	if status := testConfig.requestStatus(t, regularTestUser, http.MethodPut, path); status != http.StatusNoContent {
+		t.Fatalf(`Toggling the bookmark should return status %d, got %d`, http.StatusNoContent, status)
+	}
+
+	entry, err = regularUserClient.Entry(entry.ID)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if entry.Starred {
+		t.Fatalf(`The entry should no longer be starred`)
+	}
 }
 
 func TestShareEntryEndpoint(t *testing.T) {
@@ -774,12 +792,32 @@ func TestFlushHistoryEndpoint(t *testing.T) {
 	testConfig := requireIntegrationTestConfig(t)
 	fixture := newFeedFixture(t)
 
-	regularUserClient, _ := testConfig.newRegularUser(t)
+	regularUserClient, regularTestUser := testConfig.newRegularUser(t)
 
 	feedID := fixture.subscribe(t, regularUserClient)
 
 	result := listFeedEntries(t, regularUserClient, feedID, &miniflux.Filter{Limit: 3})
 
+	// The API replies before the history is flushed in the background.
+	waitForNoReadEntries := func() {
+		t.Helper()
+
+		deadline := time.Now().Add(5 * time.Second)
+		for {
+			readEntries, err := regularUserClient.Entries(&miniflux.Filter{Status: miniflux.EntryStatusRead})
+			if err != nil {
+				t.Fatal(err)
+			}
+			if readEntries.Total == 0 {
+				return
+			}
+			if time.Now().After(deadline) {
+				t.Fatalf(`Expected no read entries after flushing the history, got %d`, readEntries.Total)
+			}
+			time.Sleep(50 * time.Millisecond)
+		}
+	}
+
 	if err := regularUserClient.UpdateEntries([]int64{result.Entries[0].ID, result.Entries[1].ID}, miniflux.EntryStatusRead); err != nil {
 		t.Fatal(err)
 	}
@@ -787,15 +825,17 @@ func TestFlushHistoryEndpoint(t *testing.T) {
 	if err := regularUserClient.FlushHistory(); err != nil {
 		t.Fatal(err)
 	}
+	waitForNoReadEntries()
 
-	readEntries, err := regularUserClient.Entries(&miniflux.Filter{Status: miniflux.EntryStatusRead})
-	if err != nil {
+	// The endpoint also accepts the DELETE method, which the client does not use.
+	if err := regularUserClient.UpdateEntries([]int64{result.Entries[2].ID}, miniflux.EntryStatusRead); err != nil {
 		t.Fatal(err)
 	}
 
-	if readEntries.Total != 0 {
-		t.Fatalf(`Invalid total, got %d`, readEntries.Total)
+	if status := testConfig.requestStatus(t, regularTestUser, http.MethodDelete, "/v1/flush-history"); status != http.StatusAccepted {
+		t.Fatalf(`Flushing the history with DELETE should return status %d, got %d`, http.StatusAccepted, status)
 	}
+	waitForNoReadEntries()
 }
 
 func TestImportFeedEntryEndpoint(t *testing.T) {

+ 21 - 0
internal/api/integration_server_test.go

@@ -7,6 +7,7 @@ import (
 	"fmt"
 	"log/slog"
 	"math/rand/v2"
+	"net/http"
 	"net/http/httptest"
 	"os"
 	"testing"
@@ -161,6 +162,26 @@ func (c *integrationTestConfig) newRegularUser(t *testing.T) (*miniflux.Client,
 	return miniflux.NewClient(c.testBaseURL, user.Username, c.testRegularPassword), user
 }
 
+// requestStatus sends an API request authenticated as the regular user and returns the status code.
+// It covers routes that the client does not use, such as aliases and alternative methods.
+func (c *integrationTestConfig) requestStatus(t *testing.T, user *miniflux.User, method, path string) int {
+	t.Helper()
+
+	request, err := http.NewRequest(method, c.testBaseURL+path, nil)
+	if err != nil {
+		t.Fatal(err)
+	}
+	request.SetBasicAuth(user.Username, c.testRegularPassword)
+
+	response, err := http.DefaultClient.Do(request)
+	if err != nil {
+		t.Fatal(err)
+	}
+	response.Body.Close()
+
+	return response.StatusCode
+}
+
 // listFeedEntries returns the entries of the feed that match the filter.
 func listFeedEntries(t *testing.T, client *miniflux.Client, feedID int64, filter *miniflux.Filter) *miniflux.EntryResultSet {
 	t.Helper()