Merge branch 'tests/prismo-url-map' into 'develop'
[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, WebsubClientSubscription}
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,
29 %HTTPoison.Response{
30 status_code: 200,
31 body: challenge
32 }}
33 end
34
35 {:ok, sub} = Websub.verify(sub, getter)
36 assert sub.state == "active"
37 end
38
39 test "a verification of a request that doesn't return 200" do
40 sub = insert(:websub_subscription)
41
42 getter = fn _path, _headers, _options ->
43 {:ok,
44 %HTTPoison.Response{
45 status_code: 500,
46 body: ""
47 }}
48 end
49
50 {:error, sub} = Websub.verify(sub, getter)
51 # Keep the current state.
52 assert sub.state == "requested"
53 end
54
55 test "an incoming subscription request" do
56 user = insert(:user)
57
58 data = %{
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"
64 }
65
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"
71 end
72
73 test "an incoming subscription request for an existing subscription" do
74 user = insert(:user)
75
76 sub =
77 insert(:websub_subscription, state: "accepted", topic: Pleroma.Web.OStatus.feed_path(user))
78
79 data = %{
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"
85 }
86
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
94 end
95
96 def accepting_verifier(subscription) do
97 {:ok, %{subscription | state: "accepted"}}
98 end
99
100 test "initiate a subscription for a given user and topic" do
101 subscriber = insert(:user)
102 user = insert(:user, %{info: %{"topic" => "some_topic", "hub" => "some_hub"}})
103
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"
111 end
112
113 test "discovers the hub and canonical url" do
114 topic = "https://mastodon.social/users/lambadalambda.atom"
115
116 getter = fn ^topic ->
117 doc = File.read!("test/fixtures/lambadalambda.atom")
118 {:ok, %{status_code: 200, body: doc}}
119 end
120
121 {:ok, discovered} = Websub.gather_feed_data(topic, getter)
122
123 expected = %{
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.",
130 "avatar" => %{
131 "type" => "Image",
132 "url" => [
133 %{
134 "href" =>
135 "https://files.mastodon.social/accounts/avatars/000/000/264/original/1429214160519.gif?1492379244",
136 "mediaType" => "image/gif",
137 "type" => "Link"
138 }
139 ]
140 }
141 }
142
143 assert expected == discovered
144 end
145
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})
150
151 poster = fn ^hub, {:form, data}, _headers ->
152 assert Keyword.get(data, :"hub.mode") == "subscribe"
153
154 assert Keyword.get(data, :"hub.callback") ==
155 Helpers.websub_url(
156 Pleroma.Web.Endpoint,
157 :websub_subscription_confirmation,
158 websub.id
159 )
160
161 {:ok, %{status_code: 202}}
162 end
163
164 task = Task.async(fn -> Websub.request_subscription(websub, poster) end)
165
166 change = Ecto.Changeset.change(websub, %{state: "accepted"})
167 {:ok, _} = Repo.update(change)
168
169 {:ok, websub} = Task.await(task)
170
171 assert websub.state == "accepted"
172 end
173
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})
178
179 poster = fn ^hub, {:form, _data}, _headers ->
180 {:ok, %{status_code: 202}}
181 end
182
183 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
184 assert websub.state == "rejected"
185
186 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
187
188 poster = fn ^hub, {:form, _data}, _headers ->
189 {:ok, %{status_code: 400}}
190 end
191
192 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
193 assert websub.state == "rejected"
194 end
195
196 test "sign a text" do
197 signed = Websub.sign("secret", "text")
198 assert signed == "B8392C23690CCF871F37EC270BE1582DEC57A503" |> String.downcase()
199
200 _signed = Websub.sign("secret", [["て"], ['す']])
201 end
202
203 describe "renewing subscriptions" do
204 test "it renews subscriptions that have less than a day of time left" do
205 day = 60 * 60 * 24
206 now = NaiveDateTime.utc_now()
207
208 still_good =
209 insert(:websub_client_subscription, %{
210 valid_until: NaiveDateTime.add(now, 2 * day),
211 topic: "http://example.org/still_good",
212 state: "accepted"
213 })
214
215 needs_refresh =
216 insert(:websub_client_subscription, %{
217 valid_until: NaiveDateTime.add(now, day - 100),
218 topic: "http://example.org/needs_refresh",
219 state: "accepted"
220 })
221
222 _refresh = Websub.refresh_subscriptions()
223
224 assert still_good == Repo.get(WebsubClientSubscription, still_good.id)
225 refute needs_refresh == Repo.get(WebsubClientSubscription, needs_refresh.id)
226 end
227 end
228 end