(t *testing.T)
| 2011 | } |
| 2012 | |
| 2013 | func TestRequestPluginInstancesAPI(t *testing.T) { |
| 2014 | setupTaskPluginControllerTest(t) |
| 2015 | originalOptions := common.OptionMap |
| 2016 | common.OptionMap = map[string]string{} |
| 2017 | t.Cleanup(func() { common.OptionMap = originalOptions }) |
| 2018 | for _, source := range []string{ |
| 2019 | `export const meta = {apiVersion: 1, key: "rp-api", name: {en: "Limit", zh: "限制"}, version: "1.0.0", author: {name: "Test"}, |
| 2020 | configSchema: {type: "object", properties: {limit: {type: "integer", title: "Limit", default: 3, minimum: 1}}}}; |
| 2021 | export function ingress() { return {action: "continue", findings: [{rule: "seen", count: 1}]}; } |
| 2022 | export function validateConfig(config) { if (config.limit === 13) throw new Error("13 is unlucky"); }`, |
| 2023 | `export const meta = {apiVersion: 1, key: "rp-api-task", name: "Task", version: "1.0.0", author: {name: "Test"}, models: ["m"], fetchMode: "per_task"}; |
| 2024 | export function buildSubmitRequest() { return {}; } |
| 2025 | export function parseSubmitResponse() { return {}; } |
| 2026 | export function buildQueryRequest() { return {}; } |
| 2027 | export function parseTaskResult() { return {}; }`, |
| 2028 | } { |
| 2029 | plugin, err := jsplugin.DefaultRegistry.Register(source, jsplugin.Options{}) |
| 2030 | require.NoError(t, err) |
| 2031 | t.Cleanup(func() { _ = jsplugin.DefaultRegistry.Unregister(plugin.Meta.Key) }) |
| 2032 | } |
| 2033 | t.Cleanup(func() { |
| 2034 | require.NoError(t, config.GlobalConfig.LoadFromDB(map[string]string{request_plugin_setting.InstancesOption: "[]"})) |
| 2035 | }) |
| 2036 | call := func(handler gin.HandlerFunc, method, body string) (int, string) { |
| 2037 | recorder := httptest.NewRecorder() |
| 2038 | c, _ := gin.CreateTestContext(recorder) |
| 2039 | c.Request = httptest.NewRequest(method, "/api/request_plugin/instances", strings.NewReader(body)) |
| 2040 | c.Set("role", common.RoleRootUser) |
| 2041 | handler(c) |
| 2042 | return recorder.Code, recorder.Body.String() |
| 2043 | } |
| 2044 | |
| 2045 | models := make([]string, 101) |
| 2046 | for index := range models { |
| 2047 | models[index] = fmt.Sprintf("model-%d", index) |
| 2048 | } |
| 2049 | for name, tc := range map[string]struct{ body, message string }{ |
| 2050 | "enabled missing plugin": {`{"instances":[{"name":"A","plugin":"missing","enabled":true}]}`, "Plugin missing is not installed or is turned off"}, |
| 2051 | "no stage hooks": {`{"instances":[{"name":"A","plugin":"rp-api-task"}]}`, "Plugin rp-api-task has no request processing hooks"}, |
| 2052 | "schema": {`{"instances":[{"name":"A","plugin":"rp-api","config":{"limit":0}}]}`, "Request plugin instance A has an invalid config"}, |
| 2053 | "validateConfig": {`{"instances":[{"name":"A","plugin":"rp-api","config":{"limit":13}}]}`, "13 is unlucky"}, |
| 2054 | "empty name": {`{"instances":[{"name":" ","plugin":"rp-api"}]}`, "names must be 1 to 64 characters"}, |
| 2055 | "long name": {`{"instances":[{"name":"` + strings.Repeat("名", 65) + `","plugin":"rp-api"}]}`, "names must be 1 to 64 characters"}, |
| 2056 | "duplicate ids": {`{"instances":[{"id":"rp_same","name":"A","plugin":"rp-api"},{"id":"rp_same","name":"B","plugin":"rp-api"}]}`, "rp_same is used more than once"}, |
| 2057 | "empty group": {`{"instances":[{"name":"A","plugin":"rp-api","groups":["vip",""]}]}`, "has an empty group"}, |
| 2058 | "too large": {`{"instances":[{"name":"A","plugin":"rp-api","groups":["` + strings.Repeat("g", maxRequestPluginOptionBytes) + `"]}]}`, fmt.Sprintf("must not exceed %d bytes", maxRequestPluginOptionBytes)}, |
| 2059 | "empty model": {`{"instances":[{"name":"A","plugin":"rp-api","models":["gpt"," "]}]}`, "Request plugin instance A has an empty model name"}, |
| 2060 | "long model": {`{"instances":[{"name":"A","plugin":"rp-api","models":["` + strings.Repeat("m", 129) + `"]}]}`, "has a model name longer than 128 characters"}, |
| 2061 | "too many models": {`{"instances":[{"name":"A","plugin":"rp-api","models":["` + strings.Join(models, `","`) + `"]}]}`, "lists more than 100 models"}, |
| 2062 | } { |
| 2063 | t.Run(name, func(t *testing.T) { |
| 2064 | code, body := call(UpdateRequestPluginInstances, http.MethodPut, tc.body) |
| 2065 | assert.Equal(t, http.StatusBadRequest, code) |
| 2066 | assert.Contains(t, body, tc.message) |
| 2067 | assert.Empty(t, request_plugin_setting.GetInstances()) |
| 2068 | }) |
| 2069 | } |
| 2070 |
nothing calls this directly
no test coverage detected