RezinaLMS-BackEnd/internal/domain/affiliates_test.go

70 lines
2.2 KiB
Go

package domain
import (
"math"
"net/url"
"testing"
)
func TestAffiliateSettingsValidation(t *testing.T) {
rate := 10.25
valid := AffiliateSettingsInput{CommissionPercent: &rate, CommissionMode: "EVERY_COURSE", AttributionDays: 30, HoldingDays: 14, ShareBaseURL: "https://academy.example/register"}
if err := ValidateAffiliateSettings(valid); err != nil {
t.Fatal(err)
}
for _, p := range []float64{-1, 101, 0.001, math.NaN(), math.Inf(1)} {
v := valid
v.CommissionPercent = &p
if ValidateAffiliateSettings(v) == nil {
t.Errorf("accepted rate %v", p)
}
}
for _, u := range []string{"javascript:alert(1)", "https://user:pass@example.com", "/register"} {
v := valid
v.ShareBaseURL = u
if ValidateAffiliateSettings(v) == nil {
t.Errorf("accepted URL %s", u)
}
}
v := valid
v.AttributionDays = 0
if ValidateAffiliateSettings(v) == nil {
t.Fatal("accepted zero attribution")
}
v = valid
v.HoldingDays = -1
if ValidateAffiliateSettings(v) == nil {
t.Fatal("accepted negative hold")
}
v = valid
v.CommissionMode = "ANY"
if ValidateAffiliateSettings(v) == nil {
t.Fatal("accepted unknown mode")
}
}
func TestAffiliatePayoutValidation(t *testing.T) {
valid := AffiliatePayoutInput{Currency: "ETB", Method: "BANK", AccountName: "Test Artist", AccountReference: "123456"}
if err := ValidateAffiliatePayout(valid); err != nil {
t.Fatal(err)
}
invalid := []AffiliatePayoutInput{{Currency: "ETB", Method: "CASH", AccountName: "Test", AccountReference: "123"}, {Currency: "etb", Method: "BANK", AccountName: "Test", AccountReference: "123"}, {Currency: "ETB", Method: "BANK", AccountName: " ", AccountReference: "123"}}
for _, in := range invalid {
if ValidateAffiliatePayout(in) == nil {
t.Errorf("accepted %+v", in)
}
}
}
func TestAffiliateShareURL(t *testing.T) {
result := AffiliateShareURL("https://academy.example/register?campaign=makeup&ref=old#start", "ABCDEF0123456789")
u, err := url.Parse(result)
if err != nil {
t.Fatal(err)
}
if u.Query().Get("ref") != "ABCDEF0123456789" || u.Query().Get("campaign") != "makeup" || u.Fragment != "start" {
t.Fatal(result)
}
if AffiliateShareURL("", "code") != "" {
t.Fatal("blank base URL should not invent a destination")
}
}