RezinaLMS-BackEnd/internal/services/chapa/course_checkout_test.go

103 lines
4.1 KiB
Go

package chapa
import (
"Rezina-Backend/internal/config"
"Rezina-Backend/internal/domain"
"Rezina-Backend/internal/ports"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
type coursePaymentStore struct {
ports.PaymentStore
ports.ExamPrepCatalogCourseStore
ports.CoursePurchaseStore
course domain.ExamPrepCatalogCourse
owned bool
created domain.CreatePaymentInput
completed int
payment domain.Payment
}
func (s *coursePaymentStore) GetExamPrepCatalogCourseByID(context.Context, int64) (domain.ExamPrepCatalogCourse, error) {
return s.course, nil
}
func (s *coursePaymentStore) HasCoursePurchase(context.Context, int64, int64) (bool, error) {
return s.owned, nil
}
func (s *coursePaymentStore) CreatePayment(_ context.Context, in domain.CreatePaymentInput) (*domain.Payment, error) {
s.created = in
return &domain.Payment{ID: 71}, nil
}
func (s *coursePaymentStore) UpdatePaymentSessionID(context.Context, int64, string, string) error {
return nil
}
func (s *coursePaymentStore) GetPaymentByNonce(context.Context, string) (*domain.Payment, error) {
return &s.payment, nil
}
func (s *coursePaymentStore) CompleteCoursePayment(context.Context, int64, string, string) error {
s.completed++
return nil
}
type courseCheckoutUserStore struct{ ports.UserStore }
func (s courseCheckoutUserStore) GetUserByID(context.Context, int64) (domain.User, error) {
return domain.User{Email: "learner@example.com", FirstName: "Test", LastName: "Learner"}, nil
}
func TestCourseCheckoutUsesServerPriceAndNoSubscription(t *testing.T) {
var request domain.ChapaInitializeRequest
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
t.Error(err)
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"status":"success","data":{"checkout_url":"https://checkout.example/course"}}`))
}))
defer server.Close()
store := &coursePaymentStore{course: domain.ExamPrepCatalogCourse{ID: 12, Price: 1250.50, Currency: "ETB", PublishStatus: domain.ContentPublishPublished}}
svc := NewService(&config.Config{CHAPA_SECRET_KEY: "test", CHAPA_BASE_URL: server.URL}, server.Client(), store, nil, courseCheckoutUserStore{})
out, err := svc.InitiateCoursePayment(context.Background(), 42, 12, domain.CourseCheckoutRequest{})
if err != nil {
t.Fatal(err)
}
if out.Amount != 1250.50 || request.Amount != "1250.50" || request.Currency != "ETB" {
t.Fatalf("incorrect checkout: %+v / %+v", out, request)
}
if store.created.CourseID == nil || *store.created.CourseID != 12 || store.created.PlanID != nil || store.created.SubscriptionID != nil {
t.Fatalf("incorrect purchase scope: %+v", store.created)
}
store.owned = true
if _, err := svc.InitiateCoursePayment(context.Background(), 42, 12, domain.CourseCheckoutRequest{}); err != ErrCourseAlreadyOwned {
t.Fatalf("expected already owned, got %v", err)
}
store.owned = false
store.course.Price = 0
if _, err := svc.InitiateCoursePayment(context.Background(), 42, 12, domain.CourseCheckoutRequest{}); err != ErrCourseNotPurchasable {
t.Fatalf("zero price should not charge, got %v", err)
}
}
func TestCoursePaymentVerificationRejectsMismatches(t *testing.T) {
id := int64(12)
for _, tt := range []struct {
name, amount, currency string
want bool
}{
{"valid", "1250.50", "ETB", true}, {"underpayment", "1", "ETB", false}, {"wrong currency", "1250.50", "USD", false}, {"missing amount", "", "ETB", false}, {"nan", "NaN", "ETB", false},
} {
t.Run(tt.name, func(t *testing.T) {
store := &coursePaymentStore{payment: domain.Payment{ID: 71, CourseID: &id, Amount: 1250.50, Currency: "ETB", Nonce: "ref"}}
svc := NewService(nil, nil, store, nil, nil)
err := svc.applyVerifiedTransaction(context.Background(), domain.ChapaTransactionData{TxRef: "ref", Reference: "verified", Status: "success", Amount: domain.ChapaFlexibleString(tt.amount), Currency: tt.currency})
if (err == nil) != tt.want || (store.completed == 1) != tt.want {
t.Fatalf("err=%v grants=%d", err, store.completed)
}
})
}
}