Add more tests for Database tasks and DigestEmailWorker
authorSergey Suprunenko <suprunenko.s@gmail.com>
Mon, 19 Aug 2019 16:10:00 +0000 (16:10 +0000)
committerlain <lain@soykaf.club>
Mon, 19 Aug 2019 16:10:00 +0000 (16:10 +0000)
lib/pleroma/digest_email_worker.ex
test/tasks/database_test.exs
test/tasks/digest_test.exs [moved from test/mix/tasks/pleroma.digest_test.exs with 100% similarity]
test/web/digest_email_worker_test.exs [new file with mode: 0644]

index 18e67d39b07cd5d7176f36c95cdd84b539f1299c..5644d6a678d7a34385a6044ee9017d0cf6fe577b 100644 (file)
@@ -1,3 +1,7 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
 defmodule Pleroma.DigestEmailWorker do
   import Ecto.Query
 
index a8f25f500aca5077ba7c6f1a2391ecd50e9e08ca..a9925c36119de3c825ba5c388db56e463a9dd96d 100644 (file)
@@ -3,6 +3,7 @@
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Mix.Tasks.Pleroma.DatabaseTest do
+  alias Pleroma.Activity
   alias Pleroma.Object
   alias Pleroma.Repo
   alias Pleroma.User
@@ -22,6 +23,52 @@ defmodule Mix.Tasks.Pleroma.DatabaseTest do
     :ok
   end
 
+  describe "running remove_embedded_objects" do
+    test "it replaces objects with references" do
+      user = insert(:user)
+      {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
+      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
+
   describe "running update_users_following_followers_counts" do
     test "following and followers count are updated" do
       [user, user2] = insert_pair(:user)
diff --git a/test/web/digest_email_worker_test.exs b/test/web/digest_email_worker_test.exs
new file mode 100644 (file)
index 0000000..1500233
--- /dev/null
@@ -0,0 +1,31 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.DigestEmailWorkerTest do
+  use Pleroma.DataCase
+  import Pleroma.Factory
+
+  alias Pleroma.DigestEmailWorker
+  alias Pleroma.User
+  alias Pleroma.Web.CommonAPI
+
+  test "it sends digest emails" do
+    user = insert(:user)
+
+    date =
+      Timex.now()
+      |> Timex.shift(days: -10)
+      |> Timex.to_naive_datetime()
+
+    user2 = insert(:user, last_digest_emailed_at: date)
+    User.switch_email_notifications(user2, "digest", true)
+    CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}!"})
+
+    DigestEmailWorker.perform()
+
+    assert_received {:email, email}
+    assert email.to == [{user2.name, user2.email}]
+    assert email.subject == "Your digest from #{Pleroma.Config.get(:instance)[:name]}"
+  end
+end