// 功能描述:充值配置缺项、协议缺失及鉴权失败回归;版本:1.0.0。 package common import ( "git.apinb.com/heqiapp/platforms/backend/api/internal/config" "git.apinb.com/heqiapp/platforms/backend/api/internal/impl" "github.com/DATA-DOG/go-sqlmock" "gorm.io/driver/postgres" "gorm.io/gorm" "gorm.io/gorm/logger" "strings" "testing" ) // TestRechargeOptionsUnavailable 未配置渠道不能被标记可支付,无协议不伪造正文。 func TestRechargeOptionsUnavailable(t *testing.T) { connection, mock, err := sqlmock.New() if err != nil { t.Fatal(err) } defer connection.Close() db, err := gorm.Open(postgres.New(postgres.Config{Conn: connection}), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)}) if err != nil { t.Fatal(err) } oldDB, oldPayment := impl.DBService, config.Spec.Payment impl.DBService = db config.Spec.Payment.Alipay.Enabled = false config.Spec.Payment.Wechat.Enabled = false defer func() { impl.DBService = oldDB; config.Spec.Payment = oldPayment }() mock.ExpectQuery(`SELECT .* FROM "user_account".*identity = \$1 AND status = \$2`).WithArgs("alice", 1, 1). WillReturnRows(sqlmock.NewRows([]string{"id", "identity", "phone"}).AddRow(7, "alice", "13800000001")) mock.ExpectQuery(`SELECT .* FROM "cms_content".*content_type = \$1 AND title = \$2 AND publish_status = \$3 AND status = \$4.*ORDER BY version_no desc, id desc`). WithArgs("agreement", "充值协议", "published", 1, 1).WillReturnRows(sqlmock.NewRows([]string{"id"})) ctx, response := paymentTestContext("") GetRechargeOptions("user_app")(ctx) body := response.Body.String() if !strings.Contains(body, `"code":0`) || strings.Contains(body, `"app":true`) || strings.Contains(body, `"wap":true`) || strings.Contains(body, `"agreement":`) || strings.Contains(body, `"mock"`) { t.Fatal(body) } if err := mock.ExpectationsWereMet(); err != nil { t.Fatal(err) } // 账户查不到时不得继续查询协议或返回支付配置信息。 mock.ExpectQuery(`SELECT .* FROM "user_account".*identity = \$1 AND status = \$2`).WithArgs("alice", 1, 1). WillReturnRows(sqlmock.NewRows([]string{"id", "identity"})) deniedContext, deniedResponse := paymentTestContext("") GetRechargeOptions("user_app")(deniedContext) if strings.Contains(deniedResponse.Body.String(), `"channels"`) || strings.Contains(deniedResponse.Body.String(), `"code":0`) { t.Fatal("未鉴权账户取得充值配置") } if err := mock.ExpectationsWereMet(); err != nil { t.Fatal(err) } } // TestRechargeFileReadyRejectsDirectory 路径存在但为目录时不得宣告密钥配置就绪。 func TestRechargeFileReadyRejectsDirectory(t *testing.T) { if rechargeFileReady("") || rechargeFileReady(t.TempDir()) || rechargeFileReady("missing-recharge-key-file") { t.Fatal("错误接受缺失或目录配置") } }