2023-06-12 09:10:27 +00:00
|
|
|
package server_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"fusenapi/server/home-user-auth/internal/config"
|
|
|
|
"fusenapi/server/home-user-auth/internal/handler"
|
|
|
|
"fusenapi/server/home-user-auth/internal/svc"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/474420502/requests"
|
|
|
|
"github.com/zeromicro/go-zero/core/conf"
|
|
|
|
"github.com/zeromicro/go-zero/rest"
|
|
|
|
)
|
|
|
|
|
2023-06-12 10:08:34 +00:00
|
|
|
var testConfigFile = flag.String("f", "../etc/home-user-auth.yaml", "the config file")
|
|
|
|
var cnf config.Config
|
2023-06-12 09:10:27 +00:00
|
|
|
|
|
|
|
func GetTestServer() *rest.Server {
|
|
|
|
flag.Parse()
|
|
|
|
|
2023-06-12 10:08:34 +00:00
|
|
|
conf.MustLoad(*testConfigFile, &cnf)
|
2023-06-12 09:10:27 +00:00
|
|
|
|
2023-06-12 10:08:34 +00:00
|
|
|
server := rest.MustNewServer(cnf.RestConf)
|
2023-06-12 09:10:27 +00:00
|
|
|
defer server.Stop()
|
|
|
|
|
2023-06-12 10:08:34 +00:00
|
|
|
ctx := svc.NewServiceContext(cnf)
|
2023-06-12 09:10:27 +00:00
|
|
|
handler.RegisterHandlers(server, ctx)
|
|
|
|
|
2023-06-12 10:08:34 +00:00
|
|
|
fmt.Printf("Starting server at %s:%d...\n", cnf.Host, cnf.Port)
|
2023-06-12 09:10:27 +00:00
|
|
|
return server
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCaseAddressList(t *testing.T) {
|
|
|
|
server := GetTestServer()
|
|
|
|
|
|
|
|
ses := requests.NewSession()
|
2023-06-12 10:08:34 +00:00
|
|
|
tp := ses.Post(fmt.Sprintf("http://%s:%d/user/login", cnf.Host, cnf.Port))
|
2023-06-12 09:10:27 +00:00
|
|
|
tp.SetBodyJson(map[string]interface{}{
|
|
|
|
"name": "devenv@sina.cn",
|
|
|
|
"pwd": "$2y$13$6UFDMZQMEfqFYiNLpiUCi.B3fpvGEamPAjIgzUqv/u7jT05nB3pOC",
|
|
|
|
})
|
|
|
|
resp, err := tp.TestInServer(server)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
result := resp.Json()
|
|
|
|
token := result.Get("data.token")
|
2023-06-12 10:08:34 +00:00
|
|
|
if !token.Exists() {
|
|
|
|
t.Error("data.token is not exists")
|
|
|
|
}
|
2023-06-12 09:10:27 +00:00
|
|
|
ses.Header.Add("Authorization", token.String())
|
|
|
|
|
2023-06-12 10:08:34 +00:00
|
|
|
resp, err = ses.Get(fmt.Sprintf("http://%s:%d/user/address-list", cnf.Host, cnf.Port)).TestInServer(server)
|
2023-06-12 09:10:27 +00:00
|
|
|
if err != nil {
|
2023-06-12 10:08:34 +00:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
result = resp.Json().Get("code")
|
|
|
|
if !result.Exists() {
|
|
|
|
t.Error("code is not exists")
|
|
|
|
}
|
|
|
|
if result.Int() != 200 {
|
|
|
|
t.Error("code != 200")
|
2023-06-12 09:10:27 +00:00
|
|
|
}
|
|
|
|
|
2023-06-12 10:08:34 +00:00
|
|
|
result = resp.Json().Get("msg")
|
|
|
|
if !result.Exists() {
|
|
|
|
t.Error("msg is not exists")
|
|
|
|
}
|
|
|
|
if result.String() != "success" {
|
|
|
|
t.Error(result.String())
|
|
|
|
}
|
2023-06-12 09:10:27 +00:00
|
|
|
}
|