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