logo

pleroma

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

database_test.exs (4988B)


  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 Mix.Tasks.Pleroma.DatabaseTest do
  5. use Pleroma.DataCase
  6. use Oban.Testing, repo: Pleroma.Repo
  7. alias Pleroma.Activity
  8. alias Pleroma.Object
  9. alias Pleroma.Repo
  10. alias Pleroma.User
  11. alias Pleroma.Web.CommonAPI
  12. import Pleroma.Factory
  13. setup_all do
  14. Mix.shell(Mix.Shell.Process)
  15. on_exit(fn ->
  16. Mix.shell(Mix.Shell.IO)
  17. end)
  18. :ok
  19. end
  20. describe "running remove_embedded_objects" do
  21. test "it replaces objects with references" do
  22. user = insert(:user)
  23. {:ok, activity} = CommonAPI.post(user, %{status: "test"})
  24. new_data = Map.put(activity.data, "object", activity.object.data)
  25. {:ok, activity} =
  26. activity
  27. |> Activity.change(%{data: new_data})
  28. |> Repo.update()
  29. assert is_map(activity.data["object"])
  30. Mix.Tasks.Pleroma.Database.run(["remove_embedded_objects"])
  31. activity = Activity.get_by_id_with_object(activity.id)
  32. assert is_binary(activity.data["object"])
  33. end
  34. end
  35. describe "prune_objects" do
  36. test "it prunes old objects from the database" do
  37. insert(:note)
  38. deadline = Pleroma.Config.get([:instance, :remote_post_retention_days]) + 1
  39. date =
  40. Timex.now()
  41. |> Timex.shift(days: -deadline)
  42. |> Timex.to_naive_datetime()
  43. |> NaiveDateTime.truncate(:second)
  44. %{id: id} =
  45. :note
  46. |> insert()
  47. |> Ecto.Changeset.change(%{inserted_at: date})
  48. |> Repo.update!()
  49. assert length(Repo.all(Object)) == 2
  50. Mix.Tasks.Pleroma.Database.run(["prune_objects"])
  51. assert length(Repo.all(Object)) == 1
  52. refute Object.get_by_id(id)
  53. end
  54. end
  55. describe "running update_users_following_followers_counts" do
  56. test "following and followers count are updated" do
  57. [user, user2] = insert_pair(:user)
  58. {:ok, %User{} = user} = User.follow(user, user2)
  59. following = User.following(user)
  60. assert length(following) == 2
  61. assert user.follower_count == 0
  62. {:ok, user} =
  63. user
  64. |> Ecto.Changeset.change(%{follower_count: 3})
  65. |> Repo.update()
  66. assert user.follower_count == 3
  67. assert :ok == Mix.Tasks.Pleroma.Database.run(["update_users_following_followers_counts"])
  68. user = User.get_by_id(user.id)
  69. assert length(User.following(user)) == 2
  70. assert user.follower_count == 0
  71. end
  72. end
  73. describe "running fix_likes_collections" do
  74. test "it turns OrderedCollection likes into empty arrays" do
  75. [user, user2] = insert_pair(:user)
  76. {:ok, %{id: id, object: object}} = CommonAPI.post(user, %{status: "test"})
  77. {:ok, %{object: object2}} = CommonAPI.post(user, %{status: "test test"})
  78. CommonAPI.favorite(user2, id)
  79. likes = %{
  80. "first" =>
  81. "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
  82. "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
  83. "totalItems" => 3,
  84. "type" => "OrderedCollection"
  85. }
  86. new_data = Map.put(object2.data, "likes", likes)
  87. object2
  88. |> Ecto.Changeset.change(%{data: new_data})
  89. |> Repo.update()
  90. assert length(Object.get_by_id(object.id).data["likes"]) == 1
  91. assert is_map(Object.get_by_id(object2.id).data["likes"])
  92. assert :ok == Mix.Tasks.Pleroma.Database.run(["fix_likes_collections"])
  93. assert length(Object.get_by_id(object.id).data["likes"]) == 1
  94. assert Enum.empty?(Object.get_by_id(object2.id).data["likes"])
  95. end
  96. end
  97. describe "ensure_expiration" do
  98. test "it adds to expiration old statuses" do
  99. activity1 = insert(:note_activity)
  100. {:ok, inserted_at, 0} = DateTime.from_iso8601("2015-01-23T23:50:07Z")
  101. activity2 = insert(:note_activity, %{inserted_at: inserted_at})
  102. %{id: activity_id3} = insert(:note_activity)
  103. expires_at = DateTime.add(DateTime.utc_now(), 60 * 61)
  104. Pleroma.Workers.PurgeExpiredActivity.enqueue(%{
  105. activity_id: activity_id3,
  106. expires_at: expires_at
  107. })
  108. Mix.Tasks.Pleroma.Database.run(["ensure_expiration"])
  109. assert_enqueued(
  110. worker: Pleroma.Workers.PurgeExpiredActivity,
  111. args: %{activity_id: activity1.id},
  112. scheduled_at:
  113. activity1.inserted_at
  114. |> DateTime.from_naive!("Etc/UTC")
  115. |> Timex.shift(days: 365)
  116. )
  117. assert_enqueued(
  118. worker: Pleroma.Workers.PurgeExpiredActivity,
  119. args: %{activity_id: activity2.id},
  120. scheduled_at:
  121. activity2.inserted_at
  122. |> DateTime.from_naive!("Etc/UTC")
  123. |> Timex.shift(days: 365)
  124. )
  125. assert_enqueued(
  126. worker: Pleroma.Workers.PurgeExpiredActivity,
  127. args: %{activity_id: activity_id3},
  128. scheduled_at: expires_at
  129. )
  130. end
  131. end
  132. end