Merge remote-tracking branch 'pleroma/develop' into remove-user-activities
[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.Web.Push.Impl
9 alias Pleroma.Web.Push.Subscription
10
11 import Pleroma.Factory
12
13 setup_all do
14 Tesla.Mock.mock_global(fn
15 %{method: :post, url: "https://example.com/example/1234"} ->
16 %Tesla.Env{status: 200}
17
18 %{method: :post, url: "https://example.com/example/not_found"} ->
19 %Tesla.Env{status: 400}
20
21 %{method: :post, url: "https://example.com/example/bad"} ->
22 %Tesla.Env{status: 100}
23 end)
24
25 :ok
26 end
27
28 @sub %{
29 endpoint: "https://example.com/example/1234",
30 keys: %{
31 auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
32 p256dh:
33 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
34 }
35 }
36 @api_key "BASgACIHpN1GYgzSRp"
37 @message "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
38
39 test "performs sending notifications" do
40 user = insert(:user)
41 user2 = insert(:user)
42 insert(:push_subscription, user: user, data: %{alerts: %{"mention" => true}})
43 insert(:push_subscription, user: user2, data: %{alerts: %{"mention" => true}})
44
45 insert(:push_subscription,
46 user: user,
47 data: %{alerts: %{"follow" => true, "mention" => true}}
48 )
49
50 insert(:push_subscription,
51 user: user,
52 data: %{alerts: %{"follow" => true, "mention" => false}}
53 )
54
55 notif =
56 insert(:notification,
57 user: user,
58 activity: %Pleroma.Activity{
59 data: %{
60 "type" => "Create",
61 "actor" => user.ap_id,
62 "object" => %{"content" => "<Lorem ipsum dolor sit amet."}
63 }
64 }
65 )
66
67 assert Impl.perform_send(notif) == [:ok, :ok]
68 end
69
70 test "returns error if notif does not match " do
71 assert Impl.perform_send(%{}) == :error
72 end
73
74 test "successful message sending" do
75 assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
76 end
77
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 assert Impl.format_body(
102 %{
103 activity: %{
104 data: %{
105 "type" => "Create",
106 "object" => %{
107 "content" =>
108 "<span>Lorem ipsum dolor sit amet</span>, consectetur :bear: adipiscing elit. Fusce sagittis finibus turpis."
109 }
110 }
111 }
112 },
113 %{nickname: "Bob"}
114 ) ==
115 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
116 end
117
118 test "renders body for follow activity" do
119 assert Impl.format_body(%{activity: %{data: %{"type" => "Follow"}}}, %{nickname: "Bob"}) ==
120 "@Bob has followed you"
121 end
122
123 test "renders body for announce activity" do
124 user = insert(:user)
125
126 note =
127 insert(:note, %{
128 data: %{
129 "content" =>
130 "<span>Lorem ipsum dolor sit amet</span>, consectetur :bear: adipiscing elit. Fusce sagittis finibus turpis."
131 }
132 })
133
134 note_activity = insert(:note_activity, %{note: note})
135 announce_activity = insert(:announce_activity, %{user: user, note_activity: note_activity})
136
137 assert Impl.format_body(%{activity: announce_activity}, user) ==
138 "@#{user.nickname} repeated: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
139 end
140
141 test "renders body for like activity" do
142 assert Impl.format_body(%{activity: %{data: %{"type" => "Like"}}}, %{nickname: "Bob"}) ==
143 "@Bob has favorited your post"
144 end
145 end