fusenapi/server/home-user-auth/test/basic.go
2023-06-20 12:15:14 +08:00

87 lines
2.0 KiB
Go

package logic
import (
"fmt"
"fusenapi/server/home-user-auth/internal/config"
"fusenapi/server/home-user-auth/internal/handler"
"fusenapi/server/home-user-auth/internal/svc"
"log"
"testing"
"github.com/474420502/requests"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
)
var testConfigFile = "../etc/home-user-auth.yaml"
var cnf config.Config
var gserver *rest.Server
func init() {
log.SetFlags(log.Llongfile)
gserver = GetTestServer()
}
func GetTestServer() *rest.Server {
conf.MustLoad(testConfigFile, &cnf)
server := rest.MustNewServer(cnf.RestConf)
defer server.Stop()
ctx := svc.NewServiceContext(cnf)
handler.RegisterHandlers(server, ctx)
fmt.Printf("Starting server at %s:%d...\n", cnf.Host, cnf.Port)
return server
}
func GetSesssion(t *testing.T, server requests.ITestServer) *requests.Session {
ses := requests.NewSession()
return ses
}
func GetSesssionWithUserToken(t *testing.T, server requests.ITestServer) *requests.Session {
ses := requests.NewSession()
tp := ses.Post(fmt.Sprintf("http://%s:%d/user/login", cnf.Host, cnf.Port))
tp.SetBodyJson(map[string]interface{}{
"name": "devenv@sina.cn",
"pwd": "$2y$13$6UFDMZQMEfqFYiNLpiUCi.B3fpvGEamPAjIgzUqv/u7jT05nB3pOC",
})
resp, err := tp.TestExecute(server)
if err != nil {
t.Error(err)
}
result := resp.Json()
code := result.Get("code").Int()
if code != 200 {
t.Error("code is not 200")
}
token := result.Get("data.token")
if !token.Exists() {
t.Error("data.token is not exists")
}
ses.Header.Add("Authorization", token.String())
return ses
}
func GetSesssionWithGuestToken(t *testing.T, server requests.ITestServer) *requests.Session {
ses := requests.NewSession()
tp := ses.Post(fmt.Sprintf("http://%s:%d/accept/cookie", cnf.Host, cnf.Port))
resp, err := tp.TestExecute(server)
if err != nil {
t.Error(err)
}
result := resp.Json()
token := result.Get("data.token")
if !token.Exists() {
t.Error("data.token is not exists")
}
ses.Header.Add("Authorization", token.String())
return ses
}