| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- package admin
- import (
- "bytes"
- "context"
- "encoding/json"
- "net/http"
- "net/http/httptest"
- "testing"
- "github.com/Wei-Shaw/sub2api/internal/config"
- "github.com/Wei-Shaw/sub2api/internal/server/middleware"
- "github.com/Wei-Shaw/sub2api/internal/service"
- "github.com/gin-gonic/gin"
- )
- type responseEnvelope struct {
- Code int `json:"code"`
- Message string `json:"message"`
- Data json.RawMessage `json:"data"`
- }
- type opsSystemLogCaptureRepo struct {
- service.OpsRepository
- listFilter *service.OpsSystemLogFilter
- cleanupFilter *service.OpsSystemLogCleanupFilter
- }
- func (r *opsSystemLogCaptureRepo) ListSystemLogs(_ context.Context, filter *service.OpsSystemLogFilter) (*service.OpsSystemLogList, error) {
- r.listFilter = filter
- return &service.OpsSystemLogList{Logs: []*service.OpsSystemLog{}, Page: filter.Page, PageSize: filter.PageSize}, nil
- }
- func (r *opsSystemLogCaptureRepo) DeleteSystemLogs(_ context.Context, filter *service.OpsSystemLogCleanupFilter) (int64, error) {
- r.cleanupFilter = filter
- return 1, nil
- }
- func (r *opsSystemLogCaptureRepo) InsertSystemLogCleanupAudit(_ context.Context, _ *service.OpsSystemLogCleanupAudit) error {
- return nil
- }
- func newOpsSystemLogTestRouter(handler *OpsHandler, withUser bool) *gin.Engine {
- gin.SetMode(gin.TestMode)
- r := gin.New()
- if withUser {
- r.Use(func(c *gin.Context) {
- c.Set(string(middleware.ContextKeyUser), middleware.AuthSubject{UserID: 99})
- c.Next()
- })
- }
- r.GET("/logs", handler.ListSystemLogs)
- r.POST("/logs/cleanup", handler.CleanupSystemLogs)
- r.GET("/logs/health", handler.GetSystemLogIngestionHealth)
- return r
- }
- func TestOpsSystemLogHandler_ListUnavailable(t *testing.T) {
- h := NewOpsHandler(nil)
- r := newOpsSystemLogTestRouter(h, false)
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodGet, "/logs", nil)
- r.ServeHTTP(w, req)
- if w.Code != http.StatusServiceUnavailable {
- t.Fatalf("status=%d, want 503", w.Code)
- }
- }
- func TestOpsSystemLogHandler_ListInvalidUserID(t *testing.T) {
- svc := service.NewOpsService(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
- h := NewOpsHandler(svc)
- r := newOpsSystemLogTestRouter(h, false)
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodGet, "/logs?user_id=abc", nil)
- r.ServeHTTP(w, req)
- if w.Code != http.StatusBadRequest {
- t.Fatalf("status=%d, want 400", w.Code)
- }
- }
- func TestOpsSystemLogHandler_ListInvalidAccountID(t *testing.T) {
- svc := service.NewOpsService(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
- h := NewOpsHandler(svc)
- r := newOpsSystemLogTestRouter(h, false)
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodGet, "/logs?account_id=-1", nil)
- r.ServeHTTP(w, req)
- if w.Code != http.StatusBadRequest {
- t.Fatalf("status=%d, want 400", w.Code)
- }
- }
- func TestOpsSystemLogHandler_ListInvalidAPIKeyID(t *testing.T) {
- svc := service.NewOpsService(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
- h := NewOpsHandler(svc)
- r := newOpsSystemLogTestRouter(h, false)
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodGet, "/logs?api_key_id=abc", nil)
- r.ServeHTTP(w, req)
- if w.Code != http.StatusBadRequest {
- t.Fatalf("status=%d, want 400", w.Code)
- }
- }
- func TestOpsSystemLogHandler_ListMonitoringDisabled(t *testing.T) {
- svc := service.NewOpsService(nil, nil, &config.Config{
- Ops: config.OpsConfig{Enabled: false},
- }, nil, nil, nil, nil, nil, nil, nil, nil)
- h := NewOpsHandler(svc)
- r := newOpsSystemLogTestRouter(h, false)
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodGet, "/logs", nil)
- r.ServeHTTP(w, req)
- if w.Code != http.StatusNotFound {
- t.Fatalf("status=%d, want 404", w.Code)
- }
- }
- func TestOpsSystemLogHandler_ListSuccess(t *testing.T) {
- svc := service.NewOpsService(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
- h := NewOpsHandler(svc)
- r := newOpsSystemLogTestRouter(h, false)
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodGet, "/logs?time_range=30m&page=1&page_size=20", nil)
- r.ServeHTTP(w, req)
- if w.Code != http.StatusOK {
- t.Fatalf("status=%d, want 200", w.Code)
- }
- var resp responseEnvelope
- if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
- t.Fatalf("unmarshal response: %v", err)
- }
- if resp.Code != 0 {
- t.Fatalf("unexpected response code: %+v", resp)
- }
- }
- func TestOpsSystemLogHandler_ListAcceptsHost(t *testing.T) {
- repo := &opsSystemLogCaptureRepo{}
- svc := service.NewOpsService(repo, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
- h := NewOpsHandler(svc)
- r := newOpsSystemLogTestRouter(h, false)
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodGet, "/logs?host=api-node-1", nil)
- r.ServeHTTP(w, req)
- if w.Code != http.StatusOK {
- t.Fatalf("status=%d, want 200", w.Code)
- }
- if repo.listFilter == nil || repo.listFilter.Host != "api-node-1" {
- t.Fatalf("host filter = %+v, want api-node-1", repo.listFilter)
- }
- }
- func TestOpsSystemLogHandler_CleanupUnauthorized(t *testing.T) {
- svc := service.NewOpsService(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
- h := NewOpsHandler(svc)
- r := newOpsSystemLogTestRouter(h, false)
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodPost, "/logs/cleanup", bytes.NewBufferString(`{"request_id":"r1"}`))
- req.Header.Set("Content-Type", "application/json")
- r.ServeHTTP(w, req)
- if w.Code != http.StatusUnauthorized {
- t.Fatalf("status=%d, want 401", w.Code)
- }
- }
- func TestOpsSystemLogHandler_CleanupInvalidPayload(t *testing.T) {
- svc := service.NewOpsService(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
- h := NewOpsHandler(svc)
- r := newOpsSystemLogTestRouter(h, true)
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodPost, "/logs/cleanup", bytes.NewBufferString(`{bad-json`))
- req.Header.Set("Content-Type", "application/json")
- r.ServeHTTP(w, req)
- if w.Code != http.StatusBadRequest {
- t.Fatalf("status=%d, want 400", w.Code)
- }
- }
- func TestOpsSystemLogHandler_CleanupInvalidTime(t *testing.T) {
- svc := service.NewOpsService(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
- h := NewOpsHandler(svc)
- r := newOpsSystemLogTestRouter(h, true)
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodPost, "/logs/cleanup", bytes.NewBufferString(`{"start_time":"bad","request_id":"r1"}`))
- req.Header.Set("Content-Type", "application/json")
- r.ServeHTTP(w, req)
- if w.Code != http.StatusBadRequest {
- t.Fatalf("status=%d, want 400", w.Code)
- }
- }
- func TestOpsSystemLogHandler_CleanupInvalidEndTime(t *testing.T) {
- svc := service.NewOpsService(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
- h := NewOpsHandler(svc)
- r := newOpsSystemLogTestRouter(h, true)
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodPost, "/logs/cleanup", bytes.NewBufferString(`{"end_time":"bad","request_id":"r1"}`))
- req.Header.Set("Content-Type", "application/json")
- r.ServeHTTP(w, req)
- if w.Code != http.StatusBadRequest {
- t.Fatalf("status=%d, want 400", w.Code)
- }
- }
- func TestOpsSystemLogHandler_CleanupServiceUnavailable(t *testing.T) {
- svc := service.NewOpsService(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
- h := NewOpsHandler(svc)
- r := newOpsSystemLogTestRouter(h, true)
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodPost, "/logs/cleanup", bytes.NewBufferString(`{"request_id":"r1"}`))
- req.Header.Set("Content-Type", "application/json")
- r.ServeHTTP(w, req)
- if w.Code != http.StatusServiceUnavailable {
- t.Fatalf("status=%d, want 503", w.Code)
- }
- }
- func TestOpsSystemLogHandler_CleanupAcceptsAPIKeyID(t *testing.T) {
- svc := service.NewOpsService(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
- h := NewOpsHandler(svc)
- r := newOpsSystemLogTestRouter(h, true)
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodPost, "/logs/cleanup", bytes.NewBufferString(`{"api_key_id":123}`))
- req.Header.Set("Content-Type", "application/json")
- r.ServeHTTP(w, req)
- if w.Code != http.StatusServiceUnavailable {
- t.Fatalf("status=%d, want 503", w.Code)
- }
- }
- func TestOpsSystemLogHandler_CleanupAcceptsHost(t *testing.T) {
- repo := &opsSystemLogCaptureRepo{}
- svc := service.NewOpsService(repo, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
- h := NewOpsHandler(svc)
- r := newOpsSystemLogTestRouter(h, true)
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodPost, "/logs/cleanup", bytes.NewBufferString(`{"host":"api-node-1"}`))
- req.Header.Set("Content-Type", "application/json")
- r.ServeHTTP(w, req)
- if w.Code != http.StatusOK {
- t.Fatalf("status=%d, want 200", w.Code)
- }
- if repo.cleanupFilter == nil || repo.cleanupFilter.Host != "api-node-1" {
- t.Fatalf("host filter = %+v, want api-node-1", repo.cleanupFilter)
- }
- }
- func TestOpsSystemLogHandler_CleanupInvalidAPIKeyID(t *testing.T) {
- svc := service.NewOpsService(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
- h := NewOpsHandler(svc)
- r := newOpsSystemLogTestRouter(h, true)
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodPost, "/logs/cleanup", bytes.NewBufferString(`{"api_key_id":0}`))
- req.Header.Set("Content-Type", "application/json")
- r.ServeHTTP(w, req)
- if w.Code != http.StatusBadRequest {
- t.Fatalf("status=%d, want 400", w.Code)
- }
- }
- func TestOpsSystemLogHandler_CleanupMonitoringDisabled(t *testing.T) {
- svc := service.NewOpsService(nil, nil, &config.Config{
- Ops: config.OpsConfig{Enabled: false},
- }, nil, nil, nil, nil, nil, nil, nil, nil)
- h := NewOpsHandler(svc)
- r := newOpsSystemLogTestRouter(h, true)
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodPost, "/logs/cleanup", bytes.NewBufferString(`{"request_id":"r1"}`))
- req.Header.Set("Content-Type", "application/json")
- r.ServeHTTP(w, req)
- if w.Code != http.StatusNotFound {
- t.Fatalf("status=%d, want 404", w.Code)
- }
- }
- func TestOpsSystemLogHandler_Health(t *testing.T) {
- sink := service.NewOpsSystemLogSink(nil)
- svc := service.NewOpsService(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, sink)
- h := NewOpsHandler(svc)
- r := newOpsSystemLogTestRouter(h, false)
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodGet, "/logs/health", nil)
- r.ServeHTTP(w, req)
- if w.Code != http.StatusOK {
- t.Fatalf("status=%d, want 200", w.Code)
- }
- }
- func TestOpsSystemLogHandler_HealthUnavailableAndMonitoringDisabled(t *testing.T) {
- h := NewOpsHandler(nil)
- r := newOpsSystemLogTestRouter(h, false)
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodGet, "/logs/health", nil)
- r.ServeHTTP(w, req)
- if w.Code != http.StatusServiceUnavailable {
- t.Fatalf("status=%d, want 503", w.Code)
- }
- svc := service.NewOpsService(nil, nil, &config.Config{
- Ops: config.OpsConfig{Enabled: false},
- }, nil, nil, nil, nil, nil, nil, nil, nil)
- h = NewOpsHandler(svc)
- r = newOpsSystemLogTestRouter(h, false)
- w = httptest.NewRecorder()
- req = httptest.NewRequest(http.MethodGet, "/logs/health", nil)
- r.ServeHTTP(w, req)
- if w.Code != http.StatusNotFound {
- t.Fatalf("status=%d, want 404", w.Code)
- }
- }
|