2019-03-06 14:20:12 +01:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-02-25 15:04:28 +01:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-03-06 14:20:12 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.Push.ImplTest do
|
2020-12-21 12:21:40 +01:00
|
|
|
use Pleroma.DataCase, async: true
|
2019-03-06 14:20:12 +01:00
|
|
|
|
2020-10-01 12:32:11 +02:00
|
|
|
import Pleroma.Factory
|
|
|
|
|
2020-06-04 14:49:10 +02:00
|
|
|
alias Pleroma.Notification
|
2019-04-29 18:15:30 +02:00
|
|
|
alias Pleroma.Object
|
2019-10-29 19:33:17 +01:00
|
|
|
alias Pleroma.User
|
2020-06-06 09:46:07 +02:00
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
2019-04-29 18:15:30 +02:00
|
|
|
alias Pleroma.Web.CommonAPI
|
2019-03-06 14:20:12 +01:00
|
|
|
alias Pleroma.Web.Push.Impl
|
|
|
|
alias Pleroma.Web.Push.Subscription
|
|
|
|
|
2020-05-13 08:20:25 +02:00
|
|
|
setup do
|
|
|
|
Tesla.Mock.mock(fn
|
2019-03-06 14:20:12 +01:00
|
|
|
%{method: :post, url: "https://example.com/example/1234"} ->
|
|
|
|
%Tesla.Env{status: 200}
|
|
|
|
|
|
|
|
%{method: :post, url: "https://example.com/example/not_found"} ->
|
|
|
|
%Tesla.Env{status: 400}
|
|
|
|
|
|
|
|
%{method: :post, url: "https://example.com/example/bad"} ->
|
|
|
|
%Tesla.Env{status: 100}
|
|
|
|
end)
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
@sub %{
|
|
|
|
endpoint: "https://example.com/example/1234",
|
|
|
|
keys: %{
|
|
|
|
auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
|
|
|
|
p256dh:
|
|
|
|
"BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@api_key "BASgACIHpN1GYgzSRp"
|
|
|
|
@message "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
|
|
|
|
|
|
|
|
test "performs sending notifications" do
|
|
|
|
user = insert(:user)
|
|
|
|
user2 = insert(:user)
|
|
|
|
insert(:push_subscription, user: user, data: %{alerts: %{"mention" => true}})
|
|
|
|
insert(:push_subscription, user: user2, data: %{alerts: %{"mention" => true}})
|
|
|
|
|
|
|
|
insert(:push_subscription,
|
|
|
|
user: user,
|
|
|
|
data: %{alerts: %{"follow" => true, "mention" => true}}
|
|
|
|
)
|
|
|
|
|
|
|
|
insert(:push_subscription,
|
|
|
|
user: user,
|
|
|
|
data: %{alerts: %{"follow" => true, "mention" => false}}
|
|
|
|
)
|
|
|
|
|
2020-05-12 21:59:26 +02:00
|
|
|
{:ok, activity} = CommonAPI.post(user, %{status: "<Lorem ipsum dolor sit amet."})
|
2019-04-29 18:15:30 +02:00
|
|
|
|
2019-03-06 14:20:12 +01:00
|
|
|
notif =
|
|
|
|
insert(:notification,
|
|
|
|
user: user,
|
2020-06-02 14:22:16 +02:00
|
|
|
activity: activity,
|
|
|
|
type: "mention"
|
2019-03-06 14:20:12 +01:00
|
|
|
)
|
|
|
|
|
2020-04-22 13:28:34 +02:00
|
|
|
assert Impl.perform(notif) == {:ok, [:ok, :ok]}
|
2019-03-06 14:20:12 +01:00
|
|
|
end
|
|
|
|
|
2019-04-05 14:38:44 +02:00
|
|
|
@tag capture_log: true
|
2019-03-06 14:20:12 +01:00
|
|
|
test "returns error if notif does not match " do
|
2020-04-22 13:28:34 +02:00
|
|
|
assert Impl.perform(%{}) == {:error, :unknown_type}
|
2019-03-06 14:20:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test "successful message sending" do
|
|
|
|
assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
|
|
|
|
end
|
|
|
|
|
2019-04-05 14:38:44 +02:00
|
|
|
@tag capture_log: true
|
2019-03-06 14:20:12 +01:00
|
|
|
test "fail message sending" do
|
|
|
|
assert Impl.push_message(
|
|
|
|
@message,
|
|
|
|
Map.merge(@sub, %{endpoint: "https://example.com/example/bad"}),
|
|
|
|
@api_key,
|
|
|
|
%Subscription{}
|
|
|
|
) == :error
|
|
|
|
end
|
|
|
|
|
2019-10-22 01:35:43 +02:00
|
|
|
test "delete subscription if result send message between 400..500" do
|
2019-03-06 14:20:12 +01:00
|
|
|
subscription = insert(:push_subscription)
|
|
|
|
|
|
|
|
assert Impl.push_message(
|
|
|
|
@message,
|
|
|
|
Map.merge(@sub, %{endpoint: "https://example.com/example/not_found"}),
|
|
|
|
@api_key,
|
|
|
|
subscription
|
|
|
|
) == :ok
|
|
|
|
|
|
|
|
refute Pleroma.Repo.get(Subscription, subscription.id)
|
|
|
|
end
|
|
|
|
|
2020-02-25 15:04:28 +01:00
|
|
|
test "deletes subscription when token has been deleted" do
|
|
|
|
subscription = insert(:push_subscription)
|
|
|
|
|
|
|
|
Pleroma.Repo.delete(subscription.token)
|
|
|
|
|
|
|
|
refute Pleroma.Repo.get(Subscription, subscription.id)
|
|
|
|
end
|
|
|
|
|
2019-10-22 01:34:20 +02:00
|
|
|
test "renders title and body for create activity" do
|
2019-04-29 18:15:30 +02:00
|
|
|
user = insert(:user, nickname: "Bob")
|
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
2020-05-12 21:59:26 +02:00
|
|
|
status:
|
2019-04-29 18:15:30 +02:00
|
|
|
"<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
|
|
|
|
})
|
|
|
|
|
2021-01-04 13:38:31 +01:00
|
|
|
object = Object.normalize(activity, fetch: false)
|
2019-04-29 18:15:30 +02:00
|
|
|
|
2019-03-06 14:20:12 +01:00
|
|
|
assert Impl.format_body(
|
|
|
|
%{
|
2019-04-29 18:15:30 +02:00
|
|
|
activity: activity
|
2019-03-06 14:20:12 +01:00
|
|
|
},
|
2019-04-29 18:15:30 +02:00
|
|
|
user,
|
|
|
|
object
|
2019-03-06 14:20:12 +01:00
|
|
|
) ==
|
|
|
|
"@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
|
2019-10-22 01:34:20 +02:00
|
|
|
|
2020-06-02 14:22:16 +02:00
|
|
|
assert Impl.format_title(%{activity: activity, type: "mention"}) ==
|
2019-10-22 01:34:20 +02:00
|
|
|
"New Mention"
|
2019-03-06 14:20:12 +01:00
|
|
|
end
|
|
|
|
|
2019-10-22 01:34:20 +02:00
|
|
|
test "renders title and body for follow activity" do
|
2019-04-29 18:15:30 +02:00
|
|
|
user = insert(:user, nickname: "Bob")
|
|
|
|
other_user = insert(:user)
|
|
|
|
{:ok, _, _, activity} = CommonAPI.follow(user, other_user)
|
2021-01-04 13:38:31 +01:00
|
|
|
object = Object.normalize(activity, fetch: false)
|
2019-04-29 18:15:30 +02:00
|
|
|
|
2020-06-02 14:22:16 +02:00
|
|
|
assert Impl.format_body(%{activity: activity, type: "follow"}, user, object) ==
|
|
|
|
"@Bob has followed you"
|
2019-10-22 01:34:20 +02:00
|
|
|
|
2020-06-02 14:22:16 +02:00
|
|
|
assert Impl.format_title(%{activity: activity, type: "follow"}) ==
|
2019-10-22 01:34:20 +02:00
|
|
|
"New Follower"
|
2019-03-06 14:20:12 +01:00
|
|
|
end
|
|
|
|
|
2019-10-22 01:34:20 +02:00
|
|
|
test "renders title and body for announce activity" do
|
2019-03-06 14:20:12 +01:00
|
|
|
user = insert(:user)
|
|
|
|
|
2019-04-29 18:15:30 +02:00
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
2020-05-12 21:59:26 +02:00
|
|
|
status:
|
2019-04-29 18:15:30 +02:00
|
|
|
"<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
|
2019-03-06 14:20:12 +01:00
|
|
|
})
|
|
|
|
|
2020-05-21 13:16:21 +02:00
|
|
|
{:ok, announce_activity} = CommonAPI.repeat(activity.id, user)
|
2021-01-04 13:38:31 +01:00
|
|
|
object = Object.normalize(activity, fetch: false)
|
2019-03-06 14:20:12 +01:00
|
|
|
|
2019-04-29 18:15:30 +02:00
|
|
|
assert Impl.format_body(%{activity: announce_activity}, user, object) ==
|
2019-03-06 14:20:12 +01:00
|
|
|
"@#{user.nickname} repeated: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
|
2019-10-22 01:34:20 +02:00
|
|
|
|
2020-06-02 14:22:16 +02:00
|
|
|
assert Impl.format_title(%{activity: announce_activity, type: "reblog"}) ==
|
2019-10-22 01:34:20 +02:00
|
|
|
"New Repeat"
|
2019-03-06 14:20:12 +01:00
|
|
|
end
|
|
|
|
|
2019-10-22 01:34:20 +02:00
|
|
|
test "renders title and body for like activity" do
|
2019-04-29 18:15:30 +02:00
|
|
|
user = insert(:user, nickname: "Bob")
|
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
2020-05-12 21:59:26 +02:00
|
|
|
status:
|
2019-04-29 18:15:30 +02:00
|
|
|
"<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
|
|
|
|
})
|
|
|
|
|
2019-10-16 16:16:39 +02:00
|
|
|
{:ok, activity} = CommonAPI.favorite(user, activity.id)
|
2021-01-04 13:38:31 +01:00
|
|
|
object = Object.normalize(activity, fetch: false)
|
2019-04-29 18:15:30 +02:00
|
|
|
|
2020-06-02 14:22:16 +02:00
|
|
|
assert Impl.format_body(%{activity: activity, type: "favourite"}, user, object) ==
|
|
|
|
"@Bob has favorited your post"
|
2019-10-22 01:34:20 +02:00
|
|
|
|
2020-06-02 14:22:16 +02:00
|
|
|
assert Impl.format_title(%{activity: activity, type: "favourite"}) ==
|
2019-10-22 01:34:20 +02:00
|
|
|
"New Favorite"
|
|
|
|
end
|
|
|
|
|
2020-11-14 00:01:53 +01:00
|
|
|
test "renders title and body for pleroma:emoji_reaction activity" do
|
|
|
|
user = insert(:user, nickname: "Bob")
|
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
|
|
|
status: "This post is a really good post!"
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, activity} = CommonAPI.react_with_emoji(activity.id, user, "👍")
|
2021-01-04 13:38:31 +01:00
|
|
|
object = Object.normalize(activity, fetch: false)
|
2020-11-14 00:01:53 +01:00
|
|
|
|
|
|
|
assert Impl.format_body(%{activity: activity, type: "pleroma:emoji_reaction"}, user, object) ==
|
2020-11-17 20:51:37 +01:00
|
|
|
"@Bob reacted with 👍"
|
2020-11-14 00:01:53 +01:00
|
|
|
|
|
|
|
assert Impl.format_title(%{activity: activity, type: "pleroma:emoji_reaction"}) ==
|
|
|
|
"New Reaction"
|
|
|
|
end
|
|
|
|
|
2019-10-22 01:34:20 +02:00
|
|
|
test "renders title for create activity with direct visibility" do
|
|
|
|
user = insert(:user, nickname: "Bob")
|
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
2020-05-12 21:59:26 +02:00
|
|
|
visibility: "direct",
|
|
|
|
status: "This is just between you and me, pal"
|
2019-10-22 01:34:20 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
assert Impl.format_title(%{activity: activity}) ==
|
|
|
|
"New Direct Message"
|
2019-03-06 14:20:12 +01:00
|
|
|
end
|
2019-10-29 19:33:17 +01:00
|
|
|
|
|
|
|
describe "build_content/3" do
|
2020-06-04 14:49:10 +02:00
|
|
|
test "builds content for chat messages" do
|
|
|
|
user = insert(:user)
|
|
|
|
recipient = insert(:user)
|
|
|
|
|
|
|
|
{:ok, chat} = CommonAPI.post_chat_message(user, recipient, "hey")
|
2021-01-04 13:38:31 +01:00
|
|
|
object = Object.normalize(chat, fetch: false)
|
2020-06-04 14:49:10 +02:00
|
|
|
[notification] = Notification.for_user(recipient)
|
|
|
|
|
|
|
|
res = Impl.build_content(notification, user, object)
|
|
|
|
|
|
|
|
assert res == %{
|
|
|
|
body: "@#{user.nickname}: hey",
|
|
|
|
title: "New Chat Message"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-06-06 09:46:07 +02:00
|
|
|
test "builds content for chat messages with no content" do
|
|
|
|
user = insert(:user)
|
|
|
|
recipient = insert(:user)
|
|
|
|
|
|
|
|
file = %Plug.Upload{
|
2020-10-13 17:37:24 +02:00
|
|
|
content_type: "image/jpeg",
|
2020-06-06 09:46:07 +02:00
|
|
|
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)
|
2021-01-04 13:38:31 +01:00
|
|
|
object = Object.normalize(chat, fetch: false)
|
2020-06-06 09:46:07 +02:00
|
|
|
[notification] = Notification.for_user(recipient)
|
|
|
|
|
|
|
|
res = Impl.build_content(notification, user, object)
|
|
|
|
|
|
|
|
assert res == %{
|
|
|
|
body: "@#{user.nickname}: (Attachment)",
|
|
|
|
title: "New Chat Message"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-06-26 19:45:46 +02:00
|
|
|
test "hides contents of notifications when option enabled" do
|
2019-10-29 19:33:17 +01:00
|
|
|
user = insert(:user, nickname: "Bob")
|
2020-06-26 21:35:04 +02:00
|
|
|
|
|
|
|
user2 =
|
|
|
|
insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: true})
|
2019-10-29 19:33:17 +01:00
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
2020-05-12 21:59:26 +02:00
|
|
|
visibility: "direct",
|
|
|
|
status: "<Lorem ipsum dolor sit amet."
|
2019-10-29 19:33:17 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
notif = insert(:notification, user: user2, activity: activity)
|
|
|
|
|
|
|
|
actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
|
2021-01-04 13:38:31 +01:00
|
|
|
object = Object.normalize(activity, fetch: false)
|
2019-10-29 19:33:17 +01:00
|
|
|
|
|
|
|
assert Impl.build_content(notif, actor, object) == %{
|
2020-05-06 23:42:27 +02:00
|
|
|
body: "New Direct Message"
|
2019-10-29 19:33:17 +01:00
|
|
|
}
|
2020-05-07 16:13:43 +02:00
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
2020-05-12 21:59:26 +02:00
|
|
|
visibility: "public",
|
|
|
|
status: "<Lorem ipsum dolor sit amet."
|
2020-05-07 16:13:43 +02:00
|
|
|
})
|
|
|
|
|
2020-06-02 14:22:16 +02:00
|
|
|
notif = insert(:notification, user: user2, activity: activity, type: "mention")
|
2020-05-07 16:13:43 +02:00
|
|
|
|
|
|
|
actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
|
2021-01-04 13:38:31 +01:00
|
|
|
object = Object.normalize(activity, fetch: false)
|
2020-05-07 16:13:43 +02:00
|
|
|
|
|
|
|
assert Impl.build_content(notif, actor, object) == %{
|
|
|
|
body: "New Mention"
|
|
|
|
}
|
|
|
|
|
|
|
|
{:ok, activity} = CommonAPI.favorite(user, activity.id)
|
|
|
|
|
2020-06-02 14:22:16 +02:00
|
|
|
notif = insert(:notification, user: user2, activity: activity, type: "favourite")
|
2020-05-07 16:13:43 +02:00
|
|
|
|
|
|
|
actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
|
2021-01-04 13:38:31 +01:00
|
|
|
object = Object.normalize(activity, fetch: false)
|
2020-05-07 16:13:43 +02:00
|
|
|
|
|
|
|
assert Impl.build_content(notif, actor, object) == %{
|
|
|
|
body: "New Favorite"
|
|
|
|
}
|
2019-10-29 19:33:17 +01:00
|
|
|
end
|
|
|
|
|
2020-06-26 19:45:46 +02:00
|
|
|
test "returns regular content when hiding contents option disabled" do
|
2019-10-29 19:33:17 +01:00
|
|
|
user = insert(:user, nickname: "Bob")
|
2020-06-26 21:35:04 +02:00
|
|
|
|
|
|
|
user2 =
|
|
|
|
insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: false})
|
2019-10-29 19:33:17 +01:00
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
2020-05-12 21:59:26 +02:00
|
|
|
visibility: "direct",
|
|
|
|
status:
|
2019-10-29 19:33:17 +01:00
|
|
|
"<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
|
|
|
|
})
|
|
|
|
|
|
|
|
notif = insert(:notification, user: user2, activity: activity)
|
|
|
|
|
|
|
|
actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
|
2021-01-04 13:38:31 +01:00
|
|
|
object = Object.normalize(activity, fetch: false)
|
2019-10-29 19:33:17 +01:00
|
|
|
|
|
|
|
assert Impl.build_content(notif, actor, object) == %{
|
|
|
|
body:
|
|
|
|
"@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
|
|
|
|
title: "New Direct Message"
|
|
|
|
}
|
2020-05-07 16:13:43 +02:00
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
2020-05-12 21:59:26 +02:00
|
|
|
visibility: "public",
|
|
|
|
status:
|
2020-05-07 16:13:43 +02:00
|
|
|
"<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
|
|
|
|
})
|
|
|
|
|
2020-06-02 14:22:16 +02:00
|
|
|
notif = insert(:notification, user: user2, activity: activity, type: "mention")
|
2020-05-07 16:13:43 +02:00
|
|
|
|
|
|
|
actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
|
2021-01-04 13:38:31 +01:00
|
|
|
object = Object.normalize(activity, fetch: false)
|
2020-05-07 16:13:43 +02:00
|
|
|
|
|
|
|
assert Impl.build_content(notif, actor, object) == %{
|
|
|
|
body:
|
|
|
|
"@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
|
|
|
|
title: "New Mention"
|
|
|
|
}
|
|
|
|
|
|
|
|
{:ok, activity} = CommonAPI.favorite(user, activity.id)
|
|
|
|
|
2020-06-02 14:22:16 +02:00
|
|
|
notif = insert(:notification, user: user2, activity: activity, type: "favourite")
|
2020-05-07 16:13:43 +02:00
|
|
|
|
|
|
|
actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
|
2021-01-04 13:38:31 +01:00
|
|
|
object = Object.normalize(activity, fetch: false)
|
2020-05-07 16:13:43 +02:00
|
|
|
|
|
|
|
assert Impl.build_content(notif, actor, object) == %{
|
|
|
|
body: "@Bob has favorited your post",
|
|
|
|
title: "New Favorite"
|
|
|
|
}
|
2019-10-29 19:33:17 +01:00
|
|
|
end
|
|
|
|
end
|
2019-03-06 14:20:12 +01:00
|
|
|
end
|