WebPush refactoring: separate build and deliver steps

This commit is contained in:
Mark Felder 2024-06-11 17:58:02 -04:00
commit 568819c08a
3 changed files with 97 additions and 79 deletions

View file

@ -32,17 +32,6 @@ defmodule Pleroma.Web.Push.ImplTest do
: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 finibus turpis."
test "performs sending notifications" do
user = insert(:user)
user2 = insert(:user)
@ -68,39 +57,65 @@ defmodule Pleroma.Web.Push.ImplTest do
type: "mention"
)
assert Impl.perform(notif) == {:ok, [:ok, :ok]}
Impl.build(notif)
|> Enum.each(fn push -> assert match?(:ok, Impl.deliver(push)) end)
end
@tag capture_log: true
test "returns error if notif does not match " do
assert Impl.perform(%{}) == {:error, :unknown_type}
end
test "successful message sending" do
assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
assert Impl.build(%{}) == {:error, :unknown_type}
end
@tag capture_log: true
test "fail message sending" do
assert Impl.push_message(
@message,
Map.merge(@sub, %{endpoint: "https://example.com/example/bad"}),
@api_key,
%Subscription{}
) == :error
user = insert(:user)
insert(:push_subscription,
user: user,
endpoint: "https://example.com/example/bad",
data: %{alerts: %{"follow" => true}}
)
other_user = insert(:user)
{:ok, _, _, activity} = CommonAPI.follow(user, other_user)
notif =
insert(:notification,
user: user,
activity: activity,
type: "follow"
)
[push] = Impl.build(notif)
assert Impl.deliver(push) == :error
end
test "delete subscription if result send message between 400..500" do
subscription = insert(:push_subscription)
user = insert(:user)
assert Impl.push_message(
@message,
Map.merge(@sub, %{endpoint: "https://example.com/example/not_found"}),
@api_key,
subscription
) == :ok
bad_subscription =
insert(:push_subscription,
user: user,
endpoint: "https://example.com/example/not_found",
data: %{alerts: %{"follow" => true}}
)
refute Pleroma.Repo.get(Subscription, subscription.id)
other_user = insert(:user)
{:ok, _, _, activity} = CommonAPI.follow(user, other_user)
notif =
insert(:notification,
user: user,
activity: activity,
type: "follow"
)
[push] = Impl.build(notif)
assert Impl.deliver(push) == :ok
refute Pleroma.Repo.get(Subscription, bad_subscription.id)
end
test "deletes subscription when token has been deleted" do