Files
kefu/im/backend/internal/app/sms_providers_test.go
T
2026-08-27 14:04:28 +08:00

53 lines
2.2 KiB
Go

package app
import (
"strings"
"testing"
)
func TestRenderSMSTemplateParams(t *testing.T) {
aliyun, err := renderSMSTemplateParams("aliyun", `{"code":"{{code}}","minutes":"{{minutes}}"}`, "086421", 301)
if err != nil || aliyun != `{"code":"086421","minutes":"6"}` {
t.Fatalf("unexpected aliyun template parameters: %q, %v", aliyun, err)
}
tencent, err := renderSMSTemplateParams("tencent", `["{{code}}","{{minutes}}"]`, "086421", 300)
if err != nil || tencent != `["086421","5"]` {
t.Fatalf("unexpected tencent template parameters: %q, %v", tencent, err)
}
if _, err = renderSMSTemplateParams("huawei", `{"code":"{{code}}"}`, "086421", 300); err == nil {
t.Fatal("huawei parameters must reject a non-array JSON value")
}
}
func TestHuaweiWSSE(t *testing.T) {
authorization, wsse := huaweiWSSE("app-key", "secret", "abc", "2026-08-25T10:00:00Z")
if authorization != `WSSE realm="SDP",profile="UsernameToken",type="Appkey"` {
t.Fatalf("unexpected authorization header: %s", authorization)
}
if !strings.Contains(wsse, `Username="app-key"`) || !strings.Contains(wsse, `PasswordDigest="imMh4+lxH6z6wYNFDE3+ycvpRGbC7S/R2Q7ehhnXEqU="`) {
t.Fatalf("unexpected X-WSSE header: %s", wsse)
}
}
func TestParseSMSProviderEndpoint(t *testing.T) {
tests := []struct {
provider string
endpoint string
valid bool
}{
{provider: "aliyun", endpoint: "https://dysmsapi.aliyuncs.com", valid: true},
{provider: "tencent", endpoint: "https://sms.tencentcloudapi.com/", valid: true},
{provider: "huawei", endpoint: "https://smsapi.cn-north-4.myhuaweicloud.com:443/sms/batchSendSms/v1", valid: true},
{provider: "aliyun", endpoint: "http://dysmsapi.aliyuncs.com", valid: false},
{provider: "tencent", endpoint: "https://sms.tencentcloudapi.com/custom", valid: false},
{provider: "huawei", endpoint: "https://user:pass@example.com/sms/batchSendSms/v1", valid: false},
{provider: "huawei", endpoint: "https://example.com/sms/batchSendSms/v1?token=secret", valid: false},
}
for _, test := range tests {
_, err := parseSMSProviderEndpoint(test.provider, test.endpoint)
if (err == nil) != test.valid {
t.Errorf("parseSMSProviderEndpoint(%q, %q) error = %v, valid = %v", test.provider, test.endpoint, err, test.valid)
}
}
}