This repository has been archived by the owner on Dec 17, 2023. It is now read-only.
forked from plantimals/rsslay
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: better error handling, code refactoring, more tests and adjustments
- Loading branch information
Showing
13 changed files
with
278 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package events | ||
|
||
import ( | ||
"database/sql" | ||
"github.com/mmcdole/gofeed" | ||
"github.com/piraces/rsslay/pkg/feed" | ||
"github.com/piraces/rsslay/pkg/helpers" | ||
"log" | ||
"strings" | ||
) | ||
|
||
func GetParsedFeedForPubKey(pubKey string, db *sql.DB) (*gofeed.Feed, feed.Entity) { | ||
pubKey = strings.TrimSpace(pubKey) | ||
row := db.QueryRow("SELECT privatekey, url FROM feeds WHERE publickey=$1", pubKey) | ||
|
||
var entity feed.Entity | ||
err := row.Scan(&entity.PrivateKey, &entity.URL) | ||
if err != nil && err == sql.ErrNoRows { | ||
return nil, entity | ||
} else if err != nil { | ||
log.Printf("failed when trying to retrieve row with pubkey '%s': %v", pubKey, err) | ||
return nil, entity | ||
} | ||
|
||
if !helpers.IsValidHttpUrl(entity.URL) { | ||
log.Printf("retrieved invalid url from database %q, deleting...", entity.URL) | ||
feed.DeleteInvalidFeed(entity.URL, db) | ||
return nil, entity | ||
} | ||
|
||
parsedFeed, err := feed.ParseFeed(entity.URL) | ||
if err != nil { | ||
log.Printf("failed to parse feed at url %q: %v", entity.URL, err) | ||
feed.DeleteInvalidFeed(entity.URL, db) | ||
return nil, entity | ||
} | ||
|
||
return parsedFeed, entity | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package events | ||
|
||
import ( | ||
"errors" | ||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/piraces/rsslay/pkg/feed" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
const samplePubKey = "73e247ee8c4ff09a50525bed7b0869c371864c0bf2b4d6a2639acaed07613958" | ||
const samplePrivateKey = "4d0888c07093941c9db16fcffb96fdf8af49a6839e865ea6110c7ab7cbd2d3d3" | ||
const sampleValidDirectFeedUrl = "https://mastodon.social/@Gargron.rss" | ||
const sampleValidUrl = "https://mastodon.social/" | ||
|
||
func TestGetParsedFeedForPubKey(t *testing.T) { | ||
testCases := []struct { | ||
pubKey string | ||
expectedReturnUrl string | ||
expectedSqlError bool | ||
expectedSqlRow bool | ||
expectedInvalidUrl bool | ||
expectedInvalidFeed bool | ||
}{ | ||
{ | ||
pubKey: samplePubKey, | ||
expectedReturnUrl: sampleValidDirectFeedUrl, | ||
expectedSqlError: false, | ||
expectedSqlRow: true, | ||
expectedInvalidUrl: false, | ||
expectedInvalidFeed: false, | ||
}, | ||
{ | ||
pubKey: samplePubKey, | ||
expectedReturnUrl: "", | ||
expectedSqlError: false, | ||
expectedSqlRow: false, | ||
expectedInvalidUrl: false, | ||
expectedInvalidFeed: false, | ||
}, | ||
{ | ||
pubKey: samplePubKey, | ||
expectedReturnUrl: "", | ||
expectedSqlError: true, | ||
expectedSqlRow: false, | ||
expectedInvalidUrl: false, | ||
expectedInvalidFeed: false, | ||
}, | ||
{ | ||
pubKey: samplePubKey, | ||
expectedReturnUrl: "invalid", | ||
expectedSqlError: false, | ||
expectedSqlRow: true, | ||
expectedInvalidUrl: true, | ||
expectedInvalidFeed: false, | ||
}, | ||
{ | ||
pubKey: samplePubKey, | ||
expectedReturnUrl: sampleValidUrl, | ||
expectedSqlError: false, | ||
expectedSqlRow: true, | ||
expectedInvalidUrl: false, | ||
expectedInvalidFeed: true, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
db, mock, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
rows := sqlmock.NewRows([]string{"privatekey", "url"}) | ||
if tc.expectedSqlRow { | ||
rows.AddRow(samplePrivateKey, tc.expectedReturnUrl) | ||
} | ||
|
||
if tc.expectedSqlError { | ||
mock.ExpectQuery("SELECT privatekey, url FROM feeds").WillReturnError(errors.New("error")) | ||
} else { | ||
mock.ExpectQuery("SELECT privatekey, url FROM feeds").WillReturnRows(rows) | ||
} | ||
mock.ExpectClose() | ||
|
||
parsedFeed, entity := GetParsedFeedForPubKey(tc.pubKey, db) | ||
if tc.expectedSqlError { | ||
assert.Nil(t, parsedFeed) | ||
assert.Empty(t, entity) | ||
} else if !tc.expectedSqlRow { | ||
assert.Nil(t, parsedFeed) | ||
assert.Empty(t, entity) | ||
} else if tc.expectedInvalidUrl { | ||
assert.Nil(t, parsedFeed) | ||
assert.Equal(t, feed.Entity{ | ||
PublicKey: "", | ||
PrivateKey: samplePrivateKey, | ||
URL: tc.expectedReturnUrl, | ||
}, entity) | ||
} else if tc.expectedInvalidFeed { | ||
assert.Nil(t, parsedFeed) | ||
assert.Equal(t, feed.Entity{ | ||
PublicKey: "", | ||
PrivateKey: samplePrivateKey, | ||
URL: tc.expectedReturnUrl, | ||
}, entity) | ||
} else { | ||
assert.NotNil(t, parsedFeed) | ||
assert.Equal(t, feed.Entity{ | ||
PublicKey: "", | ||
PrivateKey: samplePrivateKey, | ||
URL: tc.expectedReturnUrl, | ||
}, entity) | ||
} | ||
_ = db.Close() | ||
} | ||
} |
Oops, something went wrong.