Files
Charon/backend/internal/api/handlers/logs_handler_coverage_test.go
GitHub Actions 63cebf07ab Refactor services and improve error handling
- Updated file permissions in certificate_service_test.go and log_service_test.go to use octal notation.
- Added a new doc.go file to document the services package.
- Enhanced error handling in docker_service.go, log_service.go, notification_service.go, proxyhost_service.go, remoteserver_service.go, update_service.go, and uptime_service.go by logging errors when closing resources.
- Improved log_service.go to simplify log file processing and deduplication.
- Introduced CRUD tests for notification templates in notification_service_template_test.go.
- Removed the obsolete python_compile_check.sh script.
- Updated notification_service.go to improve template management functions.
- Added tests for uptime service notifications in uptime_service_notification_test.go.
2025-12-08 05:55:17 +00:00

196 lines
5.7 KiB
Go

package handlers
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/Wikid82/charon/backend/internal/config"
"github.com/Wikid82/charon/backend/internal/services"
)
func TestLogsHandler_Read_FilterBySearch(t *testing.T) {
gin.SetMode(gin.TestMode)
tmpDir := t.TempDir()
dataDir := filepath.Join(tmpDir, "data")
os.MkdirAll(dataDir, 0o755)
dbPath := filepath.Join(dataDir, "charon.db")
logsDir := filepath.Join(dataDir, "logs")
os.MkdirAll(logsDir, 0o755)
// Write JSON log lines
content := `{"level":"info","ts":1600000000,"msg":"request handled","request":{"method":"GET","host":"example.com","uri":"/api/search","remote_ip":"1.2.3.4"},"status":200}
{"level":"error","ts":1600000060,"msg":"error occurred","request":{"method":"POST","host":"example.com","uri":"/api/submit","remote_ip":"5.6.7.8"},"status":500}
`
os.WriteFile(filepath.Join(logsDir, "access.log"), []byte(content), 0o644)
cfg := &config.Config{DatabasePath: dbPath}
svc := services.NewLogService(cfg)
h := NewLogsHandler(svc)
// Test with search filter
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Params = gin.Params{{Key: "filename", Value: "access.log"}}
c.Request = httptest.NewRequest("GET", "/logs/access.log?search=error", http.NoBody)
h.Read(c)
assert.Equal(t, 200, w.Code)
assert.Contains(t, w.Body.String(), "error")
}
func TestLogsHandler_Read_FilterByHost(t *testing.T) {
gin.SetMode(gin.TestMode)
tmpDir := t.TempDir()
dataDir := filepath.Join(tmpDir, "data")
os.MkdirAll(dataDir, 0o755)
dbPath := filepath.Join(dataDir, "charon.db")
logsDir := filepath.Join(dataDir, "logs")
os.MkdirAll(logsDir, 0o755)
content := `{"level":"info","ts":1600000000,"msg":"request handled","request":{"method":"GET","host":"example.com","uri":"/","remote_ip":"1.2.3.4"},"status":200}
{"level":"info","ts":1600000001,"msg":"request handled","request":{"method":"GET","host":"other.com","uri":"/","remote_ip":"1.2.3.4"},"status":200}
`
os.WriteFile(filepath.Join(logsDir, "access.log"), []byte(content), 0o644)
cfg := &config.Config{DatabasePath: dbPath}
svc := services.NewLogService(cfg)
h := NewLogsHandler(svc)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Params = gin.Params{{Key: "filename", Value: "access.log"}}
c.Request = httptest.NewRequest("GET", "/logs/access.log?host=example.com", http.NoBody)
h.Read(c)
assert.Equal(t, 200, w.Code)
}
func TestLogsHandler_Read_FilterByLevel(t *testing.T) {
gin.SetMode(gin.TestMode)
tmpDir := t.TempDir()
dataDir := filepath.Join(tmpDir, "data")
os.MkdirAll(dataDir, 0o755)
dbPath := filepath.Join(dataDir, "charon.db")
logsDir := filepath.Join(dataDir, "logs")
os.MkdirAll(logsDir, 0o755)
content := `{"level":"info","ts":1600000000,"msg":"info message"}
{"level":"error","ts":1600000001,"msg":"error message"}
`
os.WriteFile(filepath.Join(logsDir, "access.log"), []byte(content), 0o644)
cfg := &config.Config{DatabasePath: dbPath}
svc := services.NewLogService(cfg)
h := NewLogsHandler(svc)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Params = gin.Params{{Key: "filename", Value: "access.log"}}
c.Request = httptest.NewRequest("GET", "/logs/access.log?level=error", http.NoBody)
h.Read(c)
assert.Equal(t, 200, w.Code)
}
func TestLogsHandler_Read_FilterByStatus(t *testing.T) {
gin.SetMode(gin.TestMode)
tmpDir := t.TempDir()
dataDir := filepath.Join(tmpDir, "data")
os.MkdirAll(dataDir, 0o755)
dbPath := filepath.Join(dataDir, "charon.db")
logsDir := filepath.Join(dataDir, "logs")
os.MkdirAll(logsDir, 0o755)
content := `{"level":"info","ts":1600000000,"msg":"200 OK","request":{"host":"example.com"},"status":200}
{"level":"error","ts":1600000001,"msg":"500 Error","request":{"host":"example.com"},"status":500}
`
os.WriteFile(filepath.Join(logsDir, "access.log"), []byte(content), 0o644)
cfg := &config.Config{DatabasePath: dbPath}
svc := services.NewLogService(cfg)
h := NewLogsHandler(svc)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Params = gin.Params{{Key: "filename", Value: "access.log"}}
c.Request = httptest.NewRequest("GET", "/logs/access.log?status=500", http.NoBody)
h.Read(c)
assert.Equal(t, 200, w.Code)
}
func TestLogsHandler_Read_SortAsc(t *testing.T) {
gin.SetMode(gin.TestMode)
tmpDir := t.TempDir()
dataDir := filepath.Join(tmpDir, "data")
os.MkdirAll(dataDir, 0o755)
dbPath := filepath.Join(dataDir, "charon.db")
logsDir := filepath.Join(dataDir, "logs")
os.MkdirAll(logsDir, 0o755)
content := `{"level":"info","ts":1600000000,"msg":"first"}
{"level":"info","ts":1600000001,"msg":"second"}
`
os.WriteFile(filepath.Join(logsDir, "access.log"), []byte(content), 0o644)
cfg := &config.Config{DatabasePath: dbPath}
svc := services.NewLogService(cfg)
h := NewLogsHandler(svc)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Params = gin.Params{{Key: "filename", Value: "access.log"}}
c.Request = httptest.NewRequest("GET", "/logs/access.log?sort=asc", http.NoBody)
h.Read(c)
assert.Equal(t, 200, w.Code)
}
func TestLogsHandler_List_DirectoryIsFile(t *testing.T) {
gin.SetMode(gin.TestMode)
tmpDir := t.TempDir()
dataDir := filepath.Join(tmpDir, "data")
os.MkdirAll(dataDir, 0o755)
dbPath := filepath.Join(dataDir, "charon.db")
logsDir := filepath.Join(dataDir, "logs")
// Create logs dir as a file to cause error
os.WriteFile(logsDir, []byte("not a dir"), 0o644)
cfg := &config.Config{DatabasePath: dbPath}
svc := services.NewLogService(cfg)
h := NewLogsHandler(svc)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest("GET", "/logs", http.NoBody)
h.List(c)
// Service may handle this gracefully or error
assert.Contains(t, []int{200, 500}, w.Code)
}