mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-25 20:15:01 +03:00
e95d17a7db
Host rules could only be given to clients one by one, so giving a set of customers their own endpoints meant editing every client. A client group can now carry host rules, chosen when the group is created, from its edit dialog or from a quick-assign action on the Groups page. A subscription resolves a client's hosts in this order: the client's own host rules, then the host rules of its group, then the inbound's default pool. A group whose bound rules are all disabled or excluded from the requested format renders nothing for that inbound instead of falling back to the default pool. Binding a rule to a group narrows what its members get; other clients still see that rule in the default pool. Group names are now matched case-insensitively. On upgrade, labels that differ only in case (VIP and vip) are merged into one group, the first by name order. Adds POST /panel/api/clients/groups/:name/hosts and an optional hostGroupIds on group create and rename.
124 lines
4.0 KiB
Go
124 lines
4.0 KiB
Go
package database
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
)
|
|
|
|
func TestEnsureClientGroupRows_CreatesMissingAndCanonicalizes(t *testing.T) {
|
|
initMigrateDB(t)
|
|
db := GetDB()
|
|
if err := db.Create(&model.ClientRecord{Email: "a@x", Group: "VIP", Enable: true}).Error; err != nil {
|
|
t.Fatalf("create client a: %v", err)
|
|
}
|
|
if err := db.Create(&model.ClientRecord{Email: "b@x", Group: "vip", Enable: true}).Error; err != nil {
|
|
t.Fatalf("create client b: %v", err)
|
|
}
|
|
|
|
if err := EnsureClientGroupRows(); err != nil {
|
|
t.Fatalf("EnsureClientGroupRows: %v", err)
|
|
}
|
|
var groups []model.ClientGroup
|
|
if err := db.Order("id ASC").Find(&groups).Error; err != nil {
|
|
t.Fatalf("load groups: %v", err)
|
|
}
|
|
if len(groups) != 1 || groups[0].Name != "VIP" {
|
|
t.Fatalf("groups = %+v, want one canonical VIP", groups)
|
|
}
|
|
var clients []model.ClientRecord
|
|
if err := db.Order("email ASC").Find(&clients).Error; err != nil {
|
|
t.Fatalf("load clients: %v", err)
|
|
}
|
|
for _, client := range clients {
|
|
if client.Group != "VIP" {
|
|
t.Fatalf("client %s group = %q, want VIP", client.Email, client.Group)
|
|
}
|
|
}
|
|
|
|
if err := EnsureClientGroupRows(); err != nil {
|
|
t.Fatalf("second EnsureClientGroupRows: %v", err)
|
|
}
|
|
var count int64
|
|
if err := db.Model(&model.ClientGroup{}).Count(&count).Error; err != nil {
|
|
t.Fatalf("count groups: %v", err)
|
|
}
|
|
if count != 1 {
|
|
t.Fatalf("second run created a duplicate: %d", count)
|
|
}
|
|
}
|
|
|
|
func TestEnsureClientGroupRows_UpgradeExistingDB(t *testing.T) {
|
|
dbPath := filepath.Join(t.TempDir(), "x-ui.db")
|
|
if err := InitDB(dbPath); err != nil {
|
|
t.Fatalf("InitDB: %v", err)
|
|
}
|
|
if err := GetDB().Create(&model.ClientRecord{Email: "legacy@x", Group: "Legacy", Enable: true}).Error; err != nil {
|
|
t.Fatalf("create client: %v", err)
|
|
}
|
|
if err := CloseDB(); err != nil {
|
|
t.Fatalf("CloseDB: %v", err)
|
|
}
|
|
if err := InitDB(dbPath); err != nil {
|
|
t.Fatalf("reopen InitDB: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = CloseDB() })
|
|
|
|
var group model.ClientGroup
|
|
if err := GetDB().Where("name = ?", "Legacy").First(&group).Error; err != nil {
|
|
t.Fatalf("reopen did not create the missing group row: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPruneHostGroupClientGroups(t *testing.T) {
|
|
initMigrateDB(t)
|
|
db := GetDB()
|
|
inbound := seedInboundWithStream(t, "prune", 4599, `{"network":"tcp","security":"none"}`)
|
|
if err := db.Create(&model.Host{GroupId: "live", InboundId: inbound.Id, Remark: "L", Address: "live.example.com"}).Error; err != nil {
|
|
t.Fatalf("create host: %v", err)
|
|
}
|
|
group := &model.ClientGroup{Name: "VIP"}
|
|
if err := db.Create(group).Error; err != nil {
|
|
t.Fatalf("create group: %v", err)
|
|
}
|
|
for _, hostGroup := range []string{"live", "gone"} {
|
|
if err := db.Create(&model.HostGroupClientGroup{HostGroupId: hostGroup, ClientGroupId: group.Id}).Error; err != nil {
|
|
t.Fatalf("create binding %s: %v", hostGroup, err)
|
|
}
|
|
}
|
|
if err := PruneHostGroupClientGroups(); err != nil {
|
|
t.Fatalf("PruneHostGroupClientGroups: %v", err)
|
|
}
|
|
var ids []string
|
|
if err := db.Model(&model.HostGroupClientGroup{}).Pluck("host_group_id", &ids).Error; err != nil {
|
|
t.Fatalf("load bindings: %v", err)
|
|
}
|
|
if len(ids) != 1 || ids[0] != "live" {
|
|
t.Fatalf("bindings = %v, want only live", ids)
|
|
}
|
|
}
|
|
|
|
func TestEnsureClientGroupRows_Postgres(t *testing.T) {
|
|
if strings.TrimSpace(os.Getenv("XUI_DB_DSN")) == "" || os.Getenv("XUI_DB_TYPE") != "postgres" {
|
|
t.Skip("set XUI_DB_TYPE=postgres and XUI_DB_DSN to run the postgres test")
|
|
}
|
|
if err := InitDB(""); err != nil {
|
|
t.Fatalf("InitDB: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = CloseDB() })
|
|
GetDB().Exec("TRUNCATE TABLE clients, client_groups RESTART IDENTITY CASCADE")
|
|
if err := GetDB().Create(&model.ClientRecord{Email: "pg@x", Group: "PgGroup", Enable: true}).Error; err != nil {
|
|
t.Fatalf("create client: %v", err)
|
|
}
|
|
if err := EnsureClientGroupRows(); err != nil {
|
|
t.Fatalf("EnsureClientGroupRows: %v", err)
|
|
}
|
|
var group model.ClientGroup
|
|
if err := GetDB().Where("name = ?", "PgGroup").First(&group).Error; err != nil {
|
|
t.Fatalf("group not created: %v", err)
|
|
}
|
|
}
|