bf243ac91ccba4a750a11a9f4f7300de6e775523
[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 defmodule Pleroma.Web.WebsubTest do
7 use Pleroma.DataCase
8 alias Pleroma.Web.Websub
9 alias Pleroma.Web.Websub.WebsubServerSubscription
10 import Pleroma.Factory
11
12 test "a verification of a request that is accepted" do
13 sub = insert(:websub_subscription)
14 topic = sub.topic
15
16 getter = fn (_path, _headers, options) ->
17 %{
18 "hub.challenge": challenge,
19 "hub.lease_seconds": seconds,
20 "hub.topic": ^topic,
21 "hub.mode": "subscribe"
22 } = Keyword.get(options, :params)
23
24 assert String.to_integer(seconds) > 0
25
26 {:ok, %HTTPoison.Response{
27 status_code: 200,
28 body: challenge
29 }}
30 end
31
32 {:ok, sub} = Websub.verify(sub, getter)
33 assert sub.state == "active"
34 end
35
36 test "a verification of a request that doesn't return 200" do
37 sub = insert(:websub_subscription)
38
39 getter = fn (_path, _headers, _options) ->
40 {:ok, %HTTPoison.Response{
41 status_code: 500,
42 body: ""
43 }}
44 end
45
46 {:error, sub} = Websub.verify(sub, getter)
47 assert sub.state == "rejected"
48 end
49
50 test "an incoming subscription request" do
51 user = insert(:user)
52
53 data = %{
54 "hub.callback" => "http://example.org/sub",
55 "hub.mode" => "subscribe",
56 "hub.topic" => Pleroma.Web.OStatus.feed_path(user),
57 "hub.secret" => "a random secret",
58 "hub.lease_seconds" => "100"
59 }
60
61 {:ok, subscription } = Websub.incoming_subscription_request(user, data)
62 assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
63 assert subscription.state == "requested"
64 assert subscription.secret == "a random secret"
65 assert subscription.callback == "http://example.org/sub"
66 end
67
68 test "an incoming subscription request for an existing subscription" do
69 user = insert(:user)
70 sub = insert(:websub_subscription, state: "accepted", topic: Pleroma.Web.OStatus.feed_path(user))
71
72 data = %{
73 "hub.callback" => sub.callback,
74 "hub.mode" => "subscribe",
75 "hub.topic" => Pleroma.Web.OStatus.feed_path(user),
76 "hub.secret" => "a random secret",
77 "hub.lease_seconds" => "100"
78 }
79
80 {:ok, subscription } = Websub.incoming_subscription_request(user, data)
81 assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
82 assert subscription.state == sub.state
83 assert subscription.secret == "a random secret"
84 assert subscription.callback == sub.callback
85 assert length(Repo.all(WebsubServerSubscription)) == 1
86 assert subscription.id == sub.id
87 end
88
89 def accepting_verifier(subscription) do
90 {:ok, %{ subscription | state: "accepted" }}
91 end
92
93 test "initiate a subscription for a given user and topic" do
94 user = insert(:user)
95 topic = "http://example.org/some-topic.atom"
96
97 {:ok, websub} = Websub.subscribe(user, topic, &accepting_verifier/1)
98 assert websub.subscribers == [user.ap_id]
99 assert websub.topic == topic
100 assert is_binary(websub.secret)
101 assert websub.user == user
102 assert websub.state == "accepted"
103 end
104
105 test "discovers the hub and canonical url" do
106 topic = "https://mastodon.social/users/lambadalambda.atom"
107
108 getter = fn(^topic) ->
109 doc = File.read!("test/fixtures/lambadalambda.atom")
110 {:ok, %{status_code: 200, body: doc}}
111 end
112
113 {:ok, discovered} = Websub.discover(topic, getter)
114 assert %{hub: "https://mastodon.social/api/push", url: topic} == discovered
115 end
116
117 test "calls the hub, requests topic" do
118 hub = "https://social.heldscal.la/main/push/hub"
119 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
120 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
121
122 poster = fn (^hub, {:form, data}, _headers) ->
123 assert Keyword.get(data, :"hub.mode") == "subscribe"
124 {:ok, %{status_code: 202}}
125 end
126
127 task = Task.async(fn -> Websub.request_subscription(websub, poster) end)
128
129 change = Ecto.Changeset.change(websub, %{state: "accepted"})
130 {:ok, _} = Repo.update(change)
131
132 {:ok, websub} = Task.await(task)
133
134 assert websub.state == "accepted"
135 end
136
137 test "rejects the subscription if it can't be accepted" do
138 hub = "https://social.heldscal.la/main/push/hub"
139 topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
140 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
141
142 poster = fn (^hub, {:form, _data}, _headers) ->
143 {:ok, %{status_code: 202}}
144 end
145
146 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
147 assert websub.state == "rejected"
148
149 websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
150 poster = fn (^hub, {:form, _data}, _headers) ->
151 {:ok, %{status_code: 400}}
152 end
153
154 {:error, websub} = Websub.request_subscription(websub, poster, 1000)
155 assert websub.state == "rejected"
156 end
157 end