Record implemented MySQL pilots and remaining app integration gates. Include the existing opt-in pages-cli agent skill source without installing it.
96 lines
3.0 KiB
Go
96 lines
3.0 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"pageswiki/internal/machinelearning"
|
|
)
|
|
|
|
func TestMachineLearningSettingsAPI(t *testing.T) {
|
|
s, token, domain := configFixture(t)
|
|
call := func(auth, method, body, origin string) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest(method, "http://localhost/_/api/v1/ml/settings", strings.NewReader(body))
|
|
if auth != "" {
|
|
r.Header.Set("Authorization", "Bearer "+auth)
|
|
}
|
|
r.Header.Set("Content-Type", "application/json")
|
|
if origin != "" {
|
|
r.Header.Set("Origin", origin)
|
|
}
|
|
w := httptest.NewRecorder()
|
|
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
w := call(token, "GET", "", "")
|
|
if w.Code != 200 {
|
|
t.Fatalf("read: %d %s", w.Code, w.Body.String())
|
|
}
|
|
if !strings.Contains(w.Header().Get("Cache-Control"), "no-store") {
|
|
t.Fatal("cacheable settings")
|
|
}
|
|
p := machinelearning.Policy{Enabled: true, ScopeUUID: "22222222-2222-4222-8222-222222222222", Provider: "doc2vec", Model: strings.Repeat("a", 64), Dimensions: 128, Prefixes: []string{"/lab"}, MaxBatchBytes: 6000}
|
|
encode := func(base string) string {
|
|
b, e := json.Marshal(map[string]any{"base_generation": base, "policy": p})
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
return string(b)
|
|
}
|
|
body := encode("")
|
|
if w = call(token, "PUT", body, "https://attacker.example"); w.Code != 403 {
|
|
t.Fatal("cross origin", w.Code)
|
|
}
|
|
user, e := s.store.CreatePasswordAccount(domain, "ml-user", "synthetic long password", "User", "")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
userToken, e := s.store.CreateAuthSession(domain, user.UUID)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
for _, method := range []string{"GET", "PUT"} {
|
|
if w = call(userToken, method, body, ""); w.Code != 403 {
|
|
t.Fatal("non admin", method, w.Code)
|
|
}
|
|
}
|
|
if w = call("", "GET", "", ""); w.Code == 200 {
|
|
t.Fatal("anonymous read")
|
|
}
|
|
w = call(token, "PUT", body, "")
|
|
if w.Code != 200 {
|
|
t.Fatalf("save: %d %s", w.Code, w.Body.String())
|
|
}
|
|
var saved machinelearning.Policy
|
|
if json.Unmarshal(w.Body.Bytes(), &saved) != nil || saved.Generation == "" {
|
|
t.Fatal("missing generation")
|
|
}
|
|
if w = call(token, "PUT", body, ""); w.Code != 409 {
|
|
t.Fatal("stale save", w.Code)
|
|
}
|
|
if w = call(token, "PUT", encode(saved.Generation)+" {}", ""); w.Code != 400 {
|
|
t.Fatal("trailing JSON", w.Code)
|
|
}
|
|
p.Enabled = false
|
|
w = call(token, "PUT", encode(saved.Generation), "")
|
|
if w.Code != 200 {
|
|
t.Fatal("disable", w.Code)
|
|
}
|
|
if json.Unmarshal(w.Body.Bytes(), &saved) != nil {
|
|
t.Fatal("response")
|
|
}
|
|
p.Enabled = true
|
|
p.Provider = "openai"
|
|
p.Model = "text-embedding-3-small"
|
|
if w = call(token, "PUT", encode(saved.Generation), ""); w.Code != 422 {
|
|
t.Fatal("external consent bypass", w.Code)
|
|
}
|
|
page := configRequest(s, token, http.MethodGet, "/-/sys/config?panel=machine-learning", nil)
|
|
if page.Code != 200 || !strings.Contains(page.Body.String(), `id="panel-machine-learning"`) || !strings.Contains(page.Body.String(), "ml-settings.js") {
|
|
t.Fatal("settings panel missing", page.Code)
|
|
}
|
|
}
|