1 defmodule Pleroma.Web.WebsubMock do
6 defmodule Pleroma.Web.WebsubTest do
8 alias Pleroma.Web.Websub
9 alias Pleroma.Web.Websub.WebsubServerSubscription
10 import Pleroma.Factory
12 test "a verification of a request that is accepted" do
13 sub = insert(:websub_subscription)
16 getter = fn (_path, _headers, options) ->
18 "hub.challenge": challenge,
19 "hub.lease_seconds": seconds,
21 "hub.mode": "subscribe"
22 } = Keyword.get(options, :params)
24 assert String.to_integer(seconds) > 0
26 {:ok, %HTTPoison.Response{
32 {:ok, sub} = Websub.verify(sub, getter)
33 assert sub.state == "active"
36 test "a verification of a request that doesn't return 200" do
37 sub = insert(:websub_subscription)
39 getter = fn (_path, _headers, _options) ->
40 {:ok, %HTTPoison.Response{
46 {:error, sub} = Websub.verify(sub, getter)
47 assert sub.state == "rejected"
50 test "an incoming subscription request" do
54 "hub.callback" => "http://example.org/sub",
55 "hub.mode" => "subscribe",
56 "hub.topic" => Pleroma.Web.OStatus.feed_path(user),
57 "hub.secret" => "a random secret",
58 "hub.lease_seconds" => "100"
61 {:ok, subscription } = Websub.incoming_subscription_request(user, data)
62 assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
63 assert subscription.state == "requested"
64 assert subscription.secret == "a random secret"
65 assert subscription.callback == "http://example.org/sub"
68 test "an incoming subscription request for an existing subscription" do
70 sub = insert(:websub_subscription, state: "accepted", topic: Pleroma.Web.OStatus.feed_path(user))
73 "hub.callback" => sub.callback,
74 "hub.mode" => "subscribe",
75 "hub.topic" => Pleroma.Web.OStatus.feed_path(user),
76 "hub.secret" => "a random secret",
77 "hub.lease_seconds" => "100"
80 {:ok, subscription } = Websub.incoming_subscription_request(user, data)
81 assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
82 assert subscription.state == sub.state
83 assert subscription.secret == "a random secret"
84 assert subscription.callback == sub.callback
85 assert length(Repo.all(WebsubServerSubscription)) == 1
86 assert subscription.id == sub.id
89 def accepting_verifier(subscription) do
90 {:ok, %{ subscription | state: "accepted" }}
93 test "initiate a subscription for a given user and topic" do
95 topic = "http://example.org/some-topic.atom"
97 {:ok, websub} = Websub.subscribe(user, topic, &accepting_verifier/1)
98 assert websub.subscribers == [user.ap_id]
99 assert websub.topic == topic
100 assert is_binary(websub.secret)
101 assert websub.user == user
102 assert websub.state == "accepted"
105 test "discovers the hub and canonical url" do
106 topic = "https://mastodon.social/users/lambadalambda.atom"
108 getter = fn(^topic) ->
109 doc = File.read!("test/fixtures/lambadalambda.atom")
110 {:ok, %{status_code: 200, body: doc}}
113 {:ok, discovered} = Websub.discover(topic, getter)
114 assert %{hub: "https://mastodon.social/api/push", url: topic} == discovered
117 test "calls the hub, requests topic" do
118 hub = "https://social.heldscal.la/main/push/hub"
119 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
120 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
122 poster = fn (^hub, {:form, data}, _headers) ->
123 assert Keyword.get(data, :"hub.mode") == "subscribe"
124 {:ok, %{status_code: 202}}
127 task = Task.async(fn -> Websub.request_subscription(websub, poster) end)
129 change = Ecto.Changeset.change(websub, %{state: "accepted"})
130 {:ok, _} = Repo.update(change)
132 {:ok, websub} = Task.await(task)
134 assert websub.state == "accepted"
137 test "rejects the subscription if it can't be accepted" do
138 hub = "https://social.heldscal.la/main/push/hub"
139 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
140 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
142 poster = fn (^hub, {:form, _data}, _headers) ->
143 {:ok, %{status_code: 202}}
146 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
147 assert websub.state == "rejected"
149 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
150 poster = fn (^hub, {:form, _data}, _headers) ->
151 {:ok, %{status_code: 400}}
154 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
155 assert websub.state == "rejected"