Files
Charon/backend/internal/api/handlers/notification_template_handler_test.go
CI 5cea5755a0 feat: add external notification templates management
- Introduced NotificationTemplate model for reusable external notification templates.
- Implemented CRUD operations for external templates in NotificationService.
- Added routes for managing external templates in the API.
- Created frontend API methods for external templates.
- Enhanced Notifications page to manage external templates with a form and list view.
- Updated layout and login pages to improve UI consistency.
- Added integration tests for proxy host management with improved error handling.
2025-11-29 20:51:46 +00:00

53 lines
1.4 KiB
Go

package handlers
import (
"encoding/json"
"net/http"
"net/http/httptest"
"io"
"testing"
"strings"
"github.com/Wikid82/charon/backend/internal/models"
"github.com/Wikid82/charon/backend/internal/services"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func setupDB(t *testing.T) *gorm.DB {
db, err := gorm.Open(sqlite.Open("file::memory:"), &gorm.Config{})
require.NoError(t, err)
db.AutoMigrate(&models.NotificationTemplate{})
return db
}
func TestNotificationTemplateCRUD(t *testing.T) {
db := setupDB(t)
svc := services.NewNotificationService(db)
h := NewNotificationTemplateHandler(svc)
// Create
payload := `{"name":"Simple","config":"{\"title\": \"{{.Title}}\"}","template":"custom"}`
req := httptest.NewRequest("POST", "/", nil)
req.Body = io.NopCloser(strings.NewReader(payload))
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = req
h.Create(c)
require.Equal(t, http.StatusCreated, w.Code)
// List
req2 := httptest.NewRequest("GET", "/", nil)
w2 := httptest.NewRecorder()
c2, _ := gin.CreateTestContext(w2)
c2.Request = req2
h.List(c2)
require.Equal(t, http.StatusOK, w2.Code)
var list []models.NotificationTemplate
require.NoError(t, json.Unmarshal(w2.Body.Bytes(), &list))
require.Len(t, list, 1)
}