Merge pull request 'metrics' (#375) from stats into develop
[akkoma] / test / pleroma / web / plugs / user_tracking_plug_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Plugs.UserTrackingPlugTest do
6 use Pleroma.Web.ConnCase, async: true
7
8 import Pleroma.Factory
9
10 alias Pleroma.Web.Plugs.UserTrackingPlug
11
12 test "updates last_active_at for a new user", %{conn: conn} do
13 user = insert(:user)
14
15 assert is_nil(user.last_active_at)
16
17 test_started_at = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
18
19 %{assigns: %{user: user}} =
20 conn
21 |> assign(:user, user)
22 |> UserTrackingPlug.call(%{})
23
24 assert user.last_active_at >= test_started_at
25 assert user.last_active_at <= NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
26 end
27
28 test "doesn't update last_active_at if it was updated recently", %{conn: conn} do
29 last_active_at =
30 NaiveDateTime.utc_now()
31 |> NaiveDateTime.add(-:timer.hours(1), :millisecond)
32 |> NaiveDateTime.truncate(:second)
33
34 user = insert(:user, %{last_active_at: last_active_at})
35
36 %{assigns: %{user: user}} =
37 conn
38 |> assign(:user, user)
39 |> UserTrackingPlug.call(%{})
40
41 assert user.last_active_at == last_active_at
42 end
43
44 test "skips updating last_active_at if user ID is nil", %{conn: conn} do
45 %{assigns: %{user: user}} =
46 conn
47 |> assign(:user, %Pleroma.User{})
48 |> UserTrackingPlug.call(%{})
49
50 assert is_nil(user.last_active_at)
51 end
52
53 test "does nothing if user is not present", %{conn: conn} do
54 %{assigns: assigns} = UserTrackingPlug.call(conn, %{})
55
56 refute Map.has_key?(assigns, :user)
57 end
58 end