From 51765c794a4a11a1e0e99b2c228252d90c0d76e8 Mon Sep 17 00:00:00 2001 From: shanshanzhong Date: Tue, 16 Dec 2025 01:21:04 -0800 Subject: [PATCH] =?UTF-8?q?test(iap/apple):=20=E6=B7=BB=E5=8A=A0=E8=8B=B9?= =?UTF-8?q?=E6=9E=9C=E4=BA=A4=E6=98=93ID=E5=85=B3=E8=81=94=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E7=9A=84=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 测试苹果IAP支付配置中私钥回退逻辑的正确性 --- .../apple/attachTransactionByIdLogic_test.go | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 internal/logic/public/iap/apple/attachTransactionByIdLogic_test.go diff --git a/internal/logic/public/iap/apple/attachTransactionByIdLogic_test.go b/internal/logic/public/iap/apple/attachTransactionByIdLogic_test.go new file mode 100644 index 0000000..98d5cf3 --- /dev/null +++ b/internal/logic/public/iap/apple/attachTransactionByIdLogic_test.go @@ -0,0 +1,87 @@ +package apple + +import ( + "context" + "encoding/json" + "strings" + "testing" + + "github.com/perfect-panel/server/internal/model/order" + "github.com/perfect-panel/server/internal/model/payment" + "github.com/perfect-panel/server/internal/model/user" + "github.com/perfect-panel/server/internal/svc" + "github.com/perfect-panel/server/internal/types" + "github.com/perfect-panel/server/pkg/constant" +) + +type mockOrderModel struct { + order.Model +} + +func (m *mockOrderModel) FindOneByOrderNo(ctx context.Context, orderNo string) (*order.Order, error) { + return &order.Order{ + Id: 1, + OrderNo: orderNo, + PaymentId: 1, + }, nil +} + +type mockPaymentModel struct { + payment.Model +} + +func (m *mockPaymentModel) FindOne(ctx context.Context, id int64) (*payment.Payment, error) { + // Return a config with empty private key to trigger fallback + cfg := payment.AppleIAPConfig{ + KeyID: "some_key_id", + IssuerID: "some_issuer_id", + // PrivateKey is empty to test fallback + PrivateKey: "", + Sandbox: true, + } + cfgBytes, _ := json.Marshal(cfg) + return &payment.Payment{ + Id: id, + Platform: "apple", + Config: string(cfgBytes), + }, nil +} + +func TestAttachById_PrivateKeyFallback(t *testing.T) { + // Setup mock context + svcCtx := &svc.ServiceContext{ + OrderModel: &mockOrderModel{}, + PaymentModel: &mockPaymentModel{}, + Config: svc.ServiceContext{}.Config, // empty config + } + + // Mock user in context + ctx := context.WithValue(context.Background(), constant.CtxKeyUser, &user.User{Id: 1}) + + l := NewAttachTransactionByIdLogic(ctx, svcCtx) + + req := &types.AttachAppleTransactionByIdRequest{ + TransactionId: "test_tx_id", + OrderNo: "test_order_no", + } + + // Execute + _, err := l.AttachById(req) + + // We expect an error because GetTransactionInfo will fail to connect to Apple (or return 401/404) + // BUT, we want to ensure it is NOT "invalid private key" or "apple server api credential missing" + if err == nil { + // If it somehow succeeds (unlikely without real Apple connection), that's also fine for this test + t.Log("Success (unexpected but means key was valid)") + } else { + errMsg := err.Error() + if strings.Contains(errMsg, "invalid private key") { + t.Fatalf("Test failed: Got 'invalid private key' error, fallback did not work. Error: %v", err) + } + if strings.Contains(errMsg, "apple server api credential missing") { + t.Fatalf("Test failed: Got 'credential missing' error. Error: %v", err) + } + // If we get here, it means the key was accepted and we likely failed at network step + t.Logf("Got expected network/api error (meaning key was valid): %v", err) + } +}