Add Unraid and Gotify notifications
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package notify
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/backupper-unraid/backupper/internal/model"
|
||||
)
|
||||
|
||||
type testSecrets map[string]string
|
||||
|
||||
func (s testSecrets) Get(id string) (string, error) { return s[id], nil }
|
||||
|
||||
type roundTripFunc func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (f roundTripFunc) RoundTrip(request *http.Request) (*http.Response, error) { return f(request) }
|
||||
|
||||
func TestSendGotify(t *testing.T) {
|
||||
var body map[string]any
|
||||
client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
||||
if r.URL.Path != "/message" || r.URL.Query().Get("token") != "secret-token" {
|
||||
t.Errorf("unexpected request URL %s", r.URL.String())
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{StatusCode: http.StatusOK, Status: "200 OK", Body: io.NopCloser(strings.NewReader("{}")), Header: make(http.Header)}, nil
|
||||
})}
|
||||
|
||||
sender := &Sender{Secrets: testSecrets{"gotify-token": "secret-token"}, Client: client}
|
||||
target := model.NotificationTarget{Enabled: true, Type: "gotify", URL: "https://gotify.example.test", TokenRef: "gotify-token"}
|
||||
if err := sender.Send(context.Background(), target, "Backup failed", "repository unavailable", "failed"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if body["title"] != "Backup failed" || body["message"] != "repository unavailable" || body["priority"] != float64(8) {
|
||||
t.Fatalf("unexpected Gotify payload %#v", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendUnraid(t *testing.T) {
|
||||
var command string
|
||||
var args []string
|
||||
sender := &Sender{RunCommand: func(_ context.Context, name string, values ...string) error {
|
||||
command, args = name, values
|
||||
return nil
|
||||
}}
|
||||
target := model.NotificationTarget{Enabled: true, Type: "unraid"}
|
||||
if err := sender.Send(context.Background(), target, "Backup warning", "completed with warnings", "warning"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if command != "/usr/local/emhttp/webGui/scripts/notify" {
|
||||
t.Fatalf("unexpected command %q", command)
|
||||
}
|
||||
want := []string{"-e", "Backupper", "-s", "Backup warning", "-d", "completed with warnings", "-i", "warning", "-l", "/Tools/Backupper"}
|
||||
if !reflect.DeepEqual(args, want) {
|
||||
t.Fatalf("arguments = %#v, want %#v", args, want)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user