eec8c28fb3
Go Benchmark / Performance Regression Check (push) Has been cancelled
Cerberus Integration / Cerberus Security Stack Integration (push) Has been cancelled
Upload Coverage to Codecov / Backend Codecov Upload (push) Has been cancelled
Upload Coverage to Codecov / Frontend Codecov Upload (push) Has been cancelled
CodeQL - Analyze / CodeQL analysis (go) (push) Has been cancelled
CodeQL - Analyze / CodeQL analysis (javascript-typescript) (push) Has been cancelled
CrowdSec Integration / CrowdSec Bouncer Integration (push) Has been cancelled
Docker Build, Publish & Test / build-and-push (push) Has been cancelled
Quality Checks / Auth Route Protection Contract (push) Has been cancelled
Quality Checks / Codecov Trigger/Comment Parity Guard (push) Has been cancelled
Quality Checks / Backend (Go) (push) Has been cancelled
Quality Checks / Frontend (React) (push) Has been cancelled
Rate Limit integration / Rate Limiting Integration (push) Has been cancelled
Security Scan (PR) / Trivy Binary Scan (push) Has been cancelled
Supply Chain Verification (PR) / Verify Supply Chain (push) Has been cancelled
WAF integration / Coraza WAF Integration (push) Has been cancelled
Docker Build, Publish & Test / Security Scan PR Image (push) Has been cancelled
Repo Health Check / Repo health (push) Has been cancelled
History Rewrite Dry-Run / Dry-run preview for history rewrite (push) Has been cancelled
Prune Renovate Branches / prune (push) Has been cancelled
Renovate / renovate (push) Has been cancelled
Nightly Build & Package / sync-development-to-nightly (push) Has been cancelled
Nightly Build & Package / Trigger Nightly Validation Workflows (push) Has been cancelled
Nightly Build & Package / build-and-push-nightly (push) Has been cancelled
Nightly Build & Package / test-nightly-image (push) Has been cancelled
Nightly Build & Package / verify-nightly-supply-chain (push) Has been cancelled
Update GeoLite2 Checksum / update-checksum (push) Has been cancelled
Container Registry Prune / prune-ghcr (push) Has been cancelled
Container Registry Prune / prune-dockerhub (push) Has been cancelled
Container Registry Prune / summarize (push) Has been cancelled
Supply Chain Verification / Verify SBOM (push) Has been cancelled
Supply Chain Verification / Verify Release Artifacts (push) Has been cancelled
Supply Chain Verification / Verify Docker Image Supply Chain (push) Has been cancelled
Monitor Caddy Major Release / check-caddy-major (push) Has been cancelled
Weekly Nightly to Main Promotion / Verify Nightly Branch Health (push) Has been cancelled
Weekly Nightly to Main Promotion / Create Promotion PR (push) Has been cancelled
Weekly Nightly to Main Promotion / Trigger Missing Required Checks (push) Has been cancelled
Weekly Nightly to Main Promotion / Notify on Failure (push) Has been cancelled
Weekly Nightly to Main Promotion / Workflow Summary (push) Has been cancelled
Weekly Security Rebuild / Security Rebuild & Scan (push) Has been cancelled
161 lines
5.2 KiB
Go
Executable File
161 lines
5.2 KiB
Go
Executable File
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/require"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/Wikid82/charon/backend/internal/models"
|
|
"github.com/Wikid82/charon/backend/internal/services"
|
|
)
|
|
|
|
func setupDomainTestRouter(t *testing.T) (*gin.Engine, *gorm.DB) {
|
|
t.Helper()
|
|
|
|
dsn := "file:" + t.Name() + "?mode=memory&cache=shared"
|
|
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
|
|
require.NoError(t, err)
|
|
require.NoError(t, db.AutoMigrate(&models.Domain{}, &models.Notification{}, &models.NotificationProvider{}))
|
|
|
|
ns := services.NewNotificationService(db, nil)
|
|
h := NewDomainHandler(db, ns)
|
|
r := gin.New()
|
|
|
|
// Manually register routes since DomainHandler doesn't have a RegisterRoutes method yet
|
|
// or we can just register them here for testing
|
|
r.GET("/api/v1/domains", h.List)
|
|
r.POST("/api/v1/domains", h.Create)
|
|
r.DELETE("/api/v1/domains/:id", h.Delete)
|
|
|
|
return r, db
|
|
}
|
|
|
|
func TestDomainLifecycle(t *testing.T) {
|
|
router, _ := setupDomainTestRouter(t)
|
|
|
|
// 1. Create Domain
|
|
body := `{"name":"example.com"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/domains", strings.NewReader(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp := httptest.NewRecorder()
|
|
router.ServeHTTP(resp, req)
|
|
require.Equal(t, http.StatusCreated, resp.Code)
|
|
|
|
var created models.Domain
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &created))
|
|
require.Equal(t, "example.com", created.Name)
|
|
require.NotEmpty(t, created.UUID)
|
|
|
|
// 2. List Domains
|
|
req = httptest.NewRequest(http.MethodGet, "/api/v1/domains", http.NoBody)
|
|
resp = httptest.NewRecorder()
|
|
router.ServeHTTP(resp, req)
|
|
require.Equal(t, http.StatusOK, resp.Code)
|
|
|
|
var list []models.Domain
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &list))
|
|
require.Len(t, list, 1)
|
|
require.Equal(t, "example.com", list[0].Name)
|
|
|
|
// 3. Delete Domain
|
|
req = httptest.NewRequest(http.MethodDelete, "/api/v1/domains/"+created.UUID, http.NoBody)
|
|
resp = httptest.NewRecorder()
|
|
router.ServeHTTP(resp, req)
|
|
require.Equal(t, http.StatusOK, resp.Code)
|
|
|
|
// 4. Verify Deletion
|
|
req = httptest.NewRequest(http.MethodGet, "/api/v1/domains", http.NoBody)
|
|
resp = httptest.NewRecorder()
|
|
router.ServeHTTP(resp, req)
|
|
require.Equal(t, http.StatusOK, resp.Code)
|
|
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &list))
|
|
require.Len(t, list, 0)
|
|
}
|
|
|
|
func TestDomainErrors(t *testing.T) {
|
|
router, _ := setupDomainTestRouter(t)
|
|
|
|
// 1. Create Invalid JSON
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/domains", strings.NewReader(`{invalid}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp := httptest.NewRecorder()
|
|
router.ServeHTTP(resp, req)
|
|
require.Equal(t, http.StatusBadRequest, resp.Code)
|
|
|
|
// 2. Create Missing Name
|
|
req = httptest.NewRequest(http.MethodPost, "/api/v1/domains", strings.NewReader(`{}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp = httptest.NewRecorder()
|
|
router.ServeHTTP(resp, req)
|
|
require.Equal(t, http.StatusBadRequest, resp.Code)
|
|
}
|
|
|
|
func TestDomainDelete_NotFound(t *testing.T) {
|
|
router, _ := setupDomainTestRouter(t)
|
|
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/v1/domains/nonexistent-uuid", http.NoBody)
|
|
resp := httptest.NewRecorder()
|
|
router.ServeHTTP(resp, req)
|
|
// Handler may return 200 with deleted=true even if not found (soft delete behavior)
|
|
require.True(t, resp.Code == http.StatusOK || resp.Code == http.StatusNotFound)
|
|
}
|
|
|
|
func TestDomainCreate_Duplicate(t *testing.T) {
|
|
router, db := setupDomainTestRouter(t)
|
|
|
|
// Create first domain
|
|
body := `{"name":"duplicate.com"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/domains", strings.NewReader(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp := httptest.NewRecorder()
|
|
router.ServeHTTP(resp, req)
|
|
require.Equal(t, http.StatusCreated, resp.Code)
|
|
|
|
// Try creating duplicate
|
|
req = httptest.NewRequest(http.MethodPost, "/api/v1/domains", strings.NewReader(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp = httptest.NewRecorder()
|
|
router.ServeHTTP(resp, req)
|
|
// Should error - could be 409 Conflict or 500 depending on implementation
|
|
require.True(t, resp.Code >= 400, "Expected error status for duplicate domain")
|
|
|
|
// Verify only one exists
|
|
var count int64
|
|
db.Model(&models.Domain{}).Where("name = ?", "duplicate.com").Count(&count)
|
|
require.Equal(t, int64(1), count)
|
|
}
|
|
|
|
func TestDomainList_Empty(t *testing.T) {
|
|
router, _ := setupDomainTestRouter(t)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/domains", http.NoBody)
|
|
resp := httptest.NewRecorder()
|
|
router.ServeHTTP(resp, req)
|
|
require.Equal(t, http.StatusOK, resp.Code)
|
|
|
|
var list []models.Domain
|
|
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &list))
|
|
require.Empty(t, list)
|
|
}
|
|
|
|
func TestDomainCreate_LongName(t *testing.T) {
|
|
router, _ := setupDomainTestRouter(t)
|
|
|
|
longName := strings.Repeat("a", 300) + ".com"
|
|
body := `{"name":"` + longName + `"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/domains", strings.NewReader(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp := httptest.NewRecorder()
|
|
router.ServeHTTP(resp, req)
|
|
// Should succeed (database will truncate or accept)
|
|
require.True(t, resp.Code == http.StatusCreated || resp.Code >= 400)
|
|
}
|