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