566ce7fa5a4b9b454a69ead753947ff5f3bbabb7
[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, %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 # Keep the current state.
50 assert sub.state == "requested"
51 end
52
53 test "an incoming subscription request" do
54 user = insert(:user)
55
56 data = %{
57 "hub.callback" => "http://example.org/sub",
58 "hub.mode" => "subscribe",
59 "hub.topic" => Pleroma.Web.OStatus.feed_path(user),
60 "hub.secret" => "a random secret",
61 "hub.lease_seconds" => "100"
62 }
63
64 {:ok, subscription } = Websub.incoming_subscription_request(user, data)
65 assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
66 assert subscription.state == "requested"
67 assert subscription.secret == "a random secret"
68 assert subscription.callback == "http://example.org/sub"
69 end
70
71 test "an incoming subscription request for an existing subscription" do
72 user = insert(:user)
73 sub = insert(:websub_subscription, state: "accepted", topic: Pleroma.Web.OStatus.feed_path(user))
74
75 data = %{
76 "hub.callback" => sub.callback,
77 "hub.mode" => "subscribe",
78 "hub.topic" => Pleroma.Web.OStatus.feed_path(user),
79 "hub.secret" => "a random secret",
80 "hub.lease_seconds" => "100"
81 }
82
83 {:ok, subscription } = Websub.incoming_subscription_request(user, data)
84 assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
85 assert subscription.state == sub.state
86 assert subscription.secret == "a random secret"
87 assert subscription.callback == sub.callback
88 assert length(Repo.all(WebsubServerSubscription)) == 1
89 assert subscription.id == sub.id
90 end
91
92 def accepting_verifier(subscription) do
93 {:ok, %{ subscription | state: "accepted" }}
94 end
95
96 test "initiate a subscription for a given user and topic" do
97 subscriber = insert(:user)
98 user = insert(:user, %{info: %{ "topic" => "some_topic", "hub" => "some_hub"}})
99
100 {:ok, websub} = Websub.subscribe(subscriber, user, &accepting_verifier/1)
101 assert websub.subscribers == [subscriber.ap_id]
102 assert websub.topic == "some_topic"
103 assert websub.hub == "some_hub"
104 assert is_binary(websub.secret)
105 assert websub.user == user
106 assert websub.state == "accepted"
107 end
108
109 test "discovers the hub and canonical url" do
110 topic = "https://mastodon.social/users/lambadalambda.atom"
111
112 getter = fn(^topic) ->
113 doc = File.read!("test/fixtures/lambadalambda.atom")
114 {:ok, %{status_code: 200, body: doc}}
115 end
116
117 {:ok, discovered} = Websub.gather_feed_data(topic, getter)
118 expected = %{
119 "hub" => "https://mastodon.social/api/push",
120 "uri" => "https://mastodon.social/users/lambadalambda",
121 "nickname" => "lambadalambda",
122 "name" => "Critical Value",
123 "host" => "mastodon.social",
124 "bio" => "a cool dude.",
125 "avatar" => %{"type" => "Image", "url" => [%{"href" => "https://files.mastodon.social/accounts/avatars/000/000/264/original/1429214160519.gif?1492379244", "mediaType" => "image/gif", "type" => "Link"}]}
126 }
127
128 assert expected == discovered
129 end
130
131 test "calls the hub, requests topic" do
132 hub = "https://social.heldscal.la/main/push/hub"
133 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
134 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
135
136 poster = fn (^hub, {:form, data}, _headers) ->
137 assert Keyword.get(data, :"hub.mode") == "subscribe"
138 assert Keyword.get(data, :"hub.callback") == Helpers.websub_url(Pleroma.Web.Endpoint, :websub_subscription_confirmation, websub.id)
139 {:ok, %{status_code: 202}}
140 end
141
142 task = Task.async(fn -> Websub.request_subscription(websub, poster) end)
143
144 change = Ecto.Changeset.change(websub, %{state: "accepted"})
145 {:ok, _} = Repo.update(change)
146
147 {:ok, websub} = Task.await(task)
148
149 assert websub.state == "accepted"
150 end
151
152 test "rejects the subscription if it can't be accepted" do
153 hub = "https://social.heldscal.la/main/push/hub"
154 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
155 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
156
157 poster = fn (^hub, {:form, _data}, _headers) ->
158 {:ok, %{status_code: 202}}
159 end
160
161 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
162 assert websub.state == "rejected"
163
164 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
165 poster = fn (^hub, {:form, _data}, _headers) ->
166 {:ok, %{status_code: 400}}
167 end
168
169 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
170 assert websub.state == "rejected"
171 end
172
173 test "sign a text" do
174 signed = Websub.sign("secret", "text")
175 assert signed == "B8392C23690CCF871F37EC270BE1582DEC57A503" |> String.downcase
176
177 _signed = Websub.sign("secret", [["て"], ['す']])
178 end
179
180 describe "renewing subscriptions" do
181 test "it renews subscriptions that have less than a day of time left" do
182 day = 60 * 60 * 24
183 now = NaiveDateTime.utc_now
184 still_good = insert(:websub_client_subscription, %{valid_until: NaiveDateTime.add(now, 2 * day), topic: "http://example.org/still_good", state: "accepted"})
185 needs_refresh = insert(:websub_client_subscription, %{valid_until: NaiveDateTime.add(now, day - 100), topic: "http://example.org/needs_refresh", state: "accepted"})
186
187 _refresh = Websub.refresh_subscriptions()
188
189 assert still_good == Repo.get(WebsubClientSubscription, still_good.id)
190 refute needs_refresh == Repo.get(WebsubClientSubscription, needs_refresh.id)
191 end
192 end
193 end