feat(config): load ntfy client.yml and resolve topic origins
Parse the official ntfy client configuration, apply command-line overrides, and turn short names, host/topic pairs, and absolute URLs into an origin and topic name.
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user