128 lines
2.9 KiB
Go
128 lines
2.9 KiB
Go
package logic
|
|
|
|
import (
|
|
"fmt"
|
|
"fusenapi/server/home-user-auth/internal/types"
|
|
"testing"
|
|
|
|
"github.com/474420502/requests"
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
func TestCaseUserAddAddress(t *testing.T) {
|
|
var err error
|
|
var resp *requests.Response
|
|
var result gjson.Result
|
|
|
|
// 获取 session,并携带 JWT token
|
|
ses := GetSesssionWithUserToken(t, gserver)
|
|
|
|
// 构建新增地址请求体
|
|
addrReq := types.RequestAddAddress{
|
|
Name: "John Doe",
|
|
FirstName: "John",
|
|
LastName: "Doe",
|
|
Mobile: "123-456-7890",
|
|
Street: "Main St",
|
|
Suite: "Apt 123",
|
|
City: "New York",
|
|
State: "NY",
|
|
ZipCode: "10001",
|
|
}
|
|
|
|
// 向服务器发送 POST 请求,新增用户地址
|
|
tp := ses.Post(fmt.Sprintf("http://%s:%d/user/add-address", cnf.Host, cnf.Port))
|
|
tp.SetBodyJson(addrReq)
|
|
resp, err = tp.TestExecute(gserver)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
// 检查返回值中的 code 字段是否存在,并且值是否为 200
|
|
result = resp.Json().Get("code")
|
|
if !result.Exists() {
|
|
t.Error("code is not exists")
|
|
}
|
|
if result.Int() != 200 {
|
|
t.Error("code != 200")
|
|
}
|
|
|
|
// 检查返回值中的 msg 字段是否存在,并且值是否为 "success"
|
|
result = resp.Json().Get("msg")
|
|
if !result.Exists() {
|
|
t.Error("msg is not exists")
|
|
}
|
|
if result.String() != "success" {
|
|
t.Error(result.String())
|
|
}
|
|
|
|
// 检查返回值中的 data 字段是否存在,并且包含新增地址的 id
|
|
result = resp.Json().Get("data")
|
|
if !result.Exists() {
|
|
t.Error("data is not exists")
|
|
}
|
|
if !result.Get("id").Exists() {
|
|
t.Error("data.id is not exists")
|
|
}
|
|
|
|
addressId := result.Get("id").Int()
|
|
|
|
// 测试修改
|
|
|
|
addrReq = types.RequestAddAddress{
|
|
Id: addressId, // 获取新增的id
|
|
Name: "Jane Smith",
|
|
FirstName: "Jane",
|
|
LastName: "Smith",
|
|
Mobile: "987-654-3210",
|
|
Street: "First Ave",
|
|
Suite: "Unit 456",
|
|
City: "San Francisco",
|
|
State: "CA",
|
|
ZipCode: "94122",
|
|
}
|
|
|
|
// 向服务器发送 POST 请求,新增用户地址
|
|
tp = ses.Post(fmt.Sprintf("http://%s:%d/user/add-address", cnf.Host, cnf.Port))
|
|
tp.SetBodyJson(addrReq)
|
|
resp, err = tp.TestExecute(gserver)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
// 检查返回值中的 code 字段是否存在,并且值是否为 200
|
|
result = resp.Json().Get("code")
|
|
if !result.Exists() {
|
|
t.Error("code is not exists")
|
|
}
|
|
if result.Int() != 200 {
|
|
t.Error("code != 200")
|
|
}
|
|
|
|
// 检查返回值中的 msg 字段是否存在,并且值是否为 "success"
|
|
result = resp.Json().Get("msg")
|
|
if !result.Exists() {
|
|
t.Error("msg is not exists")
|
|
}
|
|
if result.String() != "success" {
|
|
t.Error(result.String())
|
|
}
|
|
|
|
// log.Println(resp.Json())
|
|
// 检查返回值中的 data 字段是否存在,并且包含新增地址的 id
|
|
result = resp.Json().Get("data")
|
|
if !result.Exists() {
|
|
t.Error("data is not exists")
|
|
}
|
|
if !result.Get("id").Exists() {
|
|
t.Error("data.id is not exists")
|
|
}
|
|
|
|
if result.Get("id").Int() != addressId {
|
|
t.Error("addressId is error")
|
|
}
|
|
|
|
}
|