2023-06-25 03:26:47 +00:00
|
|
|
package fstests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/474420502/requests"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetSesssion() *requests.Session {
|
|
|
|
ses := requests.NewSession()
|
|
|
|
return ses
|
|
|
|
}
|
|
|
|
|
2023-06-28 11:32:41 +00:00
|
|
|
func GetSessionWithUserToken(t *testing.T, server requests.ITestServer, Host string, Port int) *requests.Session {
|
2023-06-25 03:26:47 +00:00
|
|
|
ses := requests.NewSession()
|
|
|
|
tp := ses.Post(fmt.Sprintf("http://%s:%d/user/login", Host, 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
|
|
|
|
}
|
|
|
|
|
2023-06-28 11:32:41 +00:00
|
|
|
func GetBackendSessionWithUserToken(t *testing.T, server requests.ITestServer, Host string, Port int) *requests.Session {
|
|
|
|
ses := requests.NewSession()
|
|
|
|
tp := ses.Post(fmt.Sprintf("http://%s:%d/backend-user/login", Host, Port))
|
|
|
|
tp.SetBodyJson(map[string]interface{}{
|
|
|
|
"name": "admin@admin.com",
|
|
|
|
"pwd": "ZnVzZW5fYmFja2VuZF8yMDIz47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU=",
|
|
|
|
})
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-06-25 03:26:47 +00:00
|
|
|
func GetSesssionWithGuestToken(t *testing.T, server requests.ITestServer, Host string, Port int) *requests.Session {
|
|
|
|
ses := requests.NewSession()
|
|
|
|
tp := ses.Post(fmt.Sprintf("http://%s:%d/accept/cookie", Host, 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
|
|
|
|
}
|