76 lines
2.3 KiB
Elixir
76 lines
2.3 KiB
Elixir
defmodule Towerops.Security.FourOhFourTrackerTest do
|
|
use Towerops.DataCase
|
|
|
|
alias Towerops.Security.FourOhFourTracker
|
|
|
|
@moduletag :four_oh_four_tracker
|
|
|
|
describe "record_404/2" do
|
|
test "records a single 404 and returns :ok" do
|
|
ip = "198.51.100.#{System.unique_integer([:positive]) |> rem(255)}"
|
|
assert FourOhFourTracker.record_404(ip, "/admin") == :ok
|
|
end
|
|
|
|
test "records multiple unique paths without triggering threshold" do
|
|
ip = "198.51.100.#{System.unique_integer([:positive]) |> rem(255)}"
|
|
|
|
for i <- 1..4 do
|
|
result = FourOhFourTracker.record_404(ip, "/path-#{i}")
|
|
assert result == :ok, "Expected :ok for path #{i}, got #{inspect(result)}"
|
|
end
|
|
end
|
|
|
|
test "triggers threshold at 5 unique 404s" do
|
|
ip = "198.51.100.#{System.unique_integer([:positive]) |> rem(255)}"
|
|
|
|
for i <- 1..4 do
|
|
FourOhFourTracker.record_404(ip, "/scan-#{i}")
|
|
end
|
|
|
|
# 5th unique path should trigger
|
|
assert FourOhFourTracker.record_404(ip, "/scan-5") == :threshold_reached
|
|
end
|
|
|
|
test "duplicate paths don't count toward threshold" do
|
|
ip = "198.51.100.#{System.unique_integer([:positive]) |> rem(255)}"
|
|
|
|
# Record same path multiple times
|
|
for _ <- 1..10 do
|
|
FourOhFourTracker.record_404(ip, "/same-path")
|
|
end
|
|
|
|
# Should still be at count 1
|
|
assert FourOhFourTracker.get_count(ip) == 1
|
|
end
|
|
|
|
test "different IPs track independently" do
|
|
ip1 = "198.51.100.#{System.unique_integer([:positive]) |> rem(255)}"
|
|
ip2 = "198.51.100.#{System.unique_integer([:positive]) |> rem(255)}"
|
|
|
|
for i <- 1..3 do
|
|
FourOhFourTracker.record_404(ip1, "/path-#{i}")
|
|
end
|
|
|
|
FourOhFourTracker.record_404(ip2, "/only-one")
|
|
|
|
assert FourOhFourTracker.get_count(ip1) == 3
|
|
assert FourOhFourTracker.get_count(ip2) == 1
|
|
end
|
|
end
|
|
|
|
describe "get_count/1" do
|
|
test "returns 0 for unknown IP" do
|
|
assert FourOhFourTracker.get_count("192.0.2.254") == 0
|
|
end
|
|
|
|
test "returns correct count after recording paths" do
|
|
ip = "198.51.100.#{System.unique_integer([:positive]) |> rem(255)}"
|
|
|
|
FourOhFourTracker.record_404(ip, "/a")
|
|
FourOhFourTracker.record_404(ip, "/b")
|
|
FourOhFourTracker.record_404(ip, "/c")
|
|
|
|
assert FourOhFourTracker.get_count(ip) == 3
|
|
end
|
|
end
|
|
end
|