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
9 alias Pleroma.{Repo, Activity}
10 alias Pleroma.Web.Websub
12 test "websub subscription request", %{conn: conn} do
15 path = Pleroma.Web.OStatus.pubsub_path(user)
18 "hub.callback": "http://example.org/sub",
19 "hub.mode": "subscribe",
20 "hub.topic": Pleroma.Web.OStatus.feed_path(user),
21 "hub.secret": "a random secret",
22 "hub.lease_seconds": "100"
29 assert response(conn, 202) == "Accepted"
32 test "websub subscription confirmation", %{conn: conn} do
33 websub = insert(:websub_client_subscription)
36 "hub.mode" => "subscribe",
37 "hub.topic" => websub.topic,
38 "hub.challenge" => "some challenge",
39 "hub.lease_seconds" => "100"
44 |> get("/push/subscriptions/#{websub.id}", params)
46 websub = Repo.get(WebsubClientSubscription, websub.id)
48 assert response(conn, 200) == "some challenge"
49 assert websub.state == "accepted"
50 assert_in_delta NaiveDateTime.diff(websub.valid_until, NaiveDateTime.utc_now()), 100, 5
53 test "handles incoming feed updates", %{conn: conn} do
54 websub = insert(:websub_client_subscription)
56 signature = Websub.sign(websub.secret, doc)
60 |> put_req_header("x-hub-signature", "sha1=" <> signature)
61 |> put_req_header("content-type", "application/atom+xml")
62 |> post("/push/subscriptions/#{websub.id}", doc)
64 assert response(conn, 200) == "OK"
66 assert length(Repo.all(Activity)) == 1
69 test "rejects incoming feed updates with the wrong signature", %{conn: conn} do
70 websub = insert(:websub_client_subscription)
72 signature = Websub.sign("wrong secret", doc)
76 |> put_req_header("x-hub-signature", "sha1=" <> signature)
77 |> put_req_header("content-type", "application/atom+xml")
78 |> post("/push/subscriptions/#{websub.id}", doc)
80 assert response(conn, 500) == "Error"
82 assert length(Repo.all(Activity)) == 0