logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

impl_test.exs (11655B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.Push.ImplTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Notification
  7. alias Pleroma.Object
  8. alias Pleroma.User
  9. alias Pleroma.Web.ActivityPub.ActivityPub
  10. alias Pleroma.Web.CommonAPI
  11. alias Pleroma.Web.Push.Impl
  12. alias Pleroma.Web.Push.Subscription
  13. alias Pleroma.Web.WebPushHttpClientMock
  14. import Mock
  15. import Pleroma.Factory
  16. setup do
  17. Tesla.Mock.mock(fn
  18. %{method: :post, url: "https://example.com/example/1234"} ->
  19. %Tesla.Env{status: 200}
  20. %{method: :post, url: "https://example.com/example/not_found"} ->
  21. %Tesla.Env{status: 400}
  22. %{method: :post, url: "https://example.com/example/bad"} ->
  23. %Tesla.Env{status: 100}
  24. end)
  25. :ok
  26. end
  27. @sub %{
  28. endpoint: "https://example.com/example/1234",
  29. keys: %{
  30. auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
  31. p256dh:
  32. "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
  33. }
  34. }
  35. @api_key "BASgACIHpN1GYgzSRp"
  36. @message "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
  37. test "performs sending notifications" do
  38. user = insert(:user)
  39. user2 = insert(:user)
  40. insert(:push_subscription, user: user, data: %{alerts: %{"mention" => true}})
  41. insert(:push_subscription, user: user2, data: %{alerts: %{"mention" => true}})
  42. insert(:push_subscription,
  43. user: user,
  44. data: %{alerts: %{"follow" => true, "mention" => true}}
  45. )
  46. insert(:push_subscription,
  47. user: user,
  48. data: %{alerts: %{"follow" => true, "mention" => false}}
  49. )
  50. {:ok, activity} = CommonAPI.post(user, %{status: "<Lorem ipsum dolor sit amet."})
  51. notif =
  52. insert(:notification,
  53. user: user,
  54. activity: activity,
  55. type: "mention"
  56. )
  57. assert Impl.perform(notif) == {:ok, [:ok, :ok]}
  58. end
  59. @tag capture_log: true
  60. test "returns error if notif does not match " do
  61. assert Impl.perform(%{}) == {:error, :unknown_type}
  62. end
  63. test "successful message sending" do
  64. assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
  65. end
  66. test_with_mock "uses WebPushHttpClientMock as an HTTP client", WebPushHttpClientMock,
  67. post: fn _, _, _ -> {:ok, %{status_code: 200}} end do
  68. Impl.push_message(@message, @sub, @api_key, %Subscription{})
  69. assert_called(WebPushHttpClientMock.post("https://example.com/example/1234", :_, :_))
  70. end
  71. test_with_mock "uses Pleroma.HTTP as an HTTP client", Pleroma.HTTP,
  72. post: fn _, _, _ -> {:ok, %{status_code: 200}} end do
  73. client = Application.get_env(:web_push_encryption, :http_client)
  74. on_exit(fn -> Application.put_env(:web_push_encryption, :http_client, client) end)
  75. Application.put_env(:web_push_encryption, :http_client, Pleroma.HTTP)
  76. Impl.push_message(@message, @sub, @api_key, %Subscription{})
  77. assert_called(Pleroma.HTTP.post("https://example.com/example/1234", :_, :_))
  78. end
  79. @tag capture_log: true
  80. test "fail message sending" do
  81. assert Impl.push_message(
  82. @message,
  83. Map.merge(@sub, %{endpoint: "https://example.com/example/bad"}),
  84. @api_key,
  85. %Subscription{}
  86. ) == :error
  87. end
  88. test "delete subscription if result send message between 400..500" do
  89. subscription = insert(:push_subscription)
  90. assert Impl.push_message(
  91. @message,
  92. Map.merge(@sub, %{endpoint: "https://example.com/example/not_found"}),
  93. @api_key,
  94. subscription
  95. ) == :ok
  96. refute Pleroma.Repo.get(Subscription, subscription.id)
  97. end
  98. test "deletes subscription when token has been deleted" do
  99. subscription = insert(:push_subscription)
  100. Pleroma.Repo.delete(subscription.token)
  101. refute Pleroma.Repo.get(Subscription, subscription.id)
  102. end
  103. test "renders title and body for create activity" do
  104. user = insert(:user, nickname: "Bob")
  105. {:ok, activity} =
  106. CommonAPI.post(user, %{
  107. status:
  108. "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
  109. })
  110. object = Object.normalize(activity)
  111. assert Impl.format_body(
  112. %{
  113. activity: activity
  114. },
  115. user,
  116. object
  117. ) ==
  118. "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
  119. assert Impl.format_title(%{activity: activity, type: "mention"}) ==
  120. "New Mention"
  121. end
  122. test "renders title and body for follow activity" do
  123. user = insert(:user, nickname: "Bob")
  124. other_user = insert(:user)
  125. {:ok, _, _, activity} = CommonAPI.follow(user, other_user)
  126. object = Object.normalize(activity, false)
  127. assert Impl.format_body(%{activity: activity, type: "follow"}, user, object) ==
  128. "@Bob has followed you"
  129. assert Impl.format_title(%{activity: activity, type: "follow"}) ==
  130. "New Follower"
  131. end
  132. test "renders title and body for announce activity" do
  133. user = insert(:user)
  134. {:ok, activity} =
  135. CommonAPI.post(user, %{
  136. status:
  137. "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
  138. })
  139. {:ok, announce_activity} = CommonAPI.repeat(activity.id, user)
  140. object = Object.normalize(activity)
  141. assert Impl.format_body(%{activity: announce_activity}, user, object) ==
  142. "@#{user.nickname} repeated: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
  143. assert Impl.format_title(%{activity: announce_activity, type: "reblog"}) ==
  144. "New Repeat"
  145. end
  146. test "renders title and body for like activity" do
  147. user = insert(:user, nickname: "Bob")
  148. {:ok, activity} =
  149. CommonAPI.post(user, %{
  150. status:
  151. "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
  152. })
  153. {:ok, activity} = CommonAPI.favorite(user, activity.id)
  154. object = Object.normalize(activity)
  155. assert Impl.format_body(%{activity: activity, type: "favourite"}, user, object) ==
  156. "@Bob has favorited your post"
  157. assert Impl.format_title(%{activity: activity, type: "favourite"}) ==
  158. "New Favorite"
  159. end
  160. test "renders title for create activity with direct visibility" do
  161. user = insert(:user, nickname: "Bob")
  162. {:ok, activity} =
  163. CommonAPI.post(user, %{
  164. visibility: "direct",
  165. status: "This is just between you and me, pal"
  166. })
  167. assert Impl.format_title(%{activity: activity}) ==
  168. "New Direct Message"
  169. end
  170. describe "build_content/3" do
  171. test "builds content for chat messages" do
  172. user = insert(:user)
  173. recipient = insert(:user)
  174. {:ok, chat} = CommonAPI.post_chat_message(user, recipient, "hey")
  175. object = Object.normalize(chat, false)
  176. [notification] = Notification.for_user(recipient)
  177. res = Impl.build_content(notification, user, object)
  178. assert res == %{
  179. body: "@#{user.nickname}: hey",
  180. title: "New Chat Message"
  181. }
  182. end
  183. test "builds content for chat messages with no content" do
  184. user = insert(:user)
  185. recipient = insert(:user)
  186. file = %Plug.Upload{
  187. content_type: "image/jpg",
  188. path: Path.absname("test/fixtures/image.jpg"),
  189. filename: "an_image.jpg"
  190. }
  191. {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
  192. {:ok, chat} = CommonAPI.post_chat_message(user, recipient, nil, media_id: upload.id)
  193. object = Object.normalize(chat, false)
  194. [notification] = Notification.for_user(recipient)
  195. res = Impl.build_content(notification, user, object)
  196. assert res == %{
  197. body: "@#{user.nickname}: (Attachment)",
  198. title: "New Chat Message"
  199. }
  200. end
  201. test "hides contents of notifications when option enabled" do
  202. user = insert(:user, nickname: "Bob")
  203. user2 =
  204. insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: true})
  205. {:ok, activity} =
  206. CommonAPI.post(user, %{
  207. visibility: "direct",
  208. status: "<Lorem ipsum dolor sit amet."
  209. })
  210. notif = insert(:notification, user: user2, activity: activity)
  211. actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
  212. object = Object.normalize(activity)
  213. assert Impl.build_content(notif, actor, object) == %{
  214. body: "New Direct Message"
  215. }
  216. {:ok, activity} =
  217. CommonAPI.post(user, %{
  218. visibility: "public",
  219. status: "<Lorem ipsum dolor sit amet."
  220. })
  221. notif = insert(:notification, user: user2, activity: activity, type: "mention")
  222. actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
  223. object = Object.normalize(activity)
  224. assert Impl.build_content(notif, actor, object) == %{
  225. body: "New Mention"
  226. }
  227. {:ok, activity} = CommonAPI.favorite(user, activity.id)
  228. notif = insert(:notification, user: user2, activity: activity, type: "favourite")
  229. actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
  230. object = Object.normalize(activity)
  231. assert Impl.build_content(notif, actor, object) == %{
  232. body: "New Favorite"
  233. }
  234. end
  235. test "returns regular content when hiding contents option disabled" do
  236. user = insert(:user, nickname: "Bob")
  237. user2 =
  238. insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: false})
  239. {:ok, activity} =
  240. CommonAPI.post(user, %{
  241. visibility: "direct",
  242. status:
  243. "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
  244. })
  245. notif = insert(:notification, user: user2, activity: activity)
  246. actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
  247. object = Object.normalize(activity)
  248. assert Impl.build_content(notif, actor, object) == %{
  249. body:
  250. "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
  251. title: "New Direct Message"
  252. }
  253. {:ok, activity} =
  254. CommonAPI.post(user, %{
  255. visibility: "public",
  256. status:
  257. "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
  258. })
  259. notif = insert(:notification, user: user2, activity: activity, type: "mention")
  260. actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
  261. object = Object.normalize(activity)
  262. assert Impl.build_content(notif, actor, object) == %{
  263. body:
  264. "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
  265. title: "New Mention"
  266. }
  267. {:ok, activity} = CommonAPI.favorite(user, activity.id)
  268. notif = insert(:notification, user: user2, activity: activity, type: "favourite")
  269. actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
  270. object = Object.normalize(activity)
  271. assert Impl.build_content(notif, actor, object) == %{
  272. body: "@Bob has favorited your post",
  273. title: "New Favorite"
  274. }
  275. end
  276. end
  277. end