Merge branch 'fix/1742-random-failing-tests' into 'develop'
[akkoma] / test / stats_test.exs
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
5 defmodule Pleroma.StatsTest do
6 use Pleroma.DataCase
7 import Pleroma.Factory
8 alias Pleroma.Web.CommonAPI
9
10 describe "user count" do
11 test "it ignores internal users" do
12 _user = insert(:user, local: true)
13 _internal = insert(:user, local: true, nickname: nil)
14 _internal = Pleroma.Web.ActivityPub.Relay.get_actor()
15
16 assert match?(%{stats: %{user_count: 1}}, Pleroma.Stats.calculate_stat_data())
17 end
18 end
19
20 describe "status visibility count" do
21 test "on new status" do
22 user = insert(:user)
23 other_user = insert(:user)
24
25 CommonAPI.post(user, %{visibility: "public", status: "hey"})
26
27 Enum.each(0..1, fn _ ->
28 CommonAPI.post(user, %{
29 visibility: "unlisted",
30 status: "hey"
31 })
32 end)
33
34 Enum.each(0..2, fn _ ->
35 CommonAPI.post(user, %{
36 visibility: "direct",
37 status: "hey @#{other_user.nickname}"
38 })
39 end)
40
41 Enum.each(0..3, fn _ ->
42 CommonAPI.post(user, %{
43 visibility: "private",
44 status: "hey"
45 })
46 end)
47
48 assert %{direct: 3, private: 4, public: 1, unlisted: 2} =
49 Pleroma.Stats.get_status_visibility_count()
50 end
51
52 test "on status delete" do
53 user = insert(:user)
54 {:ok, activity} = CommonAPI.post(user, %{visibility: "public", status: "hey"})
55 assert %{public: 1} = Pleroma.Stats.get_status_visibility_count()
56 CommonAPI.delete(activity.id, user)
57 assert %{public: 0} = Pleroma.Stats.get_status_visibility_count()
58 end
59
60 test "on status visibility update" do
61 user = insert(:user)
62 {:ok, activity} = CommonAPI.post(user, %{visibility: "public", status: "hey"})
63 assert %{public: 1, private: 0} = Pleroma.Stats.get_status_visibility_count()
64 {:ok, _} = CommonAPI.update_activity_scope(activity.id, %{visibility: "private"})
65 assert %{public: 0, private: 1} = Pleroma.Stats.get_status_visibility_count()
66 end
67
68 test "doesn't count unrelated activities" do
69 user = insert(:user)
70 other_user = insert(:user)
71 {:ok, activity} = CommonAPI.post(user, %{visibility: "public", status: "hey"})
72 _ = CommonAPI.follow(user, other_user)
73 CommonAPI.favorite(other_user, activity.id)
74 CommonAPI.repeat(activity.id, other_user)
75
76 assert %{direct: 0, private: 0, public: 1, unlisted: 0} =
77 Pleroma.Stats.get_status_visibility_count()
78 end
79 end
80 end