Make nmon easier to install and run without editing YAML by hand: - Settings: point-and-click forms over config.yaml (GET/PUT /api/v1/settings/form); the YAML editor moves to /settings/yaml. - First install: with no sign-in configured nmon starts in install mode, serves only the setup wizard (sign-in, basics, devices, alerting) and closes it for good once sign-in is saved. A self-signed certificate is generated when no TLS source is given. poller.interval becomes a config setting. - Reverse proxies: opt-in --http-listen plain-HTTP listener beside HTTPS; sign-in stays required. - Webhooks: Generic, Teams Adaptive Card, Slack and Discord presets, template preview and test send. - Help: the operations, coverage and profile docs served at /help. - Polling: per-device "Poll now", a dry-run test poll returning an SNMP trace, and time-limited in-memory SNMP debug recording, in the UI and as `nmon device poll`. - Ansible: deploys into install mode when no sign-in is set, and no longer overwrites the config.yaml the wizard wrote. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017TuKgyKve2XLFtrrXWMi2w
104 lines
3.0 KiB
Go
104 lines
3.0 KiB
Go
package webui
|
|
|
|
import (
|
|
"compress/gzip"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"testing/fstest"
|
|
)
|
|
|
|
func testHandler(t *testing.T) http.Handler {
|
|
t.Helper()
|
|
big := strings.Repeat("console.log('x');\n", 200)
|
|
fsys := fstest.MapFS{
|
|
"index.html": {Data: []byte("<html>app</html>")},
|
|
"assets/a-1.js": {Data: []byte(big)},
|
|
"favicon.svg": {Data: []byte("<svg/>")},
|
|
}
|
|
api := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusTeapot)
|
|
_, _ = io.WriteString(w, "api:"+r.URL.Path)
|
|
})
|
|
h, err := Handler(fsys, api)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return h
|
|
}
|
|
|
|
func get(h http.Handler, method, p string, hdr ...string) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest(method, p, nil)
|
|
for i := 0; i+1 < len(hdr); i += 2 {
|
|
r.Header.Set(hdr[i], hdr[i+1])
|
|
}
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
|
|
func TestUnknownRoutesFallBackToIndexButAPIPathsDoNot(t *testing.T) {
|
|
h := testHandler(t)
|
|
for _, p := range []string{"/", "/devices/core-sw1", "/graph/01ABC", "/help", "/help/operations", "/help/operations?x=1"} {
|
|
w := get(h, http.MethodGet, p)
|
|
if w.Code != http.StatusOK || w.Body.String() != "<html>app</html>" {
|
|
t.Errorf("%s: %d %q", p, w.Code, w.Body.String())
|
|
}
|
|
if cc := w.Header().Get("Cache-Control"); cc != "no-cache" {
|
|
t.Errorf("%s: index cache-control %q", p, cc)
|
|
}
|
|
}
|
|
for _, p := range []string{"/api/v1/devices", "/docs", "/openapi.json", "/metrics", "/schemas/x.json"} {
|
|
if w := get(h, http.MethodGet, p); w.Code != http.StatusTeapot {
|
|
t.Errorf("%s reached the SPA: %d", p, w.Code)
|
|
}
|
|
}
|
|
if w := get(h, http.MethodPost, "/devices"); w.Code != http.StatusTeapot {
|
|
t.Errorf("POST outside the SPA: %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestHashedAssetsAreImmutableAndGzipped(t *testing.T) {
|
|
h := testHandler(t)
|
|
w := get(h, http.MethodGet, "/assets/a-1.js", "Accept-Encoding", "gzip, br")
|
|
if cc := w.Header().Get("Cache-Control"); !strings.Contains(cc, "immutable") {
|
|
t.Errorf("cache-control %q", cc)
|
|
}
|
|
if w.Header().Get("Content-Encoding") != "gzip" {
|
|
t.Fatal("not gzipped")
|
|
}
|
|
zr, err := gzip.NewReader(w.Body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if b, _ := io.ReadAll(zr); !strings.HasPrefix(string(b), "console.log") {
|
|
t.Errorf("bad body %q", b[:10])
|
|
}
|
|
if w := get(h, http.MethodGet, "/assets/a-1.js"); w.Header().Get("Content-Encoding") != "" {
|
|
t.Error("gzip sent without Accept-Encoding")
|
|
}
|
|
if w := get(h, http.MethodGet, "/assets/missing-2.js"); w.Code != http.StatusNotFound {
|
|
t.Errorf("missing asset: %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestETagRevalidates(t *testing.T) {
|
|
h := testHandler(t)
|
|
tag := get(h, http.MethodGet, "/").Header().Get("ETag")
|
|
if w := get(h, http.MethodGet, "/", "If-None-Match", tag); w.Code != http.StatusNotModified {
|
|
t.Errorf("got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestEmbeddedPlaceholderServes(t *testing.T) {
|
|
h, err := Handler(Dist(), http.NotFoundHandler())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w := get(h, http.MethodGet, "/x/y"); w.Code != http.StatusOK {
|
|
t.Errorf("got %d", w.Code)
|
|
}
|
|
}
|