eva
[akkoma] / test / web / push / impl_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Push.ImplTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Object
9 alias Pleroma.Web.CommonAPI
10 alias Pleroma.Web.Push.Impl
11 alias Pleroma.Web.Push.Subscription
12
13 import Pleroma.Factory
14
15 setup_all do
16 Tesla.Mock.mock_global(fn
17 %{method: :post, url: "https://example.com/example/1234"} ->
18 %Tesla.Env{status: 200}
19
20 %{method: :post, url: "https://example.com/example/not_found"} ->
21 %Tesla.Env{status: 400}
22
23 %{method: :post, url: "https://example.com/example/bad"} ->
24 %Tesla.Env{status: 100}
25 end)
26
27 :ok
28 end
29
30 @sub %{
31 endpoint: "https://example.com/example/1234",
32 keys: %{
33 auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
34 p256dh:
35 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
36 }
37 }
38 @api_key "BASgACIHpN1GYgzSRp"
39 @message "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
40
41 test "performs sending notifications" do
42 user = insert(:user)
43 user2 = insert(:user)
44 insert(:push_subscription, user: user, data: %{alerts: %{"mention" => true}})
45 insert(:push_subscription, user: user2, data: %{alerts: %{"mention" => true}})
46
47 insert(:push_subscription,
48 user: user,
49 data: %{alerts: %{"follow" => true, "mention" => true}}
50 )
51
52 insert(:push_subscription,
53 user: user,
54 data: %{alerts: %{"follow" => true, "mention" => false}}
55 )
56
57 {:ok, activity} = CommonAPI.post(user, %{"status" => "<Lorem ipsum dolor sit amet."})
58
59 notif =
60 insert(:notification,
61 user: user,
62 activity: activity
63 )
64
65 assert Impl.perform(notif) == [:ok, :ok]
66 end
67
68 @tag capture_log: true
69 test "returns error if notif does not match " do
70 assert Impl.perform(%{}) == :error
71 end
72
73 test "successful message sending" do
74 assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
75 end
76
77 @tag capture_log: true
78 test "fail message sending" do
79 assert Impl.push_message(
80 @message,
81 Map.merge(@sub, %{endpoint: "https://example.com/example/bad"}),
82 @api_key,
83 %Subscription{}
84 ) == :error
85 end
86
87 test "delete subsciption if restult send message between 400..500" do
88 subscription = insert(:push_subscription)
89
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
97 refute Pleroma.Repo.get(Subscription, subscription.id)
98 end
99
100 test "renders body for create activity" do
101 user = insert(:user, nickname: "Bob")
102
103 {:ok, activity} =
104 CommonAPI.post(user, %{
105 "status" =>
106 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
107 })
108
109 object = Object.normalize(activity)
110
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 end
120
121 test "renders body for follow activity" do
122 user = insert(:user, nickname: "Bob")
123 other_user = insert(:user)
124 {:ok, _, _, activity} = CommonAPI.follow(user, other_user)
125 object = Object.normalize(activity)
126
127 assert Impl.format_body(%{activity: activity}, user, object) == "@Bob has followed you"
128 end
129
130 test "renders body for announce activity" do
131 user = insert(:user)
132
133 {:ok, activity} =
134 CommonAPI.post(user, %{
135 "status" =>
136 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
137 })
138
139 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
140 object = Object.normalize(activity)
141
142 assert Impl.format_body(%{activity: announce_activity}, user, object) ==
143 "@#{user.nickname} repeated: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
144 end
145
146 test "renders body for like activity" do
147 user = insert(:user, nickname: "Bob")
148
149 {:ok, activity} =
150 CommonAPI.post(user, %{
151 "status" =>
152 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
153 })
154
155 {:ok, activity, _} = CommonAPI.favorite(activity.id, user)
156 object = Object.normalize(activity)
157
158 assert Impl.format_body(%{activity: activity}, user, object) == "@Bob has favorited your post"
159 end
160 end