2023-06-25 03:26:47 +00:00
|
|
|
package fstests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-07-06 10:23:43 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2023-06-25 03:26:47 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/474420502/requests"
|
|
|
|
)
|
|
|
|
|
2023-07-06 10:23:43 +00:00
|
|
|
func GetEtcYamlPathAuto() string {
|
|
|
|
var currentFilePath string
|
|
|
|
var ok bool
|
|
|
|
for i := 1; i < 4; i++ {
|
|
|
|
_, currentFilePath, _, ok = runtime.Caller(i)
|
|
|
|
if !ok {
|
|
|
|
panic("Error: Unable to get the current file path.")
|
|
|
|
|
|
|
|
}
|
|
|
|
dirs := strings.Split(currentFilePath, "/")
|
|
|
|
if dirs[len(dirs)-2] != "fstests" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
curdir, err := filepath.Abs(currentFilePath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
curdir = filepath.Dir(curdir)
|
|
|
|
var limitCount = 10
|
|
|
|
finfo, err := os.Stat(curdir + "/etc")
|
|
|
|
for err != nil || !finfo.IsDir() {
|
|
|
|
curdir = filepath.Dir(curdir)
|
|
|
|
finfo, err = os.Stat(curdir + "/etc")
|
|
|
|
limitCount--
|
|
|
|
if limitCount <= 0 {
|
|
|
|
panic("out limit")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lidx := strings.LastIndex(curdir, "/")
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s/etc/%s.yaml", curdir, curdir[lidx+1:])
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetCurrentServiceName() string {
|
|
|
|
_, currentFilePath, _, ok := runtime.Caller(1)
|
|
|
|
if !ok {
|
|
|
|
panic("Error: Unable to get the current file path.")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
curdir, err := filepath.Abs(currentFilePath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
curdir = filepath.Dir(curdir)
|
|
|
|
var limitCount = 10
|
|
|
|
finfo, err := os.Stat(curdir + "/etc")
|
|
|
|
for err != nil || !finfo.IsDir() {
|
|
|
|
curdir = filepath.Dir(curdir)
|
|
|
|
finfo, err = os.Stat(curdir + "/etc")
|
|
|
|
limitCount--
|
|
|
|
if limitCount <= 0 {
|
|
|
|
panic("out limit")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lidx := strings.LastIndex(curdir, "/")
|
|
|
|
// log.Println(curdir[lidx+1:])
|
|
|
|
return curdir[lidx+1:]
|
|
|
|
}
|
|
|
|
|
2023-06-25 03:26:47 +00:00
|
|
|
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()
|
2023-07-24 11:17:02 +00:00
|
|
|
tp := ses.Post(fmt.Sprintf("http://%s:%d/api/auth/login", Host, Port))
|
2023-06-25 03:26:47 +00:00
|
|
|
tp.SetBodyJson(map[string]interface{}{
|
2023-07-24 11:17:02 +00:00
|
|
|
"email": "9107058@qq.com",
|
|
|
|
"password": "t1I0hOs0/AmhZfNk9ZiNnd3YZJpI+LL6COnHAmYEJk4=",
|
2023-06-25 03:26:47 +00:00
|
|
|
})
|
|
|
|
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")
|
|
|
|
}
|
2023-07-24 11:17:02 +00:00
|
|
|
ses.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token.String()))
|
2023-06-25 03:26:47 +00:00
|
|
|
|
|
|
|
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()
|
2023-07-24 11:17:02 +00:00
|
|
|
tp := ses.Post(fmt.Sprintf("http://%s:%d/api/backend-user/login", Host, Port))
|
2023-06-28 11:32:41 +00:00
|
|
|
tp.SetBodyJson(map[string]interface{}{
|
2023-07-24 11:17:02 +00:00
|
|
|
"email": "admin@admin.com",
|
|
|
|
"pwd": "ZnVzZW5fYmFja2VuZF8yMDIz47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU=",
|
2023-06-28 11:32:41 +00:00
|
|
|
})
|
|
|
|
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")
|
|
|
|
}
|
2023-07-24 11:17:02 +00:00
|
|
|
ses.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token.String()))
|
2023-06-28 11:32:41 +00:00
|
|
|
|
|
|
|
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()
|
2023-07-24 11:17:02 +00:00
|
|
|
tp := ses.Post(fmt.Sprintf("http://%s:%d/api/auth/accept/cookie", Host, Port))
|
2023-06-25 03:26:47 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2023-07-24 11:17:02 +00:00
|
|
|
ses.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token.String()))
|
2023-06-25 03:26:47 +00:00
|
|
|
|
|
|
|
return ses
|
|
|
|
}
|