fix formatting
[akkoma] / test / web / websub / websub_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.WebsubTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Web.Router.Helpers
9 alias Pleroma.Web.Websub
10 alias Pleroma.Web.Websub.WebsubClientSubscription
11 alias Pleroma.Web.Websub.WebsubServerSubscription
12
13 import Pleroma.Factory
14 import Tesla.Mock
15
16 setup do
17 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
18 :ok
19 end
20
21 test "a verification of a request that is accepted" do
22 sub = insert(:websub_subscription)
23 topic = sub.topic
24
25 getter = fn _path, _headers, options ->
26 %{
27 "hub.challenge": challenge,
28 "hub.lease_seconds": seconds,
29 "hub.topic": ^topic,
30 "hub.mode": "subscribe"
31 } = Keyword.get(options, :params)
32
33 assert String.to_integer(seconds) > 0
34
35 {:ok,
36 %Tesla.Env{
37 status: 200,
38 body: challenge
39 }}
40 end
41
42 {:ok, sub} = Websub.verify(sub, getter)
43 assert sub.state == "active"
44 end
45
46 test "a verification of a request that doesn't return 200" do
47 sub = insert(:websub_subscription)
48
49 getter = fn _path, _headers, _options ->
50 {:ok,
51 %Tesla.Env{
52 status: 500,
53 body: ""
54 }}
55 end
56
57 {:error, sub} = Websub.verify(sub, getter)
58 # Keep the current state.
59 assert sub.state == "requested"
60 end
61
62 test "an incoming subscription request" do
63 user = insert(:user)
64
65 data = %{
66 "hub.callback" => "http://example.org/sub",
67 "hub.mode" => "subscribe",
68 "hub.topic" => Pleroma.Web.OStatus.feed_path(user),
69 "hub.secret" => "a random secret",
70 "hub.lease_seconds" => "100"
71 }
72
73 {:ok, subscription} = Websub.incoming_subscription_request(user, data)
74 assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
75 assert subscription.state == "requested"
76 assert subscription.secret == "a random secret"
77 assert subscription.callback == "http://example.org/sub"
78 end
79
80 test "an incoming subscription request for an existing subscription" do
81 user = insert(:user)
82
83 sub =
84 insert(:websub_subscription, state: "accepted", topic: Pleroma.Web.OStatus.feed_path(user))
85
86 data = %{
87 "hub.callback" => sub.callback,
88 "hub.mode" => "subscribe",
89 "hub.topic" => Pleroma.Web.OStatus.feed_path(user),
90 "hub.secret" => "a random secret",
91 "hub.lease_seconds" => "100"
92 }
93
94 {:ok, subscription} = Websub.incoming_subscription_request(user, data)
95 assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
96 assert subscription.state == sub.state
97 assert subscription.secret == "a random secret"
98 assert subscription.callback == sub.callback
99 assert length(Repo.all(WebsubServerSubscription)) == 1
100 assert subscription.id == sub.id
101 end
102
103 def accepting_verifier(subscription) do
104 {:ok, %{subscription | state: "accepted"}}
105 end
106
107 test "initiate a subscription for a given user and topic" do
108 subscriber = insert(:user)
109 user = insert(:user, %{info: %Pleroma.User.Info{topic: "some_topic", hub: "some_hub"}})
110
111 {:ok, websub} = Websub.subscribe(subscriber, user, &accepting_verifier/1)
112 assert websub.subscribers == [subscriber.ap_id]
113 assert websub.topic == "some_topic"
114 assert websub.hub == "some_hub"
115 assert is_binary(websub.secret)
116 assert websub.user == user
117 assert websub.state == "accepted"
118 end
119
120 test "discovers the hub and canonical url" do
121 topic = "https://mastodon.social/users/lambadalambda.atom"
122
123 {:ok, discovered} = Websub.gather_feed_data(topic)
124
125 expected = %{
126 "hub" => "https://mastodon.social/api/push",
127 "uri" => "https://mastodon.social/users/lambadalambda",
128 "nickname" => "lambadalambda",
129 "name" => "Critical Value",
130 "host" => "mastodon.social",
131 "bio" => "a cool dude.",
132 "avatar" => %{
133 "type" => "Image",
134 "url" => [
135 %{
136 "href" =>
137 "https://files.mastodon.social/accounts/avatars/000/000/264/original/1429214160519.gif?1492379244",
138 "mediaType" => "image/gif",
139 "type" => "Link"
140 }
141 ]
142 }
143 }
144
145 assert expected == discovered
146 end
147
148 test "calls the hub, requests topic" do
149 hub = "https://social.heldscal.la/main/push/hub"
150 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
151 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
152
153 poster = fn ^hub, {:form, data}, _headers ->
154 assert Keyword.get(data, :"hub.mode") == "subscribe"
155
156 assert Keyword.get(data, :"hub.callback") ==
157 Helpers.websub_url(
158 Pleroma.Web.Endpoint,
159 :websub_subscription_confirmation,
160 websub.id
161 )
162
163 {:ok, %{status: 202}}
164 end
165
166 task = Task.async(fn -> Websub.request_subscription(websub, poster) end)
167
168 change = Ecto.Changeset.change(websub, %{state: "accepted"})
169 {:ok, _} = Repo.update(change)
170
171 {:ok, websub} = Task.await(task)
172
173 assert websub.state == "accepted"
174 end
175
176 test "rejects the subscription if it can't be accepted" do
177 hub = "https://social.heldscal.la/main/push/hub"
178 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
179 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
180
181 poster = fn ^hub, {:form, _data}, _headers ->
182 {:ok, %{status: 202}}
183 end
184
185 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
186 assert websub.state == "rejected"
187
188 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
189
190 poster = fn ^hub, {:form, _data}, _headers ->
191 {:ok, %{status: 400}}
192 end
193
194 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
195 assert websub.state == "rejected"
196 end
197
198 test "sign a text" do
199 signed = Websub.sign("secret", "text")
200 assert signed == "B8392C23690CCF871F37EC270BE1582DEC57A503" |> String.downcase()
201
202 _signed = Websub.sign("secret", [["て"], ['す']])
203 end
204
205 describe "renewing subscriptions" do
206 test "it renews subscriptions that have less than a day of time left" do
207 day = 60 * 60 * 24
208 now = NaiveDateTime.utc_now()
209
210 still_good =
211 insert(:websub_client_subscription, %{
212 valid_until: NaiveDateTime.add(now, 2 * day),
213 topic: "http://example.org/still_good",
214 hub: "http://example.org/still_good",
215 state: "accepted"
216 })
217
218 needs_refresh =
219 insert(:websub_client_subscription, %{
220 valid_until: NaiveDateTime.add(now, day - 100),
221 topic: "http://example.org/needs_refresh",
222 hub: "http://example.org/needs_refresh",
223 state: "accepted"
224 })
225
226 _refresh = Websub.refresh_subscriptions()
227
228 assert still_good == Repo.get(WebsubClientSubscription, still_good.id)
229 refute needs_refresh == Repo.get(WebsubClientSubscription, needs_refresh.id)
230 end
231 end
232 end