1ca573d6600bf49b00eda84deadad111fc3be6b3
[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 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 subscriber = insert(:user)
97 user = insert(:user, %{info: %{ "topic" => "some_topic", "hub" => "some_hub"}})
98
99 {:ok, websub} = Websub.subscribe(subscriber, user, &accepting_verifier/1)
100 assert websub.subscribers == [subscriber.ap_id]
101 assert websub.topic == "some_topic"
102 assert websub.hub == "some_hub"
103 assert is_binary(websub.secret)
104 assert websub.user == user
105 assert websub.state == "accepted"
106 end
107
108 test "discovers the hub and canonical url" do
109 topic = "https://mastodon.social/users/lambadalambda.atom"
110
111 getter = fn(^topic) ->
112 doc = File.read!("test/fixtures/lambadalambda.atom")
113 {:ok, %{status_code: 200, body: doc}}
114 end
115
116 {:ok, discovered} = Websub.gather_feed_data(topic, getter)
117 expected = %{
118 "hub" => "https://mastodon.social/api/push",
119 "uri" => "https://mastodon.social/users/lambadalambda",
120 "nickname" => "lambadalambda",
121 "name" => "Critical Value",
122 "host" => "mastodon.social",
123 "bio" => "a cool dude.",
124 "avatar" => %{"type" => "Image", "url" => [%{"href" => "https://files.mastodon.social/accounts/avatars/000/000/264/original/1429214160519.gif?1492379244", "mediaType" => "image/gif", "type" => "Link"}]}
125 }
126
127 assert expected == discovered
128 end
129
130 test "calls the hub, requests topic" do
131 hub = "https://social.heldscal.la/main/push/hub"
132 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
133 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
134
135 poster = fn (^hub, {:form, data}, _headers) ->
136 assert Keyword.get(data, :"hub.mode") == "subscribe"
137 assert Keyword.get(data, :"hub.callback") == Helpers.websub_url(Pleroma.Web.Endpoint, :websub_subscription_confirmation, websub.id)
138 {:ok, %{status_code: 202}}
139 end
140
141 task = Task.async(fn -> Websub.request_subscription(websub, poster) end)
142
143 change = Ecto.Changeset.change(websub, %{state: "accepted"})
144 {:ok, _} = Repo.update(change)
145
146 {:ok, websub} = Task.await(task)
147
148 assert websub.state == "accepted"
149 end
150
151 test "rejects the subscription if it can't be accepted" do
152 hub = "https://social.heldscal.la/main/push/hub"
153 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
154 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
155
156 poster = fn (^hub, {:form, _data}, _headers) ->
157 {:ok, %{status_code: 202}}
158 end
159
160 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
161 assert websub.state == "rejected"
162
163 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
164 poster = fn (^hub, {:form, _data}, _headers) ->
165 {:ok, %{status_code: 400}}
166 end
167
168 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
169 assert websub.state == "rejected"
170 end
171
172 test "sign a text" do
173 signed = Websub.sign("secret", "text")
174 assert signed == "B8392C23690CCF871F37EC270BE1582DEC57A503" |> String.downcase
175
176 signed = Websub.sign("secret", [["て"], ['す']])
177 end
178
179 describe "renewing subscriptions" do
180 test "it renews subscriptions that have less than a day of time left" do
181 day = 60 * 60 * 24
182 now = NaiveDateTime.utc_now
183 still_good = insert(:websub_client_subscription, %{valid_until: NaiveDateTime.add(now, 2 * day), topic: "http://example.org/still_good", state: "accepted"})
184 needs_refresh = insert(:websub_client_subscription, %{valid_until: NaiveDateTime.add(now, day - 100), topic: "http://example.org/needs_refresh", state: "accepted"})
185
186 refresh = Websub.refresh_subscriptions()
187
188 assert still_good == Repo.get(WebsubClientSubscription, still_good.id)
189 refute needs_refresh == Repo.get(WebsubClientSubscription, needs_refresh.id)
190 end
191 end
192 end