87 lines
2.8 KiB
Go
87 lines
2.8 KiB
Go
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["00000001"] = true
|
|
mu.Unlock()
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"ok": true, "data": map[string]any{"id": "00000001", "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(), "00000001", "客厅")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ep.ID != "00000001" || ep.LoginPassword != "OnceFromNix12345" {
|
|
t.Fatalf("endpoint %+v", ep)
|
|
}
|
|
got, err := client.Lookup(context.Background(), "00000001")
|
|
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")
|
|
}
|
|
}
|