- client 模式抓取水星 MW305R DHCP 列表,记录上下线,本地看板 - server 模式收上报,按站点分组持久化,多站点看板,7天滚动 - ESP8266 低功耗固件抓原始 HTML 上报 /api/raw,服务器端解析 - 部署脚本+systemd 单元+文档,交叉编译 Linux 二进制
143 lines
3.6 KiB
Go
143 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// 测试上报: 正确 token 接收, 错误 token 拒绝
|
|
func TestReportAuth(t *testing.T) {
|
|
s := &Server{
|
|
dataDir: t.TempDir(),
|
|
token: "secret",
|
|
sites: make(map[string]*SiteData),
|
|
}
|
|
|
|
// 错误 token
|
|
req := httptest.NewRequest(http.MethodPost, "/api/report", bytes.NewBufferString(`{"site":"s1","token":"bad"}`))
|
|
w := httptest.NewRecorder()
|
|
s.handleReport(w, req)
|
|
if w.Code != http.StatusUnauthorized {
|
|
t.Fatalf("错误token应401, got %d", w.Code)
|
|
}
|
|
|
|
// 正确 token
|
|
payload := ReportPayload{
|
|
Site: "s1",
|
|
Token: "secret",
|
|
Events: []Event{
|
|
{Timestamp: time.Now(), Event: evOnline, MAC: "AA:BB", IP: "1.1.1.1", Name: "Dev"},
|
|
},
|
|
Snapshot: &ReportSnapshot{
|
|
Devices: []*Device{{MAC: "AA:BB", Name: "Dev", LastIP: "1.1.1.1", Status: evOnline}},
|
|
},
|
|
}
|
|
body, _ := json.Marshal(payload)
|
|
req = httptest.NewRequest(http.MethodPost, "/api/report", bytes.NewBuffer(body))
|
|
w = httptest.NewRecorder()
|
|
s.handleReport(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("正确token应200, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
s.mu.RLock()
|
|
sd := s.sites["s1"]
|
|
s.mu.RUnlock()
|
|
if sd == nil {
|
|
t.Fatal("站点未创建")
|
|
}
|
|
sd.mu.RLock()
|
|
if len(sd.devices) != 1 {
|
|
t.Fatalf("设备应1台, got %d", len(sd.devices))
|
|
}
|
|
if len(sd.events) != 1 {
|
|
t.Fatalf("事件应1条, got %d", len(sd.events))
|
|
}
|
|
sd.mu.RUnlock()
|
|
}
|
|
|
|
// 测试: 快照覆盖状态(离线设备出现在快照后状态更新)
|
|
func TestReportSnapshotMerge(t *testing.T) {
|
|
s := &Server{
|
|
dataDir: t.TempDir(),
|
|
sites: make(map[string]*SiteData),
|
|
}
|
|
sd := &SiteData{name: "s1", devices: make(map[string]*Device)}
|
|
s.mu.Lock()
|
|
s.sites["s1"] = sd
|
|
s.mu.Unlock()
|
|
|
|
// 第一上报: 在线
|
|
dev1 := &Device{MAC: "AA:BB", Name: "Dev", Status: evOnline, OnlineCount: 1}
|
|
sd.mu.Lock()
|
|
sd.devices["AA:BB"] = dev1
|
|
sd.mu.Unlock()
|
|
|
|
// 第二上报: 快照说该设备离线
|
|
payload := ReportPayload{
|
|
Site: "s1",
|
|
Token: "",
|
|
Snapshot: &ReportSnapshot{Devices: []*Device{{MAC: "AA:BB", Name: "Dev", Status: evOffline}}},
|
|
}
|
|
body, _ := json.Marshal(payload)
|
|
req := httptest.NewRequest(http.MethodPost, "/api/report", bytes.NewBuffer(body))
|
|
w := httptest.NewRecorder()
|
|
s.handleReport(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("应200, got %d", w.Code)
|
|
}
|
|
sd.mu.RLock()
|
|
got := sd.devices["AA:BB"]
|
|
sd.mu.RUnlock()
|
|
if got == nil || got.Status != evOffline {
|
|
t.Fatalf("设备状态应被快照覆盖为离线, got %+v", got)
|
|
}
|
|
}
|
|
|
|
// 测试: 多站点隔离
|
|
func TestMultiSite(t *testing.T) {
|
|
s := &Server{
|
|
dataDir: t.TempDir(),
|
|
sites: make(map[string]*SiteData),
|
|
}
|
|
report := func(site, mac string) {
|
|
payload := ReportPayload{
|
|
Site: site,
|
|
Snapshot: &ReportSnapshot{
|
|
Devices: []*Device{{MAC: mac, Name: site, Status: evOnline}},
|
|
},
|
|
}
|
|
body, _ := json.Marshal(payload)
|
|
req := httptest.NewRequest(http.MethodPost, "/api/report", bytes.NewBuffer(body))
|
|
s.handleReport(httptest.NewRecorder(), req)
|
|
}
|
|
report("home", "AA:01")
|
|
report("office", "BB:02")
|
|
|
|
s.mu.RLock()
|
|
if len(s.sites) != 2 {
|
|
t.Fatalf("应2个站点, got %d", len(s.sites))
|
|
}
|
|
s.mu.RUnlock()
|
|
}
|
|
|
|
// 测试 authOK 的 token 校验(header 与 query)
|
|
func TestAuthOK(t *testing.T) {
|
|
s := &Server{token: "secret"}
|
|
if !s.authOK(httptest.NewRequest("GET", "/api/sites?token=secret", nil)) {
|
|
t.Fatal("query token 应通过")
|
|
}
|
|
r := httptest.NewRequest("GET", "/api/sites", nil)
|
|
r.Header.Set("X-Token", "secret")
|
|
if !s.authOK(r) {
|
|
t.Fatal("header token 应通过")
|
|
}
|
|
if s.authOK(httptest.NewRequest("GET", "/api/sites", nil)) {
|
|
t.Fatal("无token应拒绝")
|
|
}
|
|
}
|