Skip to content
This repository has been archived by the owner on Dec 17, 2023. It is now read-only.

Commit

Permalink
feat: new workflow for tests and firsts tests #10
Browse files Browse the repository at this point in the history
  • Loading branch information
piraces committed Feb 4, 2023
1 parent bc6164e commit 4cbc573
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 1 deletion.
25 changes: 25 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: CI Build & Test

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/mmcdole/gofeed v1.2.0
github.com/nbd-wtf/go-nostr v0.12.0
github.com/rif/cache2go v1.0.0
github.com/stretchr/testify v1.8.0
golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9
)

Expand All @@ -20,6 +21,7 @@ require (
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect
Expand All @@ -28,10 +30,12 @@ require (
github.com/mmcdole/goxpp v0.0.0-20200921145534-2f3784f67354 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/cors v1.7.0 // indirect
github.com/valyala/fastjson v1.6.3 // indirect
go.opentelemetry.io/otel v1.10.0 // indirect
go.opentelemetry.io/otel/trace v1.10.0 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/text v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/valyala/fastjson v1.6.3 h1:tAKFnnwmeMGPbwJ7IwxcTPCNr3uIzoIj3/Fh90ra4xc=
github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
go.opentelemetry.io/otel v1.10.0 h1:Y7DTJMR6zs1xkS/upamJYk0SxxN4C9AqRd77jmZnyY4=
Expand All @@ -72,4 +75,8 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion pkg/feed/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func GetFeedURL(url string) string {
if href == "" {
continue
}
if !strings.HasPrefix(href, "http") {
if !strings.HasPrefix(href, "http") && !strings.HasPrefix(href, "https") {
href, _ = helpers.UrlJoin(url, href)
}
return href
Expand Down
139 changes: 139 additions & 0 deletions pkg/feed/feed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package feed

import (
"github.com/mmcdole/gofeed"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

const samplePubKey = "1870bcd5f6081ef7ea4b17204ffa4e92de51670142be0c8140e0635b355ca85f"
const sampleUrlForPublicKey = "https://nitter.moomoo.me/Bitcoin/rss"
const samplePrivateKeyForPubKey = "27660ab89e69f59bb8d9f0bd60da4a8515cdd3e2ca4f91d72a242b086d6aaaa7"
const testSecret = "test"

const sampleInvalidUrl = "https:// nostr.example/"
const sampleInvalidUrlContentType = "https://accounts.google.com/.well-known/openid-configuration"
const sampleRedirectingUrl = "https://httpstat.us/301"
const sampleValidDirectFeedUrl = "https://mastodon.social/@Gargron.rss"
const sampleValidIndirectFeedUrl = "https://www.rssboard.org/"
const sampleValidIndirectFeedUrlExpected = "http://feeds.rssboard.org/rssboard"
const sampleValidWithoutFeedUrl = "https://go.dev/"
const sampleValidWithRelativeFeedUrl = "https://golangweekly.com/"
const sampleValidWithRelativeFeedUrlExpected = "https://golangweekly.com/rss"

var actualTime = time.Now()
var sampleNitterFeed = gofeed.Feed{
Title: "Coldplay / @coldplay",
Description: "Twitter feed for: @coldplay. Generated by nitter.moomoo.me",
Link: "http://nitter.moomoo.me/coldplay",
FeedLink: "https://nitter.moomoo.me/coldplay/rss",
Links: []string{"http://nitter.moomoo.me/coldplay"},
PublishedParsed: &actualTime,
Language: "en-us",
Image: &gofeed.Image{
URL: "http://nitter.moomoo.me/pic/pbs.twimg.com%2Fprofile_images%2F1417506973877211138%2FYIm7dOQH_400x400.jpg",
Title: "Coldplay / @coldplay",
},
}
var sampleDefaultFeed = gofeed.Feed{
Title: "Golang Weekly",
Description: "A weekly newsletter about the Go programming language",
Link: "https://golangweekly.com/rss",
FeedLink: "https://golangweekly.com/rss",
Links: []string{"https://golangweekly.com/issues/446"},
PublishedParsed: &actualTime,
Language: "en-us",
Image: nil,
}

func TestGetFeedURLWithInvalidURLReturnsEmptyString(t *testing.T) {
feed := GetFeedURL(sampleInvalidUrl)
assert.Empty(t, feed)
}

func TestGetFeedURLWithInvalidContentTypeReturnsEmptyString(t *testing.T) {
feed := GetFeedURL(sampleInvalidUrlContentType)
assert.Empty(t, feed)
}

func TestGetFeedURLWithRedirectingURLReturnsEmptyString(t *testing.T) {
feed := GetFeedURL(sampleRedirectingUrl)
assert.Empty(t, feed)
}

func TestGetFeedURLWithValidUrlOfValidTypesReturnsSameUrl(t *testing.T) {
feed := GetFeedURL(sampleValidDirectFeedUrl)
assert.Equal(t, sampleValidDirectFeedUrl, feed)
}

func TestGetFeedURLWithValidUrlOfHtmlTypeWithFeedReturnsFoundFeed(t *testing.T) {
feed := GetFeedURL(sampleValidIndirectFeedUrl)
assert.Equal(t, sampleValidIndirectFeedUrlExpected, feed)
}

func TestGetFeedURLWithValidUrlOfHtmlTypeWithRelativeFeedReturnsFoundFeed(t *testing.T) {
feed := GetFeedURL(sampleValidWithRelativeFeedUrl)
assert.Equal(t, sampleValidWithRelativeFeedUrlExpected, feed)
}

func TestGetFeedURLWithValidUrlOfHtmlTypeWithoutFeedReturnsEmpty(t *testing.T) {
feed := GetFeedURL(sampleValidWithoutFeedUrl)
assert.Empty(t, feed)
}

func TestParseFeedWithValidUrlReturnsParsedFeed(t *testing.T) {
feed, err := ParseFeed(sampleValidWithRelativeFeedUrlExpected)
assert.NotNil(t, feed)
assert.NoError(t, err)
}

func TestParseFeedWithValidUrlWithoutFeedReturnsError(t *testing.T) {
feed, err := ParseFeed(sampleValidWithoutFeedUrl)
assert.Nil(t, feed)
assert.Error(t, err)
}

func TestParseFeedWithCachedUrlReturnsCachedParsedFeed(t *testing.T) {
_, _ = ParseFeed(sampleValidWithRelativeFeedUrlExpected)
feed, err := ParseFeed(sampleValidWithRelativeFeedUrlExpected)
assert.NotNil(t, feed)
assert.NoError(t, err)
}

func TestFeedToSetMetadata(t *testing.T) {
testCases := []struct {
pubKey string
feed *gofeed.Feed
originalUrl string
enableAutoRegistration bool
defaultProfilePictureUrl string
}{
{
pubKey: samplePubKey,
feed: &sampleNitterFeed,
originalUrl: sampleNitterFeed.FeedLink,
enableAutoRegistration: true,
defaultProfilePictureUrl: "https://image.example",
},
{
pubKey: samplePubKey,
feed: &sampleDefaultFeed,
originalUrl: sampleDefaultFeed.FeedLink,
enableAutoRegistration: true,
defaultProfilePictureUrl: "https://image.example",
},
}
for _, tc := range testCases {
metadata := FeedToSetMetadata(tc.pubKey, tc.feed, tc.originalUrl, tc.enableAutoRegistration, tc.defaultProfilePictureUrl)
assert.NotEmpty(t, metadata)
assert.Equal(t, samplePubKey, metadata.PubKey)
assert.Equal(t, 0, metadata.Kind)
assert.Empty(t, metadata.Sig)
}
}

func TestPrivateKeyFromFeed(t *testing.T) {
sk := PrivateKeyFromFeed(sampleUrlForPublicKey, testSecret)
assert.Equal(t, samplePrivateKeyForPubKey, sk)
}
29 changes: 29 additions & 0 deletions pkg/helpers/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package helpers

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)

const sampleInvalidUrl = "https:// nostr.example/"
const sampleValidUrl = "https://nostr.example"

func TestJoinWithInvalidUrlReturnsNil(t *testing.T) {
join, err := UrlJoin(sampleInvalidUrl)
assert.Equal(t, join, "")
assert.ErrorContains(t, err, "invalid character")
}

func TestJoinWithValidUrlAndNoExtraElementsReturnsBaseUrl(t *testing.T) {
join, err := UrlJoin(sampleValidUrl)
assert.Equal(t, sampleValidUrl, join)
assert.NoError(t, err)
}

func TestJoinWithValidUrlAndExtraElementsReturnsValidUrl(t *testing.T) {
join, err := UrlJoin(sampleValidUrl, "rss")
expectedJoinResult := fmt.Sprintf("%s/%s", sampleValidUrl, "rss")
assert.Equal(t, expectedJoinResult, join)
assert.NoError(t, err)
}

0 comments on commit 4cbc573

Please sign in to comment.