towerops/test/towerops_web/plugs/detect_eu_user_test.exs
Graham McIntire 3276a1cd8c
fix user session controller test
Update email input ID assertion to match actual template implementation
2026-02-03 13:42:24 -06:00

94 lines
3.1 KiB
Elixir

defmodule ToweropsWeb.Plugs.DetectEUUserTest do
use ToweropsWeb.ConnCase, async: true
alias ToweropsWeb.Plugs.DetectEUUser
describe "init/1" do
test "returns options unchanged" do
assert DetectEUUser.init([]) == []
assert DetectEUUser.init(some: :option) == [some: :option]
end
end
describe "call/2 - localhost" do
test "always requires consent for localhost" do
conn =
build_conn()
|> Map.put(:host, "localhost")
|> Plug.Test.init_test_session(%{})
|> DetectEUUser.call([])
assert conn.assigns.requires_cookie_consent == true
assert get_session(conn, :requires_cookie_consent) == true
assert Process.get(:requires_cookie_consent) == true
end
test "always requires consent for 127.0.0.1" do
conn =
build_conn()
|> Map.put(:host, "127.0.0.1")
|> Plug.Test.init_test_session(%{})
|> DetectEUUser.call([])
assert conn.assigns.requires_cookie_consent == true
assert get_session(conn, :requires_cookie_consent) == true
end
end
describe "call/2 - non-localhost (GeoIP integration)" do
test "requires consent when GeoIP returns nil (conservative approach)" do
# GeoIP.lookup will return nil in test environment (no data)
# This tests the conservative default behavior
conn =
build_conn()
|> Map.put(:host, "example.com")
|> Plug.Test.init_test_session(%{})
|> DetectEUUser.call([])
# Should default to requiring consent when country cannot be determined
assert conn.assigns.requires_cookie_consent == true
end
end
describe "call/2 - stores in multiple locations" do
test "stores consent requirement in session, assigns, and process dictionary" do
conn =
build_conn()
|> Map.put(:host, "localhost")
|> Plug.Test.init_test_session(%{})
|> DetectEUUser.call([])
# Check all three storage locations
assert conn.assigns.requires_cookie_consent == true
assert get_session(conn, :requires_cookie_consent) == true
assert Process.get(:requires_cookie_consent) == true
end
end
describe "call/2 - X-Forwarded-For header" do
test "processes X-Forwarded-For header when present" do
# Just verify the plug doesn't crash with X-Forwarded-For header
conn =
build_conn()
|> Map.put(:host, "example.com")
|> put_req_header("x-forwarded-for", "203.0.113.195")
|> Plug.Test.init_test_session(%{})
|> DetectEUUser.call([])
# In test env, GeoIP returns nil, so conservative default is true
assert conn.assigns.requires_cookie_consent == true
end
test "handles comma-separated X-Forwarded-For" do
# Verify the plug handles multiple IPs without crashing
conn =
build_conn()
|> Map.put(:host, "example.com")
|> put_req_header("x-forwarded-for", "203.0.113.195, 10.0.0.1, 192.168.1.1")
|> Plug.Test.init_test_session(%{})
|> DetectEUUser.call([])
assert conn.assigns.requires_cookie_consent == true
end
end
end