Add tests
authorEgor Kislitsyn <egor@kislitsyn.com>
Wed, 27 Jan 2021 14:18:46 +0000 (18:18 +0400)
committerEgor Kislitsyn <egor@kislitsyn.com>
Wed, 27 Jan 2021 14:20:07 +0000 (18:20 +0400)
test/pleroma/user_test.exs
test/pleroma/web/mastodon_api/controllers/instance_controller_test.exs
test/pleroma/web/plugs/user_tracking_plug_test.exs [new file with mode: 0644]

index b4df22c2c849c535c3bb69f707daed3ab8621995..1fab519f00f0b26ceed8de992788f552d884c00d 100644 (file)
@@ -2248,4 +2248,42 @@ defmodule Pleroma.UserTest do
     user = insert(:user, ap_id: "https://lain.com/users/lain", nickname: "lain")
     assert User.get_host(user) == "lain.com"
   end
+
+  test "update_last_active_at/1" do
+    user = insert(:user)
+    assert is_nil(user.last_active_at)
+
+    test_started_at = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
+
+    assert {:ok, user} = User.update_last_active_at(user)
+
+    assert user.last_active_at >= test_started_at
+    assert user.last_active_at <= NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
+
+    last_active_at =
+      NaiveDateTime.utc_now()
+      |> NaiveDateTime.add(-:timer.hours(24), :millisecond)
+      |> NaiveDateTime.truncate(:second)
+
+    assert {:ok, user} =
+             user
+             |> cast(%{last_active_at: last_active_at}, [:last_active_at])
+             |> User.update_and_set_cache()
+
+    assert user.last_active_at == last_active_at
+    assert {:ok, user} = User.update_last_active_at(user)
+    assert user.last_active_at >= test_started_at
+    assert user.last_active_at <= NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
+  end
+
+  test "active_user_count/1" do
+    insert(:user)
+    insert(:user, %{last_active_at: Timex.shift(NaiveDateTime.utc_now(), weeks: -5)})
+    insert(:user, %{last_active_at: Timex.shift(NaiveDateTime.utc_now(), weeks: -3)})
+    insert(:user, %{last_active_at: NaiveDateTime.utc_now()})
+
+    assert User.active_user_count() == 2
+    assert User.active_user_count(6) == 3
+    assert User.active_user_count(1) == 1
+  end
 end
index 0d4eebb73d0a9e05631656339dcc5d537d76a795..b998566591512c873f763051a5cb73a93b85a6a9 100644 (file)
@@ -47,6 +47,7 @@ defmodule Pleroma.Web.MastodonAPI.InstanceControllerTest do
     assert result["pleroma"]["metadata"]["federation"]
     assert result["pleroma"]["metadata"]["fields_limits"]
     assert result["pleroma"]["vapid_public_key"]
+    assert result["pleroma"]["stats"]["mau"] == 0
 
     assert email == from_config_email
     assert thumbnail == from_config_thumbnail
diff --git a/test/pleroma/web/plugs/user_tracking_plug_test.exs b/test/pleroma/web/plugs/user_tracking_plug_test.exs
new file mode 100644 (file)
index 0000000..8e9d59b
--- /dev/null
@@ -0,0 +1,58 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.Plugs.UserTrackingPlugTest do
+  use Pleroma.Web.ConnCase, async: true
+
+  import Pleroma.Factory
+
+  alias Pleroma.Web.Plugs.UserTrackingPlug
+
+  test "updates last_active_at for a new user", %{conn: conn} do
+    user = insert(:user)
+
+    assert is_nil(user.last_active_at)
+
+    test_started_at = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
+
+    %{assigns: %{user: user}} =
+      conn
+      |> assign(:user, user)
+      |> UserTrackingPlug.call(%{})
+
+    assert user.last_active_at >= test_started_at
+    assert user.last_active_at <= NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
+  end
+
+  test "doesn't update last_active_at if it was updated recently", %{conn: conn} do
+    last_active_at =
+      NaiveDateTime.utc_now()
+      |> NaiveDateTime.add(-:timer.hours(1), :millisecond)
+      |> NaiveDateTime.truncate(:second)
+
+    user = insert(:user, %{last_active_at: last_active_at})
+
+    %{assigns: %{user: user}} =
+      conn
+      |> assign(:user, user)
+      |> UserTrackingPlug.call(%{})
+
+    assert user.last_active_at == last_active_at
+  end
+
+  test "skips updating last_active_at if user ID is nil", %{conn: conn} do
+    %{assigns: %{user: user}} =
+      conn
+      |> assign(:user, %Pleroma.User{})
+      |> UserTrackingPlug.call(%{})
+
+    assert is_nil(user.last_active_at)
+  end
+
+  test "does nothing if user is not present", %{conn: conn} do
+    %{assigns: assigns} = UserTrackingPlug.call(conn, %{})
+
+    refute Map.has_key?(assigns, :user)
+  end
+end