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