Handle incoming websub subscriptions.
[akkoma] / test / web / websub / websub_controller_test.exs
1 defmodule Pleroma.Web.Websub.WebsubControllerTest do
2 use Pleroma.Web.ConnCase
3 import Pleroma.Factory
4 alias Pleroma.Web.Websub.WebsubClientSubscription
5 alias Pleroma.{Repo, Activity}
6 alias Pleroma.Web.Websub
7
8 test "websub subscription request", %{conn: conn} do
9 user = insert(:user)
10
11 path = Pleroma.Web.OStatus.pubsub_path(user)
12
13 data = %{
14 "hub.callback": "http://example.org/sub",
15 "hub.mode": "subscribe",
16 "hub.topic": Pleroma.Web.OStatus.feed_path(user),
17 "hub.secret": "a random secret",
18 "hub.lease_seconds": "100"
19 }
20
21 conn = conn
22 |> post(path, data)
23
24 assert response(conn, 202) == "Accepted"
25 end
26
27 test "websub subscription confirmation", %{conn: conn} do
28 websub = insert(:websub_client_subscription)
29
30 params = %{
31 "hub.mode" => "subscribe",
32 "hub.topic" => websub.topic,
33 "hub.challenge" => "some challenge",
34 "hub.lease_seconds" => 100
35 }
36
37 conn = conn
38 |> get("/push/subscriptions/#{websub.id}", params)
39
40 websub = Repo.get(WebsubClientSubscription, websub.id)
41
42 assert response(conn, 200) == "some challenge"
43 assert websub.state == "accepted"
44 end
45
46 test "handles incoming feed updates", %{conn: conn} do
47 websub = insert(:websub_client_subscription)
48 doc = "some stuff"
49 signature = Websub.sign(websub.secret, doc)
50
51 conn = conn
52 |> put_req_header("x-hub-signature", "sha1=" <> signature)
53 |> put_req_header("content-type", "application/atom+xml")
54 |> post("/push/subscriptions/#{websub.id}", doc)
55
56 assert response(conn, 200) == "OK"
57
58 assert length(Repo.all(Activity)) == 1
59 end
60
61 test "rejects incoming feed updates with the wrong signature", %{conn: conn} do
62 websub = insert(:websub_client_subscription)
63 doc = "some stuff"
64 signature = Websub.sign("wrong secret", doc)
65
66 conn = conn
67 |> put_req_header("x-hub-signature", "sha1=" <> signature)
68 |> put_req_header("content-type", "application/atom+xml")
69 |> post("/push/subscriptions/#{websub.id}", doc)
70
71 assert response(conn, 500) == "Error"
72
73 assert length(Repo.all(Activity)) == 0
74 end
75 end
76
77 defmodule Pleroma.Web.OStatusMock do
78 import Pleroma.Factory
79 def handle_incoming(_doc) do
80 insert(:note_activity)
81 end
82 end