From 75a5dd41ee4a1c196487f4cf2759a4d63bc393ef Mon Sep 17 00:00:00 2001
From: Sergey Suprunenko <suprunenko.s@gmail.com>
Date: Mon, 19 Aug 2019 16:10:00 +0000
Subject: [PATCH] Add more tests for Database tasks and DigestEmailWorker

---
 lib/pleroma/digest_email_worker.ex            |  4 ++
 test/tasks/database_test.exs                  | 47 +++++++++++++++++++
 .../digest_test.exs}                          |  0
 test/web/digest_email_worker_test.exs         | 31 ++++++++++++
 4 files changed, 82 insertions(+)
 rename test/{mix/tasks/pleroma.digest_test.exs => tasks/digest_test.exs} (100%)
 create mode 100644 test/web/digest_email_worker_test.exs

diff --git a/lib/pleroma/digest_email_worker.ex b/lib/pleroma/digest_email_worker.ex
index 18e67d39b..5644d6a67 100644
--- a/lib/pleroma/digest_email_worker.ex
+++ b/lib/pleroma/digest_email_worker.ex
@@ -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
 
diff --git a/test/tasks/database_test.exs b/test/tasks/database_test.exs
index a8f25f500..a9925c361 100644
--- a/test/tasks/database_test.exs
+++ b/test/tasks/database_test.exs
@@ -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/mix/tasks/pleroma.digest_test.exs b/test/tasks/digest_test.exs
similarity index 100%
rename from test/mix/tasks/pleroma.digest_test.exs
rename to test/tasks/digest_test.exs
diff --git a/test/web/digest_email_worker_test.exs b/test/web/digest_email_worker_test.exs
new file mode 100644
index 000000000..15002330f
--- /dev/null
+++ b/test/web/digest_email_worker_test.exs
@@ -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