mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-09-29 02:32:07 +03:00
12d51d7195
Most tests opened a throwaway panel DB with database.InitDB, which runs the full AutoMigrate + seed on an empty file every time: ~230ms, and ~850ms under -race because GORM's reflection-heavy migration is what the detector slows most. internal/web/service does this in ~550 of its 830 tests, so the CI race job spent ~10 of its ~14.6 minutes re-migrating empty databases. internal/database/dbtest.InitDB migrates once per test process, then hands each test its own copy of that file (~130ms under -race) and registers the CloseDB cleanup. The copy then goes through InitDB like a panel restart, so every test still starts from the state a fresh install has. Tests that reopen an existing file, migrate a hand-built legacy DB or target Postgres keep calling database.InitDB. Locally under -race: internal/web/service 626s (last CI run) -> 114s, internal/sub 246s -> 35s.
69 lines
2.5 KiB
Go
69 lines
2.5 KiB
Go
package service
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
)
|
|
|
|
func TestSyncInbound_UpdatesMtprotoSecretAndAdTag(t *testing.T) {
|
|
dbDir := t.TempDir()
|
|
t.Setenv("XUI_DB_FOLDER", dbDir)
|
|
dbtest.InitDB(t, filepath.Join(dbDir, "x-ui.db"))
|
|
|
|
db := database.GetDB()
|
|
|
|
mtproto := &model.Inbound{Tag: "mtproto-in", Enable: true, Port: 10004, Protocol: model.MTProto}
|
|
if err := db.Create(mtproto).Error; err != nil {
|
|
t.Fatalf("create mtproto inbound: %v", err)
|
|
}
|
|
|
|
svc := ClientService{}
|
|
const email = "tg@example.com"
|
|
const firstSecret = "ee0123456789abcdef0123456789abcdef6578616d706c652e636f6d"
|
|
const rekeyedSecret = "eefedcba9876543210fedcba98765432106578616d706c652e636f6d"
|
|
const firstTag = "0123456789abcdef0123456789abcdef"
|
|
const retaggedTag = "fedcba9876543210fedcba9876543210"
|
|
|
|
first := model.Client{Email: email, Secret: firstSecret, AdTag: firstTag, Enable: true}
|
|
if err := svc.SyncInbound(nil, mtproto.Id, []model.Client{first}); err != nil {
|
|
t.Fatalf("SyncInbound (create): %v", err)
|
|
}
|
|
|
|
var row model.ClientRecord
|
|
if err := db.Where("email = ?", email).First(&row).Error; err != nil {
|
|
t.Fatalf("lookup client row: %v", err)
|
|
}
|
|
if row.Secret != firstSecret || row.AdTag != firstTag {
|
|
t.Fatalf("create must store secret and ad tag: got secret=%q adTag=%q", row.Secret, row.AdTag)
|
|
}
|
|
|
|
rekeyed := model.Client{Email: email, Secret: rekeyedSecret, AdTag: retaggedTag, Enable: true}
|
|
if err := svc.SyncInbound(nil, mtproto.Id, []model.Client{rekeyed}); err != nil {
|
|
t.Fatalf("SyncInbound (rekey): %v", err)
|
|
}
|
|
if err := db.Where("email = ?", email).First(&row).Error; err != nil {
|
|
t.Fatalf("lookup client row after rekey: %v", err)
|
|
}
|
|
if row.Secret != rekeyedSecret {
|
|
t.Errorf("a re-keyed secret must reach the client record (sub links and the clients page read it), got %q", row.Secret)
|
|
}
|
|
if row.AdTag != retaggedTag {
|
|
t.Errorf("a changed ad tag must reach the client record, got %q", row.AdTag)
|
|
}
|
|
|
|
secretless := model.Client{Email: email, Enable: true}
|
|
if err := svc.SyncInbound(nil, mtproto.Id, []model.Client{secretless}); err != nil {
|
|
t.Fatalf("SyncInbound (secretless): %v", err)
|
|
}
|
|
if err := db.Where("email = ?", email).First(&row).Error; err != nil {
|
|
t.Fatalf("lookup client row after secretless sync: %v", err)
|
|
}
|
|
if row.Secret != rekeyedSecret || row.AdTag != retaggedTag {
|
|
t.Errorf("a payload without mtproto fields must not wipe them: got secret=%q adTag=%q", row.Secret, row.AdTag)
|
|
}
|
|
}
|