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
103 lines
2.9 KiB
Go
Executable File
103 lines
2.9 KiB
Go
Executable File
package models_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/glebarez/sqlite"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/Wikid82/charon/backend/internal/models"
|
|
)
|
|
|
|
func newSeedTestDB(t *testing.T) *gorm.DB {
|
|
t.Helper()
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
require.NoError(t, err)
|
|
require.NoError(t, db.AutoMigrate(&models.SecurityConfig{}))
|
|
return db
|
|
}
|
|
|
|
func TestSeedDefaultSecurityConfig_EmptyDB(t *testing.T) {
|
|
db := newSeedTestDB(t)
|
|
|
|
rec, err := models.SeedDefaultSecurityConfig(db)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, rec)
|
|
|
|
assert.Equal(t, "default", rec.Name)
|
|
assert.False(t, rec.Enabled)
|
|
assert.Equal(t, "disabled", rec.CrowdSecMode)
|
|
assert.Equal(t, "http://127.0.0.1:8085", rec.CrowdSecAPIURL)
|
|
assert.Equal(t, "disabled", rec.WAFMode)
|
|
assert.Equal(t, "disabled", rec.RateLimitMode)
|
|
assert.NotEmpty(t, rec.UUID)
|
|
|
|
var count int64
|
|
db.Model(&models.SecurityConfig{}).Where("name = ?", "default").Count(&count)
|
|
assert.Equal(t, int64(1), count)
|
|
}
|
|
|
|
func TestSeedDefaultSecurityConfig_Idempotent(t *testing.T) {
|
|
db := newSeedTestDB(t)
|
|
|
|
// First call — creates the row.
|
|
rec1, err := models.SeedDefaultSecurityConfig(db)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, rec1)
|
|
|
|
// Second call — must not error and must not duplicate.
|
|
rec2, err := models.SeedDefaultSecurityConfig(db)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, rec2)
|
|
|
|
assert.Equal(t, rec1.ID, rec2.ID, "ID must be identical on subsequent calls")
|
|
|
|
var count int64
|
|
db.Model(&models.SecurityConfig{}).Where("name = ?", "default").Count(&count)
|
|
assert.Equal(t, int64(1), count, "exactly one row should exist after two seed calls")
|
|
}
|
|
|
|
func TestSeedDefaultSecurityConfig_DBError(t *testing.T) {
|
|
db := newSeedTestDB(t)
|
|
|
|
sqlDB, err := db.DB()
|
|
require.NoError(t, err)
|
|
require.NoError(t, sqlDB.Close())
|
|
|
|
rec, err := models.SeedDefaultSecurityConfig(db)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, rec)
|
|
}
|
|
|
|
func TestSeedDefaultSecurityConfig_DoesNotOverwriteExisting(t *testing.T) {
|
|
db := newSeedTestDB(t)
|
|
|
|
// Pre-seed a customised row.
|
|
existing := models.SecurityConfig{
|
|
UUID: "pre-existing-uuid",
|
|
Name: "default",
|
|
Enabled: true,
|
|
CrowdSecMode: "local",
|
|
CrowdSecAPIURL: "http://192.168.1.5:8085",
|
|
WAFMode: "block",
|
|
RateLimitMode: "enabled",
|
|
}
|
|
require.NoError(t, db.Create(&existing).Error)
|
|
|
|
// Seed should find the existing row and return it unchanged.
|
|
rec, err := models.SeedDefaultSecurityConfig(db)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, rec)
|
|
|
|
assert.True(t, rec.Enabled, "existing Enabled flag must not be overwritten")
|
|
assert.Equal(t, "local", rec.CrowdSecMode, "existing CrowdSecMode must not be overwritten")
|
|
assert.Equal(t, "http://192.168.1.5:8085", rec.CrowdSecAPIURL)
|
|
assert.Equal(t, "block", rec.WAFMode)
|
|
|
|
var count int64
|
|
db.Model(&models.SecurityConfig{}).Where("name = ?", "default").Count(&count)
|
|
assert.Equal(t, int64(1), count)
|
|
}
|