Fix uploaded media plug test
[akkoma] / test / web / websub / websub_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.WebsubTest do
6 use Pleroma.DataCase
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
12 import Tesla.Mock
13
14 setup do
15 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
16 :ok
17 end
18
19 test "a verification of a request that is accepted" do
20 sub = insert(:websub_subscription)
21 topic = sub.topic
22
23 getter = fn _path, _headers, options ->
24 %{
25 "hub.challenge": challenge,
26 "hub.lease_seconds": seconds,
27 "hub.topic": ^topic,
28 "hub.mode": "subscribe"
29 } = Keyword.get(options, :params)
30
31 assert String.to_integer(seconds) > 0
32
33 {:ok,
34 %Tesla.Env{
35 status: 200,
36 body: challenge
37 }}
38 end
39
40 {:ok, sub} = Websub.verify(sub, getter)
41 assert sub.state == "active"
42 end
43
44 test "a verification of a request that doesn't return 200" do
45 sub = insert(:websub_subscription)
46
47 getter = fn _path, _headers, _options ->
48 {:ok,
49 %Tesla.Env{
50 status: 500,
51 body: ""
52 }}
53 end
54
55 {:error, sub} = Websub.verify(sub, getter)
56 # Keep the current state.
57 assert sub.state == "requested"
58 end
59
60 test "an incoming subscription request" do
61 user = insert(:user)
62
63 data = %{
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"
69 }
70
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"
76 end
77
78 test "an incoming subscription request for an existing subscription" do
79 user = insert(:user)
80
81 sub =
82 insert(:websub_subscription, state: "accepted", topic: Pleroma.Web.OStatus.feed_path(user))
83
84 data = %{
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"
90 }
91
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
99 end
100
101 def accepting_verifier(subscription) do
102 {:ok, %{subscription | state: "accepted"}}
103 end
104
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"}})
108
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"
116 end
117
118 test "discovers the hub and canonical url" do
119 topic = "https://mastodon.social/users/lambadalambda.atom"
120
121 {:ok, discovered} = Websub.gather_feed_data(topic)
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: 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: 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: 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 hub: "http://example.org/still_good",
213 state: "accepted"
214 })
215
216 needs_refresh =
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",
221 state: "accepted"
222 })
223
224 _refresh = Websub.refresh_subscriptions()
225
226 assert still_good == Repo.get(WebsubClientSubscription, still_good.id)
227 refute needs_refresh == Repo.get(WebsubClientSubscription, needs_refresh.id)
228 end
229 end
230 end