Files
Charon/backend/internal/api/handlers/coverage_quick_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

100 lines
2.8 KiB
Go

package handlers
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/Wikid82/charon/backend/internal/services"
"github.com/gin-gonic/gin"
)
// Use a real BackupService, but point it at tmpDir for isolation
func TestBackupHandlerQuick(t *testing.T) {
gin.SetMode(gin.TestMode)
tmpDir := t.TempDir()
// prepare a fake "database" so CreateBackup can find it
dbPath := filepath.Join(tmpDir, "db.sqlite")
if err := os.WriteFile(dbPath, []byte("db"), 0o644); err != nil {
t.Fatalf("failed to create tmp db: %v", err)
}
svc := &services.BackupService{DataDir: tmpDir, BackupDir: tmpDir, DatabaseName: "db.sqlite", Cron: nil}
h := NewBackupHandler(svc)
r := gin.New()
// register routes used
r.GET("/backups", h.List)
r.POST("/backups", h.Create)
r.DELETE("/backups/:filename", h.Delete)
r.GET("/backups/:filename", h.Download)
r.POST("/backups/:filename/restore", h.Restore)
// List
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/backups", http.NoBody)
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
// Create (backup)
w2 := httptest.NewRecorder()
req2 := httptest.NewRequest(http.MethodPost, "/backups", http.NoBody)
r.ServeHTTP(w2, req2)
if w2.Code != http.StatusCreated {
t.Fatalf("create expected 201 got %d", w2.Code)
}
var createResp struct {
Filename string `json:"filename"`
}
if err := json.Unmarshal(w2.Body.Bytes(), &createResp); err != nil {
t.Fatalf("invalid create json: %v", err)
}
// Delete missing
w3 := httptest.NewRecorder()
req3 := httptest.NewRequest(http.MethodDelete, "/backups/missing", http.NoBody)
r.ServeHTTP(w3, req3)
if w3.Code != http.StatusNotFound {
t.Fatalf("delete missing expected 404 got %d", w3.Code)
}
// Download missing
w4 := httptest.NewRecorder()
req4 := httptest.NewRequest(http.MethodGet, "/backups/missing", http.NoBody)
r.ServeHTTP(w4, req4)
if w4.Code != http.StatusNotFound {
t.Fatalf("download missing expected 404 got %d", w4.Code)
}
// Download present (use filename returned from create)
w5 := httptest.NewRecorder()
req5 := httptest.NewRequest(http.MethodGet, "/backups/"+createResp.Filename, http.NoBody)
r.ServeHTTP(w5, req5)
if w5.Code != http.StatusOK {
t.Fatalf("download expected 200 got %d", w5.Code)
}
// Restore missing
w6 := httptest.NewRecorder()
req6 := httptest.NewRequest(http.MethodPost, "/backups/missing/restore", http.NoBody)
r.ServeHTTP(w6, req6)
if w6.Code != http.StatusNotFound {
t.Fatalf("restore missing expected 404 got %d", w6.Code)
}
// Restore ok
w7 := httptest.NewRecorder()
req7 := httptest.NewRequest(http.MethodPost, "/backups/"+createResp.Filename+"/restore", http.NoBody)
r.ServeHTTP(w7, req7)
if w7.Code != http.StatusOK {
t.Fatalf("restore expected 200 got %d", w7.Code)
}
}