defmodule GitGudWeb.ScopeRequestTest do
@moduledoc """
The signed-in scope carries the request's client address and user
agent, for the audit log — from the conn on a plain request, from the
socket's connect info in a LiveView.
"""
use GitGudWeb.ConnCase, async: true
import GitGud.AccountsFixtures
import Phoenix.LiveViewTest
alias GitGud.Accounts.Scope
alias GitGudWeb.UserAuth
setup %{conn: conn} do
user = user_fixture()
{:ok, conn: log_in_user(conn, user), user: user}
end
test "a request's scope has its address and user agent", %{conn: conn} do
conn =
%{conn | remote_ip: {203, 0, 113, 9}}
|> put_req_header("user-agent", "gg/1.0")
|> Phoenix.ConnTest.init_test_session(%{})
|> UserAuth.fetch_current_scope_for_user([])
assert %Scope{ip: "203.0.113.9", user_agent: "gg/1.0"} = conn.assigns.current_scope
end
test "a LiveView's scope has them too", %{conn: conn} do
conn =
conn
|> Plug.Test.put_peer_data(%{address: {198, 51, 100, 4}, port: 4000, ssl_cert: nil})
|> put_req_header("user-agent", "Firefox/140")
{:ok, view, _html} = live(conn, ~p"/")
assert %Scope{ip: "198.51.100.4", user_agent: "Firefox/140"} =
:sys.get_state(view.pid).socket.assigns.current_scope
end
test "an anonymous visitor has no scope to put them on" do
assert Scope.put_request(nil, {127, 0, 0, 1}, "x") == nil
end
end
neiam
/gitgud
Git Gud
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
1
Log in to mark this repository.
1.4 KiB · text
History
8ccf537