1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Websub.WebsubControllerTest do
6 use Pleroma.Web.ConnCase
8 alias Pleroma.Web.Websub.WebsubClientSubscription
11 alias Pleroma.Web.Websub
13 test "websub subscription request", %{conn: conn} do
16 path = Pleroma.Web.OStatus.pubsub_path(user)
19 "hub.callback": "http://example.org/sub",
20 "hub.mode": "subscribe",
21 "hub.topic": Pleroma.Web.OStatus.feed_path(user),
22 "hub.secret": "a random secret",
23 "hub.lease_seconds": "100"
30 assert response(conn, 202) == "Accepted"
33 test "websub subscription confirmation", %{conn: conn} do
34 websub = insert(:websub_client_subscription)
37 "hub.mode" => "subscribe",
38 "hub.topic" => websub.topic,
39 "hub.challenge" => "some challenge",
40 "hub.lease_seconds" => "100"
45 |> get("/push/subscriptions/#{websub.id}", params)
47 websub = Repo.get(WebsubClientSubscription, websub.id)
49 assert response(conn, 200) == "some challenge"
50 assert websub.state == "accepted"
51 assert_in_delta NaiveDateTime.diff(websub.valid_until, NaiveDateTime.utc_now()), 100, 5
54 describe "websub_incoming" do
55 test "handles incoming feed updates", %{conn: conn} do
56 websub = insert(:websub_client_subscription)
58 signature = Websub.sign(websub.secret, doc)
62 |> put_req_header("x-hub-signature", "sha1=" <> signature)
63 |> put_req_header("content-type", "application/atom+xml")
64 |> post("/push/subscriptions/#{websub.id}", doc)
66 assert response(conn, 200) == "OK"
68 assert length(Repo.all(Activity)) == 1
71 test "rejects incoming feed updates with the wrong signature", %{conn: conn} do
72 websub = insert(:websub_client_subscription)
74 signature = Websub.sign("wrong secret", doc)
78 |> put_req_header("x-hub-signature", "sha1=" <> signature)
79 |> put_req_header("content-type", "application/atom+xml")
80 |> post("/push/subscriptions/#{websub.id}", doc)
82 assert response(conn, 500) == "Error"
84 assert Enum.empty?(Repo.all(Activity))