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
13 config_path = [:instance, :federating]
14 initial_setting = Pleroma.Config.get(config_path)
16 Pleroma.Config.put(config_path, true)
17 on_exit(fn -> Pleroma.Config.put(config_path, initial_setting) end)
22 test "websub subscription request", %{conn: conn} do
25 path = Pleroma.Web.OStatus.pubsub_path(user)
28 "hub.callback": "http://example.org/sub",
29 "hub.mode": "subscribe",
30 "hub.topic": Pleroma.Web.OStatus.feed_path(user),
31 "hub.secret": "a random secret",
32 "hub.lease_seconds": "100"
39 assert response(conn, 202) == "Accepted"
42 test "websub subscription confirmation", %{conn: conn} do
43 websub = insert(:websub_client_subscription)
46 "hub.mode" => "subscribe",
47 "hub.topic" => websub.topic,
48 "hub.challenge" => "some challenge",
49 "hub.lease_seconds" => "100"
54 |> get("/push/subscriptions/#{websub.id}", params)
56 websub = Repo.get(WebsubClientSubscription, websub.id)
58 assert response(conn, 200) == "some challenge"
59 assert websub.state == "accepted"
60 assert_in_delta NaiveDateTime.diff(websub.valid_until, NaiveDateTime.utc_now()), 100, 5
63 describe "websub_incoming" do
64 test "accepts incoming feed updates", %{conn: conn} do
65 websub = insert(:websub_client_subscription)
67 signature = Websub.sign(websub.secret, doc)
71 |> put_req_header("x-hub-signature", "sha1=" <> signature)
72 |> put_req_header("content-type", "application/atom+xml")
73 |> post("/push/subscriptions/#{websub.id}", doc)
75 assert response(conn, 200) == "OK"
78 test "rejects incoming feed updates with the wrong signature", %{conn: conn} do
79 websub = insert(:websub_client_subscription)
81 signature = Websub.sign("wrong secret", doc)
85 |> put_req_header("x-hub-signature", "sha1=" <> signature)
86 |> put_req_header("content-type", "application/atom+xml")
87 |> post("/push/subscriptions/#{websub.id}", doc)
89 assert response(conn, 500) == "Error"