Handle incoming websub subscriptions.
[akkoma] / test / web / websub / websub_test.exs
1 defmodule Pleroma.Web.WebsubMock do
2 def verify(sub) do
3 {:ok, sub}
4 end
5 end
6
7 defmodule Pleroma.Web.WebsubTest do
8 use Pleroma.DataCase
9 alias Pleroma.Web.Websub
10 alias Pleroma.Web.Websub.WebsubServerSubscription
11 import Pleroma.Factory
12 alias Pleroma.Web.Router.Helpers
13
14 test "a verification of a request that is accepted" do
15 sub = insert(:websub_subscription)
16 topic = sub.topic
17
18 getter = fn (_path, _headers, options) ->
19 %{
20 "hub.challenge": challenge,
21 "hub.lease_seconds": seconds,
22 "hub.topic": ^topic,
23 "hub.mode": "subscribe"
24 } = Keyword.get(options, :params)
25
26 assert String.to_integer(seconds) > 0
27
28 {:ok, %HTTPoison.Response{
29 status_code: 200,
30 body: challenge
31 }}
32 end
33
34 {:ok, sub} = Websub.verify(sub, getter)
35 assert sub.state == "active"
36 end
37
38 test "a verification of a request that doesn't return 200" do
39 sub = insert(:websub_subscription)
40
41 getter = fn (_path, _headers, _options) ->
42 {:ok, %HTTPoison.Response{
43 status_code: 500,
44 body: ""
45 }}
46 end
47
48 {:error, sub} = Websub.verify(sub, getter)
49 assert sub.state == "rejected"
50 end
51
52 test "an incoming subscription request" do
53 user = insert(:user)
54
55 data = %{
56 "hub.callback" => "http://example.org/sub",
57 "hub.mode" => "subscribe",
58 "hub.topic" => Pleroma.Web.OStatus.feed_path(user),
59 "hub.secret" => "a random secret",
60 "hub.lease_seconds" => "100"
61 }
62
63 {:ok, subscription } = Websub.incoming_subscription_request(user, data)
64 assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
65 assert subscription.state == "requested"
66 assert subscription.secret == "a random secret"
67 assert subscription.callback == "http://example.org/sub"
68 end
69
70 test "an incoming subscription request for an existing subscription" do
71 user = insert(:user)
72 sub = insert(:websub_subscription, state: "accepted", topic: Pleroma.Web.OStatus.feed_path(user))
73
74 data = %{
75 "hub.callback" => sub.callback,
76 "hub.mode" => "subscribe",
77 "hub.topic" => Pleroma.Web.OStatus.feed_path(user),
78 "hub.secret" => "a random secret",
79 "hub.lease_seconds" => "100"
80 }
81
82 {:ok, subscription } = Websub.incoming_subscription_request(user, data)
83 assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
84 assert subscription.state == sub.state
85 assert subscription.secret == "a random secret"
86 assert subscription.callback == sub.callback
87 assert length(Repo.all(WebsubServerSubscription)) == 1
88 assert subscription.id == sub.id
89 end
90
91 def accepting_verifier(subscription) do
92 {:ok, %{ subscription | state: "accepted" }}
93 end
94
95 test "initiate a subscription for a given user and topic" do
96 user = insert(:user)
97 topic = "http://example.org/some-topic.atom"
98
99 {:ok, websub} = Websub.subscribe(user, topic, &accepting_verifier/1)
100 assert websub.subscribers == [user.ap_id]
101 assert websub.topic == topic
102 assert is_binary(websub.secret)
103 assert websub.user == user
104 assert websub.state == "accepted"
105 end
106
107 test "discovers the hub and canonical url" do
108 topic = "https://mastodon.social/users/lambadalambda.atom"
109
110 getter = fn(^topic) ->
111 doc = File.read!("test/fixtures/lambadalambda.atom")
112 {:ok, %{status_code: 200, body: doc}}
113 end
114
115 {:ok, discovered} = Websub.discover(topic, getter)
116 assert %{hub: "https://mastodon.social/api/push", url: topic} == discovered
117 end
118
119 test "calls the hub, requests topic" do
120 hub = "https://social.heldscal.la/main/push/hub"
121 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
122 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
123
124 poster = fn (^hub, {:form, data}, _headers) ->
125 assert Keyword.get(data, :"hub.mode") == "subscribe"
126 assert Keyword.get(data, :"hub.callback") == Helpers.websub_url(Pleroma.Web.Endpoint, :websub_subscription_confirmation, websub.id)
127 {:ok, %{status_code: 202}}
128 end
129
130 task = Task.async(fn -> Websub.request_subscription(websub, poster) end)
131
132 change = Ecto.Changeset.change(websub, %{state: "accepted"})
133 {:ok, _} = Repo.update(change)
134
135 {:ok, websub} = Task.await(task)
136
137 assert websub.state == "accepted"
138 end
139
140 test "rejects the subscription if it can't be accepted" do
141 hub = "https://social.heldscal.la/main/push/hub"
142 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
143 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
144
145 poster = fn (^hub, {:form, _data}, _headers) ->
146 {:ok, %{status_code: 202}}
147 end
148
149 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
150 assert websub.state == "rejected"
151
152 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
153 poster = fn (^hub, {:form, _data}, _headers) ->
154 {:ok, %{status_code: 400}}
155 end
156
157 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
158 assert websub.state == "rejected"
159 end
160 end