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
8 alias Pleroma.Web.Router.Helpers
9 alias Pleroma.Web.Websub
10 alias Pleroma.Web.Websub.WebsubClientSubscription
11 alias Pleroma.Web.Websub.WebsubServerSubscription
13 import Pleroma.Factory
17 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
21 test "a verification of a request that is accepted" do
22 sub = insert(:websub_subscription)
25 getter = fn _path, _headers, options ->
27 "hub.challenge": challenge,
28 "hub.lease_seconds": seconds,
30 "hub.mode": "subscribe"
31 } = Keyword.get(options, :params)
33 assert String.to_integer(seconds) > 0
42 {:ok, sub} = Websub.verify(sub, getter)
43 assert sub.state == "active"
46 test "a verification of a request that doesn't return 200" do
47 sub = insert(:websub_subscription)
49 getter = fn _path, _headers, _options ->
57 {:error, sub} = Websub.verify(sub, getter)
58 # Keep the current state.
59 assert sub.state == "requested"
62 test "an incoming subscription request" do
66 "hub.callback" => "http://example.org/sub",
67 "hub.mode" => "subscribe",
68 "hub.topic" => Pleroma.Web.OStatus.feed_path(user),
69 "hub.secret" => "a random secret",
70 "hub.lease_seconds" => "100"
73 {:ok, subscription} = Websub.incoming_subscription_request(user, data)
74 assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
75 assert subscription.state == "requested"
76 assert subscription.secret == "a random secret"
77 assert subscription.callback == "http://example.org/sub"
80 test "an incoming subscription request for an existing subscription" do
84 insert(:websub_subscription, state: "accepted", topic: Pleroma.Web.OStatus.feed_path(user))
87 "hub.callback" => sub.callback,
88 "hub.mode" => "subscribe",
89 "hub.topic" => Pleroma.Web.OStatus.feed_path(user),
90 "hub.secret" => "a random secret",
91 "hub.lease_seconds" => "100"
94 {:ok, subscription} = Websub.incoming_subscription_request(user, data)
95 assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
96 assert subscription.state == sub.state
97 assert subscription.secret == "a random secret"
98 assert subscription.callback == sub.callback
99 assert length(Repo.all(WebsubServerSubscription)) == 1
100 assert subscription.id == sub.id
103 def accepting_verifier(subscription) do
104 {:ok, %{subscription | state: "accepted"}}
107 test "initiate a subscription for a given user and topic" do
108 subscriber = insert(:user)
109 user = insert(:user, %{info: %Pleroma.User.Info{topic: "some_topic", hub: "some_hub"}})
111 {:ok, websub} = Websub.subscribe(subscriber, user, &accepting_verifier/1)
112 assert websub.subscribers == [subscriber.ap_id]
113 assert websub.topic == "some_topic"
114 assert websub.hub == "some_hub"
115 assert is_binary(websub.secret)
116 assert websub.user == user
117 assert websub.state == "accepted"
120 test "discovers the hub and canonical url" do
121 topic = "https://mastodon.social/users/lambadalambda.atom"
123 {:ok, discovered} = Websub.gather_feed_data(topic)
126 "hub" => "https://mastodon.social/api/push",
127 "uri" => "https://mastodon.social/users/lambadalambda",
128 "nickname" => "lambadalambda",
129 "name" => "Critical Value",
130 "host" => "mastodon.social",
131 "bio" => "a cool dude.",
137 "https://files.mastodon.social/accounts/avatars/000/000/264/original/1429214160519.gif?1492379244",
138 "mediaType" => "image/gif",
145 assert expected == discovered
148 test "calls the hub, requests topic" do
149 hub = "https://social.heldscal.la/main/push/hub"
150 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
151 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
153 poster = fn ^hub, {:form, data}, _headers ->
154 assert Keyword.get(data, :"hub.mode") == "subscribe"
156 assert Keyword.get(data, :"hub.callback") ==
158 Pleroma.Web.Endpoint,
159 :websub_subscription_confirmation,
163 {:ok, %{status: 202}}
166 task = Task.async(fn -> Websub.request_subscription(websub, poster) end)
168 change = Ecto.Changeset.change(websub, %{state: "accepted"})
169 {:ok, _} = Repo.update(change)
171 {:ok, websub} = Task.await(task)
173 assert websub.state == "accepted"
176 test "rejects the subscription if it can't be accepted" do
177 hub = "https://social.heldscal.la/main/push/hub"
178 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
179 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
181 poster = fn ^hub, {:form, _data}, _headers ->
182 {:ok, %{status: 202}}
185 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
186 assert websub.state == "rejected"
188 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
190 poster = fn ^hub, {:form, _data}, _headers ->
191 {:ok, %{status: 400}}
194 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
195 assert websub.state == "rejected"
198 test "sign a text" do
199 signed = Websub.sign("secret", "text")
200 assert signed == "B8392C23690CCF871F37EC270BE1582DEC57A503" |> String.downcase()
202 _signed = Websub.sign("secret", [["て"], ['す']])
205 describe "renewing subscriptions" do
206 test "it renews subscriptions that have less than a day of time left" do
208 now = NaiveDateTime.utc_now()
211 insert(:websub_client_subscription, %{
212 valid_until: NaiveDateTime.add(now, 2 * day),
213 topic: "http://example.org/still_good",
214 hub: "http://example.org/still_good",
219 insert(:websub_client_subscription, %{
220 valid_until: NaiveDateTime.add(now, day - 100),
221 topic: "http://example.org/needs_refresh",
222 hub: "http://example.org/needs_refresh",
226 _refresh = Websub.refresh_subscriptions()
228 assert still_good == Repo.get(WebsubClientSubscription, still_good.id)
229 refute needs_refresh == Repo.get(WebsubClientSubscription, needs_refresh.id)