1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Plugs.UserTrackingPlugTest do
6 use Pleroma.Web.ConnCase, async: true
10 alias Pleroma.Web.Plugs.UserTrackingPlug
12 test "updates last_active_at for a new user", %{conn: conn} do
15 assert is_nil(user.last_active_at)
17 test_started_at = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
19 %{assigns: %{user: user}} =
21 |> assign(:user, user)
22 |> UserTrackingPlug.call(%{})
24 assert user.last_active_at >= test_started_at
25 assert user.last_active_at <= NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
28 test "doesn't update last_active_at if it was updated recently", %{conn: conn} do
30 NaiveDateTime.utc_now()
31 |> NaiveDateTime.add(-:timer.hours(1), :millisecond)
32 |> NaiveDateTime.truncate(:second)
34 user = insert(:user, %{last_active_at: last_active_at})
36 %{assigns: %{user: user}} =
38 |> assign(:user, user)
39 |> UserTrackingPlug.call(%{})
41 assert user.last_active_at == last_active_at
44 test "skips updating last_active_at if user ID is nil", %{conn: conn} do
45 %{assigns: %{user: user}} =
47 |> assign(:user, %Pleroma.User{})
48 |> UserTrackingPlug.call(%{})
50 assert is_nil(user.last_active_at)
53 test "does nothing if user is not present", %{conn: conn} do
54 %{assigns: assigns} = UserTrackingPlug.call(conn, %{})
56 refute Map.has_key?(assigns, :user)