package controller import ( "encoding/json" "fmt" "net/http" "net/http/httptest" "path/filepath" "strings" "testing" "github.com/gin-gonic/gin" "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" "github.com/mhsanaei/3x-ui/v3/internal/testpg" ) func TestGroupBulkAddResolvesInboundMembershipHTTP(t *testing.T) { cleanup, err := testpg.IsolatePackage("group_client_http") if err != nil { t.Fatal(err) } t.Cleanup(cleanup) dbtest.InitDB(t, filepath.Join(t.TempDir(), "x-ui.db")) db := database.GetDB() first := model.Inbound{Tag: "source", Port: 50111, Protocol: model.VLESS, Settings: `{"clients":[{"email":"shared"}]}`} second := model.Inbound{Tag: "other", Port: 50112, Protocol: model.VLESS, Settings: `{"clients":[{"email":"shared"},{"email":"other"}]}`} shared := model.ClientRecord{Email: "shared", Group: "previous"} other := model.ClientRecord{Email: "other", Group: "previous"} for _, row := range []any{&first, &second, &shared, &other} { if err := db.Create(row).Error; err != nil { t.Fatal(err) } } links := []model.ClientInbound{ {ClientId: shared.Id, InboundId: first.Id}, {ClientId: shared.Id, InboundId: second.Id}, {ClientId: other.Id, InboundId: second.Id}, } if err := db.Create(&links).Error; err != nil { t.Fatal(err) } originalSettings := second.Settings if err := db.Model(&second).UpdateColumn("settings", "{").Error; err != nil { t.Fatal(err) } router := gin.New() NewGroupController(router.Group("/panel/api/clients")) groupMutation(t, router, "/groups/bulkAdd", map[string]any{"group": "staff", "inboundId": first.Id}, false, "", 0) var unchanged model.ClientRecord if err := db.First(&unchanged, shared.Id).Error; err != nil || unchanged.Group != "previous" { t.Fatalf("malformed sibling inbound left a committed group change: %q, err=%v", unchanged.Group, err) } if err := db.Model(&second).UpdateColumn("settings", originalSettings).Error; err != nil { t.Fatal(err) } body := fmt.Sprintf(`{"group":"staff","inboundId":%d}`, first.Id) for _, want := range []int{1, 0} { req := httptest.NewRequest(http.MethodPost, "/panel/api/clients/groups/bulkAdd", strings.NewReader(body)) req.Header.Set("Content-Type", "application/json") out := httptest.NewRecorder() router.ServeHTTP(out, req) var result struct { Success bool Obj struct{ Affected int } } if err := json.Unmarshal(out.Body.Bytes(), &result); err != nil || !result.Success || result.Obj.Affected != want { t.Fatalf("group add returned %s, want %d changed: %v", out.Body, want, err) } } for id, want := range map[int]string{shared.Id: "staff", other.Id: "previous"} { var got model.ClientRecord if err := db.First(&got, id).Error; err != nil || got.Group != want { t.Fatalf("client %d group = %q, want %q: %v", id, got.Group, want, err) } } for _, id := range []int{first.Id, second.Id} { var inbound model.Inbound if err := db.First(&inbound, id).Error; err != nil || !strings.Contains(inbound.Settings, `"group":"staff"`) { t.Fatalf("inbound %d settings were not updated: %s (%v)", id, inbound.Settings, err) } } }