Some checks failed
Build docker and publish / build (20.15.1) (push) Has been cancelled
测试苹果IAP支付配置中私钥回退逻辑的正确性
88 lines
2.5 KiB
Go
88 lines
2.5 KiB
Go
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)
|
|
}
|
|
}
|