1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.WebsubTest do
7 alias Pleroma.Web.Websub
8 alias Pleroma.Web.Websub.{WebsubServerSubscription, WebsubClientSubscription}
10 alias Pleroma.Web.Router.Helpers
14 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
18 test "a verification of a request that is accepted" do
19 sub = insert(:websub_subscription)
22 getter = fn _path, _headers, options ->
24 "hub.challenge": challenge,
25 "hub.lease_seconds": seconds,
27 "hub.mode": "subscribe"
28 } = Keyword.get(options, :params)
30 assert String.to_integer(seconds) > 0
39 {:ok, sub} = Websub.verify(sub, getter)
40 assert sub.state == "active"
43 test "a verification of a request that doesn't return 200" do
44 sub = insert(:websub_subscription)
46 getter = fn _path, _headers, _options ->
54 {:error, sub} = Websub.verify(sub, getter)
55 # Keep the current state.
56 assert sub.state == "requested"
59 test "an incoming subscription request" do
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"
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"
77 test "an incoming subscription request for an existing subscription" do
81 insert(:websub_subscription, state: "accepted", topic: Pleroma.Web.OStatus.feed_path(user))
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"
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
100 def accepting_verifier(subscription) do
101 {:ok, %{subscription | state: "accepted"}}
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"}})
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"
117 test "discovers the hub and canonical url" do
118 topic = "https://mastodon.social/users/lambadalambda.atom"
120 {:ok, discovered} = Websub.gather_feed_data(topic)
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.",
134 "https://files.mastodon.social/accounts/avatars/000/000/264/original/1429214160519.gif?1492379244",
135 "mediaType" => "image/gif",
142 assert expected == discovered
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})
150 poster = fn ^hub, {:form, data}, _headers ->
151 assert Keyword.get(data, :"hub.mode") == "subscribe"
153 assert Keyword.get(data, :"hub.callback") ==
155 Pleroma.Web.Endpoint,
156 :websub_subscription_confirmation,
160 {:ok, %{status: 202}}
163 task = Task.async(fn -> Websub.request_subscription(websub, poster) end)
165 change = Ecto.Changeset.change(websub, %{state: "accepted"})
166 {:ok, _} = Repo.update(change)
168 {:ok, websub} = Task.await(task)
170 assert websub.state == "accepted"
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})
178 poster = fn ^hub, {:form, _data}, _headers ->
179 {:ok, %{status: 202}}
182 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
183 assert websub.state == "rejected"
185 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
187 poster = fn ^hub, {:form, _data}, _headers ->
188 {:ok, %{status: 400}}
191 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
192 assert websub.state == "rejected"
195 test "sign a text" do
196 signed = Websub.sign("secret", "text")
197 assert signed == "B8392C23690CCF871F37EC270BE1582DEC57A503" |> String.downcase()
199 _signed = Websub.sign("secret", [["て"], ['す']])
202 describe "renewing subscriptions" do
203 test "it renews subscriptions that have less than a day of time left" do
205 now = NaiveDateTime.utc_now()
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",
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",
223 _refresh = Websub.refresh_subscriptions()
225 assert still_good == Repo.get(WebsubClientSubscription, still_good.id)
226 refute needs_refresh == Repo.get(WebsubClientSubscription, needs_refresh.id)