feat: 实现账号设备分组与命令日志
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package nixmsg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestClientCreatesAndReadsEndpoint(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
online := map[string]bool{}
|
||||
var sawCSRF bool
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("POST /api/admin/login", func(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.Password != "nix-secret" || req.Username != "admin" {
|
||||
http.Error(w, "no", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
http.SetCookie(w, &http.Cookie{Name: "nixmsg_admin", Value: "session", Path: "/"})
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"ok": true, "data": map[string]any{"username": "admin"}})
|
||||
})
|
||||
mux.HandleFunc("POST /api/admin/endpoints", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("X-Nixmsg-Request") != "1" {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"ok": false, "error": map[string]string{"code": "forbidden", "message": "missing header"}})
|
||||
return
|
||||
}
|
||||
c, err := r.Cookie("nixmsg_admin")
|
||||
if err != nil || c.Value == "" {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"ok": false, "error": map[string]string{"code": "unauthorized", "message": "no"}})
|
||||
return
|
||||
}
|
||||
mu.Lock()
|
||||
sawCSRF = true
|
||||
online["e_abc"] = true
|
||||
mu.Unlock()
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"ok": true, "data": map[string]any{"id": "e_abc", "login_password": "OnceFromNix12345"}})
|
||||
})
|
||||
mux.HandleFunc("GET /api/admin/endpoints/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
mu.Lock()
|
||||
on := online[r.PathValue("id")]
|
||||
_, ok := online[r.PathValue("id")]
|
||||
mu.Unlock()
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"ok": false, "error": map[string]string{"code": "not_found", "message": "missing"}})
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"ok": true, "data": map[string]any{"id": r.PathValue("id"), "online": on}})
|
||||
})
|
||||
srv := httptest.NewServer(mux)
|
||||
defer srv.Close()
|
||||
|
||||
client, err := New(srv.URL, "admin", "nix-secret")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ep, err := client.CreateEndpoint(context.Background(), "客厅")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ep.ID != "e_abc" || ep.LoginPassword != "OnceFromNix12345" {
|
||||
t.Fatalf("endpoint %+v", ep)
|
||||
}
|
||||
got, err := client.Lookup(context.Background(), "e_abc")
|
||||
if err != nil || !got.Found || !got.Online {
|
||||
t.Fatalf("lookup %+v %v", got, err)
|
||||
}
|
||||
missing, err := client.Lookup(context.Background(), "e_missing")
|
||||
if err != nil || missing.Found {
|
||||
t.Fatalf("missing %+v %v", missing, err)
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if !sawCSRF {
|
||||
t.Fatal("csrf header was not sent")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user