This commit is contained in:
2026-03-26 13:02:43 +00:00
parent a689f8b9da
commit eb7b519e49
64 changed files with 7081 additions and 5572 deletions

View File

@@ -13,24 +13,18 @@ import (
"stream.api/internal/database/model"
appv1 "stream.api/internal/gen/proto/app/v1"
"stream.api/internal/middleware"
"stream.api/internal/modules/common"
)
func TestCreatePayment(t *testing.T) {
t.Run("plan không tồn tại", func(t *testing.T) {
db := newTestDB(t)
services := newTestAppServices(t, db)
user := seedTestUser(t, db, model.User{ID: uuid.NewString(), Email: "user@example.com", Role: ptrString("USER")})
conn, cleanup := newTestGRPCServer(t, services)
defer cleanup()
client := newPaymentsClient(conn)
_, err := client.CreatePayment(testActorOutgoingContext(user.ID, "USER"), &appv1.CreatePaymentRequest{
PlanId: uuid.NewString(),
TermMonths: 1,
PaymentMethod: paymentMethodWallet,
})
_, err := client.CreatePayment(testActorOutgoingContext(user.ID, "USER"), &appv1.CreatePaymentRequest{PlanId: uuid.NewString(), TermMonths: 1, PaymentMethod: common.PaymentMethodWallet})
assertGRPCCode(t, err, codes.NotFound)
})
@@ -39,16 +33,10 @@ func TestCreatePayment(t *testing.T) {
services := newTestAppServices(t, db)
user := seedTestUser(t, db, model.User{ID: uuid.NewString(), Email: "user@example.com", Role: ptrString("USER")})
plan := seedTestPlan(t, db, model.Plan{ID: uuid.NewString(), Name: "Starter", Price: 10, Cycle: "monthly", StorageLimit: 10, UploadLimit: 1, QualityLimit: "720p", IsActive: ptrBool(false)})
conn, cleanup := newTestGRPCServer(t, services)
defer cleanup()
client := newPaymentsClient(conn)
_, err := client.CreatePayment(testActorOutgoingContext(user.ID, "USER"), &appv1.CreatePaymentRequest{
PlanId: plan.ID,
TermMonths: 1,
PaymentMethod: paymentMethodWallet,
})
_, err := client.CreatePayment(testActorOutgoingContext(user.ID, "USER"), &appv1.CreatePaymentRequest{PlanId: plan.ID, TermMonths: 1, PaymentMethod: common.PaymentMethodWallet})
assertGRPCCode(t, err, codes.InvalidArgument)
})
@@ -57,16 +45,10 @@ func TestCreatePayment(t *testing.T) {
services := newTestAppServices(t, db)
user := seedTestUser(t, db, model.User{ID: uuid.NewString(), Email: "user@example.com", Role: ptrString("USER")})
plan := seedTestPlan(t, db, model.Plan{ID: uuid.NewString(), Name: "Starter", Price: 10, Cycle: "monthly", StorageLimit: 10, UploadLimit: 1, QualityLimit: "720p", IsActive: ptrBool(true)})
conn, cleanup := newTestGRPCServer(t, services)
defer cleanup()
client := newPaymentsClient(conn)
_, err := client.CreatePayment(testActorOutgoingContext(user.ID, "USER"), &appv1.CreatePaymentRequest{
PlanId: plan.ID,
TermMonths: 2,
PaymentMethod: paymentMethodWallet,
})
_, err := client.CreatePayment(testActorOutgoingContext(user.ID, "USER"), &appv1.CreatePaymentRequest{PlanId: plan.ID, TermMonths: 2, PaymentMethod: common.PaymentMethodWallet})
assertGRPCCode(t, err, codes.InvalidArgument)
})
@@ -75,16 +57,10 @@ func TestCreatePayment(t *testing.T) {
services := newTestAppServices(t, db)
user := seedTestUser(t, db, model.User{ID: uuid.NewString(), Email: "user@example.com", Role: ptrString("USER")})
plan := seedTestPlan(t, db, model.Plan{ID: uuid.NewString(), Name: "Starter", Price: 10, Cycle: "monthly", StorageLimit: 10, UploadLimit: 1, QualityLimit: "720p", IsActive: ptrBool(true)})
conn, cleanup := newTestGRPCServer(t, services)
defer cleanup()
client := newPaymentsClient(conn)
_, err := client.CreatePayment(testActorOutgoingContext(user.ID, "USER"), &appv1.CreatePaymentRequest{
PlanId: plan.ID,
TermMonths: 1,
PaymentMethod: "bank_transfer",
})
_, err := client.CreatePayment(testActorOutgoingContext(user.ID, "USER"), &appv1.CreatePaymentRequest{PlanId: plan.ID, TermMonths: 1, PaymentMethod: "bank_transfer"})
assertGRPCCode(t, err, codes.InvalidArgument)
})
@@ -93,18 +69,12 @@ func TestCreatePayment(t *testing.T) {
services := newTestAppServices(t, db)
user := seedTestUser(t, db, model.User{ID: uuid.NewString(), Email: "user@example.com", Role: ptrString("USER")})
plan := seedTestPlan(t, db, model.Plan{ID: uuid.NewString(), Name: "Pro", Price: 50, Cycle: "monthly", StorageLimit: 100, UploadLimit: 10, QualityLimit: "1080p", IsActive: ptrBool(true)})
seedWalletTransaction(t, db, model.WalletTransaction{ID: uuid.NewString(), UserID: user.ID, Type: walletTransactionTypeTopup, Amount: 10, Currency: ptrString("USD")})
seedWalletTransaction(t, db, model.WalletTransaction{ID: uuid.NewString(), UserID: user.ID, Type: common.WalletTransactionTypeTopup, Amount: 10, Currency: ptrString("USD")})
conn, cleanup := newTestGRPCServer(t, services)
defer cleanup()
client := newPaymentsClient(conn)
var trailer metadata.MD
_, err := client.CreatePayment(testActorOutgoingContext(user.ID, "USER"), &appv1.CreatePaymentRequest{
PlanId: plan.ID,
TermMonths: 1,
PaymentMethod: paymentMethodWallet,
}, grpc.Trailer(&trailer))
_, err := client.CreatePayment(testActorOutgoingContext(user.ID, "USER"), &appv1.CreatePaymentRequest{PlanId: plan.ID, TermMonths: 1, PaymentMethod: common.PaymentMethodWallet}, grpc.Trailer(&trailer))
assertGRPCCode(t, err, codes.InvalidArgument)
body := firstTestMetadataValue(trailer, "x-error-body")
if body == "" {
@@ -120,29 +90,22 @@ func TestCreatePayment(t *testing.T) {
services := newTestAppServices(t, db)
user := seedTestUser(t, db, model.User{ID: uuid.NewString(), Email: "user@example.com", Role: ptrString("USER")})
plan := seedTestPlan(t, db, model.Plan{ID: uuid.NewString(), Name: "Pro", Price: 20, Cycle: "monthly", StorageLimit: 100, UploadLimit: 10, QualityLimit: "1080p", IsActive: ptrBool(true)})
seedWalletTransaction(t, db, model.WalletTransaction{ID: uuid.NewString(), UserID: user.ID, Type: walletTransactionTypeTopup, Amount: 5, Currency: ptrString("USD")})
seedWalletTransaction(t, db, model.WalletTransaction{ID: uuid.NewString(), UserID: user.ID, Type: common.WalletTransactionTypeTopup, Amount: 5, Currency: ptrString("USD")})
conn, cleanup := newTestGRPCServer(t, services)
defer cleanup()
client := newPaymentsClient(conn)
resp, err := client.CreatePayment(testActorOutgoingContext(user.ID, "USER"), &appv1.CreatePaymentRequest{
PlanId: plan.ID,
TermMonths: 1,
PaymentMethod: paymentMethodTopup,
TopupAmount: ptrFloat64(15),
})
resp, err := client.CreatePayment(testActorOutgoingContext(user.ID, "USER"), &appv1.CreatePaymentRequest{PlanId: plan.ID, TermMonths: 1, PaymentMethod: common.PaymentMethodTopup, TopupAmount: ptrFloat64(15)})
if err != nil {
t.Fatalf("CreatePayment() error = %v", err)
}
if resp.Payment == nil || resp.Subscription == nil {
t.Fatalf("CreatePayment() response incomplete: %#v", resp)
}
if resp.InvoiceId != buildInvoiceID(resp.Payment.Id) {
t.Fatalf("invoice id = %q, want %q", resp.InvoiceId, buildInvoiceID(resp.Payment.Id))
if resp.InvoiceId != common.BuildInvoiceID(resp.Payment.Id) {
t.Fatalf("invoice id = %q, want %q", resp.InvoiceId, common.BuildInvoiceID(resp.Payment.Id))
}
if resp.Subscription.PaymentMethod != paymentMethodTopup {
t.Fatalf("subscription payment method = %q, want %q", resp.Subscription.PaymentMethod, paymentMethodTopup)
if resp.Subscription.PaymentMethod != common.PaymentMethodTopup {
t.Fatalf("subscription payment method = %q, want %q", resp.Subscription.PaymentMethod, common.PaymentMethodTopup)
}
if resp.Subscription.WalletAmount != 20 {
t.Fatalf("subscription wallet amount = %v, want 20", resp.Subscription.WalletAmount)
@@ -153,7 +116,6 @@ func TestCreatePayment(t *testing.T) {
if resp.WalletBalance != 0 {
t.Fatalf("wallet balance = %v, want 0", resp.WalletBalance)
}
payment := mustLoadPayment(t, db, resp.Payment.Id)
if payment.Amount != 20 {
t.Fatalf("payment amount = %v, want 20", payment.Amount)