Merge branch 'feature/return-link-for-password-reset' into 'develop'
[akkoma] / test / tasks / database_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.DatabaseTest do
6 alias Pleroma.Activity
7 alias Pleroma.Object
8 alias Pleroma.Repo
9 alias Pleroma.User
10 alias Pleroma.Web.CommonAPI
11
12 use Pleroma.DataCase
13
14 import Pleroma.Factory
15
16 setup_all do
17 Mix.shell(Mix.Shell.Process)
18
19 on_exit(fn ->
20 Mix.shell(Mix.Shell.IO)
21 end)
22
23 :ok
24 end
25
26 describe "running remove_embedded_objects" do
27 test "it replaces objects with references" do
28 user = insert(:user)
29 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
30 new_data = Map.put(activity.data, "object", activity.object.data)
31
32 {:ok, activity} =
33 activity
34 |> Activity.change(%{data: new_data})
35 |> Repo.update()
36
37 assert is_map(activity.data["object"])
38
39 Mix.Tasks.Pleroma.Database.run(["remove_embedded_objects"])
40
41 activity = Activity.get_by_id_with_object(activity.id)
42 assert is_binary(activity.data["object"])
43 end
44 end
45
46 describe "prune_objects" do
47 test "it prunes old objects from the database" do
48 insert(:note)
49 deadline = Pleroma.Config.get([:instance, :remote_post_retention_days]) + 1
50
51 date =
52 Timex.now()
53 |> Timex.shift(days: -deadline)
54 |> Timex.to_naive_datetime()
55 |> NaiveDateTime.truncate(:second)
56
57 %{id: id} =
58 :note
59 |> insert()
60 |> Ecto.Changeset.change(%{inserted_at: date})
61 |> Repo.update!()
62
63 assert length(Repo.all(Object)) == 2
64
65 Mix.Tasks.Pleroma.Database.run(["prune_objects"])
66
67 assert length(Repo.all(Object)) == 1
68 refute Object.get_by_id(id)
69 end
70 end
71
72 describe "running update_users_following_followers_counts" do
73 test "following and followers count are updated" do
74 [user, user2] = insert_pair(:user)
75 {:ok, %User{following: following, info: info} = user} = User.follow(user, user2)
76
77 assert length(following) == 2
78 assert info.follower_count == 0
79
80 {:ok, user} =
81 user
82 |> Ecto.Changeset.change(%{following: following ++ following})
83 |> User.change_info(&Ecto.Changeset.change(&1, %{follower_count: 3}))
84 |> Repo.update()
85
86 assert length(user.following) == 4
87 assert user.info.follower_count == 3
88
89 assert :ok == Mix.Tasks.Pleroma.Database.run(["update_users_following_followers_counts"])
90
91 user = User.get_by_id(user.id)
92
93 assert length(user.following) == 2
94 assert user.info.follower_count == 0
95 end
96 end
97
98 describe "running fix_likes_collections" do
99 test "it turns OrderedCollection likes into empty arrays" do
100 [user, user2] = insert_pair(:user)
101
102 {:ok, %{id: id, object: object}} = CommonAPI.post(user, %{"status" => "test"})
103 {:ok, %{object: object2}} = CommonAPI.post(user, %{"status" => "test test"})
104
105 CommonAPI.favorite(id, user2)
106
107 likes = %{
108 "first" =>
109 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
110 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
111 "totalItems" => 3,
112 "type" => "OrderedCollection"
113 }
114
115 new_data = Map.put(object2.data, "likes", likes)
116
117 object2
118 |> Ecto.Changeset.change(%{data: new_data})
119 |> Repo.update()
120
121 assert length(Object.get_by_id(object.id).data["likes"]) == 1
122 assert is_map(Object.get_by_id(object2.id).data["likes"])
123
124 assert :ok == Mix.Tasks.Pleroma.Database.run(["fix_likes_collections"])
125
126 assert length(Object.get_by_id(object.id).data["likes"]) == 1
127 assert Enum.empty?(Object.get_by_id(object2.id).data["likes"])
128 end
129 end
130 end