Merge branch 'develop' of ssh.gitgud.io:lambadalambda/pleroma into feature/unfollow...
[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
62 {:ok, subscription } = Websub.incoming_subscription_request(user, data)
63 assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
64 assert subscription.state == "requested"
65 assert subscription.secret == "a random secret"
66 assert subscription.callback == "http://example.org/sub"
67 end
68
69 test "an incoming subscription request for an existing subscription" do
70 user = insert(:user)
71 sub = insert(:websub_subscription, state: "accepted", topic: Pleroma.Web.OStatus.feed_path(user))
72
73 data = %{
74 "hub.callback" => sub.callback,
75 "hub.mode" => "subscribe",
76 "hub.topic" => Pleroma.Web.OStatus.feed_path(user),
77 "hub.secret" => "a random secret",
78 "hub.lease_seconds" => "100"
79 }
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 end