83 lines
2.4 KiB
Go
83 lines
2.4 KiB
Go
package config
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestLoadSubscribeAndTokenOverride(t *testing.T) {
|
||
|
|
dir := t.TempDir()
|
||
|
|
path := filepath.Join(dir, "client.yml")
|
||
|
|
body := []byte(`
|
||
|
|
default-host: https://ntfy.example
|
||
|
|
default-token: tk_default
|
||
|
|
subscribe:
|
||
|
|
- topic: alerts
|
||
|
|
command: notify-send
|
||
|
|
- topic: public
|
||
|
|
token: ""
|
||
|
|
- topic: other.example/ops
|
||
|
|
user: ada
|
||
|
|
password: secret
|
||
|
|
`)
|
||
|
|
if err := os.WriteFile(path, body, 0o600); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
app, err := Load(Options{Path: path, PathSet: true, Since: "1h"})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if app.Since != "1h" || len(app.Subs) != 3 {
|
||
|
|
t.Fatalf("app: %+v", app.Subs)
|
||
|
|
}
|
||
|
|
if app.Subs[0].Name != "alerts" || app.Subs[0].Origin != "https://ntfy.example" || app.Subs[0].Auth.Token != "tk_default" {
|
||
|
|
t.Fatalf("alerts: %+v", app.Subs[0])
|
||
|
|
}
|
||
|
|
if app.Subs[1].Auth.Token != "" || app.Subs[1].Auth.User != "" {
|
||
|
|
t.Fatalf("empty token should clear auth: %+v", app.Subs[1].Auth)
|
||
|
|
}
|
||
|
|
if app.Subs[2].Origin != "https://other.example" || app.Subs[2].Name != "ops" || app.Subs[2].Auth.User != "ada" || app.Subs[2].Auth.Password != "secret" {
|
||
|
|
t.Fatalf("per-topic user: %+v", app.Subs[2])
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPositionalTopicsReplaceSubscribe(t *testing.T) {
|
||
|
|
dir := t.TempDir()
|
||
|
|
path := filepath.Join(dir, "client.yml")
|
||
|
|
body := []byte("default-host: https://ntfy.example\ndefault-token: tk_default\nsubscribe:\n - topic: ignored\n")
|
||
|
|
if err := os.WriteFile(path, body, 0o600); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
app, err := Load(Options{Path: path, PathSet: true, Topics: []string{"alerts"}, User: "ada:pw"})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if len(app.Subs) != 1 || app.Subs[0].Name != "alerts" || app.Subs[0].Auth.Token != "" || app.Subs[0].Auth.User != "ada" {
|
||
|
|
t.Fatalf("%+v", app.Subs[0])
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestHostWithoutTopicsStartsEmpty(t *testing.T) {
|
||
|
|
dir := t.TempDir()
|
||
|
|
path := filepath.Join(dir, "client.yml")
|
||
|
|
body := []byte("default-host: https://ntfy.example\ndefault-token: tk_default\n")
|
||
|
|
if err := os.WriteFile(path, body, 0o600); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
app, err := Load(Options{Path: path, PathSet: true})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if app.DefaultHost != "https://ntfy.example" || len(app.Subs) != 0 || app.Auth.Token != "tk_default" {
|
||
|
|
t.Fatalf("%+v", app)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestMissingDefaultFileNeedsTopic(t *testing.T) {
|
||
|
|
_, err := Load(Options{Path: filepath.Join(t.TempDir(), "missing.yml")})
|
||
|
|
if err == nil {
|
||
|
|
t.Fatal("expected error")
|
||
|
|
}
|
||
|
|
}
|