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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions internal/api/api_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2074,6 +2074,74 @@ func TestGetFeedIconWithInexistingFeedID(t *testing.T) {
}
}

func TestGetIconWithInexistingIconID(t *testing.T) {
t.Parallel()

testConfig := newIntegrationTestConfig()
if !testConfig.isConfigured() {
t.Skip(skipIntegrationTestsMessage)
}

client := miniflux.NewClient(testConfig.testBaseURL, testConfig.testAdminUsername, testConfig.testAdminPassword)
_, err := client.Icon(123456789)
if !errors.Is(err, miniflux.ErrNotFound) {
t.Fatalf(`Fetching an inexisting icon should return a "not found" error, got %v`, err)
}
}

func TestGetIconByIconIDFromAnotherUser(t *testing.T) {
t.Parallel()

testConfig := newIntegrationTestConfig()
if !testConfig.isConfigured() {
t.Skip(skipIntegrationTestsMessage)
}

adminClient := miniflux.NewClient(testConfig.testBaseURL, testConfig.testAdminUsername, testConfig.testAdminPassword)

// The owner subscribes to a feed, which fetches and stores its icon.
ownerUser, err := adminClient.CreateUser(testConfig.genRandomUsername(), testConfig.testRegularPassword, false)
if err != nil {
t.Fatal(err)
}
defer adminClient.DeleteUser(ownerUser.ID)

ownerClient := miniflux.NewClient(testConfig.testBaseURL, ownerUser.Username, testConfig.testRegularPassword)

feedID, err := ownerClient.CreateFeed(&miniflux.FeedCreationRequest{
FeedURL: testConfig.testFeedURL,
})
if err != nil {
t.Fatal(err)
}

ownerIcon, err := ownerClient.FeedIcon(feedID)
if err != nil {
t.Fatal(err)
}
if ownerIcon == nil {
t.Fatalf(`Invalid icon, got nil`)
}

// The owner can fetch its own icon by icon ID.
if _, err := ownerClient.Icon(ownerIcon.ID); err != nil {
t.Fatalf(`The owner should be able to fetch its own icon, got %v`, err)
}

// Another user without access to that feed must not be able to fetch the icon.
otherUser, err := adminClient.CreateUser(testConfig.genRandomUsername(), testConfig.testRegularPassword, false)
if err != nil {
t.Fatal(err)
}
defer adminClient.DeleteUser(otherUser.ID)

otherClient := miniflux.NewClient(testConfig.testBaseURL, otherUser.Username, testConfig.testRegularPassword)

if _, err := otherClient.Icon(ownerIcon.ID); !errors.Is(err, miniflux.ErrNotFound) {
t.Fatalf(`Fetching an icon owned by another user should return a "not found" error, got %v`, err)
}
}

func TestGetFeedsEndpoint(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 1 addition & 1 deletion internal/api/icon_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (h *handler) getIconByIconIDHandler(w http.ResponseWriter, r *http.Request)
return
}

icon, err := h.store.IconByID(iconID)
icon, err := h.store.IconByUserAndIconID(request.UserID(r), iconID)
if err != nil {
response.JSONServerError(w, r, err)
return
Expand Down
31 changes: 20 additions & 11 deletions internal/storage/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,33 @@ func (s *Storage) HasFeedIcon(feedID int64) bool {
return result
}

// IconByID fetches a single icon by its internal identifier, returning nil when it is not found.
func (s *Storage) IconByID(iconID int64) (*model.Icon, error) {
// IconByUserAndIconID fetches a single icon by its internal identifier, scoped
// to the given user. It returns nil when the icon does not exist or is not
// associated with any feed owned by the user.
func (s *Storage) IconByUserAndIconID(userID, iconID int64) (*model.Icon, error) {
var icon model.Icon
query := `
SELECT
id,
hash,
mime_type,
content,
external_id
FROM icons
WHERE id=$1`
err := s.db.QueryRow(query, iconID).Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content, &icon.ExternalID)
i.id,
i.hash,
i.mime_type,
i.content,
i.external_id
FROM icons AS i
WHERE i.id = $2
6947 AND EXISTS (
SELECT 1
FROM feeds AS f
INNER JOIN feed_icons AS fi ON fi.feed_id = f.id
WHERE f.user_id = $1
AND fi.icon_id = $2
)`
err := s.db.QueryRow(query, userID, iconID).Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content, &icon.ExternalID)
switch {
case errors.Is(err, sql.ErrNoRows):
return nil, nil
case err != nil:
return nil, fmt.Errorf("store: cannot load icon id=%d: %w", iconID, err)
return nil, fmt.Errorf("store: cannot load icon id=%d for user_id=%d: %w", iconID, userID, err)
default:
return &icon, nil
}
Expand Down
0