Add user info gathering.
[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 user = insert(:user)
97 topic = "http://example.org/some-topic.atom"
98
99 {:ok, websub} = Websub.subscribe(user, topic, &accepting_verifier/1)
100 assert websub.subscribers == [user.ap_id]
101 assert websub.topic == topic
102 assert is_binary(websub.secret)
103 assert websub.user == user
104 assert websub.state == "accepted"
105 end
106
107 test "discovers the hub and canonical url" do
108 topic = "https://mastodon.social/users/lambadalambda.atom"
109
110 getter = fn(^topic) ->
111 doc = File.read!("test/fixtures/lambadalambda.atom")
112 {:ok, %{status_code: 200, body: doc}}
113 end
114
115 {:ok, discovered} = Websub.gather_feed_data(topic, getter)
116 expected = %{
117 hub: "https://mastodon.social/api/push",
118 uri: "https://mastodon.social/users/lambadalambda",
119 nickname: "lambadalambda",
120 name: "Critical Value"
121 }
122
123 assert expected == discovered
124 end
125
126 test "calls the hub, requests topic" do
127 hub = "https://social.heldscal.la/main/push/hub"
128 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
129 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
130
131 poster = fn (^hub, {:form, data}, _headers) ->
132 assert Keyword.get(data, :"hub.mode") == "subscribe"
133 assert Keyword.get(data, :"hub.callback") == Helpers.websub_url(Pleroma.Web.Endpoint, :websub_subscription_confirmation, websub.id)
134 {:ok, %{status_code: 202}}
135 end
136
137 task = Task.async(fn -> Websub.request_subscription(websub, poster) end)
138
139 change = Ecto.Changeset.change(websub, %{state: "accepted"})
140 {:ok, _} = Repo.update(change)
141
142 {:ok, websub} = Task.await(task)
143
144 assert websub.state == "accepted"
145 end
146
147 test "rejects the subscription if it can't be accepted" do
148 hub = "https://social.heldscal.la/main/push/hub"
149 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
150 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
151
152 poster = fn (^hub, {:form, _data}, _headers) ->
153 {:ok, %{status_code: 202}}
154 end
155
156 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
157 assert websub.state == "rejected"
158
159 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
160 poster = fn (^hub, {:form, _data}, _headers) ->
161 {:ok, %{status_code: 400}}
162 end
163
164 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
165 assert websub.state == "rejected"
166 end
167 end