1 defmodule Pleroma.Web.WebsubTest do
3 alias Pleroma.Web.Websub
4 alias Pleroma.Web.Websub.{WebsubServerSubscription, WebsubClientSubscription}
6 alias Pleroma.Web.Router.Helpers
10 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
14 test "a verification of a request that is accepted" do
15 sub = insert(:websub_subscription)
18 getter = fn _path, _headers, options ->
20 "hub.challenge": challenge,
21 "hub.lease_seconds": seconds,
23 "hub.mode": "subscribe"
24 } = Keyword.get(options, :params)
26 assert String.to_integer(seconds) > 0
35 {:ok, sub} = Websub.verify(sub, getter)
36 assert sub.state == "active"
39 test "a verification of a request that doesn't return 200" do
40 sub = insert(:websub_subscription)
42 getter = fn _path, _headers, _options ->
50 {:error, sub} = Websub.verify(sub, getter)
51 # Keep the current state.
52 assert sub.state == "requested"
55 test "an incoming subscription request" do
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"
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"
73 test "an incoming subscription request for an existing subscription" do
77 insert(:websub_subscription, state: "accepted", topic: Pleroma.Web.OStatus.feed_path(user))
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"
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
96 def accepting_verifier(subscription) do
97 {:ok, %{subscription | state: "accepted"}}
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"}})
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"
113 test "discovers the hub and canonical url" do
114 topic = "https://mastodon.social/users/lambadalambda.atom"
116 {:ok, discovered} = Websub.gather_feed_data(topic)
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.",
130 "https://files.mastodon.social/accounts/avatars/000/000/264/original/1429214160519.gif?1492379244",
131 "mediaType" => "image/gif",
138 assert expected == discovered
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})
146 poster = fn ^hub, {:form, data}, _headers ->
147 assert Keyword.get(data, :"hub.mode") == "subscribe"
149 assert Keyword.get(data, :"hub.callback") ==
151 Pleroma.Web.Endpoint,
152 :websub_subscription_confirmation,
156 {:ok, %{status: 202}}
159 task = Task.async(fn -> Websub.request_subscription(websub, poster) end)
161 change = Ecto.Changeset.change(websub, %{state: "accepted"})
162 {:ok, _} = Repo.update(change)
164 {:ok, websub} = Task.await(task)
166 assert websub.state == "accepted"
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})
174 poster = fn ^hub, {:form, _data}, _headers ->
175 {:ok, %{status: 202}}
178 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
179 assert websub.state == "rejected"
181 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
183 poster = fn ^hub, {:form, _data}, _headers ->
184 {:ok, %{status: 400}}
187 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
188 assert websub.state == "rejected"
191 test "sign a text" do
192 signed = Websub.sign("secret", "text")
193 assert signed == "B8392C23690CCF871F37EC270BE1582DEC57A503" |> String.downcase()
195 _signed = Websub.sign("secret", [["て"], ['す']])
198 describe "renewing subscriptions" do
199 test "it renews subscriptions that have less than a day of time left" do
201 now = NaiveDateTime.utc_now()
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",
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",
219 _refresh = Websub.refresh_subscriptions()
221 assert still_good == Repo.get(WebsubClientSubscription, still_good.id)
222 refute needs_refresh == Repo.get(WebsubClientSubscription, needs_refresh.id)