Merge develop
[akkoma] / test / notification_test.exs
index 27d8cace706aac541da9023d2169ab4def35f2bb..eec0a48292ccb382aa587c32775cc909fe6715cb 100644 (file)
@@ -6,7 +6,6 @@ defmodule Pleroma.NotificationTest do
   use Pleroma.DataCase
 
   import Pleroma.Factory
-  import Mock
 
   alias Pleroma.Notification
   alias Pleroma.User
@@ -49,7 +48,7 @@ defmodule Pleroma.NotificationTest do
   describe "create_notification" do
     test "it doesn't create a notification for user if the user blocks the activity author" do
       activity = insert(:note_activity)
-      author = User.get_by_ap_id(activity.data["actor"])
+      author = User.get_cached_by_ap_id(activity.data["actor"])
       user = insert(:user)
       {:ok, user} = User.block(user, author)
 
@@ -127,7 +126,7 @@ defmodule Pleroma.NotificationTest do
 
     test "it doesn't create a notification for user if he is the activity author" do
       activity = insert(:note_activity)
-      author = User.get_by_ap_id(activity.data["actor"])
+      author = User.get_cached_by_ap_id(activity.data["actor"])
 
       assert nil == Notification.create_notification(activity, author)
     end
@@ -303,31 +302,50 @@ defmodule Pleroma.NotificationTest do
       assert n2.seen == true
       assert n3.seen == false
     end
+  end
+
+  describe "for_user_since/2" do
+    defp days_ago(days) do
+      NaiveDateTime.add(
+        NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
+        -days * 60 * 60 * 24,
+        :second
+      )
+    end
 
-    test "Updates `updated_at` field" do
+    test "Returns recent notifications" do
       user1 = insert(:user)
       user2 = insert(:user)
 
       Enum.each(0..10, fn i ->
         {:ok, _activity} =
-          TwitterAPI.create_status(user1, %{
-            "status" => "#{i} hi @#{user2.nickname}"
+          CommonAPI.post(user1, %{
+            "status" => "hey ##{i} @#{user2.nickname}!"
           })
       end)
 
-      [notification | _] = Notification.for_user(user2)
+      {old, new} = Enum.split(Notification.for_user(user2), 5)
 
-      utc_now = NaiveDateTime.utc_now()
-      future = NaiveDateTime.add(utc_now, 5, :second)
+      Enum.each(old, fn notification ->
+        notification
+        |> cast(%{updated_at: days_ago(10)}, [:updated_at])
+        |> Pleroma.Repo.update!()
+      end)
 
-      with_mock NaiveDateTime, utc_now: fn -> future end do
-        Notification.set_read_up_to(user2, notification.id)
+      recent_notifications_ids =
+        user2
+        |> Notification.for_user_since(
+          NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
+        )
+        |> Enum.map(& &1.id)
 
-        Notification.for_user(user2)
-        |> Enum.each(fn notification ->
-          assert notification.updated_at > notification.inserted_at
-        end)
-      end
+      Enum.each(old, fn %{id: id} ->
+        refute id in recent_notifications_ids
+      end)
+
+      Enum.each(new, fn %{id: id} ->
+        assert id in recent_notifications_ids
+      end)
     end
   end