package notification import ( "context" "encoding/json" "io" "net/http" "net/http/httptest" "strings" "testing" ) func TestResendSenderHappyPath(t *testing.T) { tpls, err := NewTemplates() if err != nil { t.Fatalf("NewTemplates: %v", err) } var gotPath, gotAuth, gotContentType string var gotBody map[string]any srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { gotPath = r.URL.Path gotAuth = r.Header.Get("Authorization") gotContentType = r.Header.Get("Content-Type") b, _ := io.ReadAll(r.Body) _ = json.Unmarshal(b, &gotBody) _ = json.NewEncoder(w).Encode(map[string]string{"id": "resend-abc-123"}) })) t.Cleanup(srv.Close) s, err := NewResendEmailSender(ResendConfig{ APIKey: "secret-key", FromEmail: "no-reply@example.test", FromName: "GuestGuard", BaseURL: srv.URL, }, tpls) if err != nil { t.Fatalf("NewResendEmailSender: %v", err) } id, err := s.SendGuest(context.Background(), "to@example.test", "You're invited", TmplInvitation, map[string]any{ "GuestName": "Mira", "HostName": "Kay", "EventName": "Beach Day", "Link": "https://example.test/rsvp/x", }) if err != nil { t.Fatalf("SendGuest: %v", err) } if id != "resend-abc-123" { t.Fatalf("provider id: got %q want %q", id, "resend-abc-123") } if gotPath != "/emails" { t.Errorf("path: got %q want /emails", gotPath) } if gotAuth != "Bearer secret-key" { t.Errorf("auth: got %q", gotAuth) } if gotContentType != "application/json" { t.Errorf("content-type: got %q", gotContentType) } if gotBody["from"] != "GuestGuard " { t.Errorf("from: got %v", gotBody["from"]) } if !strings.Contains(gotBody["html"].(string), "Beach Day") { t.Errorf("html missing event name: %v", gotBody["html"]) } if !strings.Contains(gotBody["text"].(string), "Mira") { t.Errorf("text missing guest name: %v", gotBody["text"]) } } func TestResendSenderErrorPropagates(t *testing.T) { tpls, _ := NewTemplates() srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusUnauthorized) _, _ = w.Write([]byte(`{"message":"invalid api key"}`)) })) t.Cleanup(srv.Close) s, err := NewResendEmailSender(ResendConfig{ APIKey: "bad", FromEmail: "x@y.test", BaseURL: srv.URL, }, tpls) if err != nil { t.Fatalf("NewResendEmailSender: %v", err) } if err := s.SendVerification(context.Background(), "z@y.test", "Z", "https://link"); err == nil { t.Fatal("expected error on 401") } }