fusenapi/server/home-user-auth/test/useraddresslistlogic_test.go

74 lines
1.7 KiB
Go
Raw Normal View History

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
func GetTestServer() *rest.Server {
flag.Parse()
2023-06-12 10:08:34 +00:00
conf.MustLoad(*testConfigFile, &cnf)
2023-06-12 10:08:34 +00:00
server := rest.MustNewServer(cnf.RestConf)
defer server.Stop()
2023-06-12 10:08:34 +00:00
ctx := svc.NewServiceContext(cnf)
handler.RegisterHandlers(server, ctx)
2023-06-12 10:08:34 +00:00
fmt.Printf("Starting server at %s:%d...\n", cnf.Host, cnf.Port)
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))
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")
}
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)
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 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())
}
}