2019-03-06 05:20:12 -08:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-02-25 06:04:28 -08:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-03-06 05:20:12 -08:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.Push.ImplTest do
|
|
|
|
use Pleroma.DataCase
|
|
|
|
|
2019-04-29 09:15:30 -07:00
|
|
|
alias Pleroma.Object
|
2019-10-29 11:33:17 -07:00
|
|
|
alias Pleroma.User
|
2019-04-29 09:15:30 -07:00
|
|
|
alias Pleroma.Web.CommonAPI
|
2019-03-06 05:20:12 -08:00
|
|
|
alias Pleroma.Web.Push.Impl
|
|
|
|
alias Pleroma.Web.Push.Subscription
|
|
|
|
|
|
|
|
import Pleroma.Factory
|
|
|
|
|
|
|
|
setup_all do
|
|
|
|
Tesla.Mock.mock_global(fn
|
|
|
|
%{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}}
|
|
|
|
)
|
|
|
|
|
2019-04-29 09:15:30 -07:00
|
|
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "<Lorem ipsum dolor sit amet."})
|
|
|
|
|
2019-03-06 05:20:12 -08:00
|
|
|
notif =
|
|
|
|
insert(:notification,
|
|
|
|
user: user,
|
2019-04-29 09:15:30 -07:00
|
|
|
activity: activity
|
2019-03-06 05:20:12 -08:00
|
|
|
)
|
|
|
|
|
2019-04-05 05:38:44 -07:00
|
|
|
assert Impl.perform(notif) == [:ok, :ok]
|
2019-03-06 05:20:12 -08:00
|
|
|
end
|
|
|
|
|
2019-04-05 05:38:44 -07:00
|
|
|
@tag capture_log: true
|
2019-03-06 05:20:12 -08:00
|
|
|
test "returns error if notif does not match " do
|
2019-04-05 05:38:44 -07:00
|
|
|
assert Impl.perform(%{}) == :error
|
2019-03-06 05:20:12 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
test "successful message sending" do
|
|
|
|
assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
|
|
|
|
end
|
|
|
|
|
2019-04-05 05:38:44 -07:00
|
|
|
@tag capture_log: true
|
2019-03-06 05:20:12 -08: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-21 16:35:43 -07:00
|
|
|
test "delete subscription if result send message between 400..500" do
|
2019-03-06 05:20:12 -08: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 06:04:28 -08: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-21 16:34:20 -07:00
|
|
|
test "renders title and body for create activity" do
|
2019-04-29 09:15:30 -07:00
|
|
|
user = insert(:user, nickname: "Bob")
|
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
|
|
|
"status" =>
|
|
|
|
"<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
|
|
|
|
})
|
|
|
|
|
|
|
|
object = Object.normalize(activity)
|
|
|
|
|
2019-03-06 05:20:12 -08:00
|
|
|
assert Impl.format_body(
|
|
|
|
%{
|
2019-04-29 09:15:30 -07:00
|
|
|
activity: activity
|
2019-03-06 05:20:12 -08:00
|
|
|
},
|
2019-04-29 09:15:30 -07:00
|
|
|
user,
|
|
|
|
object
|
2019-03-06 05:20:12 -08:00
|
|
|
) ==
|
|
|
|
"@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
|
2019-10-21 16:34:20 -07:00
|
|
|
|
|
|
|
assert Impl.format_title(%{activity: activity}) ==
|
|
|
|
"New Mention"
|
2019-03-06 05:20:12 -08:00
|
|
|
end
|
|
|
|
|
2019-10-21 16:34:20 -07:00
|
|
|
test "renders title and body for follow activity" do
|
2019-04-29 09:15:30 -07:00
|
|
|
user = insert(:user, nickname: "Bob")
|
|
|
|
other_user = insert(:user)
|
|
|
|
{:ok, _, _, activity} = CommonAPI.follow(user, other_user)
|
|
|
|
object = Object.normalize(activity)
|
|
|
|
|
2019-07-28 19:43:19 -07:00
|
|
|
assert Impl.format_body(%{activity: activity}, user, object) == "@Bob has followed you"
|
2019-10-21 16:34:20 -07:00
|
|
|
|
|
|
|
assert Impl.format_title(%{activity: activity}) ==
|
|
|
|
"New Follower"
|
2019-03-06 05:20:12 -08:00
|
|
|
end
|
|
|
|
|
2019-10-21 16:34:20 -07:00
|
|
|
test "renders title and body for announce activity" do
|
2019-03-06 05:20:12 -08:00
|
|
|
user = insert(:user)
|
|
|
|
|
2019-04-29 09:15:30 -07:00
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
|
|
|
"status" =>
|
|
|
|
"<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
|
2019-03-06 05:20:12 -08:00
|
|
|
})
|
|
|
|
|
2019-04-29 09:15:30 -07:00
|
|
|
{:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
|
|
|
|
object = Object.normalize(activity)
|
2019-03-06 05:20:12 -08:00
|
|
|
|
2019-04-29 09:15:30 -07:00
|
|
|
assert Impl.format_body(%{activity: announce_activity}, user, object) ==
|
2019-03-06 05:20:12 -08:00
|
|
|
"@#{user.nickname} repeated: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
|
2019-10-21 16:34:20 -07:00
|
|
|
|
|
|
|
assert Impl.format_title(%{activity: announce_activity}) ==
|
|
|
|
"New Repeat"
|
2019-03-06 05:20:12 -08:00
|
|
|
end
|
|
|
|
|
2019-10-21 16:34:20 -07:00
|
|
|
test "renders title and body for like activity" do
|
2019-04-29 09:15:30 -07:00
|
|
|
user = insert(:user, nickname: "Bob")
|
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
|
|
|
"status" =>
|
|
|
|
"<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
|
|
|
|
})
|
|
|
|
|
2019-10-16 07:16:39 -07:00
|
|
|
{:ok, activity} = CommonAPI.favorite(user, activity.id)
|
2019-04-29 09:15:30 -07:00
|
|
|
object = Object.normalize(activity)
|
|
|
|
|
2019-07-28 19:43:19 -07:00
|
|
|
assert Impl.format_body(%{activity: activity}, user, object) == "@Bob has favorited your post"
|
2019-10-21 16:34:20 -07:00
|
|
|
|
|
|
|
assert Impl.format_title(%{activity: activity}) ==
|
|
|
|
"New Favorite"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "renders title for create activity with direct visibility" do
|
|
|
|
user = insert(:user, nickname: "Bob")
|
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
|
|
|
"visibility" => "direct",
|
|
|
|
"status" => "This is just between you and me, pal"
|
|
|
|
})
|
|
|
|
|
|
|
|
assert Impl.format_title(%{activity: activity}) ==
|
|
|
|
"New Direct Message"
|
2019-03-06 05:20:12 -08:00
|
|
|
end
|
2019-10-29 11:33:17 -07:00
|
|
|
|
|
|
|
describe "build_content/3" do
|
|
|
|
test "returns info content for direct message with enabled privacy option" do
|
|
|
|
user = insert(:user, nickname: "Bob")
|
|
|
|
user2 = insert(:user, nickname: "Rob", notification_settings: %{privacy_option: true})
|
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
|
|
|
"visibility" => "direct",
|
|
|
|
"status" => "<Lorem ipsum dolor sit amet."
|
|
|
|
})
|
|
|
|
|
|
|
|
notif = insert(:notification, user: user2, activity: activity)
|
|
|
|
|
|
|
|
actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
|
|
|
|
object = Object.normalize(activity)
|
|
|
|
|
|
|
|
assert Impl.build_content(notif, actor, object) == %{
|
|
|
|
body: "@Bob",
|
|
|
|
title: "New Direct Message"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "returns regular content for direct message with disabled privacy option" do
|
|
|
|
user = insert(:user, nickname: "Bob")
|
|
|
|
user2 = insert(:user, nickname: "Rob", notification_settings: %{privacy_option: false})
|
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
|
|
|
"visibility" => "direct",
|
|
|
|
"status" =>
|
|
|
|
"<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"])
|
|
|
|
object = Object.normalize(activity)
|
|
|
|
|
|
|
|
assert Impl.build_content(notif, actor, object) == %{
|
|
|
|
body:
|
|
|
|
"@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
|
|
|
|
title: "New Direct Message"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2019-03-06 05:20:12 -08:00
|
|
|
end
|