package test import ( "bytes" "encoding/json" "io" "net/http" "net/http/httptest" "strconv" "testing" "time" "xy_sh/internal/entities" "xy_sh/internal/router" "xy_sh/pkg/crypto" "github.com/gofiber/fiber/v2" ) func init() { crypto.InitCrypto("test_salt_12345", "test_key_16bytes") } func setupTestApp() *fiber.App { app := fiber.New() router.SetupRoutes(app) return app } func makeEncryptedRequest(t *testing.T, app *fiber.App, path string, bizData interface{}) (*http.Response, []byte) { t.Helper() bizJSON, err := json.Marshal(bizData) if err != nil { t.Fatalf("业务数据序列化失败: %v", err) } encryptedData, err := crypto.SM4CBCEncrypt(bizJSON) if err != nil { t.Fatalf("加密失败: %v", err) } timestamp := strconv.FormatInt(time.Now().UnixMilli(), 10) sign := crypto.GenerateSign(timestamp, encryptedData) reqBody := entities.EncryptedRequest{ EncryptedData: encryptedData, } reqJSON, _ := json.Marshal(reqBody) req := httptest.NewRequest(http.MethodPost, path, bytes.NewReader(reqJSON)) req.Header.Set("Content-Type", "application/json") req.Header.Set("timestamp", timestamp) req.Header.Set("sign", sign) resp, err := app.Test(req) if err != nil { t.Fatalf("请求失败: %v", err) } body, _ := io.ReadAll(resp.Body) resp.Body.Close() return resp, body } func TestCreateOrder(t *testing.T) { app := setupTestApp() bizData := entities.OrderRequest{ ActCode: "ACT001", GoodsCode: "123456", ActOrderNum: "TEST_ORDER_001", Account: "19912345678", CallbackUrl: "https://example.com/callback", } resp, body := makeEncryptedRequest(t, app, "/api/v1/order/create", bizData) if resp.StatusCode != http.StatusOK { t.Errorf("期望状态码200,实际: %d, 响应: %s", resp.StatusCode, string(body)) } var commonResp entities.CommonResponse if err := json.Unmarshal(body, &commonResp); err != nil { t.Fatalf("响应解析失败: %v, 响应: %s", err, string(body)) } if commonResp.Code != entities.CodeSuccess { t.Errorf("期望code=0,实际: %d, msg: %s", commonResp.Code, commonResp.Msg) } t.Logf("下单接口测试成功,响应: %s", string(body)) } func TestQueryOrder(t *testing.T) { app := setupTestApp() createBizData := entities.OrderRequest{ ActCode: "ACT001", GoodsCode: "123456", ActOrderNum: "TEST_ORDER_002", Account: "19912345678", } _, createBody := makeEncryptedRequest(t, app, "/api/v1/order/create", createBizData) var createResp entities.CommonResponse if err := json.Unmarshal(createBody, &createResp); err != nil { t.Fatalf("创建订单响应解析失败: %v", err) } if createResp.Code != entities.CodeSuccess { t.Fatalf("创建订单失败: %s", createResp.Msg) } encryptedData := createResp.Data.(string) decryptedData, err := crypto.SM4CBCDecrypt(encryptedData) if err != nil { t.Fatalf("解密创建订单响应失败: %v", err) } var orderResp entities.OrderResponseData if err := json.Unmarshal(decryptedData, &orderResp); err != nil { t.Fatalf("解析创建订单业务数据失败: %v", err) } queryBizData := entities.QueryOrderRequest{ ActCode: "ACT001", OrderNo: orderResp.OrderNo, } resp, body := makeEncryptedRequest(t, app, "/api/v1/order/query", queryBizData) if resp.StatusCode != http.StatusOK { t.Errorf("期望状态码200,实际: %d, 响应: %s", resp.StatusCode, string(body)) } var queryResp entities.CommonResponse if err := json.Unmarshal(body, &queryResp); err != nil { t.Fatalf("查询响应解析失败: %v", err) } if queryResp.Code != entities.CodeSuccess { t.Errorf("查询失败,code: %d, msg: %s", queryResp.Code, queryResp.Msg) } t.Logf("查询接口测试成功,响应: %s", string(body)) } func TestWxRecharge(t *testing.T) { app := setupTestApp() bizData := entities.WxRechargeRequest{ ActCode: "FBG6vdYqGE4mGX7EH/woEg==", OrderNo: "WX_TEST_ORDER_001", GoodsCode: "0001", ActOrderNum: "WX_ORDER_001", AppId: "wx1234567890", OpenId: "oABC1234567890", } resp, body := makeEncryptedRequest(t, app, "/api/v1/wx/recharge", bizData) if resp.StatusCode != http.StatusOK { t.Errorf("期望状态码200,实际: %d, 响应: %s", resp.StatusCode, string(body)) } var commonResp entities.CommonResponse if err := json.Unmarshal(body, &commonResp); err != nil { t.Fatalf("响应解析失败: %v", err) } if commonResp.Code != entities.CodeSuccess { t.Errorf("期望code=0,实际: %d, msg: %s", commonResp.Code, commonResp.Msg) } t.Logf("微信立减金充值接口测试成功,响应: %s", string(body)) } func TestCallback(t *testing.T) { app := setupTestApp() bizData := entities.CallbackRequest{ OrderNo: "HM202509291010001", ActOrderNum: "XY2025092910100001", Status: entities.StatusExpired, Account: "19912345678", } resp, body := makeEncryptedRequest(t, app, "/api/v1/callback/notify", bizData) if resp.StatusCode != http.StatusOK { t.Errorf("期望状态码200,实际: %d, 响应: %s", resp.StatusCode, string(body)) } if string(body) != "ok" { t.Errorf("期望响应'ok',实际: %s", string(body)) } t.Logf("回调通知接口测试成功") } func TestInvalidSign(t *testing.T) { app := setupTestApp() bizData := entities.OrderRequest{ ActCode: "ACT001", GoodsCode: "123456", ActOrderNum: "TEST_ORDER_003", } bizJSON, _ := json.Marshal(bizData) encryptedData, _ := crypto.SM4CBCEncrypt(bizJSON) timestamp := strconv.FormatInt(time.Now().UnixMilli(), 10) wrongSign := "wrong_signature_12345" reqBody := entities.EncryptedRequest{EncryptedData: encryptedData} reqJSON, _ := json.Marshal(reqBody) req := httptest.NewRequest(http.MethodPost, "/api/v1/order/create", bytes.NewReader(reqJSON)) req.Header.Set("Content-Type", "application/json") req.Header.Set("timestamp", timestamp) req.Header.Set("sign", wrongSign) resp, _ := app.Test(req) if resp.StatusCode != http.StatusUnauthorized { t.Errorf("期望状态码401(签名验证失败),实际: %d", resp.StatusCode) } t.Logf("签名验证失败测试成功") } func TestHealthCheck(t *testing.T) { app := setupTestApp() req := httptest.NewRequest(http.MethodGet, "/health", nil) resp, body := app.Test(req) if resp.StatusCode != http.StatusOK { t.Errorf("期望状态码200,实际: %d", resp.StatusCode) } t.Logf("健康检查接口测试成功,响应: %s", string(body)) }