mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git), so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form used by the handful of files that had ours. Shape is now: // Copyright 2026 Hanzo AI, Inc. All rights reserved. // Copyright 2015 The Gogs Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT Ours on top, theirs retained verbatim. Retained because MIT requires it, and separately because they are true — those authors did write that code. Not renamed to "The Git Authors": Git is a different project under a different license, and putting its name here would attribute this code to people who have nothing to do with it. Adding our line is the fix; overwriting theirs would have been both a licence violation and a false statement. gofmt still reports import-order drift in some of these files. It predates this change (verified against a stash) and is left alone rather than burying a one-line-per-file legal change under 3000 files of unrelated reordering. Build clean. Co-authored-by: Hanzo Dev <[email protected]>
155 lines
4.9 KiB
Go
155 lines
4.9 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package integration
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
auth_model "github.com/hanzoai/git/models/auth"
|
|
"github.com/hanzoai/git/models/unittest"
|
|
user_model "github.com/hanzoai/git/models/user"
|
|
api "github.com/hanzoai/git/modules/structs"
|
|
"github.com/hanzoai/git/tests"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type SearchResults struct {
|
|
OK bool `json:"ok"`
|
|
Data []*api.User `json:"data"`
|
|
}
|
|
|
|
func TestAPIUserSearchLoggedIn(t *testing.T) {
|
|
defer tests.PrepareTestEnv(t)()
|
|
adminUsername := "user1"
|
|
session := loginUser(t, adminUsername)
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser)
|
|
query := "user2"
|
|
req := NewRequestf(t, "GET", "/v1/users/search?q=%s", query).
|
|
AddTokenAuth(token)
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
results := DecodeJSON(t, resp, &SearchResults{})
|
|
assert.NotEmpty(t, results.Data)
|
|
for _, user := range results.Data {
|
|
assert.Contains(t, user.UserName, query)
|
|
assert.NotEmpty(t, user.Email)
|
|
}
|
|
|
|
publicToken := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser, auth_model.AccessTokenScopePublicOnly)
|
|
req = NewRequestf(t, "GET", "/v1/users/search?q=%s", query).
|
|
AddTokenAuth(publicToken)
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
|
results = DecodeJSON(t, resp, &SearchResults{})
|
|
assert.NotEmpty(t, results.Data)
|
|
for _, user := range results.Data {
|
|
assert.Contains(t, user.UserName, query)
|
|
assert.NotEmpty(t, user.Email)
|
|
assert.Equal(t, api.UserVisibilityPublic, user.Visibility)
|
|
}
|
|
}
|
|
|
|
func TestAPIUserSearchNotLoggedIn(t *testing.T) {
|
|
defer tests.PrepareTestEnv(t)()
|
|
query := "user2"
|
|
req := NewRequestf(t, "GET", "/v1/users/search?q=%s", query)
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
results := DecodeJSON(t, resp, &SearchResults{})
|
|
assert.NotEmpty(t, results.Data)
|
|
var modelUser *user_model.User
|
|
for _, user := range results.Data {
|
|
assert.Contains(t, user.UserName, query)
|
|
modelUser = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: user.ID})
|
|
assert.Equal(t, modelUser.GetPlaceholderEmail(), user.Email)
|
|
}
|
|
}
|
|
|
|
func TestAPIUserSearchSystemUsers(t *testing.T) {
|
|
defer tests.PrepareTestEnv(t)()
|
|
for _, systemUser := range []*user_model.User{
|
|
user_model.NewGhostUser(),
|
|
user_model.NewActionsUser(),
|
|
} {
|
|
t.Run(systemUser.Name, func(t *testing.T) {
|
|
req := NewRequestf(t, "GET", "/v1/users/search?uid=%d", systemUser.ID)
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
results := DecodeJSON(t, resp, &SearchResults{})
|
|
assert.NotEmpty(t, results.Data)
|
|
if assert.Len(t, results.Data, 1) {
|
|
user := results.Data[0]
|
|
assert.Equal(t, user.UserName, systemUser.Name)
|
|
assert.Equal(t, user.ID, systemUser.ID)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAPIUserSearchAdminLoggedInUserHidden(t *testing.T) {
|
|
defer tests.PrepareTestEnv(t)()
|
|
adminUsername := "user1"
|
|
session := loginUser(t, adminUsername)
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser)
|
|
query := "user31"
|
|
req := NewRequestf(t, "GET", "/v1/users/search?q=%s", query).
|
|
AddTokenAuth(token)
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
results := DecodeJSON(t, resp, &SearchResults{})
|
|
assert.NotEmpty(t, results.Data)
|
|
for _, user := range results.Data {
|
|
assert.Contains(t, user.UserName, query)
|
|
assert.NotEmpty(t, user.Email)
|
|
assert.Equal(t, api.UserVisibilityPrivate, user.Visibility)
|
|
}
|
|
}
|
|
|
|
func TestAPIUserSearchNotLoggedInUserHidden(t *testing.T) {
|
|
defer tests.PrepareTestEnv(t)()
|
|
query := "user31"
|
|
req := NewRequestf(t, "GET", "/v1/users/search?q=%s", query)
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
results := DecodeJSON(t, resp, &SearchResults{})
|
|
assert.Empty(t, results.Data)
|
|
}
|
|
|
|
func TestAPIUserSearchByEmail(t *testing.T) {
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
// admin can search user with private email
|
|
adminUsername := "user1"
|
|
session := loginUser(t, adminUsername)
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser)
|
|
query := "[email protected]"
|
|
req := NewRequestf(t, "GET", "/v1/users/search?q=%s", query).
|
|
AddTokenAuth(token)
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
results := DecodeJSON(t, resp, &SearchResults{})
|
|
assert.Len(t, results.Data, 1)
|
|
assert.Equal(t, query, results.Data[0].Email)
|
|
|
|
// no login user can not search user with private email
|
|
req = NewRequestf(t, "GET", "/v1/users/search?q=%s", query)
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
|
results = DecodeJSON(t, resp, &SearchResults{})
|
|
assert.Empty(t, results.Data)
|
|
|
|
// user can search self with private email
|
|
user2 := "user2"
|
|
session = loginUser(t, user2)
|
|
token = getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser)
|
|
req = NewRequestf(t, "GET", "/v1/users/search?q=%s", query).
|
|
AddTokenAuth(token)
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
|
|
|
results = DecodeJSON(t, resp, &SearchResults{})
|
|
assert.Len(t, results.Data, 1)
|
|
assert.Equal(t, query, results.Data[0].Email)
|
|
}
|