logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

stats_test.exs (3838B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.StatsTest do
  5. use Pleroma.DataCase
  6. import Pleroma.Factory
  7. alias Pleroma.Stats
  8. alias Pleroma.Web.CommonAPI
  9. describe "user count" do
  10. test "it ignores internal users" do
  11. _user = insert(:user, local: true)
  12. _internal = insert(:user, local: true, nickname: nil)
  13. _internal = Pleroma.Web.ActivityPub.Relay.get_actor()
  14. assert match?(%{stats: %{user_count: 1}}, Stats.calculate_stat_data())
  15. end
  16. end
  17. describe "status visibility sum count" do
  18. test "on new status" do
  19. instance2 = "instance2.tld"
  20. user = insert(:user)
  21. other_user = insert(:user, %{ap_id: "https://#{instance2}/@actor"})
  22. CommonAPI.post(user, %{visibility: "public", status: "hey"})
  23. Enum.each(0..1, fn _ ->
  24. CommonAPI.post(user, %{
  25. visibility: "unlisted",
  26. status: "hey"
  27. })
  28. end)
  29. Enum.each(0..2, fn _ ->
  30. CommonAPI.post(user, %{
  31. visibility: "direct",
  32. status: "hey @#{other_user.nickname}"
  33. })
  34. end)
  35. Enum.each(0..3, fn _ ->
  36. CommonAPI.post(user, %{
  37. visibility: "private",
  38. status: "hey"
  39. })
  40. end)
  41. assert %{"direct" => 3, "private" => 4, "public" => 1, "unlisted" => 2} =
  42. Stats.get_status_visibility_count()
  43. end
  44. test "on status delete" do
  45. user = insert(:user)
  46. {:ok, activity} = CommonAPI.post(user, %{visibility: "public", status: "hey"})
  47. assert %{"public" => 1} = Stats.get_status_visibility_count()
  48. CommonAPI.delete(activity.id, user)
  49. assert %{"public" => 0} = Stats.get_status_visibility_count()
  50. end
  51. test "on status visibility update" do
  52. user = insert(:user)
  53. {:ok, activity} = CommonAPI.post(user, %{visibility: "public", status: "hey"})
  54. assert %{"public" => 1, "private" => 0} = Stats.get_status_visibility_count()
  55. {:ok, _} = CommonAPI.update_activity_scope(activity.id, %{visibility: "private"})
  56. assert %{"public" => 0, "private" => 1} = Stats.get_status_visibility_count()
  57. end
  58. test "doesn't count unrelated activities" do
  59. user = insert(:user)
  60. other_user = insert(:user)
  61. {:ok, activity} = CommonAPI.post(user, %{visibility: "public", status: "hey"})
  62. _ = CommonAPI.follow(user, other_user)
  63. CommonAPI.favorite(other_user, activity.id)
  64. CommonAPI.repeat(activity.id, other_user)
  65. assert %{"direct" => 0, "private" => 0, "public" => 1, "unlisted" => 0} =
  66. Stats.get_status_visibility_count()
  67. end
  68. end
  69. describe "status visibility by instance count" do
  70. test "single instance" do
  71. local_instance = Pleroma.Web.Endpoint.url() |> String.split("//") |> Enum.at(1)
  72. instance2 = "instance2.tld"
  73. user1 = insert(:user)
  74. user2 = insert(:user, %{ap_id: "https://#{instance2}/@actor"})
  75. CommonAPI.post(user1, %{visibility: "public", status: "hey"})
  76. Enum.each(1..5, fn _ ->
  77. CommonAPI.post(user1, %{
  78. visibility: "unlisted",
  79. status: "hey"
  80. })
  81. end)
  82. Enum.each(1..10, fn _ ->
  83. CommonAPI.post(user1, %{
  84. visibility: "direct",
  85. status: "hey @#{user2.nickname}"
  86. })
  87. end)
  88. Enum.each(1..20, fn _ ->
  89. CommonAPI.post(user2, %{
  90. visibility: "private",
  91. status: "hey"
  92. })
  93. end)
  94. assert %{"direct" => 10, "private" => 0, "public" => 1, "unlisted" => 5} =
  95. Stats.get_status_visibility_count(local_instance)
  96. assert %{"direct" => 0, "private" => 20, "public" => 0, "unlisted" => 0} =
  97. Stats.get_status_visibility_count(instance2)
  98. end
  99. end
  100. end