package controller import ( "encoding/json" "fmt" "net/http" "net/http/httptest" "net/url" "slices" "testing" "github.com/mhsanaei/3x-ui/v3/internal/database" "github.com/mhsanaei/3x-ui/v3/internal/database/model" "github.com/mhsanaei/3x-ui/v3/internal/testpg" "github.com/mhsanaei/3x-ui/v3/internal/util/crypto" ) func TestClientUserSuggestionsHTTP(t *testing.T) { cleanup, err := testpg.IsolatePackage("routing_user_suggestions") if err != nil { t.Fatal(err) } t.Cleanup(cleanup) router, api := newAPIAuthTestEngine(t) NewClientController(router.Group("/base/panel/api/clients", api.checkAPIAuth, api.enforceTokenScope)) db := database.GetDB() for _, scope := range []string{model.ApiScopeAdmin, model.ApiScopeMonitor, model.ApiScopeNodeSync, model.ApiScopeNodeAdmin} { token := model.ApiToken{Name: scope, Token: crypto.HashTokenSHA256(scope), Enabled: true, Scope: scope} if err := db.Create(&token).Error; err != nil { t.Fatal(err) } } seeds := []model.ClientRecord{ {Email: "literal_%@test", Password: "private", Group: "ignored"}, {Email: "literal_ab@test", Comment: "only-in-a-comment"}, } if err := db.Create(&seeds).Error; err != nil { t.Fatal(err) } clients := make([]model.ClientRecord, 50000) for i := range clients { clients[i] = model.ClientRecord{Email: fmt.Sprintf("fixture-%05d@test", i)} } if err := db.CreateInBatches(&clients, 100).Error; err != nil { t.Fatal(err) } request := func(scope, search string) *httptest.ResponseRecorder { req := httptest.NewRequest(http.MethodGet, "/base/panel/api/clients/userSuggestions?search="+url.QueryEscape(search)+"&pageSize=50000", nil) req.Header.Set("X-Requested-With", "XMLHttpRequest") if scope != "" { req.Header.Set("Authorization", "Bearer "+scope) } w := httptest.NewRecorder() router.ServeHTTP(w, req) return w } for _, scope := range []string{"", model.ApiScopeMonitor, model.ApiScopeNodeSync, model.ApiScopeNodeAdmin} { want := http.StatusForbidden if scope == "" { want = http.StatusUnauthorized } if got := request(scope, "fixture"); got.Code != want { t.Errorf("scope %q: status = %d, want %d", scope, got.Code, want) } } for _, tt := range []struct { search string count int first string }{ {"", 50, "fixture-00000@test"}, {"FIXTURE-", 50, "fixture-00000@test"}, {"49999@", 1, "fixture-49999@test"}, {" LITERAL_% ", 1, "literal_%@test"}, {"only-in-a-comment", 0, ""}, } { t.Run(tt.search, func(t *testing.T) { w := request(model.ApiScopeAdmin, tt.search) var body struct { Success bool `json:"success"` Msg string `json:"msg"` Obj struct { Emails []string `json:"emails"` } `json:"obj"` } decoder := json.NewDecoder(w.Body) decoder.DisallowUnknownFields() if err := decoder.Decode(&body); w.Code != http.StatusOK || err != nil || !body.Success { t.Fatalf("status = %d, response = %+v, error = %v", w.Code, body, err) } emails := body.Obj.Emails if emails == nil || len(emails) != tt.count || !slices.IsSorted(emails) || (tt.count > 0 && emails[0] != tt.first) { t.Fatalf("search %q: emails = %v, want %d starting with %q", tt.search, emails, tt.count, tt.first) } }) } }