test(singleuser): add tests for singleuser apis

This commit is contained in:
hexxa 2020-12-06 14:32:45 +08:00
parent 2bcb337b4c
commit 24adbcbe63
11 changed files with 265 additions and 82 deletions

55
src/client/singleuser.go Normal file
View file

@ -0,0 +1,55 @@
package client
import (
"fmt"
"net/http"
su "github.com/ihexxa/quickshare/src/handlers/singleuserhdr"
"github.com/parnurzeal/gorequest"
)
type SingleUserClient struct {
addr string
r *gorequest.SuperAgent
}
func NewSingleUserClient(addr string) *SingleUserClient {
gr := gorequest.New()
return &SingleUserClient{
addr: addr,
r: gr,
}
}
func (cl *SingleUserClient) url(urlpath string) string {
return fmt.Sprintf("%s%s", cl.addr, urlpath)
}
func (cl *SingleUserClient) Login(user, pwd string) (*http.Response, string, []error) {
return cl.r.Post(cl.url("/v1/users/login")).
Send(su.LoginReq{
User: user,
Pwd: pwd,
}).
End()
}
func (cl *SingleUserClient) Logout(user string, token *http.Cookie) (*http.Response, string, []error) {
return cl.r.Post(cl.url("/v1/users/logout")).
Send(su.LogoutReq{
User: user,
}).
AddCookie(token).
End()
}
func (cl *SingleUserClient) SetPwd(user, oldPwd, newPwd string, token *http.Cookie) (*http.Response, string, []error) {
return cl.r.Patch(cl.url("/v1/users/pwd")).
Send(su.SetPwdReq{
User: user,
OldPwd: oldPwd,
NewPwd: newPwd,
}).
AddCookie(token).
End()
}