Skip to content

Commit

Permalink
Add testing for config
Browse files Browse the repository at this point in the history
  • Loading branch information
waybackarchiver committed Aug 28, 2022
1 parent 8f41ad4 commit 0c55da2
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 12 deletions.
161 changes: 152 additions & 9 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,42 @@ import (
)

func TestSlotName(t *testing.T) {
expected := "Internet Archive"
got := SlotName(SLOT_IA)
tests := []struct {
slot string
name string
}{
{SLOT_IA, "Internet Archive"},
{"something", UNKNOWN},
}

if got != expected {
t.Fatalf(`Unexpected get the slot name description, got %v instead of %s`, got, expected)
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
got := SlotName(test.slot)

if got != test.name {
t.Fatalf(`Unexpected get the slot name description, got %v instead of %s`, got, test.name)
}
})
}
}

func TestSlotNameNotExist(t *testing.T) {
expected := UNKNOWN
got := SlotName("something")
func TestSlotExtra(t *testing.T) {
tests := []struct {
slot string
extra string
}{
{SLOT_IA, "https://web.archive.org/"},
{"something", UNKNOWN},
}

if got != expected {
t.Fatalf(`Unexpected get the slot name description, got %v instead of %s`, got, expected)
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
got := SlotExtra(test.slot)

if got != test.extra {
t.Errorf(`Unexpected get the slot's extra data, got %v instead of %s`, got, test.extra)
}
})
}
}

Expand Down Expand Up @@ -419,6 +441,24 @@ func TestTelegramHelptext(t *testing.T) {
}
}

func TestPublishToChannel(t *testing.T) {
os.Clearenv()
os.Setenv("WAYBACK_TELEGRAM_CHANNEL", "foo")
os.Setenv("WAYBACK_TELEGRAM_TOKEN", "tg:token")

parser := NewParser()
opts, err := parser.ParseEnvironmentVariables()
if err != nil {
t.Fatalf(`Parsing environment variables failed: %v`, err)
}

ok := opts.PublishToChannel()

if !ok {
t.Fatalf(`Unexpected publish to telegram channel, got %v instead of true`, ok)
}
}

func TestTorPrivateKey(t *testing.T) {
os.Clearenv()
os.Setenv("WAYBACK_TOR_PRIVKEY", "tor:private:key")
Expand Down Expand Up @@ -490,6 +530,24 @@ func TestTorRemotePorts(t *testing.T) {
}
}

func TestTorrcFile(t *testing.T) {
os.Clearenv()
os.Setenv("WAYBACK_TORRC", "/path/to/torrc")

parser := NewParser()
opts, err := parser.ParseEnvironmentVariables()
if err != nil {
t.Fatalf(`Parsing environment variables failed: %v`, err)
}

expected := `/path/to/torrc`
got := opts.TorrcFile()

if got != expected {
t.Fatalf(`Unexpected Set torrc file, got %v instead of %v`, got, expected)
}
}

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

Expand Down Expand Up @@ -597,6 +655,25 @@ func TestGitHubRepo(t *testing.T) {
}
}

func TestPublishToIssues(t *testing.T) {
os.Clearenv()
os.Setenv("WAYBACK_GITHUB_REPO", "github-repo")
os.Setenv("WAYBACK_GITHUB_TOKEN", "github:token")
os.Setenv("WAYBACK_GITHUB_OWNER", "github-owner")

parser := NewParser()
opts, err := parser.ParseEnvironmentVariables()
if err != nil {
t.Fatalf(`Parsing environment variables failed: %v`, err)
}

ok := opts.PublishToIssues()

if !ok {
t.Fatalf(`Unexpected publish to github issue, got %v instead of true`, ok)
}
}

func TestNotionToken(t *testing.T) {
os.Clearenv()
os.Setenv("WAYBACK_NOTION_TOKEN", "notion:token")
Expand Down Expand Up @@ -633,6 +710,24 @@ func TestNotionDatabaseID(t *testing.T) {
}
}

func TestPublishToNotion(t *testing.T) {
os.Clearenv()
os.Setenv("WAYBACK_NOTION_TOKEN", "notion:token")
os.Setenv("WAYBACK_NOTION_DATABASE_ID", "uuid4")

parser := NewParser()
opts, err := parser.ParseEnvironmentVariables()
if err != nil {
t.Fatalf(`Parsing environment variables failed: %v`, err)
}

ok := opts.PublishToNotion()

if !ok {
t.Fatalf(`Unexpected publish to notion, got %v instead of true`, ok)
}
}

func TestMastodonServer(t *testing.T) {
os.Clearenv()
os.Setenv("WAYBACK_MASTODON_SERVER", "https://mastodon.social")
Expand Down Expand Up @@ -705,6 +800,45 @@ func TestMastodonAccessToken(t *testing.T) {
}
}

func TestPublishToMastodon(t *testing.T) {
os.Clearenv()
os.Setenv("WAYBACK_MASTODON_KEY", "foo")
os.Setenv("WAYBACK_MASTODON_SECRET", "foo")
os.Setenv("WAYBACK_MASTODON_TOKEN", "foo")

parser := NewParser()
opts, err := parser.ParseEnvironmentVariables()
if err != nil {
t.Fatalf(`Parsing environment variables failed: %v`, err)
}

ok := opts.PublishToMastodon()

if !ok {
t.Fatalf(`Unexpected publish to mastodon, got %v instead of true`, ok)
}
}

func TestPublishToTwitter(t *testing.T) {
os.Clearenv()
os.Setenv("WAYBACK_TWITTER_CONSUMER_KEY", "foo")
os.Setenv("WAYBACK_TWITTER_CONSUMER_SECRET", "foo")
os.Setenv("WAYBACK_TWITTER_ACCESS_TOKEN", "foo")
os.Setenv("WAYBACK_TWITTER_ACCESS_SECRET", "foo")

parser := NewParser()
opts, err := parser.ParseEnvironmentVariables()
if err != nil {
t.Fatalf(`Parsing environment variables failed: %v`, err)
}

ok := opts.PublishToTwitter()

if !ok {
t.Fatalf(`Unexpected publish to twitter, got %v instead of true`, ok)
}
}

func TestIRCNick(t *testing.T) {
os.Clearenv()
os.Setenv("WAYBACK_IRC_NICK", "foo")
Expand Down Expand Up @@ -1302,6 +1436,15 @@ func TestMaxMediaSize(t *testing.T) {
}
}

func TestMaxAttachSize(t *testing.T) {
parser := NewParser()
opts, _ := parser.ParseEnvironmentVariables()
got := opts.MaxAttachSize("telegram")
if got != maxAttachSizeTelegram {
t.Fatalf(`Unexpected set wayback timeout got %d instead of %d`, got, maxAttachSizeTelegram)
}
}

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

Expand Down
10 changes: 7 additions & 3 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ const (
defWaybackMeiliEndpoint = ""
defWaybackMeiliIndexing = "capsules"
defWaybackMeiliApikey = ""

maxAttachSizeTelegram = 50000000 // 50MB
maxAttachSizeDiscord = 8000000 // 8MB
maxAttachSizeSlack = 5000000000 // 5GB
)

var (
Expand Down Expand Up @@ -692,9 +696,9 @@ func (o *Options) MaxMediaSize() uint64 {
// scope: telegram
func (o *Options) MaxAttachSize(scope string) int64 {
scopes := map[string]int64{
"telegram": 50000000, // 50MB
"discord": 8000000, // 8MB
"slack": 5000000000, // 5GB
"telegram": maxAttachSizeTelegram,
"discord": maxAttachSizeDiscord,
"slack": maxAttachSizeSlack,
}
return scopes[scope]
}
Expand Down
15 changes: 15 additions & 0 deletions storage/telegram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,18 @@ func TestPlayback(t *testing.T) {
t.Errorf("Unexpected query playback, got %s instead of %s", pb.Source, dt)
}
}

func TestRemovePlayback(t *testing.T) {
dbpath := tmpPath()
defer os.Remove(dbpath)

s, err := Open(dbpath)
if err != nil {
t.Fatalf("Unexpected open a bolt db: %v", err)
}
defer s.Close()

if s.RemovePlayback(0) != nil {
t.Error("Unexpected remove playback data")
}
}

0 comments on commit 0c55da2

Please sign in to comment.