analytics/test/plausible_web/live/sentry_context_test.exs
Adrian Gruntkowski 373d4dd665
Implement token-based sessions (#4463)
* Turn `Plausible.Auth.UserSession` into full schema

* Implement token based sessions and use them as default

* Ignore expired user sessions during retrieval from DB

* Implement plug bumping user session last used and timeout timestamps

* Implement Oban worker removing expired user sessions with grace period

* Implement legacy session conversion on touch, when applicable

* Update `UserAuth` moduledoc

* Extend `UserAuth` tests to account for db-backed session tokens

* Update CHANGELOG

* Add tests for `UserSessionTouch` plug

* Add test for `CleanUserSessions` worker

* Add logging of legacy session retrievals

* Use single update permitting stale records  when touching user session

* Don't fetch session and user for external API endpoints (/api/event too)

* Refactor `Users.with_subscription/1` and expose helper query

* Skip fetching session in legacy `SessionTimeoutPlug`

* Rely on user session assign from `AuthContext` in `SentryContext`

* Silence legacy session warnings in `UserSessionTouchTest`

* Rely on session assign from `AuthPlug` in `SuperAdminOnlyPlug`

* Change `UserAuth` to get session, user and last subscription in one go

* Avoid refetching user session in `AuthorizeSiteAccess` plug

* Fix code formatting

* Refactor `UserAuth.get_user_token/1` (h/t @aerosol)

* Remove bogus empty opts from `scope` declarations in router

* Only touch session once an hour and keep `user.last_seen` in sync

* Bring back logging of legacy token use
2024-09-03 11:34:37 +02:00

83 lines
2.0 KiB
Elixir

defmodule PlausibleWeb.Live.SentryContextTest do
use PlausibleWeb.ConnCase, async: true
import Phoenix.LiveViewTest
defmodule SampleLV do
use PlausibleWeb, :live_view
def mount(_params, %{"test" => test_pid}, socket) do
socket = assign(socket, test_pid: test_pid)
{:ok, socket}
end
def render(assigns) do
~H"""
ok computer
"""
end
def handle_event("get_sentry_context", _params, socket) do
context = Sentry.Context.get_all()
send(socket.assigns.test_pid, {:context, context})
{:noreply, socket}
end
end
describe "sentry context" do
test "basic shape", %{conn: conn} do
context_hook(conn)
assert_receive {:context, context}
assert %{
extra: %{},
request: %{
env: %{
"REMOTE_ADDR" => "127.0.0.1",
"REMOTE_PORT" => port,
"SEVER_NAME" => "www.example.com"
},
url: :not_mounted_at_router
},
user: %{},
tags: %{},
breadcrumbs: []
} = context
assert is_integer(port)
end
test "user-agent is included", %{conn: conn} do
conn
|> put_req_header("user-agent", "Firefox")
|> context_hook()
assert_receive {:context, context}
assert context.request.headers["User-Agent"] == "Firefox"
end
end
describe "sentry context with logged in user" do
setup [:create_user, :log_in]
test "user_id is included", %{conn: conn, user: user} do
context_hook(conn)
assert_receive {:context, context}
assert context.user.id == user.id
end
end
defp context_hook(conn, extra_session \\ %{}) do
lv = get_liveview(conn, extra_session)
assert render(lv) =~ "ok computer"
render_hook(lv, :get_sentry_context, %{})
end
defp get_liveview(conn, extra_session) do
{:ok, lv, _html} =
live_isolated(conn, SampleLV, session: Map.merge(%{"test" => self()}, extra_session))
lv
end
end