Merge branch 'develop' into dtluna/pleroma-refactor/1
[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 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 "avatar" => %{"type" => "Image", "url" => [%{"href" => "https://files.mastodon.social/accounts/avatars/000/000/264/original/1429214160519.gif?1492379244", "mediaType" => "image/gif", "type" => "Link"}]}
124 }
125
126 assert expected == discovered
127 end
128
129 test "calls the hub, requests topic" do
130 hub = "https://social.heldscal.la/main/push/hub"
131 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
132 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
133
134 poster = fn (^hub, {:form, data}, _headers) ->
135 assert Keyword.get(data, :"hub.mode") == "subscribe"
136 assert Keyword.get(data, :"hub.callback") == Helpers.websub_url(Pleroma.Web.Endpoint, :websub_subscription_confirmation, websub.id)
137 {:ok, %{status_code: 202}}
138 end
139
140 task = Task.async(fn -> Websub.request_subscription(websub, poster) end)
141
142 change = Ecto.Changeset.change(websub, %{state: "accepted"})
143 {:ok, _} = Repo.update(change)
144
145 {:ok, websub} = Task.await(task)
146
147 assert websub.state == "accepted"
148 end
149
150 test "rejects the subscription if it can't be accepted" do
151 hub = "https://social.heldscal.la/main/push/hub"
152 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
153 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
154
155 poster = fn (^hub, {:form, _data}, _headers) ->
156 {:ok, %{status_code: 202}}
157 end
158
159 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
160 assert websub.state == "rejected"
161
162 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
163 poster = fn (^hub, {:form, _data}, _headers) ->
164 {:ok, %{status_code: 400}}
165 end
166
167 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
168 assert websub.state == "rejected"
169 end
170
171 test "sign a text" do
172 signed = Websub.sign("secret", "text")
173 assert signed == "B8392C23690CCF871F37EC270BE1582DEC57A503" |> String.downcase
174
175 signed = Websub.sign("secret", [["て"], ['す']])
176 end
177 end