1 defmodule Pleroma.Web.Websub.WebsubControllerTest do
2 use Pleroma.Web.ConnCase
4 alias Pleroma.Web.Websub.WebsubClientSubscription
5 alias Pleroma.{Repo, Activity}
6 alias Pleroma.Web.Websub
8 test "websub subscription request", %{conn: conn} do
11 path = Pleroma.Web.OStatus.pubsub_path(user)
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"
25 assert response(conn, 202) == "Accepted"
28 test "websub subscription confirmation", %{conn: conn} do
29 websub = insert(:websub_client_subscription)
32 "hub.mode" => "subscribe",
33 "hub.topic" => websub.topic,
34 "hub.challenge" => "some challenge",
35 "hub.lease_seconds" => "100"
40 |> get("/push/subscriptions/#{websub.id}", params)
42 websub = Repo.get(WebsubClientSubscription, websub.id)
44 assert response(conn, 200) == "some challenge"
45 assert websub.state == "accepted"
46 assert_in_delta NaiveDateTime.diff(websub.valid_until, NaiveDateTime.utc_now()), 100, 5
49 test "handles incoming feed updates", %{conn: conn} do
50 websub = insert(:websub_client_subscription)
52 signature = Websub.sign(websub.secret, doc)
56 |> put_req_header("x-hub-signature", "sha1=" <> signature)
57 |> put_req_header("content-type", "application/atom+xml")
58 |> post("/push/subscriptions/#{websub.id}", doc)
60 assert response(conn, 200) == "OK"
62 assert length(Repo.all(Activity)) == 1
65 test "rejects incoming feed updates with the wrong signature", %{conn: conn} do
66 websub = insert(:websub_client_subscription)
68 signature = Websub.sign("wrong secret", doc)
72 |> put_req_header("x-hub-signature", "sha1=" <> signature)
73 |> put_req_header("content-type", "application/atom+xml")
74 |> post("/push/subscriptions/#{websub.id}", doc)
76 assert response(conn, 500) == "Error"
78 assert length(Repo.all(Activity)) == 0