system_api_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package httpapi_test
  2. import (
  3. "testing"
  4. "github.com/gnacho/netpulse/server-go/internal/httpapi"
  5. )
  6. // SPEC-65 D65-6: GET /api/system/info — autenticado (cualquier usuario),
  7. // datos reales del proceso con la forma exacta del contrato.
  8. func TestSystemInfo(t *testing.T) {
  9. srv := makeTestServer(t)
  10. // Sin sesión → 401
  11. res := get(t, srv.URL, "/api/system/info", "")
  12. if res.StatusCode != 401 {
  13. res.Body.Close()
  14. t.Fatalf("sin cookie: got %d want 401", res.StatusCode)
  15. }
  16. res.Body.Close()
  17. _, cookie, _ := loginCookie(t, srv.URL, "admin", "test123456")
  18. res = get(t, srv.URL, "/api/system/info", cookie)
  19. if res.StatusCode != 200 {
  20. t.Fatalf("con cookie: got %d want 200", res.StatusCode)
  21. }
  22. body := readJSON(t, res)
  23. // Forma exacta: las 11 claves del contrato, ni una más
  24. want := map[string]bool{
  25. "version": true, "goVersion": true, "os": true, "arch": true, "distro": true,
  26. "kernel": true, "cpuModel": true, "cpuCores": true, "memTotalMb": true,
  27. "uptimeS": true, "demo": true,
  28. }
  29. if len(body) != len(want) {
  30. t.Fatalf("claves: got %v", body)
  31. }
  32. for k := range want {
  33. if _, ok := body[k]; !ok {
  34. t.Fatalf("falta clave %q en %v", k, body)
  35. }
  36. }
  37. if body["version"] != httpapi.Version {
  38. t.Fatalf("version=%v, want %s (misma fuente que update.go)", body["version"], httpapi.Version)
  39. }
  40. if goVer, ok := body["goVersion"].(string); !ok || len(goVer) < 3 || goVer[:2] != "go" {
  41. t.Fatalf("goVersion: %v", body["goVersion"])
  42. }
  43. if cores, ok := body["cpuCores"].(float64); !ok || cores < 1 {
  44. t.Fatalf("cpuCores: %v", body["cpuCores"])
  45. }
  46. if up, ok := body["uptimeS"].(float64); !ok || up < 0 {
  47. t.Fatalf("uptimeS: %v", body["uptimeS"])
  48. }
  49. if body["demo"] != true {
  50. t.Fatalf("demo: %v (el servidor de test es DEMO_MODE=1)", body["demo"])
  51. }
  52. }