|
| 1 | +package alert |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "gjallar/internal/config" |
| 12 | +) |
| 13 | + |
| 14 | +func TestSignal(t *testing.T) { |
| 15 | + var got map[string]any |
| 16 | + status := 201 |
| 17 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 18 | + if r.Method != http.MethodPost || r.Header.Get("Content-Type") != "application/json" { |
| 19 | + t.Errorf("method=%s content-type=%s", r.Method, r.Header.Get("Content-Type")) |
| 20 | + } |
| 21 | + json.NewDecoder(r.Body).Decode(&got) |
| 22 | + w.WriteHeader(status) |
| 23 | + })) |
| 24 | + defer srv.Close() |
| 25 | + |
| 26 | + s := NewSignal(srv.URL, "+33616400522", []string{"+33689816957", "+33616400522"}) |
| 27 | + if err := s.Send(context.Background(), "[Gjallar] DOWN: web", "web — boom"); err != nil { |
| 28 | + t.Fatal(err) |
| 29 | + } |
| 30 | + if got["number"] != "+33616400522" { |
| 31 | + t.Errorf("number = %v", got["number"]) |
| 32 | + } |
| 33 | + if recs, _ := got["recipients"].([]any); len(recs) != 2 || recs[0] != "+33689816957" { |
| 34 | + t.Errorf("recipients = %v", got["recipients"]) |
| 35 | + } |
| 36 | + if msg, _ := got["message"].(string); !strings.Contains(msg, "DOWN: web") || !strings.Contains(msg, "boom") { |
| 37 | + t.Errorf("message = %q", got["message"]) |
| 38 | + } |
| 39 | + |
| 40 | + status = 400 |
| 41 | + if err := s.Send(context.Background(), "t", "m"); err == nil || !strings.Contains(err.Error(), "status 400") { |
| 42 | + t.Errorf("err = %v", err) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestBuildNotifiersSignal(t *testing.T) { |
| 47 | + ns, err := BuildNotifiers(map[string]config.Alert{ |
| 48 | + "sig": {Type: "signal", URL: "http://h/v2/send", Number: "+336", Recipients: []string{"+337"}}, |
| 49 | + }) |
| 50 | + if err != nil || ns["sig"] == nil { |
| 51 | + t.Fatalf("BuildNotifiers: %v", err) |
| 52 | + } |
| 53 | +} |
0 commit comments