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
9 alias Pleroma.Web.Websub.WebsubClientSubscription
10 import Pleroma.Factory
11 alias Pleroma.Web.Router.Helpers
15 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
19 test "a verification of a request that is accepted" do
20 sub = insert(:websub_subscription)
23 getter = fn _path, _headers, options ->
25 "hub.challenge": challenge,
26 "hub.lease_seconds": seconds,
28 "hub.mode": "subscribe"
29 } = Keyword.get(options, :params)
31 assert String.to_integer(seconds) > 0
40 {:ok, sub} = Websub.verify(sub, getter)
41 assert sub.state == "active"
44 test "a verification of a request that doesn't return 200" do
45 sub = insert(:websub_subscription)
47 getter = fn _path, _headers, _options ->
55 {:error, sub} = Websub.verify(sub, getter)
56 # Keep the current state.
57 assert sub.state == "requested"
60 test "an incoming subscription request" do
64 "hub.callback" => "http://example.org/sub",
65 "hub.mode" => "subscribe",
66 "hub.topic" => Pleroma.Web.OStatus.feed_path(user),
67 "hub.secret" => "a random secret",
68 "hub.lease_seconds" => "100"
71 {:ok, subscription} = Websub.incoming_subscription_request(user, data)
72 assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
73 assert subscription.state == "requested"
74 assert subscription.secret == "a random secret"
75 assert subscription.callback == "http://example.org/sub"
78 test "an incoming subscription request for an existing subscription" do
82 insert(:websub_subscription, state: "accepted", topic: Pleroma.Web.OStatus.feed_path(user))
85 "hub.callback" => sub.callback,
86 "hub.mode" => "subscribe",
87 "hub.topic" => Pleroma.Web.OStatus.feed_path(user),
88 "hub.secret" => "a random secret",
89 "hub.lease_seconds" => "100"
92 {:ok, subscription} = Websub.incoming_subscription_request(user, data)
93 assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
94 assert subscription.state == sub.state
95 assert subscription.secret == "a random secret"
96 assert subscription.callback == sub.callback
97 assert length(Repo.all(WebsubServerSubscription)) == 1
98 assert subscription.id == sub.id
101 def accepting_verifier(subscription) do
102 {:ok, %{subscription | state: "accepted"}}
105 test "initiate a subscription for a given user and topic" do
106 subscriber = insert(:user)
107 user = insert(:user, %{info: %Pleroma.User.Info{topic: "some_topic", hub: "some_hub"}})
109 {:ok, websub} = Websub.subscribe(subscriber, user, &accepting_verifier/1)
110 assert websub.subscribers == [subscriber.ap_id]
111 assert websub.topic == "some_topic"
112 assert websub.hub == "some_hub"
113 assert is_binary(websub.secret)
114 assert websub.user == user
115 assert websub.state == "accepted"
118 test "discovers the hub and canonical url" do
119 topic = "https://mastodon.social/users/lambadalambda.atom"
121 {:ok, discovered} = Websub.gather_feed_data(topic)
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: 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: 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: 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",
212 hub: "http://example.org/still_good",
217 insert(:websub_client_subscription, %{
218 valid_until: NaiveDateTime.add(now, day - 100),
219 topic: "http://example.org/needs_refresh",
220 hub: "http://example.org/needs_refresh",
224 _refresh = Websub.refresh_subscriptions()
226 assert still_good == Repo.get(WebsubClientSubscription, still_good.id)
227 refute needs_refresh == Repo.get(WebsubClientSubscription, needs_refresh.id)