Merge remote-tracking branch 'origin/develop' into benchmark-finishing
[akkoma] / test / web / websub / websub_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Websub.WebsubControllerTest do
6 use Pleroma.Web.ConnCase
7 import Pleroma.Factory
8 alias Pleroma.Repo
9 alias Pleroma.Web.Websub
10 alias Pleroma.Web.Websub.WebsubClientSubscription
11
12 clear_config_all([:instance, :federating]) do
13 Pleroma.Config.put([:instance, :federating], true)
14 end
15
16 test "websub subscription request", %{conn: conn} do
17 user = insert(:user)
18
19 path = Pleroma.Web.OStatus.pubsub_path(user)
20
21 data = %{
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"
27 }
28
29 conn =
30 conn
31 |> post(path, data)
32
33 assert response(conn, 202) == "Accepted"
34 end
35
36 test "websub subscription confirmation", %{conn: conn} do
37 websub = insert(:websub_client_subscription)
38
39 params = %{
40 "hub.mode" => "subscribe",
41 "hub.topic" => websub.topic,
42 "hub.challenge" => "some challenge",
43 "hub.lease_seconds" => "100"
44 }
45
46 conn =
47 conn
48 |> get("/push/subscriptions/#{websub.id}", params)
49
50 websub = Repo.get(WebsubClientSubscription, websub.id)
51
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
55 end
56
57 describe "websub_incoming" do
58 test "accepts incoming feed updates", %{conn: conn} do
59 websub = insert(:websub_client_subscription)
60 doc = "some stuff"
61 signature = Websub.sign(websub.secret, doc)
62
63 conn =
64 conn
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)
68
69 assert response(conn, 200) == "OK"
70 end
71
72 test "rejects incoming feed updates with the wrong signature", %{conn: conn} do
73 websub = insert(:websub_client_subscription)
74 doc = "some stuff"
75 signature = Websub.sign("wrong secret", doc)
76
77 conn =
78 conn
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)
82
83 assert response(conn, 500) == "Error"
84 end
85 end
86 end