Merge pull request #1031 from trheyi/main
Deprecate studio API functions and comment out test cases
This commit is contained in:
commit
2e630c5b03
2 changed files with 194 additions and 190 deletions
|
|
@ -29,6 +29,7 @@ type cfunc struct {
|
||||||
|
|
||||||
// Start start the studio api server
|
// Start start the studio api server
|
||||||
func Start(cfg config.Config) (err error) {
|
func Start(cfg config.Config) (err error) {
|
||||||
|
return fmt.Errorf("studio is deprecated")
|
||||||
|
|
||||||
// recive interrupt signal
|
// recive interrupt signal
|
||||||
interrupt := make(chan os.Signal, 1)
|
interrupt := make(chan os.Signal, 1)
|
||||||
|
|
@ -92,6 +93,7 @@ func Stop() {
|
||||||
|
|
||||||
// Load studio config
|
// Load studio config
|
||||||
func Load(cfg config.Config) error {
|
func Load(cfg config.Config) error {
|
||||||
|
return fmt.Errorf("studio is deprecated")
|
||||||
|
|
||||||
err := loadDSL(cfg)
|
err := loadDSL(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -101,12 +103,14 @@ func Load(cfg config.Config) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadDSL(cfg config.Config) error {
|
func loadDSL(cfg config.Config) error {
|
||||||
|
return fmt.Errorf("studio is deprecated")
|
||||||
dslDenyList := []string{cfg.DataRoot}
|
dslDenyList := []string{cfg.DataRoot}
|
||||||
dfs = dsl.New(cfg.AppSource).DenyAbs(dslDenyList...)
|
dfs = dsl.New(cfg.AppSource).DenyAbs(dslDenyList...)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadScripts() error {
|
func loadScripts() error {
|
||||||
|
return fmt.Errorf("studio is deprecated")
|
||||||
exts := []string{"*.js"}
|
exts := []string{"*.js"}
|
||||||
return application.App.Walk("studio", func(root, file string, isdir bool) error {
|
return application.App.Walk("studio", func(root, file string, isdir bool) error {
|
||||||
if isdir {
|
if isdir {
|
||||||
|
|
|
||||||
|
|
@ -1,241 +1,241 @@
|
||||||
package studio
|
package studio
|
||||||
|
|
||||||
import (
|
// import (
|
||||||
"bytes"
|
// "bytes"
|
||||||
"fmt"
|
// "fmt"
|
||||||
"io"
|
// "io"
|
||||||
"net/http"
|
// "net/http"
|
||||||
"testing"
|
// "testing"
|
||||||
"time"
|
// "time"
|
||||||
|
|
||||||
jsoniter "github.com/json-iterator/go"
|
// jsoniter "github.com/json-iterator/go"
|
||||||
"github.com/stretchr/testify/assert"
|
// "github.com/stretchr/testify/assert"
|
||||||
"github.com/yaoapp/yao/config"
|
// "github.com/yaoapp/yao/config"
|
||||||
"github.com/yaoapp/yao/helper"
|
// "github.com/yaoapp/yao/helper"
|
||||||
"github.com/yaoapp/yao/test"
|
// "github.com/yaoapp/yao/test"
|
||||||
)
|
// )
|
||||||
|
|
||||||
type kv map[string]interface{}
|
// type kv map[string]interface{}
|
||||||
type arr []interface{}
|
// type arr []interface{}
|
||||||
|
|
||||||
func TestLoad(t *testing.T) {
|
// func TestLoad(t *testing.T) {
|
||||||
|
|
||||||
test.Prepare(t, config.Conf)
|
// test.Prepare(t, config.Conf)
|
||||||
defer test.Clean()
|
// defer test.Clean()
|
||||||
|
|
||||||
err := Load(config.Conf)
|
// err := Load(config.Conf)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
t.Fatal(err)
|
// t.Fatal(err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
// res, err := gou.Yao.Engine.RootCall(map[string]interface{}{}, "table", "Ping")
|
// // res, err := gou.Yao.Engine.RootCall(map[string]interface{}{}, "table", "Ping")
|
||||||
// assert.Nil(t, err)
|
// // assert.Nil(t, err)
|
||||||
// assert.Equal(t, "PONG", res)
|
// // assert.Equal(t, "PONG", res)
|
||||||
|
|
||||||
// _, err = gou.Yao.Engine.Call(map[string]interface{}{}, "table", "Ping")
|
// // _, err = gou.Yao.Engine.Call(map[string]interface{}{}, "table", "Ping")
|
||||||
|
// // assert.NotNil(t, err)
|
||||||
|
// // assert.Contains(t, err.Error(), "The table does not loaded")
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func TestStartStop(t *testing.T) {
|
||||||
|
|
||||||
|
// test.Prepare(t, config.Conf)
|
||||||
|
// defer test.Clean()
|
||||||
|
|
||||||
|
// var err error
|
||||||
|
// go func() { err = Start(config.Conf) }()
|
||||||
|
// if err != nil {
|
||||||
|
// t.Fatal(err)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// time.Sleep(500 * time.Millisecond)
|
||||||
|
// Stop()
|
||||||
|
// time.Sleep(100 * time.Millisecond)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func TestStartStopError(t *testing.T) {
|
||||||
|
|
||||||
|
// test.Prepare(t, config.Conf)
|
||||||
|
// defer test.Clean()
|
||||||
|
|
||||||
|
// var err error
|
||||||
|
// go func() { err = Start(config.Conf) }()
|
||||||
|
// if err != nil {
|
||||||
|
// t.Fatal(err)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// time.Sleep(500 * time.Millisecond)
|
||||||
|
// go func() { err = Start(config.Conf) }()
|
||||||
|
// time.Sleep(100 * time.Millisecond)
|
||||||
// assert.NotNil(t, err)
|
// assert.NotNil(t, err)
|
||||||
// assert.Contains(t, err.Error(), "The table does not loaded")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStartStop(t *testing.T) {
|
// Stop()
|
||||||
|
// time.Sleep(100 * time.Millisecond)
|
||||||
|
// }
|
||||||
|
|
||||||
test.Prepare(t, config.Conf)
|
// func TestAPI(t *testing.T) {
|
||||||
defer test.Clean()
|
|
||||||
|
|
||||||
var err error
|
// test.Prepare(t, config.Conf)
|
||||||
go func() { err = Start(config.Conf) }()
|
// defer test.Clean()
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(500 * time.Millisecond)
|
// Load(config.Conf)
|
||||||
Stop()
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStartStopError(t *testing.T) {
|
// var err error
|
||||||
|
// go func() { err = Start(config.Conf) }()
|
||||||
|
// if err != nil {
|
||||||
|
// t.Fatal(err)
|
||||||
|
// }
|
||||||
|
// defer Stop()
|
||||||
|
// time.Sleep(500 * time.Millisecond)
|
||||||
|
|
||||||
test.Prepare(t, config.Conf)
|
// code, row := httpGet[kv]("/dsl/ReadFile?name=/models/user.mod.yao", t)
|
||||||
defer test.Clean()
|
// assert.Equal(t, 200, code)
|
||||||
|
// assert.Equal(t, "User", row["name"])
|
||||||
|
|
||||||
var err error
|
// code, rows := httpGet[arr]("/dsl/ReadDir?name=/models", t)
|
||||||
go func() { err = Start(config.Conf) }()
|
// assert.Equal(t, 200, code)
|
||||||
if err != nil {
|
// assert.Equal(t, 8, len(rows))
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(500 * time.Millisecond)
|
// code, rows = httpGet[arr]("/dsl/ReadDir?name=/models&recursive=1", t)
|
||||||
go func() { err = Start(config.Conf) }()
|
// assert.Equal(t, 200, code)
|
||||||
time.Sleep(100 * time.Millisecond)
|
// assert.Equal(t, 13, len(rows))
|
||||||
assert.NotNil(t, err)
|
|
||||||
|
|
||||||
Stop()
|
// code, length := httpPost[int]("/dsl/WriteFile?name=/models/foo.mod.yao", []byte(`{"name":"foo"}`), t)
|
||||||
time.Sleep(100 * time.Millisecond)
|
// assert.Equal(t, 200, code)
|
||||||
}
|
// assert.Equal(t, 19, length)
|
||||||
|
|
||||||
func TestAPI(t *testing.T) {
|
// code, _ = httpPost[kv]("/dsl/Remove?name=/models/foo.mod.yao", nil, t)
|
||||||
|
// assert.Equal(t, 200, code)
|
||||||
|
|
||||||
test.Prepare(t, config.Conf)
|
// code, _ = httpPost[kv]("/dsl/Mkdir?name=/models/bar", nil, t)
|
||||||
defer test.Clean()
|
// assert.Equal(t, 200, code)
|
||||||
|
|
||||||
Load(config.Conf)
|
// code, _ = httpPost[kv]("/dsl/Remove?name=/models/bar", nil, t)
|
||||||
|
// assert.Equal(t, 200, code)
|
||||||
|
|
||||||
var err error
|
// code, _ = httpPost[kv]("/dsl/MkdirAll?name=/models/bar/hi", nil, t)
|
||||||
go func() { err = Start(config.Conf) }()
|
// assert.Equal(t, 200, code)
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer Stop()
|
|
||||||
time.Sleep(500 * time.Millisecond)
|
|
||||||
|
|
||||||
code, row := httpGet[kv]("/dsl/ReadFile?name=/models/user.mod.yao", t)
|
// code, _ = httpPost[kv]("/dsl/RemoveAll?name=/models/bar", nil, t)
|
||||||
assert.Equal(t, 200, code)
|
// assert.Equal(t, 200, code)
|
||||||
assert.Equal(t, "User", row["name"])
|
|
||||||
|
|
||||||
code, rows := httpGet[arr]("/dsl/ReadDir?name=/models", t)
|
// code, res := httpPostJSON[arr](
|
||||||
assert.Equal(t, 200, code)
|
// "/service/table",
|
||||||
assert.Equal(t, 8, len(rows))
|
// kv{
|
||||||
|
// "method": "UnitTest",
|
||||||
|
// "args": []interface{}{
|
||||||
|
// "foo", 1, 0.618,
|
||||||
|
// kv{"string": "world", "int": 1, "float": 0.618},
|
||||||
|
// arr{"foo", 1, 0.618},
|
||||||
|
// },
|
||||||
|
// }, t)
|
||||||
|
|
||||||
code, rows = httpGet[arr]("/dsl/ReadDir?name=/models&recursive=1", t)
|
// assert.Equal(t, 200, code)
|
||||||
assert.Equal(t, 200, code)
|
// assert.Equal(t, "foo", res[0])
|
||||||
assert.Equal(t, 13, len(rows))
|
// assert.Equal(t, float64(1), res[1])
|
||||||
|
// assert.Equal(t, 0.618, res[2])
|
||||||
|
// assert.Equal(t, "world", res[3].(map[string]interface{})["string"])
|
||||||
|
// assert.Equal(t, float64(1), res[3].(map[string]interface{})["int"])
|
||||||
|
// assert.Equal(t, 0.618, res[3].(map[string]interface{})["float"])
|
||||||
|
// assert.Equal(t, "foo", res[4].([]interface{})[0])
|
||||||
|
// assert.Equal(t, float64(1), res[4].([]interface{})[1])
|
||||||
|
// assert.Equal(t, 0.618, res[4].([]interface{})[2])
|
||||||
|
|
||||||
code, length := httpPost[int]("/dsl/WriteFile?name=/models/foo.mod.yao", []byte(`{"name":"foo"}`), t)
|
// code, excp := httpPostJSON[kv]("/service/table", kv{"method": "UnitTest", "args": []interface{}{"throw-test"}}, t)
|
||||||
assert.Equal(t, 200, code)
|
// assert.Equal(t, 418, code)
|
||||||
assert.Equal(t, 19, length)
|
// assert.Equal(t, float64(418), excp["code"])
|
||||||
|
// assert.Equal(t, "I'm a teapot", excp["message"])
|
||||||
|
// }
|
||||||
|
|
||||||
code, _ = httpPost[kv]("/dsl/Remove?name=/models/foo.mod.yao", nil, t)
|
// func httpGet[T kv | arr | interface{} | map[string]interface{} | int | []interface{}](url string, t *testing.T) (int, T) {
|
||||||
assert.Equal(t, 200, code)
|
|
||||||
|
|
||||||
code, _ = httpPost[kv]("/dsl/Mkdir?name=/models/bar", nil, t)
|
// var data T
|
||||||
assert.Equal(t, 200, code)
|
// url = fmt.Sprintf("http://127.0.0.1:%d%s", config.Conf.Studio.Port, url)
|
||||||
|
// req, err := http.NewRequest("GET", url, nil)
|
||||||
|
// if err != nil {
|
||||||
|
// t.Fatal(err)
|
||||||
|
// }
|
||||||
|
|
||||||
code, _ = httpPost[kv]("/dsl/Remove?name=/models/bar", nil, t)
|
// token := getToken(t)
|
||||||
assert.Equal(t, 200, code)
|
// req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||||
|
|
||||||
code, _ = httpPost[kv]("/dsl/MkdirAll?name=/models/bar/hi", nil, t)
|
// client := http.Client{}
|
||||||
assert.Equal(t, 200, code)
|
// res, err := client.Do(req)
|
||||||
|
// if err != nil {
|
||||||
|
// t.Fatal(err)
|
||||||
|
// }
|
||||||
|
|
||||||
code, _ = httpPost[kv]("/dsl/RemoveAll?name=/models/bar", nil, t)
|
// if res.Body != nil {
|
||||||
assert.Equal(t, 200, code)
|
// body, err := io.ReadAll(res.Body)
|
||||||
|
// if err != nil {
|
||||||
|
// t.Fatal(err)
|
||||||
|
// }
|
||||||
|
|
||||||
code, res := httpPostJSON[arr](
|
// if body != nil && len(body) > 0 {
|
||||||
"/service/table",
|
// err = jsoniter.Unmarshal(body, &data)
|
||||||
kv{
|
// if err != nil {
|
||||||
"method": "UnitTest",
|
// t.Fatal(fmt.Sprintf("%s\n%s\n", err.Error(), string(body)))
|
||||||
"args": []interface{}{
|
// }
|
||||||
"foo", 1, 0.618,
|
// }
|
||||||
kv{"string": "world", "int": 1, "float": 0.618},
|
// }
|
||||||
arr{"foo", 1, 0.618},
|
|
||||||
},
|
|
||||||
}, t)
|
|
||||||
|
|
||||||
assert.Equal(t, 200, code)
|
// return res.StatusCode, data
|
||||||
assert.Equal(t, "foo", res[0])
|
// }
|
||||||
assert.Equal(t, float64(1), res[1])
|
|
||||||
assert.Equal(t, 0.618, res[2])
|
|
||||||
assert.Equal(t, "world", res[3].(map[string]interface{})["string"])
|
|
||||||
assert.Equal(t, float64(1), res[3].(map[string]interface{})["int"])
|
|
||||||
assert.Equal(t, 0.618, res[3].(map[string]interface{})["float"])
|
|
||||||
assert.Equal(t, "foo", res[4].([]interface{})[0])
|
|
||||||
assert.Equal(t, float64(1), res[4].([]interface{})[1])
|
|
||||||
assert.Equal(t, 0.618, res[4].([]interface{})[2])
|
|
||||||
|
|
||||||
code, excp := httpPostJSON[kv]("/service/table", kv{"method": "UnitTest", "args": []interface{}{"throw-test"}}, t)
|
// func httpPost[T kv | arr | interface{} | map[string]interface{} | int | []interface{}](url string, payload []byte, t *testing.T) (int, T) {
|
||||||
assert.Equal(t, 418, code)
|
|
||||||
assert.Equal(t, float64(418), excp["code"])
|
|
||||||
assert.Equal(t, "I'm a teapot", excp["message"])
|
|
||||||
}
|
|
||||||
|
|
||||||
func httpGet[T kv | arr | interface{} | map[string]interface{} | int | []interface{}](url string, t *testing.T) (int, T) {
|
// var data T
|
||||||
|
// var buff *bytes.Buffer = bytes.NewBuffer([]byte{})
|
||||||
|
|
||||||
var data T
|
// if payload != nil {
|
||||||
url = fmt.Sprintf("http://127.0.0.1:%d%s", config.Conf.Studio.Port, url)
|
// buff = bytes.NewBuffer(payload)
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
// }
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
token := getToken(t)
|
// url = fmt.Sprintf("http://127.0.0.1:%d%s", config.Conf.Studio.Port, url)
|
||||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
// req, err := http.NewRequest("POST", url, buff)
|
||||||
|
// if err != nil {
|
||||||
|
// t.Fatal(err)
|
||||||
|
// }
|
||||||
|
|
||||||
client := http.Client{}
|
// token := getToken(t)
|
||||||
res, err := client.Do(req)
|
// req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if res.Body != nil {
|
// client := http.Client{}
|
||||||
body, err := io.ReadAll(res.Body)
|
// res, err := client.Do(req)
|
||||||
if err != nil {
|
// if res.Body != nil {
|
||||||
t.Fatal(err)
|
// body, err := io.ReadAll(res.Body)
|
||||||
}
|
// if err != nil {
|
||||||
|
// t.Fatal(err)
|
||||||
|
// }
|
||||||
|
|
||||||
if body != nil && len(body) > 0 {
|
// if body != nil && string(body) != "" {
|
||||||
err = jsoniter.Unmarshal(body, &data)
|
// err = jsoniter.Unmarshal(body, &data)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
t.Fatal(fmt.Sprintf("%s\n%s\n", err.Error(), string(body)))
|
// t.Fatal(err)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
return res.StatusCode, data
|
// return res.StatusCode, data
|
||||||
}
|
// }
|
||||||
|
|
||||||
func httpPost[T kv | arr | interface{} | map[string]interface{} | int | []interface{}](url string, payload []byte, t *testing.T) (int, T) {
|
// func httpPostJSON[T kv | arr | interface{} | map[string]interface{} | int | []interface{}](url string, payload interface{}, t *testing.T) (int, T) {
|
||||||
|
// var data []byte
|
||||||
|
// var err error
|
||||||
|
// if payload != nil {
|
||||||
|
// data, err = jsoniter.Marshal(payload)
|
||||||
|
// if err != nil {
|
||||||
|
// t.Fatal(err)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return httpPost[T](url, data, t)
|
||||||
|
// }
|
||||||
|
|
||||||
var data T
|
// func getToken(t *testing.T) string {
|
||||||
var buff *bytes.Buffer = bytes.NewBuffer([]byte{})
|
// return helper.JwtMake(
|
||||||
|
// 1,
|
||||||
if payload != nil {
|
// map[string]interface{}{"id": 1, "user_id": 1, "user": kv{"id": 1, "name": "test"}},
|
||||||
buff = bytes.NewBuffer(payload)
|
// map[string]interface{}{"issuer": "unit-test", "timeout": 3600},
|
||||||
}
|
// []byte(config.Conf.Studio.Secret),
|
||||||
|
// ).Token
|
||||||
url = fmt.Sprintf("http://127.0.0.1:%d%s", config.Conf.Studio.Port, url)
|
// }
|
||||||
req, err := http.NewRequest("POST", url, buff)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
token := getToken(t)
|
|
||||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
||||||
|
|
||||||
client := http.Client{}
|
|
||||||
res, err := client.Do(req)
|
|
||||||
if res.Body != nil {
|
|
||||||
body, err := io.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if body != nil && string(body) != "" {
|
|
||||||
err = jsoniter.Unmarshal(body, &data)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.StatusCode, data
|
|
||||||
}
|
|
||||||
|
|
||||||
func httpPostJSON[T kv | arr | interface{} | map[string]interface{} | int | []interface{}](url string, payload interface{}, t *testing.T) (int, T) {
|
|
||||||
var data []byte
|
|
||||||
var err error
|
|
||||||
if payload != nil {
|
|
||||||
data, err = jsoniter.Marshal(payload)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return httpPost[T](url, data, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getToken(t *testing.T) string {
|
|
||||||
return helper.JwtMake(
|
|
||||||
1,
|
|
||||||
map[string]interface{}{"id": 1, "user_id": 1, "user": kv{"id": 1, "name": "test"}},
|
|
||||||
map[string]interface{}{"issuer": "unit-test", "timeout": 3600},
|
|
||||||
[]byte(config.Conf.Studio.Secret),
|
|
||||||
).Token
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue