Files
bee/audit/internal/webui/api_system_test.go

51 lines
1.5 KiB
Go

package webui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"bee/audit/internal/app"
)
func TestSystemTimeEndpointReportsEpochLocalTimeAndTimezone(t *testing.T) {
before := time.Now().Add(-time.Second).UnixMilli()
rec := httptest.NewRecorder()
NewHandler(HandlerOptions{}).ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/system/time", nil))
after := time.Now().Add(time.Second).UnixMilli()
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
}
var body struct {
EpochMS int64 `json:"epoch_ms"`
LocalTime string `json:"local_time"`
Timezone string `json:"timezone"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("decode response: %v", err)
}
if body.EpochMS < before || body.EpochMS > after {
t.Fatalf("epoch_ms = %d, want value in [%d, %d]", body.EpochMS, before, after)
}
if _, err := time.ParseInLocation("2006-01-02 15:04:05", body.LocalTime, time.Local); err != nil {
t.Fatalf("local_time = %q: %v", body.LocalTime, err)
}
if strings.TrimSpace(body.Timezone) == "" {
t.Fatal("timezone must not be empty")
}
}
func TestNetworkDHCPRejectsMalformedJSONBeforeChangingNetwork(t *testing.T) {
h := &handler{opts: HandlerOptions{App: &app.App{}}}
rec := httptest.NewRecorder()
h.handleAPINetworkDHCP(rec, httptest.NewRequest(http.MethodPost, "/api/network/dhcp", strings.NewReader("{")))
if rec.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusBadRequest)
}
}