fix(clients): fix incorrect client naming

This commit is contained in:
hexxa 2022-08-06 10:01:28 +08:00 committed by Hexxa
parent f0293fbc4c
commit 48b0ff2df0
14 changed files with 106 additions and 41 deletions

View file

@ -11,24 +11,24 @@ import (
"github.com/parnurzeal/gorequest" "github.com/parnurzeal/gorequest"
) )
type SingleUserClient struct { type UsersClient struct {
addr string addr string
r *gorequest.SuperAgent r *gorequest.SuperAgent
} }
func NewSingleUserClient(addr string) *SingleUserClient { func NewUsersClient(addr string) *UsersClient {
gr := gorequest.New() gr := gorequest.New()
return &SingleUserClient{ return &UsersClient{
addr: addr, addr: addr,
r: gr, r: gr,
} }
} }
func (cl *SingleUserClient) url(urlpath string) string { func (cl *UsersClient) url(urlpath string) string {
return fmt.Sprintf("%s%s", cl.addr, urlpath) return fmt.Sprintf("%s%s", cl.addr, urlpath)
} }
func (cl *SingleUserClient) Login(user, pwd string) (*http.Response, string, []error) { func (cl *UsersClient) Login(user, pwd string) (*http.Response, string, []error) {
return cl.r.Post(cl.url("/v1/users/login")). return cl.r.Post(cl.url("/v1/users/login")).
Send(multiusers.LoginReq{ Send(multiusers.LoginReq{
User: user, User: user,
@ -37,13 +37,13 @@ func (cl *SingleUserClient) Login(user, pwd string) (*http.Response, string, []e
End() End()
} }
func (cl *SingleUserClient) Logout(token *http.Cookie) (*http.Response, string, []error) { func (cl *UsersClient) Logout(token *http.Cookie) (*http.Response, string, []error) {
return cl.r.Post(cl.url("/v1/users/logout")). return cl.r.Post(cl.url("/v1/users/logout")).
AddCookie(token). AddCookie(token).
End() End()
} }
func (cl *SingleUserClient) SetPwd(oldPwd, newPwd string, token *http.Cookie) (*http.Response, string, []error) { func (cl *UsersClient) SetPwd(oldPwd, newPwd string, token *http.Cookie) (*http.Response, string, []error) {
return cl.r.Patch(cl.url("/v1/users/pwd")). return cl.r.Patch(cl.url("/v1/users/pwd")).
Send(multiusers.SetPwdReq{ Send(multiusers.SetPwdReq{
OldPwd: oldPwd, OldPwd: oldPwd,
@ -53,7 +53,7 @@ func (cl *SingleUserClient) SetPwd(oldPwd, newPwd string, token *http.Cookie) (*
End() End()
} }
func (cl *SingleUserClient) ForceSetPwd(userID, newPwd string, token *http.Cookie) (*http.Response, string, []error) { func (cl *UsersClient) ForceSetPwd(userID, newPwd string, token *http.Cookie) (*http.Response, string, []error) {
return cl.r.Patch(cl.url("/v1/users/pwd/force-set")). return cl.r.Patch(cl.url("/v1/users/pwd/force-set")).
Send(multiusers.ForceSetPwdReq{ Send(multiusers.ForceSetPwdReq{
ID: userID, ID: userID,
@ -63,7 +63,7 @@ func (cl *SingleUserClient) ForceSetPwd(userID, newPwd string, token *http.Cooki
End() End()
} }
func (cl *SingleUserClient) SetUser(ID uint64, role string, quota *db.Quota, token *http.Cookie) (*http.Response, string, []error) { func (cl *UsersClient) SetUser(ID uint64, role string, quota *db.Quota, token *http.Cookie) (*http.Response, string, []error) {
return cl.r.Patch(cl.url("/v1/users/")). return cl.r.Patch(cl.url("/v1/users/")).
Send(multiusers.SetUserReq{ Send(multiusers.SetUserReq{
ID: ID, ID: ID,
@ -74,7 +74,7 @@ func (cl *SingleUserClient) SetUser(ID uint64, role string, quota *db.Quota, tok
End() End()
} }
func (cl *SingleUserClient) AddUser(name, pwd, role string, token *http.Cookie) (*http.Response, *multiusers.AddUserResp, []error) { func (cl *UsersClient) AddUser(name, pwd, role string, token *http.Cookie) (*http.Response, *multiusers.AddUserResp, []error) {
resp, body, errs := cl.r.Post(cl.url("/v1/users/")). resp, body, errs := cl.r.Post(cl.url("/v1/users/")).
AddCookie(token). AddCookie(token).
Send(multiusers.AddUserReq{ Send(multiusers.AddUserReq{
@ -97,14 +97,14 @@ func (cl *SingleUserClient) AddUser(name, pwd, role string, token *http.Cookie)
return resp, auResp, errs return resp, auResp, errs
} }
func (cl *SingleUserClient) DelUser(id string, token *http.Cookie) (*http.Response, string, []error) { func (cl *UsersClient) DelUser(id string, token *http.Cookie) (*http.Response, string, []error) {
return cl.r.Delete(cl.url("/v1/users/")). return cl.r.Delete(cl.url("/v1/users/")).
AddCookie(token). AddCookie(token).
Param(handlers.UserIDParam, id). Param(handlers.UserIDParam, id).
End() End()
} }
func (cl *SingleUserClient) ListUsers(token *http.Cookie) (*http.Response, *multiusers.ListUsersResp, []error) { func (cl *UsersClient) ListUsers(token *http.Cookie) (*http.Response, *multiusers.ListUsersResp, []error) {
resp, body, errs := cl.r.Get(cl.url("/v1/users/list")). resp, body, errs := cl.r.Get(cl.url("/v1/users/list")).
AddCookie(token). AddCookie(token).
End() End()
@ -121,7 +121,7 @@ func (cl *SingleUserClient) ListUsers(token *http.Cookie) (*http.Response, *mult
return resp, lsResp, errs return resp, lsResp, errs
} }
func (cl *SingleUserClient) AddRole(role string, token *http.Cookie) (*http.Response, string, []error) { func (cl *UsersClient) AddRole(role string, token *http.Cookie) (*http.Response, string, []error) {
return cl.r.Post(cl.url("/v1/roles/")). return cl.r.Post(cl.url("/v1/roles/")).
AddCookie(token). AddCookie(token).
Send(multiusers.AddRoleReq{ Send(multiusers.AddRoleReq{
@ -130,7 +130,7 @@ func (cl *SingleUserClient) AddRole(role string, token *http.Cookie) (*http.Resp
End() End()
} }
func (cl *SingleUserClient) DelRole(role string, token *http.Cookie) (*http.Response, string, []error) { func (cl *UsersClient) DelRole(role string, token *http.Cookie) (*http.Response, string, []error) {
return cl.r.Delete(cl.url("/v1/roles/")). return cl.r.Delete(cl.url("/v1/roles/")).
AddCookie(token). AddCookie(token).
Send(multiusers.DelRoleReq{ Send(multiusers.DelRoleReq{
@ -139,7 +139,7 @@ func (cl *SingleUserClient) DelRole(role string, token *http.Cookie) (*http.Resp
End() End()
} }
func (cl *SingleUserClient) ListRoles(token *http.Cookie) (*http.Response, *multiusers.ListRolesResp, []error) { func (cl *UsersClient) ListRoles(token *http.Cookie) (*http.Response, *multiusers.ListRolesResp, []error) {
resp, body, errs := cl.r.Get(cl.url("/v1/roles/list")). resp, body, errs := cl.r.Get(cl.url("/v1/roles/list")).
AddCookie(token). AddCookie(token).
End() End()
@ -156,7 +156,7 @@ func (cl *SingleUserClient) ListRoles(token *http.Cookie) (*http.Response, *mult
return resp, lsResp, errs return resp, lsResp, errs
} }
func (cl *SingleUserClient) Self(token *http.Cookie) (*http.Response, *multiusers.SelfResp, []error) { func (cl *UsersClient) Self(token *http.Cookie) (*http.Response, *multiusers.SelfResp, []error) {
resp, body, errs := cl.r.Get(cl.url("/v1/users/self")). resp, body, errs := cl.r.Get(cl.url("/v1/users/self")).
AddCookie(token). AddCookie(token).
End() End()
@ -173,7 +173,7 @@ func (cl *SingleUserClient) Self(token *http.Cookie) (*http.Response, *multiuser
return resp, selfResp, errs return resp, selfResp, errs
} }
func (cl *SingleUserClient) SetPreferences(prefers *db.Preferences, token *http.Cookie) (*http.Response, string, []error) { func (cl *UsersClient) SetPreferences(prefers *db.Preferences, token *http.Cookie) (*http.Response, string, []error) {
return cl.r.Patch(cl.url("/v1/users/preferences")). return cl.r.Patch(cl.url("/v1/users/preferences")).
Send(multiusers.SetPreferencesReq{ Send(multiusers.SetPreferencesReq{
Preferences: prefers, Preferences: prefers,
@ -182,13 +182,13 @@ func (cl *SingleUserClient) SetPreferences(prefers *db.Preferences, token *http.
End() End()
} }
func (cl *SingleUserClient) IsAuthed(token *http.Cookie) (*http.Response, string, []error) { func (cl *UsersClient) IsAuthed(token *http.Cookie) (*http.Response, string, []error) {
return cl.r.Get(cl.url("/v1/users/isauthed")). return cl.r.Get(cl.url("/v1/users/isauthed")).
AddCookie(token). AddCookie(token).
End() End()
} }
func (cl *SingleUserClient) ResetUsedSpace(userID uint64, token *http.Cookie) (*http.Response, string, []error) { func (cl *UsersClient) ResetUsedSpace(userID uint64, token *http.Cookie) (*http.Response, string, []error) {
return cl.r.Put(cl.url("/v1/users/used-space")). return cl.r.Put(cl.url("/v1/users/used-space")).
Send(multiusers.ResetUsedSpaceReq{ Send(multiusers.ResetUsedSpaceReq{
UserID: userID, UserID: userID,

View file

@ -164,6 +164,7 @@ export interface IFilesClient {
generateHash: (filePath: string) => Promise<Response>; generateHash: (filePath: string) => Promise<Response>;
download: (url: string) => Promise<Response>; download: (url: string) => Promise<Response>;
search: (keywords: string[]) => Promise<Response<SearchItemsResp>>; search: (keywords: string[]) => Promise<Response<SearchItemsResp>>;
reindex: () => Promise<Response>;
} }
export interface ISettingsClient { export interface ISettingsClient {

View file

@ -268,6 +268,20 @@ export class UserForm extends React.Component<
<div className="hr"></div> <div className="hr"></div>
<div>
<Flexbox
children={List([
<span>{this.props.msg.pkg.get("action.reindex.desc")}</span>,
<button className="button-default" onClick={resetUsedSpace}>
{this.props.msg.pkg.get("action.reindex")}
</button>,
])}
childrenStyles={List([{}, { justifyContent: "flex-end" }])}
/>
</div>
<div className="hr"></div>
<Flexbox <Flexbox
className="margin-t-m" className="margin-t-m"
children={List([ children={List([
@ -491,6 +505,10 @@ export class AdminPane extends React.Component<Props, State, {}> {
} }
}; };
reindex = async () => {
return updater().reindex();
}
addUser = async () => { addUser = async () => {
if (this.state.newUserPwd1 !== this.state.newUserPwd2) { if (this.state.newUserPwd1 !== this.state.newUserPwd2) {
Env().alertMsg(this.props.msg.pkg.get("settings.pwd.notSame")); Env().alertMsg(this.props.msg.pkg.get("settings.pwd.notSame"));

View file

@ -1034,6 +1034,11 @@ export class Updater {
return ""; return "";
}; };
reindex = async (): Promise<string> => {
const resp = await this.filesClient.reindex();
return resp.status === 200 ? "" : errServer;
}
hasResult = (): boolean => { hasResult = (): boolean => {
return this.props.filesInfo.searchResults.size > 0; return this.props.filesInfo.searchResults.size > 0;
}; };

View file

@ -158,5 +158,7 @@ export const msgs: Map<string, string> = Map({
"term.results": "Results", "term.results": "Results",
"term.noResult": "No result found", "term.noResult": "No result found",
"action.go": "Go", "action.go": "Go",
"hint.keywords": "Please input keyword(s), separated by spaces" "hint.keywords": "Please input keyword(s), separated by spaces",
"action.reindex": "Reindex",
"action.reindex.desc": "Reconstruct the searching index",
}); });

View file

@ -155,5 +155,7 @@ export const msgs: Map<string, string> = Map({
"term.results": "结果", "term.results": "结果",
"term.noResult": "未找到结果", "term.noResult": "未找到结果",
"action.go": "前往", "action.go": "前往",
"hint.keywords": "请输入关键字,以空格分隔" "hint.keywords": "请输入关键字,以空格分隔",
"action.reindex": "重新索引",
"action.reindex.desc": "重新建立搜索索引",
}); });

View file

@ -48,7 +48,7 @@ func TestConcurrency(t *testing.T) {
t.Fatal("fail to start server") t.Fatal("fail to start server")
} }
usersCl := client.NewSingleUserClient(addr) usersCl := client.NewUsersClient(addr)
resp, _, errs := usersCl.Login(adminName, adminPwd) resp, _, errs := usersCl.Login(adminName, adminPwd)
if len(errs) > 0 { if len(errs) > 0 {
t.Fatal(errs) t.Fatal(errs)
@ -67,7 +67,7 @@ func TestConcurrency(t *testing.T) {
filesCount := 10 filesCount := 10
mockClient := func(name, pwd string, wg *sync.WaitGroup) { mockClient := func(name, pwd string, wg *sync.WaitGroup) {
usersCl := client.NewSingleUserClient(addr) usersCl := client.NewUsersClient(addr)
resp, _, errs := usersCl.Login(name, pwd) resp, _, errs := usersCl.Login(name, pwd)
if len(errs) > 0 { if len(errs) > 0 {
t.Fatal(errs) t.Fatal(errs)

View file

@ -62,7 +62,7 @@ func TestFileHandlers(t *testing.T) {
t.Fatal("fail to start server") t.Fatal("fail to start server")
} }
usersCl := client.NewSingleUserClient(addr) usersCl := client.NewUsersClient(addr)
resp, _, errs := usersCl.Login(adminName, adminPwd) resp, _, errs := usersCl.Login(adminName, adminPwd)
if len(errs) > 0 { if len(errs) > 0 {
t.Fatal(errs) t.Fatal(errs)
@ -405,7 +405,7 @@ func TestFileHandlers(t *testing.T) {
} }
} }
userUsersCl := client.NewSingleUserClient(addr) userUsersCl := client.NewUsersClient(addr)
resp, _, errs := userUsersCl.Login("demo", "Quicksh@re") resp, _, errs := userUsersCl.Login("demo", "Quicksh@re")
if len(errs) > 0 { if len(errs) > 0 {
t.Fatal(errs) t.Fatal(errs)
@ -918,6 +918,19 @@ func TestFileHandlers(t *testing.T) {
t.Fatal("search result not match") t.Fatal("search result not match")
} }
userFilesClient, err := loginFilesClient(addr, "demo", "Quicksh@re")
if err != nil {
t.Fatal(err)
}
resp, searchItemsResp, errs = userFilesClient.SearchItems(keywords)
if len(errs) > 0 {
t.Fatal(errs)
} else if resp.StatusCode != 200 {
t.Fatal(resp.StatusCode)
} else if len(searchItemsResp.Results) > 0 {
t.Fatal("shoud return empty results")
}
// delete paths // delete paths
for pathname := range toDelete { for pathname := range toDelete {
resp, _, errs := cl.Delete(pathname) resp, _, errs := cl.Delete(pathname)

View file

@ -114,7 +114,7 @@ func TestPermissions(t *testing.T) {
tmpAdmin, tmpAdminPwd := "tmpAdmin", "1234" tmpAdmin, tmpAdminPwd := "tmpAdmin", "1234"
tmpNewRole := "tmpNewRole" tmpNewRole := "tmpNewRole"
cl := client.NewSingleUserClient(addr) cl := client.NewUsersClient(addr)
token := &http.Cookie{} token := &http.Cookie{}
if requireAuth { if requireAuth {
resp, _, errs := cl.Login(user, pwd) resp, _, errs := cl.Login(user, pwd)
@ -290,7 +290,7 @@ func TestPermissions(t *testing.T) {
// Mkdir // Mkdir
// Move // Move
cl := client.NewSingleUserClient(addr) cl := client.NewUsersClient(addr)
token := &http.Cookie{} token := &http.Cookie{}
if requireAuth { if requireAuth {
@ -381,7 +381,7 @@ func TestPermissions(t *testing.T) {
}) })
uploadSample := func() { uploadSample := func() {
cl := client.NewSingleUserClient(addr) cl := client.NewUsersClient(addr)
token := &http.Cookie{} token := &http.Cookie{}
resp, _, errs := cl.Login("user2", "1234") resp, _, errs := cl.Login("user2", "1234")
@ -411,7 +411,7 @@ func TestPermissions(t *testing.T) {
// GenerateHash // GenerateHash
// Search // Search
cl := client.NewSingleUserClient(addr) cl := client.NewUsersClient(addr)
token := &http.Cookie{} token := &http.Cookie{}
if requireAuth { if requireAuth {
resp, _, errs := cl.Login(user, pwd) resp, _, errs := cl.Login(user, pwd)
@ -575,7 +575,7 @@ func TestPermissions(t *testing.T) {
// sharing permission tests // sharing permission tests
enableSharing := func() { enableSharing := func() {
cl := client.NewSingleUserClient(addr) cl := client.NewUsersClient(addr)
token := &http.Cookie{} token := &http.Cookie{}
resp, _, errs := cl.Login("share", "1234") resp, _, errs := cl.Login("share", "1234")
@ -631,7 +631,7 @@ func TestPermissions(t *testing.T) {
// ListSharingIDs // ListSharingIDs
// GetSharingDir // GetSharingDir
cl := client.NewSingleUserClient(addr) cl := client.NewUsersClient(addr)
token := &http.Cookie{} token := &http.Cookie{}
homePath := "/" homePath := "/"
desc := user desc := user
@ -727,7 +727,7 @@ func TestPermissions(t *testing.T) {
// SetClientCfg // SetClientCfg
// ReportErrors // ReportErrors
cl := client.NewSingleUserClient(addr) cl := client.NewUsersClient(addr)
token := &http.Cookie{} token := &http.Cookie{}
desc := user desc := user
errReports := &settings.ClientErrorReports{ errReports := &settings.ClientErrorReports{

View file

@ -62,7 +62,7 @@ func TestSettingsHandlers(t *testing.T) {
t.Fatal("fail to start server") t.Fatal("fail to start server")
} }
usersCl := client.NewSingleUserClient(addr) usersCl := client.NewUsersClient(addr)
resp, _, errs := usersCl.Login(adminName, adminPwd) resp, _, errs := usersCl.Login(adminName, adminPwd)
if len(errs) > 0 { if len(errs) > 0 {
t.Fatal(errs) t.Fatal(errs)

View file

@ -63,7 +63,7 @@ func TestSpaceLimit(t *testing.T) {
t.Fatal("fail to start server") t.Fatal("fail to start server")
} }
usersCl := client.NewSingleUserClient(addr) usersCl := client.NewUsersClient(addr)
resp, _, errs := usersCl.Login(adminName, adminPwd) resp, _, errs := usersCl.Login(adminName, adminPwd)
if len(errs) > 0 { if len(errs) > 0 {
t.Fatal(errs) t.Fatal(errs)
@ -100,7 +100,7 @@ func TestSpaceLimit(t *testing.T) {
} }
t.Run("test space limiting: Upload", func(t *testing.T) { t.Run("test space limiting: Upload", func(t *testing.T) {
usersCl := client.NewSingleUserClient(addr) usersCl := client.NewUsersClient(addr)
resp, _, errs := usersCl.Login(getUserName(0), userPwd) resp, _, errs := usersCl.Login(getUserName(0), userPwd)
if len(errs) > 0 { if len(errs) > 0 {
t.Fatal(errs) t.Fatal(errs)
@ -230,7 +230,7 @@ func TestSpaceLimit(t *testing.T) {
}) })
t.Run("ResetUsedSpace", func(t *testing.T) { t.Run("ResetUsedSpace", func(t *testing.T) {
usersCl := client.NewSingleUserClient(addr) usersCl := client.NewUsersClient(addr)
resp, _, errs := usersCl.Login("test", "test") resp, _, errs := usersCl.Login("test", "test")
if len(errs) > 0 { if len(errs) > 0 {
t.Fatal(errs) t.Fatal(errs)

View file

@ -72,7 +72,7 @@ func TestUsersHandlers(t *testing.T) {
defer srv.Shutdown() defer srv.Shutdown()
fs := srv.depsFS() fs := srv.depsFS()
usersCl := client.NewSingleUserClient(addr) usersCl := client.NewUsersClient(addr)
settingsCl := client.NewSettingsClient(addr) settingsCl := client.NewSettingsClient(addr)
if !isServerReady(addr) { if !isServerReady(addr) {
@ -82,7 +82,7 @@ func TestUsersHandlers(t *testing.T) {
var err error var err error
t.Run("test inited users", func(t *testing.T) { t.Run("test inited users", func(t *testing.T) {
usersCl := client.NewSingleUserClient(addr) usersCl := client.NewUsersClient(addr)
resp, _, errs := usersCl.Login(adminName, adminPwd) resp, _, errs := usersCl.Login(adminName, adminPwd)
if len(errs) > 0 { if len(errs) > 0 {
t.Fatal(errs) t.Fatal(errs)
@ -164,7 +164,7 @@ func TestUsersHandlers(t *testing.T) {
} }
for _, user := range users { for _, user := range users {
usersCl := client.NewSingleUserClient(addr) usersCl := client.NewUsersClient(addr)
resp, _, errs := usersCl.Login(user.Name, user.Pwd) resp, _, errs := usersCl.Login(user.Name, user.Pwd)
if len(errs) > 0 { if len(errs) > 0 {
t.Fatal(errs) t.Fatal(errs)
@ -451,7 +451,7 @@ func TestUsersHandlers(t *testing.T) {
}) })
t.Run("Login, SetPreferences, Self, Logout", func(t *testing.T) { t.Run("Login, SetPreferences, Self, Logout", func(t *testing.T) {
usersCl := client.NewSingleUserClient(addr) usersCl := client.NewUsersClient(addr)
resp, _, errs := usersCl.Login(adminName, adminNewPwd) resp, _, errs := usersCl.Login(adminName, adminNewPwd)
if len(errs) > 0 { if len(errs) > 0 {
t.Fatal(errs) t.Fatal(errs)

View file

@ -2,6 +2,7 @@ package server
import ( import (
"encoding/base64" "encoding/base64"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
@ -16,6 +17,7 @@ import (
"github.com/ihexxa/quickshare/src/client" "github.com/ihexxa/quickshare/src/client"
"github.com/ihexxa/quickshare/src/db" "github.com/ihexxa/quickshare/src/db"
fspkg "github.com/ihexxa/quickshare/src/fs" fspkg "github.com/ihexxa/quickshare/src/fs"
q "github.com/ihexxa/quickshare/src/handlers"
) )
func startTestServer(config string) *Server { func startTestServer(config string) *Server {
@ -57,7 +59,7 @@ func getUserName(id int) string {
} }
func addUsers(t *testing.T, addr, userPwd string, userCount int, adminToken *http.Cookie) map[string]string { func addUsers(t *testing.T, addr, userPwd string, userCount int, adminToken *http.Cookie) map[string]string {
usersCl := client.NewSingleUserClient(addr) usersCl := client.NewUsersClient(addr)
users := map[string]string{} users := map[string]string{}
for i := range make([]int, userCount) { for i := range make([]int, userCount) {
userName := getUserName(i) userName := getUserName(i)
@ -220,3 +222,25 @@ func assertResp(t *testing.T, resp *http.Response, errs []error, expectedCode in
t.Fatal(desc, resp.StatusCode, expectedCode) t.Fatal(desc, resp.StatusCode, expectedCode)
} }
} }
func joinErrs(errs []error) error {
msgs := []string{}
for _, err := range errs {
msgs = append(msgs, err.Error())
}
return errors.New(strings.Join(msgs, ","))
}
func loginFilesClient(addr, user, pwd string) (*client.FilesClient, error) {
usersCl := client.NewUsersClient(addr)
resp, _, errs := usersCl.Login(user, pwd)
if len(errs) > 0 {
return nil, joinErrs(errs)
} else if resp.StatusCode != 200 {
return nil, fmt.Errorf("unexpected code(%d)", resp.StatusCode)
}
token := client.GetCookie(resp.Cookies(), q.TokenCookie)
return client.NewFilesClient(addr, token), nil
}

Binary file not shown.