1 defmodule Pleroma.Web.WebsubMock do
7 defmodule Pleroma.Web.WebsubTest do
9 alias Pleroma.Web.Websub
10 alias Pleroma.Web.Websub.{WebsubServerSubscription, WebsubClientSubscription}
11 import Pleroma.Factory
12 alias Pleroma.Web.Router.Helpers
16 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
20 test "a verification of a request that is accepted" do
21 sub = insert(:websub_subscription)
24 getter = fn _path, _headers, options ->
26 "hub.challenge": challenge,
27 "hub.lease_seconds": seconds,
29 "hub.mode": "subscribe"
30 } = Keyword.get(options, :params)
32 assert String.to_integer(seconds) > 0
41 {:ok, sub} = Websub.verify(sub, getter)
42 assert sub.state == "active"
45 test "a verification of a request that doesn't return 200" do
46 sub = insert(:websub_subscription)
48 getter = fn _path, _headers, _options ->
56 {:error, sub} = Websub.verify(sub, getter)
57 # Keep the current state.
58 assert sub.state == "requested"
61 test "an incoming subscription request" do
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"
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"
79 test "an incoming subscription request for an existing subscription" do
83 insert(:websub_subscription, state: "accepted", topic: Pleroma.Web.OStatus.feed_path(user))
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"
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
102 def accepting_verifier(subscription) do
103 {:ok, %{subscription | state: "accepted"}}
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"}})
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"
119 test "discovers the hub and canonical url" do
120 topic = "https://mastodon.social/users/lambadalambda.atom"
122 {:ok, discovered} = Websub.gather_feed_data(topic)
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.",
136 "https://files.mastodon.social/accounts/avatars/000/000/264/original/1429214160519.gif?1492379244",
137 "mediaType" => "image/gif",
144 assert expected == discovered
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})
152 poster = fn ^hub, {:form, data}, _headers ->
153 assert Keyword.get(data, :"hub.mode") == "subscribe"
155 assert Keyword.get(data, :"hub.callback") ==
157 Pleroma.Web.Endpoint,
158 :websub_subscription_confirmation,
162 {:ok, %{status: 202}}
165 task = Task.async(fn -> Websub.request_subscription(websub, poster) end)
167 change = Ecto.Changeset.change(websub, %{state: "accepted"})
168 {:ok, _} = Repo.update(change)
170 {:ok, websub} = Task.await(task)
172 assert websub.state == "accepted"
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})
180 poster = fn ^hub, {:form, _data}, _headers ->
181 {:ok, %{status: 202}}
184 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
185 assert websub.state == "rejected"
187 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
189 poster = fn ^hub, {:form, _data}, _headers ->
190 {:ok, %{status: 400}}
193 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
194 assert websub.state == "rejected"
197 test "sign a text" do
198 signed = Websub.sign("secret", "text")
199 assert signed == "B8392C23690CCF871F37EC270BE1582DEC57A503" |> String.downcase()
201 _signed = Websub.sign("secret", [["て"], ['す']])
204 describe "renewing subscriptions" do
205 test "it renews subscriptions that have less than a day of time left" do
207 now = NaiveDateTime.utc_now()
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",
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",
225 _refresh = Websub.refresh_subscriptions()
227 assert still_good == Repo.get(WebsubClientSubscription, still_good.id)
228 refute needs_refresh == Repo.get(WebsubClientSubscription, needs_refresh.id)