Merge branch 'features/sec-websocket-protocol-header' into 'develop'
[akkoma] / test / web / websub / websub_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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 setup_all do
13 config_path = [:instance, :federating]
14 initial_setting = Pleroma.Config.get(config_path)
15
16 Pleroma.Config.put(config_path, true)
17 on_exit(fn -> Pleroma.Config.put(config_path, initial_setting) end)
18
19 :ok
20 end
21
22 test "websub subscription request", %{conn: conn} do
23 user = insert(:user)
24
25 path = Pleroma.Web.OStatus.pubsub_path(user)
26
27 data = %{
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"
33 }
34
35 conn =
36 conn
37 |> post(path, data)
38
39 assert response(conn, 202) == "Accepted"
40 end
41
42 test "websub subscription confirmation", %{conn: conn} do
43 websub = insert(:websub_client_subscription)
44
45 params = %{
46 "hub.mode" => "subscribe",
47 "hub.topic" => websub.topic,
48 "hub.challenge" => "some challenge",
49 "hub.lease_seconds" => "100"
50 }
51
52 conn =
53 conn
54 |> get("/push/subscriptions/#{websub.id}", params)
55
56 websub = Repo.get(WebsubClientSubscription, websub.id)
57
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
61 end
62
63 describe "websub_incoming" do
64 test "accepts incoming feed updates", %{conn: conn} do
65 websub = insert(:websub_client_subscription)
66 doc = "some stuff"
67 signature = Websub.sign(websub.secret, doc)
68
69 conn =
70 conn
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)
74
75 assert response(conn, 200) == "OK"
76 end
77
78 test "rejects incoming feed updates with the wrong signature", %{conn: conn} do
79 websub = insert(:websub_client_subscription)
80 doc = "some stuff"
81 signature = Websub.sign("wrong secret", doc)
82
83 conn =
84 conn
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)
88
89 assert response(conn, 500) == "Error"
90 end
91 end
92 end