2019-05-16 13:04:08 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 14:44:49 -08:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-05-16 13:04:08 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Mix.Tasks.Pleroma.DatabaseTest do
|
2020-08-22 10:46:01 -07:00
|
|
|
use Pleroma.DataCase
|
|
|
|
use Oban.Testing, repo: Pleroma.Repo
|
|
|
|
|
2019-08-19 09:10:00 -07:00
|
|
|
alias Pleroma.Activity
|
2019-08-10 11:47:40 -07:00
|
|
|
alias Pleroma.Object
|
2019-05-16 13:04:08 -07:00
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.User
|
2019-08-10 11:47:40 -07:00
|
|
|
alias Pleroma.Web.CommonAPI
|
|
|
|
|
2019-05-16 13:04:08 -07:00
|
|
|
import Pleroma.Factory
|
|
|
|
|
|
|
|
setup_all do
|
|
|
|
Mix.shell(Mix.Shell.Process)
|
|
|
|
|
|
|
|
on_exit(fn ->
|
|
|
|
Mix.shell(Mix.Shell.IO)
|
|
|
|
end)
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2019-08-19 09:10:00 -07:00
|
|
|
describe "running remove_embedded_objects" do
|
|
|
|
test "it replaces objects with references" do
|
|
|
|
user = insert(:user)
|
2020-05-12 12:59:26 -07:00
|
|
|
{:ok, activity} = CommonAPI.post(user, %{status: "test"})
|
2019-08-19 09:10:00 -07:00
|
|
|
new_data = Map.put(activity.data, "object", activity.object.data)
|
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
activity
|
|
|
|
|> Activity.change(%{data: new_data})
|
|
|
|
|> Repo.update()
|
|
|
|
|
|
|
|
assert is_map(activity.data["object"])
|
|
|
|
|
|
|
|
Mix.Tasks.Pleroma.Database.run(["remove_embedded_objects"])
|
|
|
|
|
|
|
|
activity = Activity.get_by_id_with_object(activity.id)
|
|
|
|
assert is_binary(activity.data["object"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "prune_objects" do
|
|
|
|
test "it prunes old objects from the database" do
|
|
|
|
insert(:note)
|
|
|
|
deadline = Pleroma.Config.get([:instance, :remote_post_retention_days]) + 1
|
|
|
|
|
|
|
|
date =
|
|
|
|
Timex.now()
|
|
|
|
|> Timex.shift(days: -deadline)
|
|
|
|
|> Timex.to_naive_datetime()
|
|
|
|
|> NaiveDateTime.truncate(:second)
|
|
|
|
|
|
|
|
%{id: id} =
|
|
|
|
:note
|
|
|
|
|> insert()
|
|
|
|
|> Ecto.Changeset.change(%{inserted_at: date})
|
|
|
|
|> Repo.update!()
|
|
|
|
|
|
|
|
assert length(Repo.all(Object)) == 2
|
|
|
|
|
|
|
|
Mix.Tasks.Pleroma.Database.run(["prune_objects"])
|
|
|
|
|
|
|
|
assert length(Repo.all(Object)) == 1
|
|
|
|
refute Object.get_by_id(id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-16 13:04:08 -07:00
|
|
|
describe "running update_users_following_followers_counts" do
|
|
|
|
test "following and followers count are updated" do
|
|
|
|
[user, user2] = insert_pair(:user)
|
2019-10-24 00:42:14 -07:00
|
|
|
{:ok, %User{} = user} = User.follow(user, user2)
|
2019-10-10 12:35:32 -07:00
|
|
|
|
|
|
|
following = User.following(user)
|
2019-05-16 13:04:08 -07:00
|
|
|
|
|
|
|
assert length(following) == 2
|
2019-10-16 11:59:21 -07:00
|
|
|
assert user.follower_count == 0
|
2019-05-16 13:04:08 -07:00
|
|
|
|
|
|
|
{:ok, user} =
|
|
|
|
user
|
2019-10-24 00:42:14 -07:00
|
|
|
|> Ecto.Changeset.change(%{follower_count: 3})
|
2019-05-16 13:04:08 -07:00
|
|
|
|> Repo.update()
|
|
|
|
|
2019-10-16 11:59:21 -07:00
|
|
|
assert user.follower_count == 3
|
2019-05-16 13:04:08 -07:00
|
|
|
|
|
|
|
assert :ok == Mix.Tasks.Pleroma.Database.run(["update_users_following_followers_counts"])
|
|
|
|
|
|
|
|
user = User.get_by_id(user.id)
|
|
|
|
|
2019-10-10 12:35:32 -07:00
|
|
|
assert length(User.following(user)) == 2
|
2019-10-16 11:59:21 -07:00
|
|
|
assert user.follower_count == 0
|
2019-05-16 13:04:08 -07:00
|
|
|
end
|
|
|
|
end
|
2019-08-10 11:47:40 -07:00
|
|
|
|
|
|
|
describe "running fix_likes_collections" do
|
|
|
|
test "it turns OrderedCollection likes into empty arrays" do
|
|
|
|
[user, user2] = insert_pair(:user)
|
|
|
|
|
2020-05-12 12:59:26 -07:00
|
|
|
{:ok, %{id: id, object: object}} = CommonAPI.post(user, %{status: "test"})
|
|
|
|
{:ok, %{object: object2}} = CommonAPI.post(user, %{status: "test test"})
|
2019-08-10 11:47:40 -07:00
|
|
|
|
2019-10-16 07:16:39 -07:00
|
|
|
CommonAPI.favorite(user2, id)
|
2019-08-10 11:47:40 -07:00
|
|
|
|
|
|
|
likes = %{
|
|
|
|
"first" =>
|
|
|
|
"http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
|
|
|
|
"id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
|
|
|
|
"totalItems" => 3,
|
|
|
|
"type" => "OrderedCollection"
|
|
|
|
}
|
|
|
|
|
|
|
|
new_data = Map.put(object2.data, "likes", likes)
|
|
|
|
|
|
|
|
object2
|
|
|
|
|> Ecto.Changeset.change(%{data: new_data})
|
|
|
|
|> Repo.update()
|
|
|
|
|
|
|
|
assert length(Object.get_by_id(object.id).data["likes"]) == 1
|
|
|
|
assert is_map(Object.get_by_id(object2.id).data["likes"])
|
|
|
|
|
|
|
|
assert :ok == Mix.Tasks.Pleroma.Database.run(["fix_likes_collections"])
|
|
|
|
|
|
|
|
assert length(Object.get_by_id(object.id).data["likes"]) == 1
|
|
|
|
assert Enum.empty?(Object.get_by_id(object2.id).data["likes"])
|
|
|
|
end
|
|
|
|
end
|
2020-08-08 05:29:40 -07:00
|
|
|
|
|
|
|
describe "ensure_expiration" do
|
|
|
|
test "it adds to expiration old statuses" do
|
2020-08-22 10:46:01 -07:00
|
|
|
activity1 = insert(:note_activity)
|
2020-08-08 05:29:40 -07:00
|
|
|
|
2020-08-22 10:46:01 -07:00
|
|
|
{:ok, inserted_at, 0} = DateTime.from_iso8601("2015-01-23T23:50:07Z")
|
|
|
|
activity2 = insert(:note_activity, %{inserted_at: inserted_at})
|
2020-08-08 05:29:40 -07:00
|
|
|
|
2020-08-22 10:46:01 -07:00
|
|
|
%{id: activity_id3} = insert(:note_activity)
|
2020-08-08 05:29:40 -07:00
|
|
|
|
2020-08-22 10:46:01 -07:00
|
|
|
expires_at = DateTime.add(DateTime.utc_now(), 60 * 61)
|
2020-08-08 05:29:40 -07:00
|
|
|
|
2020-08-22 10:46:01 -07:00
|
|
|
Pleroma.Workers.PurgeExpiredActivity.enqueue(%{
|
|
|
|
activity_id: activity_id3,
|
|
|
|
expires_at: expires_at
|
|
|
|
})
|
2020-08-08 05:29:40 -07:00
|
|
|
|
|
|
|
Mix.Tasks.Pleroma.Database.run(["ensure_expiration"])
|
|
|
|
|
2020-08-22 10:46:01 -07:00
|
|
|
assert_enqueued(
|
|
|
|
worker: Pleroma.Workers.PurgeExpiredActivity,
|
|
|
|
args: %{activity_id: activity1.id},
|
|
|
|
scheduled_at:
|
|
|
|
activity1.inserted_at
|
|
|
|
|> DateTime.from_naive!("Etc/UTC")
|
|
|
|
|> Timex.shift(days: 365)
|
|
|
|
)
|
|
|
|
|
|
|
|
assert_enqueued(
|
|
|
|
worker: Pleroma.Workers.PurgeExpiredActivity,
|
|
|
|
args: %{activity_id: activity2.id},
|
|
|
|
scheduled_at:
|
|
|
|
activity2.inserted_at
|
|
|
|
|> DateTime.from_naive!("Etc/UTC")
|
|
|
|
|> Timex.shift(days: 365)
|
|
|
|
)
|
|
|
|
|
|
|
|
assert_enqueued(
|
|
|
|
worker: Pleroma.Workers.PurgeExpiredActivity,
|
|
|
|
args: %{activity_id: activity_id3},
|
|
|
|
scheduled_at: expires_at
|
|
|
|
)
|
2020-08-08 05:29:40 -07:00
|
|
|
end
|
|
|
|
end
|
2019-05-16 13:04:08 -07:00
|
|
|
end
|