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
9 alias Pleroma.Web.Websub
10 alias Pleroma.Web.Websub.WebsubClientSubscription
12 clear_config_all([:instance, :federating]) do
13 Pleroma.Config.put([:instance, :federating], true)
16 test "websub subscription request", %{conn: conn} do
19 path = Pleroma.Web.OStatus.pubsub_path(user)
22 "hub.callback": "http://example.org/sub",
23 "hub.mode": "subscribe",
24 "hub.topic": Pleroma.Web.OStatus.feed_path(user),
25 "hub.secret": "a random secret",
26 "hub.lease_seconds": "100"
33 assert response(conn, 202) == "Accepted"
36 test "websub subscription confirmation", %{conn: conn} do
37 websub = insert(:websub_client_subscription)
40 "hub.mode" => "subscribe",
41 "hub.topic" => websub.topic,
42 "hub.challenge" => "some challenge",
43 "hub.lease_seconds" => "100"
48 |> get("/push/subscriptions/#{websub.id}", params)
50 websub = Repo.get(WebsubClientSubscription, websub.id)
52 assert response(conn, 200) == "some challenge"
53 assert websub.state == "accepted"
54 assert_in_delta NaiveDateTime.diff(websub.valid_until, NaiveDateTime.utc_now()), 100, 5
57 describe "websub_incoming" do
58 test "accepts incoming feed updates", %{conn: conn} do
59 websub = insert(:websub_client_subscription)
61 signature = Websub.sign(websub.secret, doc)
65 |> put_req_header("x-hub-signature", "sha1=" <> signature)
66 |> put_req_header("content-type", "application/atom+xml")
67 |> post("/push/subscriptions/#{websub.id}", doc)
69 assert response(conn, 200) == "OK"
72 test "rejects incoming feed updates with the wrong signature", %{conn: conn} do
73 websub = insert(:websub_client_subscription)
75 signature = Websub.sign("wrong secret", doc)
79 |> put_req_header("x-hub-signature", "sha1=" <> signature)
80 |> put_req_header("content-type", "application/atom+xml")
81 |> post("/push/subscriptions/#{websub.id}", doc)
83 assert response(conn, 500) == "Error"