Files
EasyDNS-Web/internal/server/release_011_test.go
T
Mateusz Gruszczyński de13d851a8
Go quality gates / test (push) Waiting to run
v0.1.1
2026-11-30 14:44:28 +01:00

142 lines
5.2 KiB
Go

package server
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"git.linuxiarz.pl/gru/EasyDNS-Web/internal/config"
)
func release011Backend() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/api/v1/health":
fmt.Fprint(w, `{"status":"ok"}`)
case "/readyz":
fmt.Fprint(w, `{"status":"ready"}`)
case "/api/v1/status":
fmt.Fprint(w, `{"version":"0.1.4","mode":"hybrid","roles":{"primary":1,"secondary":0}}`)
case "/api/v1/policies":
fmt.Fprint(w, `{}`)
default:
fmt.Fprint(w, `{}`)
}
})
}
func TestRelease011CopyAndFooter(t *testing.T) {
s := testPanel(t, release011Backend())
w := httptest.NewRecorder()
s.Handler().ServeHTTP(w, httptest.NewRequest(http.MethodGet, "http://panel.test/login", nil))
if w.Code != http.StatusOK {
t.Fatalf("login status %d", w.Code)
}
body := w.Body.String()
for _, removed := range []string{
"LOCAL CONTROL ROOM", "Access the EasyDNS administration panel.",
"The EasyDNS bearer token stays on the server.",
"Panel authentication is separate from EasyDNS REST API credentials.",
} {
if strings.Contains(body, removed) {
t.Fatalf("obsolete login copy: %q", removed)
}
}
if !strings.Contains(body, "Sign in") || !strings.Contains(body, "name=\"password\"") {
t.Fatal("login form missing")
}
w = panelRequest(t, s, http.MethodGet, "/configuration", nil, nil)
if w.Code != http.StatusOK {
t.Fatalf("configuration status %d: %s", w.Code, w.Body.String())
}
body = w.Body.String()
for _, removed := range []string{"Only verified EasyDNS API operations are available", "What this panel cannot change"} {
if strings.Contains(body, removed) {
t.Fatalf("obsolete configuration/footer copy: %q", removed)
}
}
if !strings.Contains(body, `<footer class="footer">EasyDNS · independent management panel</footer>`) {
t.Fatalf("footer caption missing: %s", body)
}
if !strings.Contains(body, "Configuration workflow") || !strings.Contains(body, `href="/status"`) {
t.Fatal("configuration workflow missing")
}
}
func TestRelease011ConfiguredTOMLMultiServerAndInventory(t *testing.T) {
backend := httptest.NewServer(release011Backend())
defer backend.Close()
remote := httptest.NewServer(release011Backend())
defer remote.Close()
state := t.TempDir()
// An old UI preference must not override a current TOML configuration.
if err := os.WriteFile(filepath.Join(state, "settings.json"), []byte(`{"enabled":false}`), 0600); err != nil {
t.Fatal(err)
}
cfg := config.Config{
Listen: "127.0.0.1:8088", APIURL: backend.URL, APIToken: "local-token",
Username: "admin", Password: "a-long-admin-password", SessionAuto: true,
SessionKeyFile: filepath.Join(state, "session.key"), NodesDir: state,
MultiServer: true, MultiServerExplicit: true,
LocalName: "DNS Primary", LocalRole: "primary", LocalRoleExplicit: true,
Servers: []config.ServerSeed{{Slot: 1, Name: "DNS Secondary", Role: "secondary", URL: remote.URL, Token: "remote-token"}},
}
s, err := New(cfg)
if err != nil {
t.Fatal(err)
}
if !s.nodes.Enabled() {
t.Fatal("explicit TOML setting lost to saved preference")
}
w := panelRequest(t, s, http.MethodGet, "/configuration", nil, nil)
if w.Code != http.StatusOK {
t.Fatalf("configuration status %d", w.Code)
}
if strings.Contains(w.Body.String(), "Panel settings · Multi-server management") || strings.Contains(w.Body.String(), `action="/configuration/multi-server"`) {
t.Fatal("TOML-managed mode has a redundant UI toggle")
}
w = panelRequest(t, s, http.MethodPost, "/configuration/multi-server", url.Values{}, nil)
if w.Code != http.StatusForbidden || !s.nodes.Enabled() {
t.Fatalf("configured mode was editable: %d", w.Code)
}
w = panelRequest(t, s, http.MethodGet, "/servers", nil, nil)
if w.Code != http.StatusOK {
t.Fatalf("servers status %d: %s", w.Code, w.Body.String())
}
body := w.Body.String()
for _, required := range []string{"DNS Primary", "DNS Secondary", ">LOCAL</span>", ">REMOTE</span>", "Primary", "Secondary"} {
if !strings.Contains(body, required) {
t.Fatalf("missing inventory %q", required)
}
}
for _, obsolete := range []string{">ENV</span>", "Configured in .env / systemd", `action="/servers/local/role"`} {
if strings.Contains(body, obsolete) {
t.Fatalf("fixed server exposes obsolete/editor copy: %q", obsolete)
}
}
remoteID := s.nodes.All()[1].ID
if strings.Contains(body, `action="/servers/`+remoteID+`/role"`) || strings.Contains(body, `href="/servers/`+remoteID+`/remove"`) {
t.Fatal("configuration-managed remote server shows mutable actions")
}
w = panelRequest(t, s, http.MethodPost, "/servers/local/role", url.Values{"role": {"secondary"}}, nil)
if w.Code != http.StatusForbidden {
t.Fatalf("fixed local role writable: %d", w.Code)
}
w = panelRequest(t, s, http.MethodPost, "/servers/"+remoteID+"/role", url.Values{"role": {"unassigned"}}, nil)
if w.Code != http.StatusForbidden {
t.Fatalf("fixed remote role writable: %d", w.Code)
}
w = panelRequest(t, s, http.MethodGet, "/servers/"+remoteID+"/remove", nil, nil)
if w.Code != http.StatusForbidden {
t.Fatalf("fixed remote removal available: %d", w.Code)
}
}