Files
EdgeAPI/internal/rpc/services/service_admin_test.go

81 lines
1.8 KiB
Go
Raw Normal View History

2020-07-24 09:17:48 +08:00
package services
2020-07-22 22:17:53 +08:00
import (
"context"
"encoding/base64"
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/TeaOSLab/EdgeAPI/internal/encrypt"
2020-07-24 09:17:48 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/rpc/pb"
2020-07-22 22:17:53 +08:00
"github.com/iwind/TeaGo/assert"
"github.com/iwind/TeaGo/maps"
stringutil "github.com/iwind/TeaGo/utils/string"
"google.golang.org/grpc/metadata"
"testing"
"time"
)
2020-07-24 09:17:48 +08:00
func TestAdminService_Login(t *testing.T) {
2020-07-22 22:17:53 +08:00
a := assert.NewAssertion(t)
2020-07-24 09:17:48 +08:00
service := &AdminService{
2020-07-22 22:17:53 +08:00
debug: true,
}
2020-07-24 09:17:48 +08:00
resp, err := service.Login(testCtx(t), &pb.AdminLoginRequest{
2020-07-22 22:17:53 +08:00
Username: "admin",
Password: stringutil.Md5("123456"),
})
if err != nil {
t.Fatal(err)
}
a.LogJSON(resp)
}
2020-07-24 09:17:48 +08:00
func TestAdminService_CreateLog(t *testing.T) {
service := &AdminService{debug: true}
2020-07-22 22:17:53 +08:00
2020-07-24 09:17:48 +08:00
resp, err := service.CreateLog(testCtx(t), &pb.AdminCreateLogRequest{
2020-07-22 22:17:53 +08:00
Level: "info",
Description: "这是一个测试日志",
Action: "/login",
Ip: "127.0.0.1",
})
if err != nil {
t.Fatal(err)
}
t.Log(resp)
}
2020-07-24 09:17:48 +08:00
func TestAdminService_FindAllEnabledClusters(t *testing.T) {
service := &AdminService{
debug: true,
}
resp, err := service.FindAllEnabledClusters(testCtx(t), &pb.AdminFindAllEnabledClustersRequest{})
if err != nil {
t.Fatal(err)
}
t.Log(resp)
}
2020-07-22 22:17:53 +08:00
func testCtx(t *testing.T) context.Context {
ctx := context.Background()
nodeId := "H6sjDf779jimnVPnBFSgZxvr6Ca0wQ0z"
token := maps.Map{
"timestamp": time.Now().Unix(),
"adminId": 1,
}
data := token.AsJSON()
method, err := encrypt.NewMethodInstance(teaconst.EncryptMethod, "hMHjmEng0SIcT3yiA3HIoUjogwAC9cur", nodeId)
if err != nil {
t.Fatal(err)
}
data, err = method.Encrypt(data)
if err != nil {
t.Fatal(err)
}
tokenString := base64.StdEncoding.EncodeToString(data)
ctx = metadata.AppendToOutgoingContext(ctx, "nodeId", nodeId, "token", tokenString)
return ctx
}