wifi_channelplan_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // wifi_channelplan_test.go — tests de GET /api/wifi/channel-plan (#452).
  2. package httpapi_test
  3. import (
  4. "crypto/hmac"
  5. "crypto/sha256"
  6. "encoding/hex"
  7. "encoding/json"
  8. "io"
  9. "net/http"
  10. "strings"
  11. "testing"
  12. "time"
  13. "github.com/gnacho/netpulse/agent/probe"
  14. )
  15. func TestChannelPlanEndpoint(t *testing.T) {
  16. ts := makeTestServer(t)
  17. // Crear agente/token y hacer login admin.
  18. token, cookie := createAgentAndLogin(t, ts)
  19. // Construir payload con scans y empujarlo.
  20. payload := probe.Payload{
  21. Router: "rt1",
  22. Ts: time.Now().Unix(),
  23. Version: "test",
  24. Data: probe.PayloadData{
  25. Wireless: &probe.WirelessData{
  26. Clients: map[string]probe.WirelessClient{},
  27. Radios: []probe.Radio{
  28. {Name: "2.4 GHz", Channel: 6, WidthMhz: 20, PowerDbm: 20, Clients: 0},
  29. },
  30. Scans: []probe.ScanResult{
  31. {Iface: "wlan0", BSSID: "00:11:22:33:44:55", SSID: "vecino", Channel: 6, Freq: 2437, Signal: -50},
  32. },
  33. },
  34. },
  35. }
  36. res := pushPayload(t, ts, token, "10.0.9.1", payload)
  37. if res.StatusCode != http.StatusAccepted {
  38. b, _ := io.ReadAll(res.Body)
  39. t.Fatalf("ingest: %d %s", res.StatusCode, string(b))
  40. }
  41. res.Body.Close()
  42. // Consultar channel-plan.
  43. req, _ := http.NewRequest("GET", ts.URL+"/api/wifi/channel-plan?routerId=rt1", nil)
  44. req.Header.Set("Cookie", "session="+cookie)
  45. res, err := http.DefaultClient.Do(req)
  46. if err != nil {
  47. t.Fatalf("channel-plan: %v", err)
  48. }
  49. defer res.Body.Close()
  50. if res.StatusCode != http.StatusOK {
  51. b, _ := io.ReadAll(res.Body)
  52. t.Fatalf("channel-plan: %d %s", res.StatusCode, string(b))
  53. }
  54. var body struct {
  55. RouterID string `json:"routerId"`
  56. Radios []channelplanRadioJSON `json:"radios"`
  57. Scans []channelplanScanJSON `json:"scans"`
  58. }
  59. if err := json.NewDecoder(res.Body).Decode(&body); err != nil {
  60. t.Fatalf("decode: %v", err)
  61. }
  62. if len(body.Scans) != 1 {
  63. t.Fatalf("esperaba 1 scan, got %d", len(body.Scans))
  64. }
  65. if len(body.Radios) != 1 {
  66. t.Fatalf("esperaba 1 radio, got %d", len(body.Radios))
  67. }
  68. if body.Radios[0].Recommended == 6 {
  69. t.Errorf("no debería recomendar canal 6 ocupado")
  70. }
  71. }
  72. type channelplanRadioJSON struct {
  73. Name string `json:"name"`
  74. Channel int `json:"channel"`
  75. Recommended int `json:"recommended"`
  76. }
  77. type channelplanScanJSON struct {
  78. BSSID string `json:"bssid"`
  79. SSID string `json:"ssid"`
  80. Channel int `json:"channel"`
  81. Signal int `json:"signal"`
  82. }
  83. func createAgentAndLogin(t *testing.T, ts *testServer) (string, string) {
  84. t.Helper()
  85. token := "agent-token-rt1"
  86. sum := sha256.Sum256([]byte(token))
  87. _, err := ts.db.Exec("INSERT INTO kv (key, value) VALUES (?, ?)", "agent.token.rt1", hex.EncodeToString(sum[:]))
  88. if err != nil {
  89. t.Fatalf("insert agent token: %v", err)
  90. }
  91. _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
  92. return token, cookie
  93. }
  94. func pushPayload(t *testing.T, ts *testServer, token, ip string, payload probe.Payload) *http.Response {
  95. t.Helper()
  96. data, _ := json.Marshal(payload)
  97. req, _ := http.NewRequest("POST", ts.URL+"/api/ingest/agent", strings.NewReader(string(data)))
  98. req.Header.Set("Content-Type", "application/json")
  99. req.Header.Set("X-Forwarded-For", ip)
  100. if token != "" {
  101. req.Header.Set("Authorization", "Bearer "+token)
  102. mac := hmac.New(sha256.New, []byte(token))
  103. mac.Write(data)
  104. req.Header.Set("X-Agent-Signature", hex.EncodeToString(mac.Sum(nil)))
  105. }
  106. res, err := http.DefaultClient.Do(req)
  107. if err != nil {
  108. t.Fatalf("ingest: %v", err)
  109. }
  110. return res
  111. }