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