Files
masyunis/lib/threshold_identity_test.go
T
sol_bot Bot e07de898cd Import Anubis v1.28.2
Replace the tree with upstream TecharoHQ/anubis v1.28.2 (a1384fbf42e3).
Masyunis customizations are re-applied in the following commits.
2026-11-28 14:52:26 +03:00

46 lines
1.4 KiB
Go

package lib
import (
"log/slog"
"net/http/httptest"
"testing"
"github.com/TecharoHQ/anubis/lib/config"
"github.com/TecharoHQ/anubis/lib/policy"
)
func TestThresholdPolicyHashesAreDistinct(t *testing.T) {
srv := spawnAnubis(t, Options{Policy: loadPolicies(t, "testdata/zero_difficulty.yaml", 0)})
srv.policy.Bots = nil
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("X-Real-IP", "192.0.2.1")
hashes := make(map[string]string)
for _, name := range []string{"easy", "hard", "default", "harder", "expression"} {
srv.policy.Thresholds = nil
if name != "default" {
thresholdName, expression, difficulty := name, "true", 1
if name == "harder" {
thresholdName = "easy"
difficulty = 2
}
if name == "expression" {
thresholdName = "easy"
expression = "weight >= 0"
}
threshold, err := policy.ParsedThresholdFromConfig(config.Threshold{Name: thresholdName, Expression: &config.ExpressionOrList{Expression: expression}, Action: config.RuleChallenge, Challenge: &config.ChallengeRules{Algorithm: "fast", Difficulty: difficulty}})
if err != nil {
t.Fatal(err)
}
srv.policy.Thresholds = []*policy.Threshold{threshold}
}
_, bot, err := srv.check(req, slog.Default())
if err != nil {
t.Fatal(err)
}
if other, ok := hashes[bot.Hash()]; ok {
t.Errorf("%s shares policy hash with %s", name, other)
}
hashes[bot.Hash()] = name
}
}