7b77e696b60ce4bada517659d2d263a2b5311343
[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
81 {:ok, subscription } = Websub.incoming_subscription_request(user, data)
82 assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
83 assert subscription.state == sub.state
84 assert subscription.secret == "a random secret"
85 assert subscription.callback == sub.callback
86 assert length(Repo.all(WebsubServerSubscription)) == 1
87 assert subscription.id == sub.id
88 end
89
90 test "initiate a subscription for a given user and topic" do
91 user = insert(:user)
92 topic = "http://example.org/some-topic.atom"
93
94 {:ok, websub} = Websub.subscribe(user, topic)
95 assert websub.subscribers == [user.ap_id]
96 assert websub.topic == topic
97 assert is_binary(websub.secret)
98 assert websub.state == "accepted"
99 end
100 end