Merge remote-tracking branch 'upstream/develop' into email-fix-develop
[akkoma] / test / web / push / impl_test.exs
index 26c65bc8207c8b941b9708c931e82eda4c121382..c7c17e156185d17d698a39f84bc78cd08e30f902 100644 (file)
@@ -5,12 +5,16 @@
 defmodule Pleroma.Web.Push.ImplTest do
   use Pleroma.DataCase
 
+  alias Pleroma.Notification
   alias Pleroma.Object
   alias Pleroma.User
+  alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.CommonAPI
   alias Pleroma.Web.Push.Impl
   alias Pleroma.Web.Push.Subscription
+  alias Pleroma.Web.WebPushHttpClientMock
 
+  import Mock
   import Pleroma.Factory
 
   setup do
@@ -76,6 +80,22 @@ defmodule Pleroma.Web.Push.ImplTest do
     assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
   end
 
+  test_with_mock "uses WebPushHttpClientMock as an HTTP client", WebPushHttpClientMock,
+    post: fn _, _, _ -> {:ok, %{status_code: 200}} end do
+    Impl.push_message(@message, @sub, @api_key, %Subscription{})
+    assert_called(WebPushHttpClientMock.post("https://example.com/example/1234", :_, :_))
+  end
+
+  test_with_mock "uses Pleroma.HTTP as an HTTP client", Pleroma.HTTP,
+    post: fn _, _, _ -> {:ok, %{status_code: 200}} end do
+    client = Application.get_env(:web_push_encryption, :http_client)
+    on_exit(fn -> Application.put_env(:web_push_encryption, :http_client, client) end)
+    Application.put_env(:web_push_encryption, :http_client, Pleroma.HTTP)
+
+    Impl.push_message(@message, @sub, @api_key, %Subscription{})
+    assert_called(Pleroma.HTTP.post("https://example.com/example/1234", :_, :_))
+  end
+
   @tag capture_log: true
   test "fail message sending" do
     assert Impl.push_message(
@@ -196,9 +216,51 @@ defmodule Pleroma.Web.Push.ImplTest do
   end
 
   describe "build_content/3" do
-    test "hides details for notifications when privacy option enabled" do
+    test "builds content for chat messages" do
+      user = insert(:user)
+      recipient = insert(:user)
+
+      {:ok, chat} = CommonAPI.post_chat_message(user, recipient, "hey")
+      object = Object.normalize(chat, false)
+      [notification] = Notification.for_user(recipient)
+
+      res = Impl.build_content(notification, user, object)
+
+      assert res == %{
+               body: "@#{user.nickname}: hey",
+               title: "New Chat Message"
+             }
+    end
+
+    test "builds content for chat messages with no content" do
+      user = insert(:user)
+      recipient = insert(:user)
+
+      file = %Plug.Upload{
+        content_type: "image/jpg",
+        path: Path.absname("test/fixtures/image.jpg"),
+        filename: "an_image.jpg"
+      }
+
+      {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
+
+      {:ok, chat} = CommonAPI.post_chat_message(user, recipient, nil, media_id: upload.id)
+      object = Object.normalize(chat, false)
+      [notification] = Notification.for_user(recipient)
+
+      res = Impl.build_content(notification, user, object)
+
+      assert res == %{
+               body: "@#{user.nickname}: (Attachment)",
+               title: "New Chat Message"
+             }
+    end
+
+    test "hides contents of notifications when option enabled" do
       user = insert(:user, nickname: "Bob")
-      user2 = insert(:user, nickname: "Rob", notification_settings: %{privacy_option: true})
+
+      user2 =
+        insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: true})
 
       {:ok, activity} =
         CommonAPI.post(user, %{
@@ -242,9 +304,11 @@ defmodule Pleroma.Web.Push.ImplTest do
              }
     end
 
-    test "returns regular content for notifications with privacy option disabled" do
+    test "returns regular content when hiding contents option disabled" do
       user = insert(:user, nickname: "Bob")
-      user2 = insert(:user, nickname: "Rob", notification_settings: %{privacy_option: false})
+
+      user2 =
+        insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: false})
 
       {:ok, activity} =
         CommonAPI.post(user, %{