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

71 lines
1.9 KiB
Go
Raw Normal View History

2023-07-05 11:00:33 +00:00
package test
2023-06-28 11:32:41 +00:00
import (
"fmt"
"fusenapi/server/home-user-auth/internal/types"
2023-07-05 11:00:33 +00:00
fstests "fusenapi/utils/fstests"
2023-06-28 11:32:41 +00:00
"testing"
"github.com/474420502/requests"
"github.com/tidwall/gjson"
)
func TestUserContactService(t *testing.T) {
var err error
var resp *requests.Response
var result gjson.Result
// 获取 session并携带 JWT token
ses := fstests.GetSessionWithUserToken(t, gserver, cnf.Host, cnf.Port)
tp := ses.Post(fmt.Sprintf("http://%s:%d/user/contact-service", cnf.Host, cnf.Port))
2023-06-29 10:04:59 +00:00
req := types.RequestContactService{Type: "order", RelationID: 481, Email: "9107058@qq.com", Name: "zhang"}
2023-06-28 11:32:41 +00:00
tp.SetBodyJson(req)
// 向服务器发送 GET 请求,获取用户基本信息
resp, err = tp.TestExecute(gserver)
if err != nil {
t.Error(err)
}
2023-06-29 10:04:59 +00:00
respjson := resp.Json()
2023-06-28 11:32:41 +00:00
// 检查返回值中的 code 字段是否存在,并且值是否为 200
2023-06-29 10:04:59 +00:00
result = respjson.Get("code")
2023-06-28 11:32:41 +00:00
if !result.Exists() {
t.Error("code is not exists")
}
if result.Int() != 200 {
t.Error("code != 200")
}
// 检查返回值中的 msg 字段是否存在,并且值是否为 "success"
2023-06-29 10:04:59 +00:00
result = respjson.Get("msg")
2023-06-28 11:32:41 +00:00
if !result.Exists() {
t.Error("msg is not exists")
}
if result.String() != "success" {
t.Error(result.String())
}
// 检查返回值中的 data 字段是否存在
2023-06-29 10:04:59 +00:00
result = respjson.Get("data")
2023-06-28 11:32:41 +00:00
if !result.Exists() {
t.Error("data is not exists")
}
2023-06-29 10:04:59 +00:00
// 检查返回值中的 msg 字段是否存在,并且值是否为 "success"
result = respjson.Get("msg")
2023-06-28 11:32:41 +00:00
if !result.Exists() {
2023-06-29 10:04:59 +00:00
t.Error("msg does not exist")
2023-06-28 11:32:41 +00:00
}
2023-06-29 10:04:59 +00:00
data := respjson.Get("data")
2023-06-28 11:32:41 +00:00
2023-06-29 10:04:59 +00:00
// 补充检查返回值中的每个字段是否存在
fieldKeys := []string{"id", "type", "relation_id", "user_id", "name", "email", "phone", "remark", "is_handle", "ctime", "handle_remark", "handle_uid", "handle_time"}
for _, key := range fieldKeys {
field := data.Get(key)
if !field.Exists() {
t.Errorf("Field '%s' does not exist", key)
}
2023-06-28 11:32:41 +00:00
}
}