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
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 getter = fn ^topic ->
117 doc = File.read!("test/fixtures/lambadalambda.atom")
118 {:ok, %{status_code: 200, body: doc}}
121 {:ok, discovered} = Websub.gather_feed_data(topic, getter)
124 "hub" => "https://mastodon.social/api/push",
125 "uri" => "https://mastodon.social/users/lambadalambda",
126 "nickname" => "lambadalambda",
127 "name" => "Critical Value",
128 "host" => "mastodon.social",
129 "bio" => "a cool dude.",
135 "https://files.mastodon.social/accounts/avatars/000/000/264/original/1429214160519.gif?1492379244",
136 "mediaType" => "image/gif",
143 assert expected == discovered
146 test "calls the hub, requests topic" do
147 hub = "https://social.heldscal.la/main/push/hub"
148 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
149 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
151 poster = fn ^hub, {:form, data}, _headers ->
152 assert Keyword.get(data, :"hub.mode") == "subscribe"
154 assert Keyword.get(data, :"hub.callback") ==
156 Pleroma.Web.Endpoint,
157 :websub_subscription_confirmation,
161 {:ok, %{status_code: 202}}
164 task = Task.async(fn -> Websub.request_subscription(websub, poster) end)
166 change = Ecto.Changeset.change(websub, %{state: "accepted"})
167 {:ok, _} = Repo.update(change)
169 {:ok, websub} = Task.await(task)
171 assert websub.state == "accepted"
174 test "rejects the subscription if it can't be accepted" do
175 hub = "https://social.heldscal.la/main/push/hub"
176 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
177 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
179 poster = fn ^hub, {:form, _data}, _headers ->
180 {:ok, %{status_code: 202}}
183 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
184 assert websub.state == "rejected"
186 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
188 poster = fn ^hub, {:form, _data}, _headers ->
189 {:ok, %{status_code: 400}}
192 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
193 assert websub.state == "rejected"
196 test "sign a text" do
197 signed = Websub.sign("secret", "text")
198 assert signed == "B8392C23690CCF871F37EC270BE1582DEC57A503" |> String.downcase()
200 _signed = Websub.sign("secret", [["て"], ['す']])
203 describe "renewing subscriptions" do
204 test "it renews subscriptions that have less than a day of time left" do
206 now = NaiveDateTime.utc_now()
209 insert(:websub_client_subscription, %{
210 valid_until: NaiveDateTime.add(now, 2 * day),
211 topic: "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",
222 _refresh = Websub.refresh_subscriptions()
224 assert still_good == Repo.get(WebsubClientSubscription, still_good.id)
225 refute needs_refresh == Repo.get(WebsubClientSubscription, needs_refresh.id)