53 lines
2.0 KiB
Go
53 lines
2.0 KiB
Go
package whatsapp
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
channelmodel "github.com/gochat/gochat/internal/model/channel"
|
|
)
|
|
|
|
func TestWhatsAppService_FetchHealthStatusFormatsChatwootPayload(t *testing.T) {
|
|
t.Setenv("FRONTEND_URL", "https://app.example.com")
|
|
|
|
svc := NewWhatsAppService(nil)
|
|
svc.graphAPIBase = "https://graph.example.test/v22.0"
|
|
svc.client.SetTransport(roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
|
require.Equal(t, "/v22.0/phone-123", r.URL.Path)
|
|
assert.Contains(t, r.URL.Query().Get("fields"), "quality_rating")
|
|
assert.Equal(t, "token-abc", r.URL.Query().Get("access_token"))
|
|
body := `{"id":"phone-123","display_phone_number":"+1 555 0100","verified_name":"Support","name_status":"APPROVED","quality_rating":"GREEN","messaging_limit_tier":"TIER_1K","account_mode":"LIVE","code_verification_status":"VERIFIED","webhook_configuration":{"application":"app-1"},"throughput":{"level":"STANDARD"},"last_onboarded_time":"2026-06-05T00:00:00Z","platform_type":"CLOUD_API","certificate":"cert-data"}`
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Header: http.Header{"Content-Type": []string{"application/json"}},
|
|
Body: io.NopCloser(bytes.NewBufferString(body)),
|
|
}, nil
|
|
}))
|
|
channel := &channelmodel.ChannelWhatsApp{
|
|
PhoneNumber: "+1555010000",
|
|
PhoneNumberID: "phone-123",
|
|
BusinessAccountID: "waba-456",
|
|
AccessToken: "token-abc",
|
|
}
|
|
|
|
payload, err := svc.FetchHealthStatus(context.Background(), channel)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "phone-123", payload["id"])
|
|
assert.Equal(t, "GREEN", payload["quality_rating"])
|
|
assert.Equal(t, "https://app.example.com/webhooks/whatsapp/+1555010000", payload["expected_webhook_url"])
|
|
assert.Equal(t, "waba-456", payload["business_id"])
|
|
assert.NotContains(t, payload, "healthy")
|
|
}
|
|
|
|
type roundTripFunc func(*http.Request) (*http.Response, error)
|
|
|
|
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
return f(req)
|
|
}
|