Merge branch 'develop' into 'remove-avatar-header'
[akkoma] / test / notification_test.exs
index 581db58a80296f51817ed2260a0b874440bd6eb8..1d36f14bfcb076de8b113a6371ad6bf606f61f58 100644 (file)
@@ -8,6 +8,7 @@ defmodule Pleroma.NotificationTest do
   alias Pleroma.User
   alias Pleroma.Web.ActivityPub.Transmogrifier
   alias Pleroma.Web.CommonAPI
+  alias Pleroma.Web.Streamer
   alias Pleroma.Web.TwitterAPI.TwitterAPI
   import Pleroma.Factory
 
@@ -44,13 +45,42 @@ defmodule Pleroma.NotificationTest do
   end
 
   describe "create_notification" do
+    setup do
+      GenServer.start(Streamer, %{}, name: Streamer)
+
+      on_exit(fn ->
+        if pid = Process.whereis(Streamer) do
+          Process.exit(pid, :kill)
+        end
+      end)
+    end
+
+    test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do
+      user = insert(:user)
+      task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
+      task_user_notification = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
+      Streamer.add_socket("user", %{transport_pid: task.pid, assigns: %{user: user}})
+
+      Streamer.add_socket(
+        "user:notification",
+        %{transport_pid: task_user_notification.pid, assigns: %{user: user}}
+      )
+
+      activity = insert(:note_activity)
+
+      notify = Notification.create_notification(activity, user)
+      assert notify.user_id == user.id
+      Task.await(task)
+      Task.await(task_user_notification)
+    end
+
     test "it doesn't create a notification for user if the user blocks the activity author" do
       activity = insert(:note_activity)
       author = User.get_cached_by_ap_id(activity.data["actor"])
       user = insert(:user)
       {:ok, user} = User.block(user, author)
 
-      assert nil == Notification.create_notification(activity, user)
+      refute Notification.create_notification(activity, user)
     end
 
     test "it doesn't create a notificatin for the user if the user mutes the activity author" do
@@ -60,7 +90,7 @@ defmodule Pleroma.NotificationTest do
       muter = Repo.get(User, muter.id)
       {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
 
-      assert nil == Notification.create_notification(activity, muter)
+      refute Notification.create_notification(activity, muter)
     end
 
     test "it doesn't create a notification for an activity from a muted thread" do
@@ -75,34 +105,7 @@ defmodule Pleroma.NotificationTest do
           "in_reply_to_status_id" => activity.id
         })
 
-      assert nil == Notification.create_notification(activity, muter)
-    end
-
-    test "it disables notifications from people on remote instances" do
-      user = insert(:user, info: %{notification_settings: %{"remote" => false}})
-      other_user = insert(:user)
-
-      create_activity = %{
-        "@context" => "https://www.w3.org/ns/activitystreams",
-        "type" => "Create",
-        "to" => ["https://www.w3.org/ns/activitystreams#Public"],
-        "actor" => other_user.ap_id,
-        "object" => %{
-          "type" => "Note",
-          "content" => "Hi @#{user.nickname}",
-          "attributedTo" => other_user.ap_id
-        }
-      }
-
-      {:ok, %{local: false} = activity} = Transmogrifier.handle_incoming(create_activity)
-      assert nil == Notification.create_notification(activity, user)
-    end
-
-    test "it disables notifications from people on the local instance" do
-      user = insert(:user, info: %{notification_settings: %{"local" => false}})
-      other_user = insert(:user)
-      {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})
-      assert nil == Notification.create_notification(activity, user)
+      refute Notification.create_notification(activity, muter)
     end
 
     test "it disables notifications from followers" do
@@ -110,7 +113,14 @@ defmodule Pleroma.NotificationTest do
       followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
       User.follow(follower, followed)
       {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
-      assert nil == Notification.create_notification(activity, followed)
+      refute Notification.create_notification(activity, followed)
+    end
+
+    test "it disables notifications from non-followers" do
+      follower = insert(:user)
+      followed = insert(:user, info: %{notification_settings: %{"non_followers" => false}})
+      {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
+      refute Notification.create_notification(activity, followed)
     end
 
     test "it disables notifications from people the user follows" do
@@ -119,14 +129,21 @@ defmodule Pleroma.NotificationTest do
       User.follow(follower, followed)
       follower = Repo.get(User, follower.id)
       {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
-      assert nil == Notification.create_notification(activity, follower)
+      refute Notification.create_notification(activity, follower)
+    end
+
+    test "it disables notifications from people the user does not follow" do
+      follower = insert(:user, info: %{notification_settings: %{"non_follows" => false}})
+      followed = insert(:user)
+      {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
+      refute Notification.create_notification(activity, follower)
     end
 
     test "it doesn't create a notification for user if he is the activity author" do
       activity = insert(:note_activity)
       author = User.get_cached_by_ap_id(activity.data["actor"])
 
-      assert nil == Notification.create_notification(activity, author)
+      refute Notification.create_notification(activity, author)
     end
 
     test "it doesn't create a notification for follow-unfollow-follow chains" do
@@ -136,7 +153,7 @@ defmodule Pleroma.NotificationTest do
       Notification.create_notification(activity, followed_user)
       TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
       {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
-      assert nil == Notification.create_notification(activity_dupe, followed_user)
+      refute Notification.create_notification(activity_dupe, followed_user)
     end
 
     test "it doesn't create a notification for like-unlike-like chains" do
@@ -147,7 +164,7 @@ defmodule Pleroma.NotificationTest do
       Notification.create_notification(fav_status, liked_user)
       TwitterAPI.unfav(user, status.id)
       {:ok, dupe} = TwitterAPI.fav(user, status.id)
-      assert nil == Notification.create_notification(dupe, liked_user)
+      refute Notification.create_notification(dupe, liked_user)
     end
 
     test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
@@ -163,7 +180,7 @@ defmodule Pleroma.NotificationTest do
       Notification.create_notification(retweeted_activity, retweeted_user)
       TwitterAPI.unrepeat(user, status.id)
       {:ok, dupe} = TwitterAPI.repeat(user, status.id)
-      assert nil == Notification.create_notification(dupe, retweeted_user)
+      refute Notification.create_notification(dupe, retweeted_user)
     end
 
     test "it doesn't create duplicate notifications for follow+subscribed users" do