plugins/plugins/wechat_cpn/internal/wechat_cpn_test.go

120 lines
3.0 KiB
Go

package internal
import (
"context"
"encoding/json"
"fmt"
"gitea.cdlsxd.cn/sdk/plugin/proto"
"github.com/stretchr/testify/assert"
"plugins/utils/wechat"
"testing"
)
var server = &WeChatCpnService{}
func config() []byte {
c := &wechat.Server{
MchID: "1605446142", // 证书所属商户
MchCertificateSerialNumber: "4D081089DEB385316CBDCB55C070287E4920AC76",
}
marshal, _ := json.Marshal(c)
return marshal
}
func TestConfig(t *testing.T) {
t.Run("TestConfig", func(t *testing.T) {
c := config()
fmt.Printf("%s\n", string(c))
assert.NotEmpty(t, c)
})
}
func TestOrder(t *testing.T) {
request := &proto.OrderRequest{
Config: config(),
Order: &proto.OrderRequest_Order{
OrderNo: "202411141940575304120010",
Account: "oknbq5kTqBqmzvrRqRoLB8V26DCU",
Extra: []byte(`{"app_id":"wx83fd6da8093f55b7","stock_creator_mchid":"1605446142"}`),
},
Product: &proto.OrderRequest_Product{
ProductNo: "19161060",
Extra: []byte(`{}`),
},
}
t.Run("TestOrder", func(t *testing.T) {
got, err := server.Order(context.Background(), request)
if err != nil {
t.Errorf("Order() error = %v", err)
return
}
fmt.Printf("%+v", got)
assert.Equal(t, int(proto.Status_SUCCESS), int(got.Result.Status))
})
}
func TestQuery(t *testing.T) {
request := &proto.QueryRequest{
Config: config(),
Order: &proto.QueryRequest_Order{
OrderNo: "",
TradeNo: "69445765514",
Account: "oO3vO5AxRWgTjmMD38FTvnB5Rq6o",
Extra: []byte(`{"app_id":"wx9ed74283ad25bca1"}`),
},
}
t.Run("TestQuery", func(t *testing.T) {
got, err := server.Query(context.Background(), request)
if err != nil {
t.Errorf("Query() error = %v", err)
return
}
fmt.Printf("%+v \n", got)
assert.Equal(t, int(proto.Status_SUCCESS), int(got.Result.Status))
})
}
func TestNotify(t *testing.T) {
in := &proto.NotifyRequest{
Config: config(),
Queries: []byte(``),
Headers: []byte(``),
Body: []byte(`{
"id": "240fd59e-13a3-5e8b-84a8-6608d535f606",
"create_time": "2024-08-19T17:29:15+08:00",
"resource_type": "encrypt-resource",
"event_type": "COUPON.USE",
"summary": "代金券核销通知",
"original_type": "coupon",
"associated_data": "coupon",
"plain_text": {
"stock_creator_mchid": "1652465541",
"stock_id": "19345605",
"coupon_id": "71916331907",
"coupon_name": "三湘银行200元立减",
"description": "",
"status": "USED",
"create_time": "2024-08-19T17:28:11+08:00",
"coupon_type": "NORMAL",
"no_cash": false,
"singleitem": false,
"consume_information": {
"consume_time": "2024-08-19T17:29:15+08:00",
"consume_mchid": "1319713401",
"transaction_id": "4200002399202408192628783906"
}
}
}
`),
}
t.Run("TestNotify", func(t *testing.T) {
got, err := server.Notify(context.Background(), in)
if err != nil {
t.Errorf("Notify() error = %v", err)
return
}
fmt.Printf("TestNotify : %+v \n", got)
assert.Equal(t, int(proto.Status_SUCCESS), int(got.Result.Status))
})
}