mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-30 06:16:37 +08:00
943818f7d4
将 OpenFlare 与平台业务的数据访问从 model 与 apps 直连迁入 repository, model 仅保留实体与无 IO 规则;补充 code-check 架构守卫与开发规范。
81 lines
2.4 KiB
Go
81 lines
2.4 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package waf
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/Rain-kl/Wavelet/internal/repository"
|
|
|
|
db "github.com/Rain-kl/Wavelet/internal/infra/persistence"
|
|
"github.com/Rain-kl/Wavelet/internal/model"
|
|
"github.com/glebarez/sqlite"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func setupWAFTestDB(t *testing.T) func() {
|
|
t.Helper()
|
|
|
|
sqliteDB, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{
|
|
DisableForeignKeyConstraintWhenMigrating: true,
|
|
})
|
|
require.NoError(t, err)
|
|
require.NoError(t, sqliteDB.AutoMigrate(
|
|
&model.OpenFlareWAFRuleGroup{},
|
|
&model.OpenFlareWAFIPGroup{},
|
|
&model.OpenFlareWAFRuleGroupBinding{},
|
|
&model.OriginProxyRoute{},
|
|
))
|
|
|
|
db.SetDB(sqliteDB)
|
|
return func() {
|
|
db.SetDB(nil)
|
|
}
|
|
}
|
|
|
|
func TestPruneIPGroupExtIPs(t *testing.T) {
|
|
group := &model.OpenFlareWAFIPGroup{
|
|
ExtIPs: `[{"ip":"203.0.113.10","captured_at":"2026-06-18T10:00:00Z"},{"ip":"203.0.113.11","captured_at":"2026-06-18T11:00:00Z"}]`,
|
|
}
|
|
err := pruneIPGroupExtIPs(group, []string{"203.0.113.10"})
|
|
require.NoError(t, err)
|
|
assert.JSONEq(t, `[{"ip":"203.0.113.10","captured_at":"2026-06-18T10:00:00Z"}]`, group.ExtIPs)
|
|
}
|
|
|
|
func TestUpdateIPGroupPrunesAutomaticExtIPs(t *testing.T) {
|
|
cleanup := setupWAFTestDB(t)
|
|
defer cleanup()
|
|
ctx := context.Background()
|
|
|
|
created, err := CreateIPGroup(ctx, IPGroupInput{
|
|
Name: "auto group",
|
|
Type: wafIPGroupTypeAutomatic,
|
|
Enabled: true,
|
|
AutoConfig: []byte(`{"lookback":"60m","ttl":-1,"rules":[{"name":"scan","expr":"request_count > 1"}]}`),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
group, err := repository.GetOpenFlareWAFIPGroupByID(ctx, created.ID)
|
|
require.NoError(t, err)
|
|
group.IPList = `["203.0.113.10","203.0.113.11"]`
|
|
group.ExtIPs = `[{"ip":"203.0.113.10","captured_at":"2026-06-18T10:00:00Z"},{"ip":"203.0.113.11","captured_at":"2026-06-18T11:00:00Z"}]`
|
|
require.NoError(t, repository.UpdateOpenFlareWAFIPGroup(ctx, group))
|
|
|
|
updated, err := UpdateIPGroup(ctx, created.ID, IPGroupInput{
|
|
Name: created.Name,
|
|
Type: created.Type,
|
|
Enabled: created.Enabled,
|
|
IPList: []string{"203.0.113.10"},
|
|
AutoConfig: created.AutoConfig,
|
|
})
|
|
require.NoError(t, err)
|
|
require.Len(t, updated.IPList, 1)
|
|
assert.Equal(t, "203.0.113.10", updated.IPList[0])
|
|
require.Len(t, updated.ExtIPs, 1)
|
|
assert.Equal(t, "203.0.113.10", updated.ExtIPs[0].IP)
|
|
}
|