8000 [pull] main from miniflux:main by pull[bot] · Pull Request #351 · suhaoGe/miniflux · GitHub
[go: up one dir, main page]

Skip to content
17 changes: 17 additions & 0 deletions internal/api/api_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2517,6 +2517,23 @@ func TestGetAllEntriesEndpointWithFilter(t *testing.T) {
t.Fatalf(`Invalid title, got empty`)
}

emptyPage, err := regularUserClient.Entries(&miniflux.Filter{
FeedID: feedID,
Limit: 1,
Offset: feedEntries.Total,
})
if err != nil {
t.Fatal(err)
}

if len(emptyPage.Entries) != 0 {
t.Fatalf(`Expected no entries beyond the final page, got %d`, len(emptyPage.Entries))
}

if emptyPage.Total != feedEntries.Total {
t.Fatalf(`Expected total %d beyond the final page, got %d`, feedEntries.Total, emptyPage.Total)
}

recentEntries, err := regularUserClient.Entries(&miniflux.Filter{Order: "published_at", Direction: "desc"})
if err != nil {
t.Fatal(err)
Expand Down
3 changes: 3 additions & 0 deletions internal/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,9 @@ func NewConfigOptions() *configOptions {
parsedIntValue: 1,
rawValue: "1",
valueType: intType,
validator: func(rawValue string) error {
return validateGreaterOrEqualThan(rawValue, 1)
},
},
"SCHEDULER_ENTRY_FREQUENCY_MAX_INTERVAL": {
parsedDuration: 24 * time.Hour,
Expand Down
8 changes: 8 additions & 0 deletions internal/config/options_parsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,14 @@ func TestSchedulerEntryFrequencyFactorOptionParsing(t *testing.T) {
if configParser.options.SchedulerEntryFrequencyFactor() != 2 {
t.Fatalf("Expected SCHEDULER_ENTRY_FREQUENCY_FACTOR to be 2")
}

if err := configParser.parseLines([]string{"SCHEDULER_ENTRY_FREQUENCY_FACTOR=0"}); err == nil {
t.Fatalf("Expected an error for SCHEDULER_ENTRY_FREQUENCY_FACTOR=0")
}

if err := configParser.parseLines([]string{"SCHEDULER_ENTRY_FREQUENCY_FACTOR=-1"}); err == nil {
t.Fatalf("Expected an error for SCHEDULER_ENTRY_FREQUENCY_FACTOR=-1")
}
}

func TestYouTubeEmbedUrlOverrideOptionParsing(t *testing.T) {
8000 Expand Down
2 changes: 1 addition & 1 deletion internal/http/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func createUnixSocketListener(socketFile string) net.Listener {
printErrorAndExit(`Server failed to listen on Unix socket %s: %v`, socketFile, err)
}

if err := os.Chmod(socketFile, 0666); err != nil {
if err := os.Chmod(socketFile, 0660); err != nil {
printErrorAndExit(`Unable to change socket permission for %s: %v`, socketFile, err)
}

Expand Down
33 changes: 33 additions & 0 deletions internal/http/server/server_test.go
8000
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package server

import (
"os"
"runtime"
"testing"
)

Expand Down Expand Up @@ -187,3 +189,34 @@ func TestAnyTLS(t *testing.T) {
})
}
}

func TestCreateUnixSocketListenerPermissions(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Unix sockets are not supported on Windows")
}

tempFile, err := os.CreateTemp("/tmp", "miniflux-*.sock")
if err != nil {
t.Fatalf("Unable to allocate Unix socket path: %v", err)
}
socketFile := tempFile.Name()
if err := tempFile.Close(); err != nil {
t.Fatalf("Unable to close temporary file: %v", err)
}
if err := os.Remove(socketFile); err != nil {
t.Fatalf("Unable to prepare Unix socket path: %v", err)
}
t.Cleanup(func() { os.Remove(socketFile) })

listener := createUnixSocketListener(socketFile)
t.Cleanup(func() { listener.Close() })

fileInfo, err := os.Stat(socketFile)
if err != nil {
t.Fatalf("Unable to stat Unix socket: %v", err)
}

if got, want := fileInfo.Mode().Perm(), os.FileMode(0660); got != want {
t.Errorf("Unix socket permissions = %04o, want %04o", got, want)
}
}
1 change: 1 addition & 0 deletions internal/reader/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func CreateFeed(store *storage.Storage, userID int64, feedCreationRequest *model
subscription.BlockFilterEntryRules = feedCreationRequest.BlockFilterEntryRules
subscription.KeepFilterEntryRules = feedCreationRequest.KeepFilterEntryRules
subscription.HideGlobally = feedCreationRequest.HideGlobally
subscription.NoMediaPlayer = feedCreationRequest.NoMediaPlayer
subscription.EtagHeader = responseHandler.ETag()
subscription.LastModifiedHeader = responseHandler.LastModified()
subscription.FeedURL = responseHandler.EffectiveURL()
Expand Down
7 changes: 4 additions & 3 deletions internal/storage/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,9 @@ func (s *Storage) RemoveCategory(userID, categoryID int64) error {
return nil
}

// RemoveAndReplaceCategoriesByName deletes the given categories, replacing those categories with the user's first
// category on affected feeds.
// RemoveAndReplaceCategoriesByName deletes categories with the given titles and
// reassigns affected feeds to the user's first remaining category. It returns
// an error if the deletion would leave the user without any categories.
func (s *Storage) RemoveAndReplaceCategoriesByName(userid int64, titles []string) error {
tx, err := s.db.Begin()
if err != nil {
Expand All @@ -251,7 +252,7 @@ func (s *Storage) RemoveAndReplaceCategoriesByName(userid int64, titles []string

titleParam := pq.Array(titles)
var count int
query := "SELECT count(*) FROM categories WHERE user_id = $1 and title != ANY($2)"
query := "SELECT count(*) FROM categories WHERE user_id = $1 AND title <> ALL($2)"
err = tx.QueryRow(query, userid, titleParam).Scan(&count)
if err != nil {
tx.Rollback()
Expand Down
32 changes: 22 additions & 10 deletions internal/storage/entry_query_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,19 @@ func (e *EntryQueryBuilder) WithSorting(column, direction string) *EntryQueryBui
return e
}

// WithLimit set the limit.
// WithLimit sets the limit. A non-positive limit is clamped to
// model.MaxEntryLimit so callers cannot request an unbounded result set.
func (e *EntryQueryBuilder) WithLimit(limit int) *EntryQueryBuilder {
if limit > 0 {
e.limit = min(limit, model.MaxEntryLimit)
}
return e
return e.WithLimitAndMaximum(limit, model.MaxEntryLimit)
}

// WithLimitAndMaximum sets the limit, capped at the given maximum.
// A non-positive limit is clamped to the maximum.
func (e *EntryQueryBuilder) WithLimitAndMaximum(limit, maximum int) *EntryQueryBuilder {
if limit > 0 {
e.limit = min(limit, maximum)
if limit <= 0 || limit > maximum {
limit = maximum
}
e.limit = limit
return e
}

Expand Down Expand Up @@ -273,10 +273,22 @@ func (e *EntryQueryBuilder) GetEntries() (model.Entries, error) {
}

// GetEntriesWithCount returns a list of entries and the total count of matching
// rows (ignoring limit/offset) in a single query using a window function.
// This avoids a separate CountEntries() round-trip.
// rows, ignoring limit and offset. It uses a window function for non-empty pages
// and falls back to a separate count when the requested offset returns no rows.
func (e *EntryQueryBuilder) GetEntriesWithCount() (model.Entries, int, error) {
return e.fetchEntries(true)
entries, total, err := e.fetchEntries(true)
if err != nil {
return nil, 0, err
}

if len(entries) == 0 && e.offset > 0 {
total, err = e.CountEntries()
if err != nil {
return nil, 0, err
}
}

return entries, total, nil
}

// fetchEntries is the shared implementation for GetEntries and GetEntriesWithCount.
Expand Down
6 changes: 1 addition & 5 deletions internal/validator/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,7 @@ func ValidateFeedModification(store *storage.Storage, userID, feedID int64, requ
}
}

if request.ProxyURL != nil {
if *request.ProxyURL == "" {
return locale.NewLocalizedError("error.proxy_url_not_empty")
}

if request.ProxyURL != nil && *request.ProxyURL != "" {
if !urllib.IsValidProxyURL(*request.ProxyURL) {
return locale.NewLocalizedError("error.invalid_feed_proxy_url")
}
Expand Down
43 changes: 43 additions & 0 deletions internal/validator/feed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package validator // import "miniflux.app/v2/internal/validator"

import (
"testing"

"miniflux.app/v2/internal/model"
)

func TestValidateFeedModificationProxyURL(t *testing.T) {
tests := []struct {
name string
proxyURL string
wantErr bool
}{
{
name: "empty proxy URL",
proxyURL: "",
wantErr: false,
},
{
name: "valid proxy URL",
proxyURL: "http://127.0.0.1:3128",
wantErr: false,
},
{
name: "invalid proxy URL",
proxyURL: "example.org",
wantErr: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
request := &model.FeedModificationRequest{ProxyURL: &tc.proxyURL}
if err := ValidateFeedModification(nil, 0, 0, request); (err != nil) != tc.wantErr {
t.Fatalf("expected error %v, got %v", tc.wantErr, err)
}
})
}
}
13 changes: 11 additions & 2 deletions internal/validator/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func ValidateUserModification(store *storage.Storage, userID int64, changes *mod
}

if changes.EntryOrder != nil {
if err := ValidateEntryOrder(*changes.EntryOrder); err != nil {
return locale.NewLocalizedError("error.invalid_entry_order")
if err := validateEntrySortingOrder(*changes.EntryOrder); err != nil {
return err
}
}

Expand Down Expand Up @@ -218,6 +218,15 @@ func validateEntriesPerPage(entriesPerPage int) *locale.LocalizedError {
return nil
}

// validateEntrySortingOrder must accept only the values of the
// entry_sorting_order enum type in the database.
func validateEntrySortingOrder(order string) *locale.LocalizedError {
if order != "published_at" && order != "created_at" {
return locale.NewLocalizedError("error.invalid_entry_order")
}
return nil
}

func validateCategoriesSortingOrder(order string) *locale.LocalizedError {
if order != "alphabetical" && order != "unread_count" {
return locale.NewLocalizedError("error.invalid_categories_sorting_order")
Expand Down
19 changes: 19 additions & 0 deletions internal/validator/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ func TestValidateEntriesPerPage(t *testing.T) {
}
}

func TestValidateEntrySortingOrder(t *testing.T) {
for _, order := range []string{"published_at", "created_at"} {
if err := validateEntrySortingOrder(order); err != nil {
t.Errorf("expected valid order %q to pass, got %v", order, err)
}
}

// Valid entry-listing orders that are not part of the entry_sorting_order enum.
for _, order := range []string{"id", "status", "changed_at", "category_title", "category_id", "title", "author"} {
if err := validateEntrySortingOrder(order); err == nil {
t.Errorf("expected order %q to be rejected", order)
}
}

if err := validateEntrySortingOrder("invalid"); err == nil {
t.Error("expected invalid order to fail")
}
}

func TestValidateCategoriesSortingOrder(t *testing.T) {
for _, order := range []string{"alphabetical", "unread_count"} {
if err := validateCategoriesSortingOrder(order); err != nil {
Expand Down
0